-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeque.c
673 lines (600 loc) · 15.7 KB
/
deque.c
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
double-ended queue
/dek/ (deque) A queue which can have items added or removed from either end.
The Knuth reference below reports that the name was coined by E. J. Schweppe.
[D. E. Knuth, "The Art of Computer Programming. Volume 1: Fundamental Algorithms", second
edition, Sections 2.2.1, 2.6, Addison-Wesley, 1973].
Parts of this code's algorithms have been adapted from
http://github.com/posborne/simpleds
*/
#include <string.h>
#include <assert.h>
#include "containers.h"
#include "ccl_internal.h"
struct deque_t {
DequeInterface *VTable;
size_t count;
unsigned Flags;
size_t ElementSize;
DlistElement *head;
DlistElement *tail;
CompareFunction compare;
ErrorFunction RaiseError; /* Error function */
ContainerAllocator *Allocator;
unsigned timestamp;
DestructorFunction DestructorFn;
};
typedef DlistElement *DequeNode;
static int default_comparator(const void *left,const void *right,CompareInfo *ExtraArgs)
{
size_t siz=((Deque *)ExtraArgs->ContainerLeft)->ElementSize;
return memcmp(left,right,siz);
}
/* Create a deque and return a reference, if memory cannot be allocated for
* the deque, NULL will be returned.
*
*/
static Deque * Init(Deque *d, size_t elementsize)
{
memset(d,0,sizeof(struct deque_t));
/* store a pointer to the comparison function */
d->compare = default_comparator;
d->ElementSize = elementsize;
d->VTable =&iDeque;
d->Allocator = CurrentAllocator;
return d;
}
static Deque * Create(size_t elementsize)
{
Deque * d = CurrentAllocator->malloc(sizeof(struct deque_t));
if (d == NULL) {
iError.RaiseError("iDeque.Create",CONTAINER_ERROR_BADARG);
return NULL;
}
return Init(d,elementsize);
}
/* Free the data allocated for the deque and all nodes */
static int Finalize(Deque * d)
{
unsigned Flags;
int r;
if (d == NULL) return iError.NullPtrError("iDeque.Finalize");
Flags = d->Flags;
r = iDeque.Clear(d);
if (r < 0) return r;
if (Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(d,CCL_FINALIZE,NULL,NULL);
d->Allocator->free(d);
return r;
}
/* Append the specified item to the right end of the deque (head).
*/
static int Add(Deque * d,const void* item)
{
DequeNode newNode;
if (d == NULL)
return iError.NullPtrError("iDeque.Add");
if (d->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(d,CCL_ADD,item,NULL);
/* allocate memory for the new node and put it in a valid state */
newNode = d->Allocator->malloc(sizeof(DlistElement)+d->ElementSize);
if (newNode == NULL) {
iError.RaiseError("iDeque.Add",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
newNode->Previous = d->head;
newNode->Next = NULL;
memcpy(newNode->Data,item,d->ElementSize);
if (d->head != NULL) {
d->head->Next = newNode;
}
if (d->tail == NULL) {
d->tail = newNode; /* only one item */
}
d->head = newNode;
++d->count;
return 1;
}
/* Append the specified item to the left end of the deque (tail).
*/
static int AddLeft(Deque * d, void* item) {
DequeNode newNode;
assert(d != NULL);
/* create the new node and put it in a valid state */
newNode = d->Allocator->malloc(sizeof(DlistElement)+d->ElementSize);
newNode->Next = d->tail;
newNode->Previous = NULL;
memcpy(newNode->Data, item,d->ElementSize);
if (d->tail != NULL) {
d->tail->Previous = newNode;
}
if (d->head == NULL) {
d->head = d->tail;
}
d->tail = newNode;
++d->count;
return 1;
}
/* Clear the specified deque, this will free all data structures related to the
* deque itself but will not free anything free the items being pointed to.
*
* This operation is O(n) where n is the number of elements in the deque.
*/
static int Clear(Deque * d)
{
DequeNode tmp;
if (d == NULL) {
return iError.NullPtrError("iDeque.Clear");
}
if (d->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(d,CCL_CLEAR,NULL,NULL);
while (d->head != NULL) {
tmp = d->head;
d->head = tmp->Next;
if (d->DestructorFn)
d->DestructorFn(tmp);
d->Allocator->free(tmp);
}
d->head = NULL;
d->tail = NULL;
d->count = 0;
return 0;
}
/* Remove the rightmost element from the deque and return a reference to the
* Data pointed to by the deque node. If there is no rightmost element
* then NULL will be returned.
*
* This operation is O(1), constant time.
*/
static int PopFront(Deque * d,void *outbuf)
{
DequeNode PreviousHead;
void* Data;
if (d == NULL || outbuf == NULL) {
return iError.NullPtrError("iDeque.PopBack");
}
if ((PreviousHead = d->head) == NULL) {
return 0;
}
else {
d->head = PreviousHead->Previous;
d->count--;
Data = PreviousHead->Data;
if (d->DestructorFn)
d->DestructorFn(PreviousHead);
if (d->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(d,CCL_POP,Data,NULL);
d->Allocator->free(PreviousHead);
memcpy(outbuf,Data,d->ElementSize);
return 1;
}
}
/* Get the Data of the deque tail or NULL if the deque is empty */
static int PeekFront(Deque * d,void *outbuf)
{
if (d == NULL || outbuf == NULL) {
iError.RaiseError("iDeque.PopBack",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (d->head == NULL) {
return 0;
}
memcpy(outbuf,d->head->Data,d->ElementSize);
return 1;
}
/* Remove the leftmost element from the deque and return a reference to the
* Data pointed to by the deque node. If there is no leftmost element
* then NULL will be returned.
*
* This operation is O(1), constant time.
*/
static int PopBack(Deque * d,void *outbuf)
{
DequeNode PreviousTail;
void* Data;
if (d == NULL || outbuf == NULL) {
iError.RaiseError("iDeque.PopBack",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (d->tail == NULL) {
return 0;
}
else {
PreviousTail = d->tail;
d->tail = PreviousTail->Next;
if (d->tail != NULL) {
d->tail->Previous = NULL;
}
d->count--;
Data = PreviousTail->Data;
if (d->DestructorFn)
d->DestructorFn(PreviousTail);
d->Allocator->free(PreviousTail);
memcpy(outbuf,Data,d->ElementSize);
return 1;
}
}
/* Get the Data of the deque head (leftmost element) or NULL if empty */
static int PeekBack(Deque * d,void *outbuf)
{
if (d == NULL || outbuf == NULL) {
iError.RaiseError("iDeque.PopBack",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
if (d->tail == NULL) {
return 0;
}
memcpy(outbuf,d->tail->Data,d->ElementSize);
return 1;
}
/* Remove the first occurrence of item from the deque, starting from the left.
* A reference to the removed node Data will be returned, otherwise NULL
* will be returned (if the item cannot be found).
*
* Note that the comparison is done on the Datas of the data pointers, so
* even if I had two strings "foo" and "foo" at different places in memory,
* we would not get a match.
*
* This operation executes in O(n) time where n is the number of elements in
* the deque due to a linear search for the item.
*/
static int EraseInternal(Deque * d,const void* item,int all)
{
DequeNode tmp = d->tail,deleted;
CompareInfo ci;
ci.ContainerLeft = d;
ci.ExtraArgs = NULL;
while (tmp != NULL) {
if ((d->compare)(tmp->Data, item,&ci) == 0) {
if (tmp->Previous != NULL) {
tmp->Previous->Next = tmp->Next;
}
if (tmp->Next != NULL) {
tmp->Next->Previous = tmp->Previous;
}
if (d->DestructorFn)
d->DestructorFn(tmp);
deleted = tmp;
tmp = tmp->Previous;
d->Allocator->free(deleted);
d->count--;
if (all == 0) return 1;
}
if (tmp)
tmp = tmp->Next;
}
return 0; /* item not found in deque */
}
static int Erase(Deque * d,const void* item)
{
return EraseInternal(d,item,0);
}
static int EraseAll(Deque * d,const void* item)
{
return EraseInternal(d,item,1);
}
/* Return the number of items in the deque */
static size_t GetCount(Deque * d) {
return d->count;
}
/* Copy the deque and return a reference to the new deque */
static Deque *Copy(Deque * d)
{
Deque * NewDeque;
DequeNode tmp;
if (d == NULL) {
iError.NullPtrError("iDeque.Copy");
return NULL;
}
NewDeque = Create(d->ElementSize);
tmp = d->head;
while (tmp != NULL) {
Add(NewDeque, tmp->Data);
tmp = tmp->Next;
}
NewDeque->Allocator = d->Allocator;
NewDeque->compare = d->compare;
NewDeque->Flags = NewDeque->Flags;
NewDeque->RaiseError = d->RaiseError;
if (d->Flags & CONTAINER_HAS_OBSERVER)
iObserver.Notify(d,CCL_COPY,NewDeque,NULL);
return NewDeque;
}
/* Reverse the order of the items in the deque */
static int Reverse(Deque * d)
{
DequeNode currNode;
DequeNode NextNode;
if (d == NULL) {
return iError.NullPtrError("iDeque.Reverse");
}
if (d->count == 0)
return 0;
currNode = d->head;
while (currNode != NULL) {
NextNode = currNode->Next;
currNode->Next = currNode->Previous;
currNode->Previous = NextNode;
currNode = NextNode;
}
/* flip head and tail */
currNode = d->tail;
d->tail = d->head;
d->head = currNode;
return 1;
}
/* Return TRUE if the deque contains the specified item and FALSE if not */
static size_t Contains(Deque * d, void* item)
{
size_t pos;
DequeNode tmp = d->head;
CompareInfo ci;
ci.ContainerLeft = d;
ci.ExtraArgs = NULL;
pos=1;
while (tmp != NULL) {
if (d->compare(tmp->Data, item,&ci) == 0) {
return pos;
}
tmp = tmp->Next;
pos++;
}
return 0; /* item not found in deque */
}
static int Equal(Deque * d, Deque *d1)
{
DequeNode tmp = d->head;
DequeNode tmp1 = d1->head;
CompareInfo ci;
ci.ContainerLeft = d;
ci.ExtraArgs = NULL;
if (d->ElementSize != d1->ElementSize)
return 0;
while (tmp != NULL && tmp1 != NULL) {
if (d->compare(tmp->Data, tmp1->Data,&ci) != 0) {
return 0;
}
tmp = tmp->Next;
tmp1 = tmp1->Next;
}
if (tmp != tmp1) return 0;
return 1;
}
static void Apply(Deque * d, int (*Applyfn)(void *,void * arg),void *arg)
{
DequeNode tmp = d->head;
while (tmp != NULL) {
Applyfn(tmp->Data, arg);
tmp = tmp->Next;
}
}
static unsigned GetFlags(Deque *d) {
return d->Flags;
}
static unsigned SetFlags(Deque *d,unsigned newflags){
unsigned oldflags = d->Flags;
d->Flags = newflags;
d->timestamp++;
return oldflags;
}
static int Save(const Deque *d,FILE *stream, SaveFunction saveFn,void *arg)
{
size_t i;
DequeNode tmp = d->head;
if (fwrite(d,1,sizeof(Deque),stream) == 0)
return EOF;
if (saveFn == NULL) {
for (i=0; i<d->count; i++) {
if (0 == fwrite(tmp->Data,1,d->ElementSize,stream))
return EOF;
tmp = tmp->Next;
}
}
else {
for (i=0; i< d->count; i++) {
if (saveFn(tmp->Data,arg,stream) <= 0)
return EOF;
tmp = tmp->Next;
}
}
return 0;
}
static Deque *Load(FILE *stream, ReadFunction loadFn,void *arg)
{
size_t i;
char *buf;
Deque D,*d;
if (fread(&D,1,sizeof(Deque),stream) == 0)
return NULL;
d = Create(D.ElementSize);
if (d == NULL)
return NULL;
buf = malloc(D.ElementSize);
if (buf == NULL) {
Finalize(d);
iError.RaiseError("iDeque.Load",CONTAINER_ERROR_NOMEMORY);
return NULL;
}
for (i=0; i<D.count;i++) {
if (loadFn == NULL) {
if (fread(buf,1,D.ElementSize,stream) == 0)
break;
}
else {
if (loadFn(buf,arg,stream) <= 0) {
break;
}
}
Add(d,buf);
}
free(buf);
d->count = D.count;
d->Flags = D.Flags;
return d;
}
static ErrorFunction SetErrorFunction(Deque *l,ErrorFunction fn)
{
ErrorFunction old;
if (l == NULL) return iError.RaiseError;
old = l->RaiseError;
l->RaiseError = (fn) ? fn : iError.EmptyErrorFunction;
return old;
}
static size_t Sizeof(Deque *d)
{
size_t result = sizeof(Deque);
if (d == NULL)
return result;
result += d->count*(sizeof(DequeNode)+d->ElementSize);
return result;
}
struct DequeIterator {
Iterator it;
Deque *D;
size_t index;
DequeNode Current;
unsigned timestamp;
char ElementBuffer[1];
};
static void *GetNext(Iterator *it)
{
struct DequeIterator *li = (struct DequeIterator *)it;
Deque *D = li->D;
void *result;
if (li->index >= (D->count-1) || li->Current == NULL)
return NULL;
if (li->timestamp != D->timestamp) {
D->RaiseError("GetNext",CONTAINER_ERROR_OBJECT_CHANGED);
return NULL;
}
li->Current = li->Current->Next;
li->index++;
result = li->Current->Data;
return result;
}
static void *GetPrevious(Iterator *it)
{
struct DequeIterator *li = (struct DequeIterator *)it;
Deque *L = li->D;
DequeNode rvp;
size_t i;
if (li->index >= L->count || li->index == 0)
return NULL;
if (li->timestamp != L->timestamp) {
L->RaiseError("GetNext",CONTAINER_ERROR_OBJECT_CHANGED);
return NULL;
}
rvp = L->head;
i=0;
li->index--;
if (li->index > 0) {
while (rvp != NULL && i < li->index) {
rvp = rvp->Next;
i++;
}
}
if (rvp == NULL) return NULL;
li->Current = rvp;
return rvp->Data;
}
static void *GetCurrent(Iterator *it)
{
struct DequeIterator *li = (struct DequeIterator *)it;
return li->Current;
}
static void *GetFirst(Iterator *it)
{
struct DequeIterator *li = (struct DequeIterator *)it;
Deque *L = li->D;
if (L->count == 0)
return NULL;
if (li->timestamp != L->timestamp) {
L->RaiseError("GetNext",CONTAINER_ERROR_OBJECT_CHANGED);
return NULL;
}
li->index = 0;
li->Current = L->head;
return L->head->Data;
}
static Iterator *NewIterator(Deque *L)
{
struct DequeIterator *result = L->Allocator->malloc(sizeof(struct DequeIterator));
if (result == NULL)
return NULL;
result->it.GetNext = GetNext;
result->it.GetPrevious = GetPrevious;
result->it.GetFirst = GetFirst;
result->it.GetCurrent = GetCurrent;
result->D = L;
result->timestamp = L->timestamp;
return &result->it;
}
static int InitIterator(Deque *L,void *buf)
{
struct DequeIterator *result = buf;
result->it.GetNext = GetNext;
result->it.GetPrevious = GetPrevious;
result->it.GetFirst = GetFirst;
result->it.GetCurrent = GetCurrent;
result->D = L;
result->timestamp = L->timestamp;
return 1;
}
static int DeleteIterator(Iterator *it)
{
struct DequeIterator *li;
Deque *L;
if (it == NULL) {
iError.RaiseError("DeleteIterator",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
li = (struct DequeIterator *)it;
L = li->D;
L->Allocator->free(it);
return 1;
}
static DestructorFunction SetDestructor(Deque *cb,DestructorFunction fn)
{
DestructorFunction oldfn;
if (cb == NULL)
return NULL;
oldfn = cb->DestructorFn;
if (fn)
cb->DestructorFn = fn;
return oldfn;
}
static size_t SizeofIterator(Deque *l)
{
return sizeof(struct DequeIterator);
}
DequeInterface iDeque = {
GetCount,
GetFlags,
SetFlags,
Clear,
Contains,
Erase,
EraseAll,
Finalize,
Apply,
Equal,
Copy,
SetErrorFunction,
Sizeof,
NewIterator,
InitIterator,
DeleteIterator,
SizeofIterator,
Save,
Load,
Add,
AddLeft,
Reverse,
PopBack,
PeekBack,
PopFront,
PeekFront,
Create,
Init,
SetDestructor,
};