]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/linkedlist.qh
Merge branch 'TimePath/csqc_viewmodels' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / linkedlist.qh
1 #ifndef LINKEDLIST_H
2 #define LINKEDLIST_H
3
4 CLASS(LinkedListNode, Object)
5         ATTRIB(LinkedListNode, ll_data, entity, NULL)
6         ATTRIB(LinkedListNode, ll_prev, LinkedListNode, NULL)
7         ATTRIB(LinkedListNode, ll_next, LinkedListNode, NULL)
8 ENDCLASS(LinkedListNode)
9
10 CLASS(LinkedList, Object)
11         ATTRIB(LinkedList, ll_head, LinkedListNode, NULL);
12         ATTRIB(LinkedList, ll_tail, LinkedListNode, NULL);
13 ENDCLASS(LinkedList)
14
15 #define LL_NEW() NEW(LinkedList)
16
17 #define LL_EMPTY(ll) (ll.ll_head == NULL)
18
19 /**
20  * Push to tail
21  */
22 entity LL_PUSH(LinkedList this, entity e)
23 {
24         LinkedListNode n = NEW(LinkedListNode);
25         n.ll_data = e;
26         LinkedListNode tail = n.ll_prev = this.ll_tail;
27         this.ll_tail = (tail) ? tail.ll_next = n : this.ll_head = n;
28         return e;
29 }
30
31 /**
32  * Pop from tail
33  */
34 entity LL_POP(LinkedList this)
35 {
36         if (!this.ll_tail) return NULL;
37         LinkedListNode n = this.ll_tail;
38         entity e = n.ll_data;
39         LinkedListNode prev = n.ll_prev;
40         if (prev) prev.ll_next = NULL;
41         else this.ll_head = this.ll_tail = NULL;
42         return e;
43 }
44
45 #define LL_EACH(list, cond, body) \
46         do                                                                  \
47         {                                                                   \
48                 noref int i = 0;                                                \
49                 for (entity _it = list.ll_head; _it; (_it = _it.ll_next, ++i))  \
50                 {                                                               \
51                         noref entity it = _it.ll_data;                              \
52                         if (cond) { body }                                          \
53                 }                                                               \
54         }                                                                   \
55         while (0)
56
57 #endif