]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/linkedlist.qh
Merge branch 'terencehill/quickmenu_file_example' 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 /**
18  * Push to tail
19  */
20 entity LL_PUSH(LinkedList this, entity e)
21 {
22         LinkedListNode n = NEW(LinkedListNode);
23         n.ll_data = e;
24         LinkedListNode tail = n.ll_prev = this.ll_tail;
25         this.ll_tail = (tail) ? tail.ll_next = n : this.ll_head = n;
26         return e;
27 }
28
29 /**
30  * Pop from tail
31  */
32 entity LL_POP(LinkedList this)
33 {
34         if (!this.ll_tail) return NULL;
35         LinkedListNode n = this.ll_tail;
36         entity e = n.ll_data;
37         LinkedListNode prev = n.ll_prev;
38         if (prev) prev.ll_next = NULL;
39         else this.ll_head = this.ll_tail = NULL;
40         return e;
41 }
42
43 #define LL_EACH(list, cond, body) \
44         do                                                                  \
45         {                                                                   \
46                 noref int i = 0;                                                \
47                 for (entity _it = list.ll_head; _it; (_it = _it.ll_next, ++i))  \
48                 {                                                               \
49                         noref entity it = _it.ll_data;                              \
50                         if (cond) { body }                                          \
51                 }                                                               \
52         }                                                                   \
53         while (0)
54
55 #endif