-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
113 lines (98 loc) · 2.21 KB
/
queue.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
#include "containers.h"
/* -------------------------------------------------------------------
* QUEUES
* -------------------------------------------------------------------*/
typedef struct _Queue {
QueueInterface *VTable;
List *Items;
} _Queue;
static size_t QSize(Queue *Q)
{
return iList.Size(Q->Items);
}
static size_t Sizeof(Queue *q)
{
if (q == NULL) return sizeof(Queue);
return sizeof(*q) + iList.Sizeof(q->Items);
}
static int Finalize(Queue *Q)
{
const ContainerAllocator *allocator = iList.GetAllocator(Q->Items);
iList.Finalize(Q->Items);
allocator->free(Q);
return 1;
}
static int QClear(Queue *Q)
{
return iList.Clear(Q->Items);
}
static int Dequeue(Queue *Q,void *result)
{
return iList.PopFront(Q->Items,result);
}
static int Enqueue(Queue *Q,void *newval)
{
return iList.Add(Q->Items,newval);
}
static Queue *CreateWithAllocator(size_t ElementSize,ContainerAllocator *allocator)
{
Queue *result = allocator->malloc(sizeof(Queue));
if (result == NULL)
return NULL;
result->Items = iList.CreateWithAllocator(ElementSize,allocator);
if (result->Items == NULL) {
allocator->free(result);
return NULL;
}
result->VTable = &iQueue;
return result;
}
static Queue *Create(size_t ElementSize)
{
return CreateWithAllocator(ElementSize,CurrentAllocator);
}
static int Front(Queue *Q,void *result)
{
size_t idx;
if (Q == NULL) {
iError.RaiseError("iQueue.Front",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
idx = iList.Size(Q->Items);
if (idx == 0)
return 0;
return iList.CopyElement(Q->Items,0,result);
}
static int Back(Queue *Q,void *result)
{
size_t idx;
if (Q == NULL) {
iError.RaiseError("iQueue.Front",CONTAINER_ERROR_BADARG);
return CONTAINER_ERROR_BADARG;
}
idx = iList.Size(Q->Items);
if (idx == 0)
return 0;
return iList.CopyElement(Q->Items,idx-1,result);
}
static List *GetData(Queue *q)
{
if (q == NULL) {
iError.RaiseError("iQueue.GetData",CONTAINER_ERROR_BADARG);
return NULL;
}
return q->Items;
}
QueueInterface iQueue = {
Create,
CreateWithAllocator,
QSize,
Sizeof,
Enqueue,
Dequeue,
QClear,
Finalize,
Front,
Back,
GetData,
};