diff options
| author | kr.angelov <kr.angelov@gmail.com> | 2012-01-20 13:41:10 +0000 |
|---|---|---|
| committer | kr.angelov <kr.angelov@gmail.com> | 2012-01-20 13:41:10 +0000 |
| commit | 2eee382a62a909d5a3f2f5eda94f30fe68fd5335 (patch) | |
| tree | b0b0d513535895f244214aebf6358e172b8dce6d /src/runtime/c/gu/choice.c | |
| parent | b9728357126f8b9a6311cca17d9f0dcc2a7bfb9b (diff) | |
initial import of the C runtime
Diffstat (limited to 'src/runtime/c/gu/choice.c')
| -rw-r--r-- | src/runtime/c/gu/choice.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/runtime/c/gu/choice.c b/src/runtime/c/gu/choice.c new file mode 100644 index 000000000..b1e4a3a5e --- /dev/null +++ b/src/runtime/c/gu/choice.c @@ -0,0 +1,73 @@ +#include <gu/choice.h> +#include <gu/seq.h> +#include <gu/assert.h> +#include <gu/log.h> + +struct GuChoice { + GuBuf* path; + size_t path_idx; +}; + +GuChoice* +gu_new_choice(GuPool* pool) +{ + GuChoice* ch = gu_new(GuChoice, pool); + ch->path = gu_new_buf(uint8_t, pool); + ch->path_idx = 0; + return ch; +} + +GuChoiceMark +gu_choice_mark(GuChoice* ch) +{ + gu_assert(ch->path_idx <= gu_buf_length(ch->path)); + gu_debug("%p@%d: mark", ch, ch->path_idx); + return (GuChoiceMark){ch->path_idx}; +} + +void +gu_choice_reset(GuChoice* ch, GuChoiceMark mark) +{ + gu_assert(ch->path_idx <= gu_buf_length(ch->path)); + gu_debug("%p@%d: reset %d", ch, ch->path_idx, mark.path_idx); + gu_require(mark.path_idx <= ch->path_idx ); + ch->path_idx = mark.path_idx; +} + +int +gu_choice_next(GuChoice* ch, int n_choices) +{ + gu_assert(n_choices >= 0); + gu_require(n_choices <= UINT8_MAX); + gu_assert(ch->path_idx <= gu_buf_length(ch->path)); + if (n_choices == 0) { + return -1; + } + int i = 0; + if (gu_buf_length(ch->path) > ch->path_idx) { + i = (int) gu_buf_get(ch->path, uint8_t, ch->path_idx); + gu_assert(i <= n_choices); + } else { + gu_buf_push(ch->path, uint8_t, n_choices); + i = n_choices; + } + int ret = (i == 0) ? -1 : n_choices - i; + gu_debug("%p@%d: %d", ch, ch->path_idx, ret); + ch->path_idx++; + return ret; +} + +bool +gu_choice_advance(GuChoice* ch) +{ + gu_assert(ch->path_idx <= gu_buf_length(ch->path)); + + while (gu_buf_length(ch->path) > ch->path_idx) { + uint8_t last = gu_buf_pop(ch->path, uint8_t); + if (last > 1) { + gu_buf_push(ch->path, uint8_t, last-1); + return true; + } + } + return false; +} |
