-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
103 lines (98 loc) · 6.17 KB
/
list.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
/**
* @file list.c
* @author Vladimír Hucovič (xhucov00)
* @brief implementation of double linked list
*
*/
#include "list.h"
#include "variable.h"
#include "misc.h"
#ifndef GENERIC_LIST
#define GENERIC_LIST(TYPE, TYPE_NAME) \
\
void TYPE_NAME##ListInit(TYPE_NAME##List* list) { \
list->first = NULL; \
list->last = NULL; \
list->active = NULL; \
list->len = 0; \
} \
\
TYPE_NAME##Elem* TYPE_NAME##ListGetFirst(TYPE_NAME##List* list){ \
return list->first; \
} \
void TYPE_NAME##ListAppend(TYPE_NAME##List* list, TYPE data){ \
TYPE_NAME##Elem* new = malloc(sizeof(TYPE_NAME##Elem)); \
CHECK_ALLOCATION(new) \
new->data = data; \
new->next = NULL; \
new->prev = list->last; \
if(!list->len){ \
list->first = new; \
list->last = new; \
list->len++; \
return; \
} \
list->last->next = new; \
list->last = new; \
list->len++; \
} \
void TYPE_NAME##ListDispose(TYPE_NAME##List* list){ \
TYPE_NAME##Elem* iter; \
TYPE_NAME##Elem* deleted; \
iter = list->first; \
while(iter){ \
deleted = iter; \
iter = iter->next; \
free(deleted); \
} \
list->first = NULL; \
list->last = NULL; \
list->len = 0; \
\
} \
void TYPE_NAME##ListFirst(TYPE_NAME##List* list){ \
list->active = list->first; \
} \
void TYPE_NAME##ListLast(TYPE_NAME##List* list){ \
list->active = list->last; \
} \
void TYPE_NAME##ListNext(TYPE_NAME##List* list){ \
if(!list->active){ \
return; \
} \
list->active = list->active->next; \
} \
void TYPE_NAME##ListPrev(TYPE_NAME##List* list){ \
if(!list->active){ \
return; \
} \
list->active = list->active->prev; \
} \
TYPE TYPE_NAME##ListGetValue(TYPE_NAME##List* list){ \
return list->active->data; \
} \
bool TYPE_NAME##ListIsActive(TYPE_NAME##List* list){ \
if(list->active){ \
return true; \
} \
return false; \
}
GENERIC_LIST(char, char)
GENERIC_LIST(Token*, tok)
GENERIC_LIST(variable, var)
GENERIC_LIST(Nonterminal*, nonterm)
// Converts the charList to char*
char* charListToString(charList *l){
char* str = malloc((l->len+1)*sizeof(char));
CHECK_ALLOCATION(str)
charElem* iter = charListGetFirst(l);
int i = 0;
while(i < l->len){
str[i] = iter->data;
iter = iter->next;
i++;
}
str[i] = '\0';
return str;
}
#endif