Skip to content

Commit e1cefa3

Browse files
authored
Merge pull request #79 from saxbophone/josh/58-progress-callback
Add optional callback function pointer argument to plot_spiral()
2 parents 3edd475 + cdbc99e commit e1cefa3

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

saxbospiral/solve.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,23 @@ status_t resize_spiral(
240240
/*
241241
* given a pointer to a spiral spiral for which the length of all its lines are
242242
* not yet known, a perfection threshold (-1 for no perfection, or otherwise
243-
* the maximmum line length at which to allow aggressive optimisation) and the
244-
* index of the highest line to plot to, calculate the length needed for each
245-
* line in the spiral up to this index (to avoid line overlap) and store these
246-
* in a the spiral struct that is pointed to by the pointer
243+
* the maximmum line length at which to allow aggressive optimisation), the
244+
* index of the highest line to plot to and a pointer to a callback function,
245+
* calculate the length needed for each line in the spiral up to this index
246+
* (to avoid line overlap) and store these in a the spiral struct that is
247+
* pointed to by the pointer
248+
* the function pointer can be NULL, if it is not then it will be called every
249+
* time a new line of the spiral is solved. The function should be of return
250+
* type void and take three arguments: a pointer to a spiral_t struct, an
251+
* integer specifying the index of the latest solved line and an integer
252+
* specifying the index of the highest line that will be solved.
247253
* returns a status struct (used for error information)
248254
*/
249255
status_t plot_spiral(
250-
spiral_t * spiral, int perfection_threshold, uint64_t max_line
256+
spiral_t * spiral, int perfection_threshold, uint64_t max_line,
257+
void(* progress_callback)(
258+
spiral_t* spiral, uint64_t latest_line, uint64_t target_line
259+
)
251260
) {
252261
// set up result status
253262
status_t result = {{0, 0, 0}, 0};
@@ -260,6 +269,10 @@ status_t plot_spiral(
260269
if(result.diagnostic != OPERATION_OK) {
261270
return result;
262271
}
272+
// call callback if given
273+
if(progress_callback != NULL) {
274+
progress_callback(spiral, i, max_index);
275+
}
263276
}
264277
// all ok
265278
result.diagnostic = OPERATION_OK;

saxbospiral/solve.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,23 @@ status_t resize_spiral(
2525
/*
2626
* given a pointer to a spiral spiral for which the length of all its lines are
2727
* not yet known, a perfection threshold (-1 for no perfection, or otherwise
28-
* the maximmum line length at which to allow aggressive optimisation) and the
29-
* index of the highest line to plot to, calculate the length needed for each
30-
* line in the spiral up to this index (to avoid line overlap) and store these
31-
* in a the spiral struct that is pointed to by the pointer
28+
* the maximmum line length at which to allow aggressive optimisation), the
29+
* index of the highest line to plot to and a pointer to a callback function,
30+
* calculate the length needed for each line in the spiral up to this index
31+
* (to avoid line overlap) and store these in a the spiral struct that is
32+
* pointed to by the pointer
33+
* the function pointer can be NULL, if it is not then it will be called every
34+
* time a new line of the spiral is solved. The function should be of return
35+
* type void and take three arguments: a pointer to a spiral_t struct, an
36+
* integer specifying the index of the latest solved line and an integer
37+
* specifying the index of the highest line that will be solved.
3238
* returns a status struct (used for error information)
3339
*/
3440
status_t plot_spiral(
35-
spiral_t * spiral, int perfection_threshold, uint64_t max_line
41+
spiral_t * spiral, int perfection_threshold, uint64_t max_line,
42+
void(* progress_callback)(
43+
spiral_t* spiral, uint64_t latest_line, uint64_t target_line
44+
)
3645
);
3746

3847
#ifdef __cplusplus

sxp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ bool run(
213213
(uint64_t)total_lines : lines_to_plot
214214
);
215215
// we must plot all lines from spiral file
216-
if(handle_error(plot_spiral(&spiral, perfection, lines_to_plot))) {
216+
if(handle_error(plot_spiral(&spiral, perfection, lines_to_plot, NULL))) {
217217
// handle errors
218218
return false;
219219
}

tests.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ bool test_plot_spiral() {
204204
}
205205

206206
// call plot_spiral on spiral
207-
plot_spiral(&spiral, 1, 16);
207+
plot_spiral(&spiral, 1, 16, NULL);
208208

209209
// check solved count
210210
if(spiral.solved_count != expected.solved_count) {
@@ -248,7 +248,7 @@ bool test_plot_spiral_partial() {
248248
}
249249

250250
// call plot_spiral on spiral, with instruction to only plot up to line 9
251-
plot_spiral(&spiral, 1, 9);
251+
plot_spiral(&spiral, 1, 9, NULL);
252252

253253
// check solved count
254254
if(spiral.solved_count != expected.solved_count) {

0 commit comments

Comments
 (0)