aboutsummaryrefslogtreecommitdiff
path: root/deque/src/squeue.c
blob: 0c24c93c2ebb01229794fc5765a2f4c57ce1cf0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 *
 * File: squeue.h
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 *
 * Copyright (c) 2003 Bryan Jurish.  All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 */

#include <stdlib.h>
#include <errno.h>
#include "squeue.h"

/*----------------------------------------------------------------------
 * Creation / Deletion
 *----------------------------------------------------------------------*/
squeue_ptr squeue_new(unsigned int size) {
  squeue_ptr sq = (squeue_ptr)malloc(sizeof(squeue_t));
  if (!sq || !size) {
    errno = ENOMEM;
    return NULL;
  }
  sq->data = (void **)malloc(size*sizeof(void *));
  if (!sq->data) {
    errno = ENOMEM;
    return NULL;
  }
# ifdef SQUEUE_DEBUG
  memset(sq->data,0,size*sizeof(void *));
# endif
  sq->size = size-1;
  sq->head = NULL;
  sq->tail = NULL;
  return sq;
}

void squeue_destroy(squeue_ptr sq) {
  squeue_clear(sq);
  free(sq->data);
  free(sq);
}

/*----------------------------------------------------------------------
 * Predicates
 *----------------------------------------------------------------------*/
#ifdef SQUEUE_DEBUG

char squeue_empty(squeue_ptr sq) {
  return (!sq->head);
}

char squeue_full(squeue_ptr sq) {
  return (sq->head && sq->head == squeue_next(sq,sq->tail));
}

#endif


/*----------------------------------------------------------------------
 * utilities
 *----------------------------------------------------------------------*/
#ifdef SQUEUE_DEBUG

void **squeue_prev(squeue_t *sq, void **p) {
  //return (p && p > sq->data ? p-1 : p+sq->size);
  return (p && p > sq->data ? p-1 : sq->data+sq->size);
}

void **squeue_next(squeue_t *sq, void **p) {
  return (p && p < sq->data+sq->size ? p+1 : sq->data);
}

#endif

/*----------------------------------------------------------------------
 * Manipulation
 *----------------------------------------------------------------------*/

#ifdef SQUEUE_DEBUG

void squeue_clear(squeue_ptr sq) {
  sq->head = NULL;
  sq->tail = NULL;
# ifdef SQUEUE_DEBUG
  memset(sq->data,0,sq->size*sizeof(void *));
# endif
}

#endif

void **squeue_prepend(squeue_ptr sq, void *data) {
  sq->head = squeue_prev(sq,sq->head);
  if (!sq->tail) {
    // -- empty-queue
    sq->tail = sq->head;
  } else if (sq->head == sq->tail) {
    // -- xrun (full queue)
    sq->head = squeue_next(sq,sq->head);
    return NULL;
    // alternative: push the overwritten older pointer along a notch
    //sq->tail = squeue_prev(sq,sq->tail);
  }
  // -- set the data
  *(sq->head) = data;
  return sq->head;
}

void **squeue_append(squeue_ptr sq, void *data) {
  sq->tail = squeue_next(sq,sq->tail);
  if (!sq->head) {
    // -- empty-queue check
    sq->head = sq->tail;
  } else if (sq->tail == sq->head) {
    // -- xrun (full queue)
    sq->tail = squeue_prev(sq,sq->tail);
    return NULL;
    // alternative: push the overwritten older pointer along a notch
    //sq->head = squeue_next(sq,sq->head);
  }
  // -- set the data
  *(sq->tail) = data;
  return sq->tail;
}


/*----------------------------------------------------------------------
 * Access
 *----------------------------------------------------------------------*/

void *squeue_shift(squeue_ptr sq) {
  if (squeue_empty(sq)) return NULL;
  else {
    void *data = *(sq->head);
    // -- empty queue check
    if (sq->head == sq->tail) {
      sq->head = NULL;
      sq->tail = NULL;
    } else {
      sq->head = squeue_next(sq, sq->head);
    }
    return data;
  }
}

void *squeue_pop(squeue_ptr sq) {
  if (squeue_empty(sq)) return NULL;
  else {
    void *data = *(sq->tail);
    if (sq->head == sq->tail) {
      sq->head = NULL;
      sq->tail = NULL;
    } else {
      sq->tail = squeue_prev(sq, sq->tail);
    }
    return data;
  }
}

#ifdef SQUEUE_DEBUG

// Returns the first datum in the queue, or NULL if queue is empty.
void *squeue_peek_head(squeue_ptr sq) {
  return sq->head ? *(sq->head) : NULL;
}

// Returns the final datum in the queue, or NULL if queue is empty.
void *squeue_peek_tail(squeue_ptr sq) {
  return sq->tail ? *(sq->tail) : NULL;
}

#endif

/*----------------------------------------------------------------------
 * Iteration
 *----------------------------------------------------------------------*/
#ifdef SQUEUE_DEBUG

// Returns a pointer-pointer to the first datum in the queue,
// or NULL if queue is empty.
squeue_iter_t squeue_iter_first(squeue_t *sq) { return sq->head; }

// Returns a pointer-pointer to the final datum in the queue,
// or NULL if queue is empty.
squeue_iter_t squeue_iter_last(squeue_t *sq) { return sq->tail; }



// Returns a pointer to the next datum in the queue, or
// NULL if queue is empty.
squeue_iter_t squeue_iter_next(squeue_t *sq, squeue_iter_t p) {
  return p == sq->tail ? NULL : squeue_next(sq,p);
}

// Returns a pointer-pointer to the final datum in the queue,
// or NULL if queue is empty.
extern squeue_iter_t squeue_iter_prev(squeue_t *sq, squeue_iter_t p) {
  return p == sq->head ? NULL : squeue_prev(sq,p);
}


// Returns a true value if p is a valid iterator value, false otherwise.
// Warning: this is not even as paranoid as it ought to be!
extern char squeue_iter_valid(squeue_t *sq, squeue_iter_t p) {
  return (p && p >= sq->data && p <= sq->data+sq->size);
}

/// Get the datum from an iterator.
void *squeue_iter_data(squeue_iter_t p) { return *p; }


#endif