]> git.cworth.org Git - rrsolve/blob - src/rrs_solution.c
* src/Makefile.am (rrsolve_LDFLAGS): Fix to not ovverride user
[rrsolve] / src / rrs_solution.c
1 /* rrsolve - Simple RR solver and librr client.
2  *
3  * Copyright © 2003 Carl Worth
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of Carl Worth
10  * not be used in advertising or publicity pertaining to distribution
11  * of the software without specific, written prior permission.
12  * Carl Worth makes no representations about the suitability of this
13  * software for any purpose.  It is provided "as is" without express
14  * or implied warranty.
15  * 
16  * CARL WORTH DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
18  * NO EVENT SHALL CARL WORTH BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
20  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
21  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: Carl Worth <carl@theworths.org>
25  */
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "rrsolve.h"
31
32 rr_status_t
33 rrs_solution_init (rrs_solution_t *solution)
34 {
35     solution->move = NULL;
36     solution->size = 0;
37     solution->num_moves = 0;
38
39     return RR_STATUS_SUCCESS;
40 }
41
42 void
43 rrs_solution_fini (rrs_solution_t *solution)
44 {
45     free (solution->move);
46     solution->move = NULL;
47     solution->size = 0;
48     solution->num_moves = 0;
49 }
50
51 #define RRS_SOLUTION_GROWTH 5
52 static rr_status_t
53 _rrs_solution_grow (rrs_solution_t *solution)
54 {
55     rrs_move_t *new_move;
56
57     if (solution->num_moves >= solution->size) {
58         solution->size += RRS_SOLUTION_GROWTH;
59         new_move = realloc (solution->move, solution->size * sizeof (rrs_move_t));
60         if (new_move == NULL) {
61             solution->size -= RRS_SOLUTION_GROWTH;
62             return RR_STATUS_NO_MEMORY;
63         }
64         solution->move = new_move;
65     }
66
67     return RR_STATUS_SUCCESS;
68 }
69
70 rr_status_t
71 rrs_solution_push (rrs_solution_t *solution,
72                    rr_robot_t robot, rr_direction_t dir)
73 {
74     rrs_move_t *move;
75
76     _rrs_solution_grow (solution);
77
78     move = &solution->move[solution->num_moves++];
79     move->robot = robot;
80     move->dir = dir;
81
82     return RR_STATUS_SUCCESS;
83 }
84
85 rr_status_t
86 rrs_solution_pop (rrs_solution_t *solution,
87                   rr_robot_t *robot, rr_direction_t *dir)
88 {
89     rrs_move_t *move;
90
91     if (solution->num_moves < 1)
92         return RR_STATUS_HISTORY_EMPTY;
93
94     move = &solution->move[--solution->num_moves];
95
96     if (robot)
97         *robot = move->robot;
98     if (dir)
99         *dir = move->dir;
100
101     return RR_STATUS_SUCCESS;
102 }
103
104 rr_status_t
105 rrs_solution_prepend (rrs_solution_t *solution,
106                       rr_robot_t robot, rr_direction_t dir)
107 {
108     rrs_move_t *move;
109
110     _rrs_solution_grow (solution);
111
112     memmove (&solution->move[1], &solution->move[0],
113              solution->num_moves * sizeof (rrs_move_t));
114     move = &solution->move[0];
115     move->robot = robot;
116     move->dir = dir;
117     solution->num_moves++;
118
119     return RR_STATUS_SUCCESS;
120 }