-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericList.c
178 lines (163 loc) · 3.83 KB
/
GenericList.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
#include "GenericList.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef struct GenListNode_t {
void* data;
struct GenListNode_t* pNext;
}GenListNode;
struct GenList_t {
GenListNode* head;
Compare cmp;
size_t dataSize;
size_t count;
};
static GenListErr genListCopyNodeData(void* data, size_t dataSize, void* buf, size_t bufSize)
{
if (!data || !buf) {
return GEN_LIST_ERR;
}
memcpy(buf, data, bufSize < dataSize ? bufSize : dataSize);
return GEN_LIST_NO_ERR;
}
static GenListNode* genLisNewNode(void* data, size_t dataSize)
{
if (!data) {
return NULL;
}
GenListNode* node = malloc(sizeof(*node));
if (!node) {
return NULL;
}
memset(node, 0, sizeof(*node));
node->data = malloc(dataSize);
if (!node->data) {
return NULL;
}
memcpy(node->data, data, dataSize);
return node;
}
static GenListErr genListDestroyNode(GenListNode* node)
{
if (!node) {
return GEN_LIST_ERR;
}
free(node->data);
free(node);
return GEN_LIST_NO_ERR;
}
GenList* genListNew(size_t dataSize, Compare cmp)
{
GenList* list = malloc(sizeof(*list));
if (!list) {
return NULL;
}
memset(list, 0, sizeof(*list));
list->dataSize = dataSize;
if (cmp) {
list->cmp = cmp;
}
return list;
}
GenListErr genListPushHead(GenList* list, void* data)
{
if (!list || !data) {
return GEN_LIST_ERR;
}
if (list->count == SIZE_MAX) {
return GEN_LIST_ERR;
}
GenListNode* node = genLisNewNode(data, list->dataSize);
if (!node) {
return GEN_LIST_ERR;
}
node->pNext = list->head;
list->head = node;
list->count++;
return GEN_LIST_NO_ERR;
}
GenListErr genListPopHead(GenList* list, void* buf, size_t size)
{
GenListNode* node = NULL;
GenListErr status = GEN_LIST_NO_ERR;
if (!list) {
return GEN_LIST_ERR;
}
if (list->head) {
status = genListCopyNodeData(list->head->data, list->dataSize, buf, size);
if (status != GEN_LIST_NO_ERR) {
return status;
}
node = list->head;
list->head = node->pNext;
genListDestroyNode(node);
if (list->count)
list->count--;
}
return GEN_LIST_NO_ERR;
}
GenListErr genListGetIndex(GenList* list, size_t index, void* buf, size_t size)
{
GenListNode* node = NULL;
size_t count = 0;
if (!list) {
return GEN_LIST_ERR;
}
node = list->head;
while(node && count < list->count) {
if (count == index) {
return genListCopyNodeData(node->data, list->dataSize, buf, size);
}
node = node->pNext;
count++;
}
return GEN_LIST_ERR;
}
GenListErr genListSearchNode(GenList* list, void* data, size_t *index)
{
GenListNode* node = NULL;
size_t count = 0;
if (!list || !data) {
return GEN_LIST_ERR;
}
node = list->head;
while(node) {
if (list->cmp) {
if (list->cmp(node->data, data) == GEN_LIST_NO_ERR) {
if (index){
*index = count;
}
return GEN_LIST_NO_ERR;
}
}
node = node->pNext;
count++;
}
return GEN_LIST_ERR;
}
GenListErr genListDestroy(GenList* list)
{
GenListNode* node = NULL;
GenListErr status = GEN_LIST_NO_ERR;
if (!list) {
return GEN_LIST_ERR;
}
while(list->head) {
node = list->head;
list->head = node->pNext;
status = genListDestroyNode(node);
if (status != GEN_LIST_NO_ERR) {
return status;
}
}
free(list);
return GEN_LIST_NO_ERR;
}
GenListErr genListGetSize(GenList* list, size_t *size)
{
if (!list || !size) {
return GEN_LIST_ERR;
}
*size = list->count;
return GEN_LIST_NO_ERR;
}