summaryrefslogtreecommitdiff
path: root/match.c
diff options
context:
space:
mode:
Diffstat (limited to 'match.c')
-rw-r--r--match.c51
1 files changed, 33 insertions, 18 deletions
diff --git a/match.c b/match.c
index ee35798..977bfe1 100644
--- a/match.c
+++ b/match.c
@@ -11,7 +11,7 @@ struct leg *leg_init(int pts)
l->start = l->rem = pts;
l->size_visits = 16;
- l->visits = calloc(l->size_visits, sizeof(*(l->visits)));
+ l->visits = calloc(l->size_visits, sizeof(*l->visits));
return l;
}
@@ -31,19 +31,43 @@ void leg_free(struct leg *l)
void leg_grow_visits(struct leg *l)
{
- size_t bytes = l->size_visits * sizeof(*(l->visits));
+ size_t bytes = l->size_visits * sizeof(*l->visits);
l->size_visits *= 2;
l->visits = realloc(l->visits, 2 * bytes);
memset((char *)l->visits + bytes, 0, bytes);
}
+void leg_undo_visit(struct leg *l)
+{
+ l->rem += l->visits[--l->n_visits].points;
+ ++l->undone_visits;
+}
+
+struct visit *leg_redo_visit(struct leg *l)
+{
+ struct visit *v = l->visits + l->n_visits++;
+ l->rem = v->rem;
+ --l->undone_visits;
+ return v;
+}
+
+static void leg_clear_first_undone(struct leg *l)
+{
+ free(l->visits[l->n_visits].darts);
+ free(l->visits[l->n_visits].ccoords);
+ memset(l->visits + l->n_visits, 0, sizeof(*l->visits));
+ --l->undone_visits;
+}
+
struct visit *leg_comp_visit(struct leg *l, bool redo_undone)
{
- // FIXME redo_undone=false should free next undone
if (redo_undone && l->undone_visits)
- leg_redo_visit(l);
- else
- comp_visit(l);
+ return leg_redo_visit(l);
+
+ if (l->undone_visits)
+ leg_clear_first_undone(l);
+
+ comp_visit(l);
struct visit *v = l->visits + l->n_visits - 1;
return v;
@@ -54,6 +78,9 @@ struct visit *leg_pts_visit(struct leg *l, int pts)
if (!is_points_valid(pts, l->rem))
return NULL;
+ if (l->undone_visits)
+ leg_clear_first_undone(l);
+
if (l->n_visits == l->size_visits)
leg_grow_visits(l);
struct visit *v = l->visits + l->n_visits++;
@@ -64,18 +91,6 @@ struct visit *leg_pts_visit(struct leg *l, int pts)
return v;
}
-void leg_undo_visit(struct leg *l)
-{
- l->rem += l->visits[--l->n_visits].points;
- ++l->undone_visits;
-}
-
-void leg_redo_visit(struct leg *l)
-{
- l->rem = l->visits[l->n_visits++].rem;
- --l->undone_visits;
-}
-
struct match *match_init()
{
struct match *m = calloc(1, sizeof(*m));