-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.h
100 lines (87 loc) · 2.12 KB
/
list.h
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
#ifndef __LIST_H
#define __LIST_H
#include <stdlib.h>
#include <gc/gc.h>
#include <stdio.h>
#include <limits.h>
static const short EOL_ELEMENT = SHRT_MIN; // mark the end of list
static const short DEL_ELEMENT = SHRT_MIN + 1; // mark deleted element
static const short CROSSED_ELEMENT = SHRT_MIN + 2; // mark crossed-out element
static const short NONE = SHRT_MIN + 2; // representing None
/* is current position the end of list ? */
#define EOL(x) ((x) == EOL_ELEMENT)
/* does current position contain a None object */
#define NA(x) ((x) == DEL_ELEMENT)
/* an interator-style function, for use with while(.) to iterate over all
* elements in a list and execute the while-loop body */
#define FOREACH(x) for (short *pp = NULL, *p = foreach((x), &pp); \
p != NULL; \
p = foreach((x), &pp))
short* foreach(short*, short**);
void print(short*);
static inline short* makelist(int len, short init)
{
short* c = GC_MALLOC_ATOMIC(sizeof(short) * (len + 1));
if (init != NONE) {
for (int i = 0; i < len; i ++)
*(c + i) = init;
}
*(c + len) = EOL_ELEMENT;
return c;
}
static inline int len(short* list)
{
int l = 0;
FOREACH(list) {
if (*p != CROSSED_ELEMENT)
l ++;
}
return l;
}
/* append will always do a FOREACH first, to clean the gaps introduced by del
* and cross */
static inline void append(short* list, short element)
{
short* last = list;
FOREACH(list) {
last = p;
}
if (EOL(*last)) {
*last = element;
*(last + 1) = EOL_ELEMENT;
} else {
*(last + 1) = element;
*(last + 2) = EOL_ELEMENT;
}
}
static inline short* find(short* list, short query)
{
short *result = NULL;
FOREACH(list) {
if (result == NULL && *p == query) {
result = p;
break;
}
}
return result;
}
static inline short* del(short* list, short query)
{
short* pointer = find(list, query);
if (pointer) *pointer = DEL_ELEMENT;
return pointer;
}
static inline void cross(short* element)
{
if (element) *element = CROSSED_ELEMENT;
}
static inline short* copy(short* list)
{
int l = len(list);
short* c = makelist(l, EOL_ELEMENT);
short *c2 = c;
FOREACH(list)
*(c2 ++) = *p;
return c;
}
#endif