aboutsummaryrefslogtreecommitdiff
path: root/deque/src/dsqueue.c
blob: e328af01cafc5f17ac3f39fcc4bde51068544ca5 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * File: dsqueue.c
 * 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 "dsqueue.h"
#include "squeue.h"

/*----------------------------------------------------------------------
 * Creation / Deletion
 *----------------------------------------------------------------------*/
dsqueue_ptr dsqueue_new(unsigned blocksize) {
  dsqueue_ptr dsq = (dsqueue_ptr)malloc(sizeof(dsqueue_t));
  if (!dsq) {
    errno = ENOMEM;
    return NULL;
  }
  dsq->blocksize = blocksize;
  dsq->trash = NULL;
  dsq->head = NULL;
  dsq->tail = NULL;
  return dsq;
}

void dsqueue_destroy(dsqueue_ptr dsq) {
  dsqueue_block_t *dsqe, *dsqe_next;
  dsqueue_clear(dsq);
  for (dsqe = dsq->head; dsqe != NULL; dsqe = dsqe_next) {
    squeue_destroy(dsqe->sq);
    dsqe_next = dsqe->next;
    free(dsqe);
  }
  for (dsqe = dsq->trash; dsqe != NULL; dsqe = dsqe_next) {
    squeue_destroy(dsqe->sq);
    dsqe_next = dsqe->next;
    free(dsqe);
  }
  free(dsq);
}

/*----------------------------------------------------------------------
 * Predicates
 *----------------------------------------------------------------------*/
char dsqueue_is_empty(dsqueue_ptr dsq) {
  while (dsq->head && squeue_empty(dsq->head->sq)) {
    dsqueue_block_t *dsqe = dsqueue_block_shift(dsq);
    dsqe->next = dsq->trash;
    if (dsq->trash) dsq->trash->prev = dsqe;
    dsq->trash = dsqe;
  }
  return dsq->head ? 0 : 1;
}

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

void dsqueue_clear(dsqueue_ptr dsq) {
  if (dsq->head && dsq->tail) {
    dsq->tail->next = dsq->trash;
    if (dsq->trash) dsq->trash->prev = dsq->tail;
    dsq->trash = dsq->head;
  }
  dsq->head = NULL;
  dsq->tail = NULL;
}

dsqueue_block_t *dsqueue_prepend(dsqueue_ptr dsq, void *data) {
  dsqueue_block_t *dsqe = dsq->head;
  while (!dsqe || squeue_full(dsqe->sq)) {
    dsqe = dsqueue_block_new(dsq);
    if (!dsqe) {
      errno = ENOMEM;
      return NULL;
    }
    dsqueue_block_prepend(dsq,dsqe);
  }
  squeue_prepend(dsqe->sq,data);
  return dsqe;
}

dsqueue_block_t *dsqueue_append(dsqueue_ptr dsq, void *data) {
  dsqueue_block_t *dsqe = dsq->tail;
  while (!dsqe || squeue_full(dsqe->sq)) {
    dsqe = dsqueue_block_new(dsq);
    if (!dsqe) {
      errno = ENOMEM;
      return NULL;
    }
    dsqueue_block_append(dsq,dsqe);
  }
  squeue_append(dsqe->sq,data);
  return dsqe;
}



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

void *dsqueue_shift(dsqueue_ptr dsq) {
  dsqueue_trim_front(dsq);
  return dsq->head ? squeue_shift(dsq->head->sq) : NULL;
}

void *dsqueue_pop(dsqueue_ptr dsq) {
  dsqueue_trim_back(dsq);
  return dsq->tail ? squeue_pop(dsq->tail->sq) : NULL;
}


/*----------------------------------------------------------------------
 * Iterators
 *----------------------------------------------------------------------*/

// Returns an iterator for the first datum in the queue,
// or NULL if queue is empty.
dsqueue_iter_t dsqueue_iter_first(dsqueue_t *dsq) {
  dsqueue_iter_t dsqi = {NULL,NULL};
  dsqueue_trim_front(dsq);
  if (dsq->head) {
    dsqi.block = dsq->head;
    dsqi.sqi = squeue_iter_first(dsqi.block->sq);
  }
  return dsqi;
}

/// Returns an iterator for the final datum in the queue,
/// or NULL if queue is empty.
dsqueue_iter_t dsqueue_iter_last(dsqueue_t *dsq) {
  dsqueue_iter_t dsqi = {NULL,NULL};
  dsqueue_trim_back(dsq);
  if (dsq->tail) {
    dsqi.block = dsq->tail;
    dsqi.sqi = squeue_iter_last(dsqi.block->sq);
  }
  return dsqi;
}


/// Returns an iterator for the next datum in the queue, or
/// NULL if already at end-of-queue.
dsqueue_iter_t dsqueue_iter_next(dsqueue_t *dsq, dsqueue_iter_t dsqi) {
  while (dsqi.block) {
    dsqi.sqi = squeue_iter_next(dsqi.block->sq, dsqi.sqi);
    if (dsqi.sqi) break;
    dsqi.block = dsqi.block->next;
  }
  return dsqi;
}

/// Returns an iterator for the previous datum in the queue,
/// or NULL if already at beginning-of-queue.
dsqueue_iter_t dsqueue_iter_prev(dsqueue_t *dsq, dsqueue_iter_t dsqi) {
  while (dsqi.block) {
    dsqi.sqi = squeue_iter_prev(dsqi.block->sq, dsqi.sqi);
    if (dsqi.sqi) break;
    dsqi.block = dsqi.block->prev;
  }
  return dsqi;
}

/// Returns a true value if p is a valid iterator value, false otherwise.
char dsqueue_iter_valid(dsqueue_t *dsq, dsqueue_iter_t dsqi) {
  return (dsqi.block && dsqi.sqi && 
	  squeue_iter_valid(dsqi.block->sq, dsqi.sqi));
}

/// Get the datum from an iterator.
void *dsqueue_iter_data(dsqueue_iter_t dsqi) {
  return (dsqi.block && dsqi.sqi ? squeue_iter_data(dsqi.sqi) : NULL);
}



/*----------------------------------------------------------------------
 * Utilities
 *----------------------------------------------------------------------*/

// Allocate and return a new block.
dsqueue_block_t *dsqueue_block_new(dsqueue_t *dsq) {
  // -- first try trashbin
  dsqueue_block_t *dsqe = dsq->trash;
  if (dsqe) {
    dsq->trash = dsqe->next;
    if (dsq->trash) dsq->trash->prev = NULL;
  } else {
    dsqe = (dsqueue_block_t *)malloc(sizeof(dsqueue_block_t));
    if (!dsqe) {
      errno = ENOMEM;
      return NULL;
    }
    dsqe->sq = squeue_new(dsq->blocksize);
    if (!dsqe->sq) {
      errno = ENOMEM;
      free(dsqe);
      return NULL;
    }
  }
  // -- initialize
  dsqe->prev = NULL;
  dsqe->next = NULL;
  return dsqe;
}


#ifdef DSQUEUE_DEBUG

dsqueue_block_t *dsqueue_block_prepend(dsqueue_t *dsq, dsqueue_block_t *e) {
  if (!e) return NULL;
  else if (dsq->head) dsq->head->prev = e;
  else dsq->tail = e;
  e->next = dsq->head;
  dsq->head = e;
  return dsq->head;
}

dsqueue_block_t *dsqueue_block_append(dsqueue_t *dsq, dsqueue_block_t *e) {
  if (!e) return NULL;
  else if (dsq->tail) dsq->tail->next = e;
  else dsq->head = e;
  e->prev = dsq->tail;
  dsq->tail = e;
  return dsq->tail;
}

#endif


dsqueue_block_t *dsqueue_block_shift(dsqueue_t *dsq) {
  dsqueue_block_t *dsqe = dsq->head;
  if (!dsqe) return dsqe;
  dsq->head = dsqe->next;
  if (dsq->head) {
    dsq->head->prev = NULL;
  } else {
    dsq->tail = NULL;
  }
  dsqe->next = NULL;
  return dsqe;
}
dsqueue_block_t *dsqueue_block_pop(dsqueue_t *dsq) {
  dsqueue_block_t *dsqe = dsq->tail;
  if (!dsqe) return dsqe;
  dsq->tail = dsqe->prev;
  if (dsq->tail) {
    dsq->tail->next = NULL;
  } else {
    dsq->head = NULL;
  }
  dsqe->prev = NULL;
  return dsqe;
}


void dsqueue_trim_front(dsqueue_t *dsq) {
  while (dsq->head && squeue_empty(dsq->head->sq)) {
    dsqueue_block_t *dsqe = dsqueue_block_shift(dsq);
    if (dsq->trash) dsq->trash->prev = dsqe;
    dsqe->next = dsq->trash;
    dsq->trash = dsqe;
  }
}

void dsqueue_trim_back(dsqueue_t *dsq) {
  while (dsq->tail && squeue_empty(dsq->tail->sq)) {
    dsqueue_block_t *dsqe = dsqueue_block_pop(dsq);
    if (dsq->trash) dsq->trash->prev = dsqe;
    dsqe->next = dsq->trash;
    dsq->trash = dsqe;
  }
}