mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-10 14:41:50 +00:00
common/inlist: added a inline list data structure.
Inline lists mean that their nodes' pointers are part of the same memory as the data. This has the benefit of fragmenting memory less and avoiding node->data indirection.
This commit is contained in:
parent
c30019e6dd
commit
e89eb09a33
2 changed files with 102 additions and 0 deletions
|
@ -3,6 +3,7 @@ source_file = [
|
|||
'tvgBezier.h',
|
||||
'tvgFormat.h',
|
||||
'tvgCompressor.h',
|
||||
'tvgInlist.h',
|
||||
'tvgList.h',
|
||||
'tvgMath.h',
|
||||
'tvgStr.h',
|
||||
|
|
101
src/common/tvgInlist.h
Normal file
101
src/common/tvgInlist.h
Normal file
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* Copyright (c) 2023 the ThorVG project. All rights reserved.
|
||||
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _TVG_INLIST_H_
|
||||
#define _TVG_INLIST_H_
|
||||
|
||||
namespace tvg {
|
||||
|
||||
//NOTE: declare this in your list item
|
||||
#define INLIST_ITEM(T) \
|
||||
T* prev; \
|
||||
T* next
|
||||
|
||||
template<typename T>
|
||||
struct Inlist
|
||||
{
|
||||
T *head = nullptr;
|
||||
T *tail = nullptr;
|
||||
|
||||
void free()
|
||||
{
|
||||
while (head) {
|
||||
auto t = head;
|
||||
head = t->next;
|
||||
delete(t);
|
||||
}
|
||||
head = tail = nullptr;
|
||||
}
|
||||
|
||||
void back(T* element)
|
||||
{
|
||||
if (tail) {
|
||||
tail->next = element;
|
||||
element->prev = tail;
|
||||
element->next = nullptr;
|
||||
} else {
|
||||
head = tail = element;
|
||||
element->prev = nullptr;
|
||||
element->next = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void front(T* element)
|
||||
{
|
||||
if (head) {
|
||||
element->prev = nullptr;
|
||||
element->next = head;
|
||||
head->prev = element;
|
||||
} else {
|
||||
head = tail = element;
|
||||
element->prev = nullptr;
|
||||
element->next = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
T* back()
|
||||
{
|
||||
if (!tail) return nullptr;
|
||||
auto t = tail;
|
||||
tail = t->prev;
|
||||
if (!tail) head = nullptr;
|
||||
return t;
|
||||
}
|
||||
|
||||
T* front()
|
||||
{
|
||||
if (!head) return nullptr;
|
||||
auto t = head;
|
||||
head = t->next;
|
||||
if (!head) tail = nullptr;
|
||||
return t;
|
||||
}
|
||||
|
||||
bool empty()
|
||||
{
|
||||
return head ? false : true;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // _TVG_INLIST_H_
|
Loading…
Add table
Reference in a new issue