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 #include "../../lv_conf_internal.h"
24 #if LV_USE_THORVG_INTERNAL
25 
26 #ifndef _TVG_INLIST_H_
27 #define _TVG_INLIST_H_
28 
29 namespace tvg {
30 
31 //NOTE: declare this in your list item
32 #define INLIST_ITEM(T) \
33     T* prev; \
34     T* next
35 
36 template<typename T>
37 struct Inlist
38 {
39     T* head = nullptr;
40     T* tail = nullptr;
41 
freeInlist42     void free()
43     {
44         while (head) {
45             auto t = head;
46             head = t->next;
47             delete(t);
48         }
49         head = tail = nullptr;
50     }
51 
backInlist52     void back(T* element)
53     {
54         if (tail) {
55             tail->next = element;
56             element->prev = tail;
57             element->next = nullptr;
58             tail = element;
59         } else {
60             head = tail = element;
61             element->prev = nullptr;
62             element->next = nullptr;
63         }
64     }
65 
frontInlist66     void front(T* element)
67     {
68         if (head) {
69             head->prev = element;
70             element->prev = nullptr;
71             element->next = head;
72             head = element;
73         } else {
74             head = tail = element;
75             element->prev = nullptr;
76             element->next = nullptr;
77         }
78     }
79 
backInlist80     T* back()
81     {
82         if (!tail) return nullptr;
83         auto t = tail;
84         tail = t->prev;
85         if (!tail) head = nullptr;
86         return t;
87     }
88 
frontInlist89     T* front()
90     {
91         if (!head) return nullptr;
92         auto t = head;
93         head = t->next;
94         if (!head) tail = nullptr;
95         return t;
96     }
97 
removeInlist98     void remove(T* element)
99     {
100         if (element->prev) element->prev->next = element->next;
101         if (element->next) element->next->prev = element->prev;
102         if (element == head) head = element->next;
103         if (element == tail) tail = element->prev;
104     }
105 
emptyInlist106     bool empty() const
107     {
108         return head ? false : true;
109     }
110 };
111 
112 }
113 
114 #endif // _TVG_INLIST_H_
115 
116 #endif /* LV_USE_THORVG_INTERNAL */
117 
118