diff options
Diffstat (limited to 'include/actions.c')
| -rw-r--r-- | include/actions.c | 130 |
1 files changed, 58 insertions, 72 deletions
diff --git a/include/actions.c b/include/actions.c index 4f226e1..c926e52 100644 --- a/include/actions.c +++ b/include/actions.c @@ -26,14 +26,14 @@ #define CLR_STONE NUM_MASK static inline void -list_append(action_list_t *list, const enum A_TYPE type, - const int8_t loc, const uint8_t data0, - const uint8_t data1); +lq_append(action_lq_t *lq, const enum A_TYPE type, + const int8_t loc, const uint8_t data0, + const uint8_t data1); static inline void -list_prepend(action_list_t *list, const enum A_TYPE type, - const int8_t loc, const uint8_t data0, - const uint8_t data1); +lq_prepend(action_lq_t *lq, const enum A_TYPE type, + const int8_t loc, const uint8_t data0, + const uint8_t data1); static inline void inline_next_ply(void); @@ -45,42 +45,50 @@ inline_prev_ply(void); // Exported method implementations // =================================================================== -int action_move_to_front(const action_t action, - action_list_t *list) { - action_node_t *n = list->head; - - // TODO: what if it's not in the list? - - while (n) { - if (n->action == action) { - const action_t t = list->head->action; - list->head->action = action; - n->action = t; +int action_move_to_front(const action_t action, action_lq_t *lq) { + for (int k=lq->i_f; k<lq->i_b; k++) { + if (lq->actions[k] == action) { + for (int j=k; j>lq->i_f; j--) { + lq->actions[j] = lq->actions[j-1]; + } + lq->actions[lq->i_f] = action; return EXIT_SUCCESS; } - n = n->next; } - return EXIT_FAILURE; } +action_lq_t *action_lq_copy_with_mtf(action_lq_t *lq, action_t front) { + // Normalise, empty list ---> null + if (lq == NULL || lq->length == 0) return NULL; + action_lq_t *copy = malloc(sizeof(action_lq_t)); + + copy->length = lq->length; + copy->i_b = lq->i_b; + copy->i_f = lq->i_f; -void action_list_free(action_list_t *list) { - if (list) { - action_node_t *n = list->head, *nn; - while (n) { - nn = n->next; - free(n); - n = nn; + for (int source=lq->i_f, dest=copy->i_f+1; + source < lq->i_b; source++, dest++) { + if (lq->actions[source] == front) { + source++; + if (source >= lq->i_b) break; } - free(list); + copy->actions[dest] = lq->actions[source]; } + + copy->actions[copy->i_f] = front; + + return copy; +} + +void action_lq_free(action_lq_t *lq) { + free(lq); } // Keep track of move offsets int8_t move_deltas[4]; -void action_list_init(void) { +void action_lq_init(void) { move_deltas[0] = +board_size; move_deltas[1] = -board_size; move_deltas[2] = -1; @@ -89,12 +97,13 @@ void action_list_init(void) { // We bias place over move by prepending place actions and appending // move actions to the generated list -action_list_t *action_list_generate(void) { - action_list_t *list = malloc(sizeof(struct action_list_s)); +action_lq_t *action_lq_generate(void) { + action_lq_t *lq = malloc(sizeof(action_lq_t)); // TODO: trap errno - list->length = 0; - list->head = NULL; + lq->length = 0; + lq->i_f = ACTION_AR_LEN; + lq->i_b = ACTION_AR_LEN; /* * The check for whether it's a black piece to be played is actually @@ -183,9 +192,9 @@ action_list_t *action_list_generate(void) { (steps == end_stops[dir]) && crushes[dir]; // Store the move - list_append(list, A_MOVE, loc, - (crush << 7) | gaps, - (dir<<4) | num); + lq_append(lq, A_MOVE, loc, + (crush << 7) | gaps, + (dir<<4) | num); } /* * With thanks to @@ -205,16 +214,16 @@ action_list_t *action_list_generate(void) { else if (material) { // Empty square, generate placements if (flat) { - list_prepend(list, A_PLACE, loc, STONE_FLAT, 0); + lq_prepend(lq, A_PLACE, loc, STONE_FLAT, 0); if (standing) - list_prepend(list, A_PLACE, loc, STONE_STANDING,0); + lq_prepend(lq, A_PLACE, loc, STONE_STANDING,0); } if (cap) - list_prepend(list, A_PLACE, loc, STONE_CAPSTONE, 0); + lq_prepend(lq, A_PLACE, loc, STONE_CAPSTONE, 0); } } } - return list; + return lq; } void action_take(const action_t action) { @@ -383,42 +392,19 @@ void action_to_ptn(const action_t action, char* out_ptn) { // =================================================================== static inline void -list_append(action_list_t *list, const enum A_TYPE type, - const int8_t loc, const uint8_t data0, - const uint8_t data1) { - action_node_t *new = malloc(sizeof(action_node_t)); - // TODO: trap errno - - new->next = NULL; - new->action = A_BUILD(type, loc, data0, data1); - - if (list->length) { - list->tail->next = new; - list->tail = new; - } else { - list->head = new; - list->tail = new; - } - - list->length++; +lq_append(action_lq_t *lq, const enum A_TYPE type, + const int8_t loc, const uint8_t data0, + const uint8_t data1) { + lq->actions[lq->i_b++] = A_BUILD(type, loc, data0, data1); + lq->length++; } static inline void -list_prepend(action_list_t *list, const enum A_TYPE type, - const int8_t loc, const uint8_t data0, - const uint8_t data1) { - action_node_t *new = malloc(sizeof(action_list_t)); - // TODO: trap errno - - new->next = list->head; - list->head = new; - new->action = A_BUILD(type, loc, data0, data1); - - if (list->length == 0) { - list->tail = new; - } - - list->length++; +lq_prepend(action_lq_t *lq, const enum A_TYPE type, + const int8_t loc, const uint8_t data0, + const uint8_t data1) { + lq->actions[--lq->i_f] = A_BUILD(type, loc, data0, data1); + lq->length++; } static inline void |
