diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-24 01:19:38 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-24 01:19:38 -0700 |
commit | 125ce0ef81d8d7d684a92c1c5366d5b38880d8fe (patch) | |
tree | 7950c149d4a12355444654fa801584d615df6fc0 /match.c | |
parent | 85c101979a181a76e6250fb1abb75216e08384a3 (diff) | |
download | dartboat-125ce0ef81d8d7d684a92c1c5366d5b38880d8fe.tar.gz dartboat-125ce0ef81d8d7d684a92c1c5366d5b38880d8fe.tar.xz |
clear undone visit before overwriting with new visit
Diffstat (limited to 'match.c')
-rw-r--r-- | match.c | 51 |
1 files changed, 33 insertions, 18 deletions
@@ -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)); |