Add a STYLUS_BASED variant to magnets
[sgt-puzzles/ydirson.git] / tdq.c
blobd66f9f4a8343981ae386c53dbb635a1e7b6c72ca
1 /*
2 * tdq.c: implement a 'to-do queue', a simple de-duplicating to-do
3 * list mechanism.
4 */
6 #include <assert.h>
8 #include "puzzles.h"
11 * Implementation: a tdq consists of a circular buffer of size n
12 * storing the integers currently in the queue, plus an array of n
13 * booleans indicating whether each integer is already there.
15 * Using a circular buffer of size n to store between 0 and n items
16 * inclusive has an obvious failure mode: if the input and output
17 * pointers are the same, how do you know whether that means the
18 * buffer is full or empty?
20 * In this application we have a simple way to tell: in the former
21 * case, the flags array is all 1s, and in the latter case it's all
22 * 0s. So we could spot that case and check, say, flags[0].
24 * However, it's even easier to simply determine whether the queue is
25 * non-empty by testing flags[buffer[op]] - that way we don't even
26 * _have_ to compare ip against op.
29 struct tdq {
30 int n;
31 int *queue;
32 int ip, op; /* in pointer, out pointer */
33 char *flags;
36 tdq *tdq_new(int n)
38 int i;
39 tdq *tdq = snew(struct tdq);
40 tdq->queue = snewn(n, int);
41 tdq->flags = snewn(n, char);
42 for (i = 0; i < n; i++) {
43 tdq->queue[i] = 0;
44 tdq->flags[i] = 0;
46 tdq->n = n;
47 tdq->ip = tdq->op = 0;
48 return tdq;
51 void tdq_free(tdq *tdq)
53 sfree(tdq->queue);
54 sfree(tdq->flags);
55 sfree(tdq);
58 void tdq_add(tdq *tdq, int k)
60 assert((unsigned)k < (unsigned)tdq->n);
61 if (!tdq->flags[k]) {
62 tdq->queue[tdq->ip] = k;
63 tdq->flags[k] = 1;
64 if (++tdq->ip == tdq->n)
65 tdq->ip = 0;
69 int tdq_remove(tdq *tdq)
71 int ret = tdq->queue[tdq->op];
73 if (!tdq->flags[ret])
74 return -1;
76 tdq->flags[ret] = 0;
77 if (++tdq->op == tdq->n)
78 tdq->op = 0;
80 return ret;
83 void tdq_fill(tdq *tdq)
85 int i;
86 for (i = 0; i < tdq->n; i++)
87 tdq_add(tdq, i);