yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
ec7ab914f
master
1// slang-internally-linked-list.h 2#ifndef SLANG_INTERNALLY_LINKED_LIST_H 3#define SLANG_INTERNALLY_LINKED_LIST_H 4 5// This file provides support for the idiom of a linked 6// list of values where the "next" pointer is stored in 7// the values themselves (thus requiring no additional 8// allocation for list nodes, at the price of any given 9// value only being able to appear in a single list). 10 11#include "slang-basic.h" 12 13namespace Slang 14{ 15 16/// A linked list where the elements are themselves the nodes. 17/// 18/// The type parameter `T` should be a type that publicly 19/// inherits from `InternallyLinkedList<T>::Node`. 20/// 21template < typename T > 22struct InternallyLinkedList 23{ 24public : 25struct Node 26 { 27public : 28Node () {} 29 30private : 31friend struct InternallyLinkedList < T > ; 32T * _next = nullptr ; 33 }; 34 35struct Iterator 36{ 37public : 38Iterator () {} 39 40Iterator ( T * node) 41: _node (node) 42{ 43} 44 45T * operator * () const { return _node; } 46 47void operator ++ () { _node = static_cast < Node const *> (_node) -> _next ; } 48 49bool operator != ( Iterator const & that ) const { return _node != that. _node ; } 50 51private: 52T * _node = nullptr ; 53}; 54 55Iterator begin () { return Iterator (_first); } 56 57Iterator end () { return Iterator (); } 58 59T * getFirst () const { return _first; } 60 61T * getLast () const { return _last; } 62 63void add ( T * element) 64{ 65SLANG_ASSERT (element != nullptr ); 66if (!_last) 67{ 68SLANG_ASSERT (_first == nullptr ); 69 70_first = element; 71_last = element; 72} 73else 74{ 75SLANG_ASSERT (_first != nullptr ); 76 77_last -> _next = element; 78_last = element; 79} 80} 81 82void insertAfter ( T * existingElement, T * newElement) 83{ 84SLANG_ASSERT (existingElement != nullptr ); 85SLANG_ASSERT (newElement != nullptr ); 86if (existingElement == _last) 87{ 88add (newElement); 89} 90else 91{ 92newElement -> _next = existingElement -> _next ; 93existingElement -> _next = newElement; 94} 95} 96 97void append ( InternallyLinkedList < T > const & other) 98{ 99if (!other. _first ) 100{ 101} 102else if (!_last) 103{ 104_first = other. _first ; 105_last = other. _last ; 106} 107else 108{ 109SLANG_ASSERT (_first != nullptr ); 110 111_last -> _next = other. _first ; 112_last = other. _last ; 113} 114} 115 116private : 117T * _first = nullptr ; 118T * _last = nullptr ; 119}; 120 121template < typename T > 122using InternallyLinkedListNode = InternallyLinkedList < T > ::Node; 123 124} // namespace Slang 125 126#endif