0.2.0-dev Download ZIP
135 lines ·3.3 KB Raw
| 1 | /* |
| 2 | * Copyright (c) 2023 - 2026 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 | #include <cstdint> |
| 27 | |
| 28 | namespace tvg |
| 29 | { |
| 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 | uint32_t count = 0; |
| 42 | |
| 43 | ~Inlist() |
| 44 | { |
| 45 | free(); |
| 46 | } |
| 47 | |
| 48 | void free() |
| 49 | { |
| 50 | while (head) { |
| 51 | auto t = head; |
| 52 | head = t->next; |
| 53 | delete(t); |
| 54 | } |
| 55 | head = tail = nullptr; |
| 56 | count = 0; |
| 57 | } |
| 58 | |
| 59 | void back(T* element) |
| 60 | { |
| 61 | if (tail) { |
| 62 | tail->next = element; |
| 63 | element->prev = tail; |
| 64 | element->next = nullptr; |
| 65 | tail = element; |
| 66 | } else { |
| 67 | head = tail = element; |
| 68 | element->prev = nullptr; |
| 69 | element->next = nullptr; |
| 70 | } |
| 71 | ++count; |
| 72 | } |
| 73 | |
| 74 | void front(T* element) |
| 75 | { |
| 76 | if (head) { |
| 77 | head->prev = element; |
| 78 | element->prev = nullptr; |
| 79 | element->next = head; |
| 80 | head = element; |
| 81 | } else { |
| 82 | head = tail = element; |
| 83 | element->prev = nullptr; |
| 84 | element->next = nullptr; |
| 85 | } |
| 86 | ++count; |
| 87 | } |
| 88 | |
| 89 | T* back() |
| 90 | { |
| 91 | if (!tail) return nullptr; |
| 92 | --count; |
| 93 | auto t = tail; |
| 94 | tail = t->prev; |
| 95 | if (!tail) head = nullptr; |
| 96 | return t; |
| 97 | } |
| 98 | |
| 99 | T* front() |
| 100 | { |
| 101 | if (!head) return nullptr; |
| 102 | --count; |
| 103 | auto t = head; |
| 104 | head = t->next; |
| 105 | if (!head) tail = nullptr; |
| 106 | return t; |
| 107 | } |
| 108 | |
| 109 | void remove(T* element) |
| 110 | { |
| 111 | if (element->prev) element->prev->next = element->next; |
| 112 | if (element->next) element->next->prev = element->prev; |
| 113 | if (element == head) head = element->next; |
| 114 | if (element == tail) tail = element->prev; |
| 115 | element->prev = element->next = nullptr; |
| 116 | --count; |
| 117 | } |
| 118 | |
| 119 | bool empty() const |
| 120 | { |
| 121 | return head ? false : true; |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | #define INLIST_FOREACH(inlist, cur) \ |
| 126 | for (auto cur = inlist.head; cur; cur = cur->next) |
| 127 | |
| 128 | #define INLIST_SAFE_FOREACH(inlist, cur) \ |
| 129 | auto cur = inlist.head; \ |
| 130 | auto next = cur ? cur->next : nullptr; \ |
| 131 | for (; cur; cur = next, next = (cur ? cur->next : nullptr)) |
| 132 | |
| 133 | } |
| 134 | |
| 135 | #endif // _TVG_INLIST_H_ |