From 125ce0ef81d8d7d684a92c1c5366d5b38880d8fe Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 24 May 2022 01:19:38 -0700 Subject: clear undone visit before overwriting with new visit --- match.c | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) (limited to 'match.c') 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)); -- cgit v1.2.3-70-g09d2