2121 * You should have received a copy of the GNU Affero General Public License
2222 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2323 */
24+ #include <assert.h>
2425#include <stdlib.h>
2526
2627#include "saxbospiral.h"
3132extern "C" {
3233#endif
3334
34- // returns the sum of all line lengths within the given indexes
35+ /*
36+ * returns the sum of all line lengths within the given indexes
37+ *
38+ * Asserts:
39+ * - That start and end indexes are less than or equal to the spiral size
40+ * - That spiral.lines is not NULL
41+ */
3542size_t sxbp_sum_lines (sxbp_spiral_t spiral , size_t start , size_t end ) {
43+ // preconditional assertions
44+ assert (start <= spiral .size );
45+ assert (end <= spiral .size );
46+ assert (spiral .lines != NULL );
3647 size_t size = 0 ;
3748 for (size_t i = start ; i < end ; i ++ ) {
3849 size += spiral .lines [i ].length ;
@@ -49,22 +60,31 @@ size_t sxbp_sum_lines(sxbp_spiral_t spiral, size_t start, size_t end) {
4960 * each line segment is only one unit long, meaning multiple ones are needed for
5061 * lines longer than one unit.
5162 * returns a status struct with error information (if any)
63+ *
64+ * Asserts:
65+ * - That the output struct's items pointer is NULL
66+ * - That start and end indexes are less than or equal to the spiral size
67+ * - That spiral.lines is not NULL
5268 */
5369sxbp_status_t sxbp_spiral_points (
5470 sxbp_spiral_t spiral , sxbp_co_ord_array_t * output ,
5571 sxbp_co_ord_t start_point , size_t start , size_t end
5672) {
73+ // preconditional assertions
74+ assert (output -> items == NULL );
75+ assert (start <= spiral .size );
76+ assert (end <= spiral .size );
77+ assert (spiral .lines != NULL );
5778 // prepare result status
58- sxbp_status_t result = {{ 0 , 0 , 0 }, 0 } ;
79+ sxbp_status_t result ;
5980 // the amount of space needed is the sum of all line lengths + 1 for end
6081 size_t size = sxbp_sum_lines (spiral , start , end ) + 1 ;
6182 // allocate memory
6283 output -> items = calloc (sizeof (sxbp_co_ord_t ), size );
6384 // catch malloc error
6485 if (output -> items == NULL ) {
6586 // set error information then early return
66- result .location = SXBP_DEBUG ;
67- result .diagnostic = SXBP_MALLOC_REFUSED ;
87+ result = SXBP_MALLOC_REFUSED ;
6888 return result ;
6989 }
7090 output -> size = size ;
@@ -86,22 +106,29 @@ sxbp_status_t sxbp_spiral_points(
86106 }
87107 }
88108 // all good
89- result . diagnostic = SXBP_OPERATION_OK ;
109+ result = SXBP_OPERATION_OK ;
90110 return result ;
91111}
92112
93113/*
94- * given a pointer to a spiral struct an limit, which is the index of the last
114+ * given a pointer to a spiral struct and limit, which is the index of the last
95115 * line to use, calculate and store the co-ordinates of all line segments that
96116 * would make up the spiral if the current lengths and directions were used.
97117 * each line segment is only one unit long, meaning multiple ones are needed for
98118 * lines longer than one unit. The co-ords are stored in the spiral's
99119 * co_ord_cache member and are re-used if they are still valid
100120 * returns a status struct with error information (if any)
121+ *
122+ * Asserts:
123+ * - That spiral->lines is not NULL
124+ * - That limit is less than or equal to spiral->size
101125 */
102126sxbp_status_t sxbp_cache_spiral_points (sxbp_spiral_t * spiral , size_t limit ) {
127+ // preconditional assertions
128+ assert (spiral -> lines != NULL );
129+ assert (limit <= spiral -> size );
103130 // prepare result status
104- sxbp_status_t result = {{ 0 , 0 , 0 }, 0 } ;
131+ sxbp_status_t result ;
105132 // the amount of space needed is the sum of all line lengths + 1 for end
106133 size_t size = sxbp_sum_lines (* spiral , 0 , limit ) + 1 ;
107134 // allocate / reallocate memory
@@ -122,8 +149,7 @@ sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
122149 // catch malloc failure
123150 if (spiral -> co_ord_cache .co_ords .items == NULL ) {
124151 // set error information then early return
125- result .location = SXBP_DEBUG ;
126- result .diagnostic = SXBP_MALLOC_REFUSED ;
152+ result = SXBP_MALLOC_REFUSED ;
127153 return result ;
128154 }
129155 spiral -> co_ord_cache .co_ords .size = size ;
@@ -152,7 +178,7 @@ sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
152178 * spiral , & missing , current , smallest , limit
153179 );
154180 // return errors from previous call if needed
155- if (calculate_result . diagnostic != SXBP_OPERATION_OK ) {
181+ if (calculate_result != SXBP_OPERATION_OK ) {
156182 return calculate_result ;
157183 }
158184 // add the missing co-ords to the cache
@@ -168,7 +194,7 @@ sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
168194 limit > spiral -> co_ord_cache .validity
169195 ) ? limit : spiral -> co_ord_cache .validity ;
170196 // return ok
171- result . diagnostic = SXBP_OPERATION_OK ;
197+ result = SXBP_OPERATION_OK ;
172198 return result ;
173199}
174200
0 commit comments