Debian Codegraph @

thirdparty/thorvg/src/common/tvgInlist.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2023 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #ifndef _TVG_INLIST_H_
24 #define _TVG_INLIST_H_
25
26 namespace tvg {
27
28 //NOTE: declare this in your list item
29 #define INLIST_ITEM(T) \
30 T* prev; \
31 T* next
32
33 template<typename T>
34 struct Inlist
35 {
36 T* head = nullptr;
37 T* tail = nullptr;
38
39 void free()
40 {
41 while (head) {
42 auto t = head;
43 head = t->next;
44 delete(t);
45 }
46 head = tail = nullptr;
47 }
48
49 void back(T* element)
50 {
51 if (tail) {
52 tail->next = element;
53 element->prev = tail;
54 element->next = nullptr;
55 tail = element;
56 } else {
57 head = tail = element;
58 element->prev = nullptr;
59 element->next = nullptr;
60 }
61 }
62
63 void front(T* element)
64 {
65 if (head) {
66 head->prev = element;
67 element->prev = nullptr;
68 element->next = head;
69 head = element;
70 } else {
71 head = tail = element;
72 element->prev = nullptr;
73 element->next = nullptr;
74 }
75 }
76
77 T* back()
78 {
79 if (!tail) return nullptr;
80 auto t = tail;
81 tail = t->prev;
82 if (!tail) head = nullptr;
83 return t;
84 }
85
86 T* front()
87 {
88 if (!head) return nullptr;
89 auto t = head;
90 head = t->next;
91 if (!head) tail = nullptr;
92 return t;
93 }
94
95 void remove(T* element)
96 {
97 if (element->prev) element->prev->next = element->next;
98 if (element->next) element->next->prev = element->prev;
99 if (element == head) head = element->next;
100 if (element == tail) tail = element->prev;
101 }
102
103 bool empty() const
104 {
105 return head ? false : true;
106 }
107 };
108
109 }
110
111 #endif // _TVG_INLIST_H_