OpenBarnyard
 
Loading...
Searching...
No Matches
T2SortedList.h
Go to the documentation of this file.
1#pragma once
2#include "T2DList.h"
3#include "T2Iterator.h"
4
6
7// -1 => push right here before a_rcVal2
8// 0 => they are equal, add new element after it to preserve original ordering
9// 1 => should add somewhere after a_rcVal2, so go to the next node
10template <class T>
12{
13 TINT operator()( const T& a_rcVal1, const T& a_rcVal2 ) const
14 {
15 if ( a_rcVal1 < a_rcVal2 )
16 return 1;
17
18 return -1;
19 }
20};
21
22template <typename T, typename Container = T2DList<T>, typename SortResults = T2SortedListDefaultSortResults<T>>
23class T2SortedList : private Container
24{
25public:
26 using Iterator = Container::Iterator;
27 using Node = Container::Node;
28
29public:
30 T2SortedList() = default;
31 ~T2SortedList() = default;
32
33 Iterator FindInsertionPoint( const T& a_rcValue )
34 {
35 T2_FOREACH( *this, it )
36 {
37 if ( SortResults()( a_rcValue, *it.Get() ) < 0 )
38 return it;
39 }
40
41 return End();
42 }
43
44 Iterator Push( T& a_rValue )
45 {
46 a_rValue.InsertBefore( FindInsertionPoint( a_rValue ) );
47 return &a_rValue;
48 }
49
50 Iterator Push( T* a_pValue ) { return Push( *a_pValue ); }
51
52 T* PopBack() { return Container::PopBack(); }
53 T* PopFront() { return Container::PopFront(); }
54
55 // Removes element from the list and adds it again to make sure the order is right
56 Iterator ReInsert( T& a_rValue )
57 {
58 if ( a_rValue.IsLinked() )
59 Erase( &a_rValue );
60
61 return Push( a_rValue );
62 }
63
64 // Removes element from the list and adds it again to make sure the order is right
65 Iterator ReInsert( T* a_pValue )
66 {
67 return ReInsert( *a_pValue );
68 }
69
70 void Delete( Iterator a_It ) { Container::Delete( a_It ); }
71 void DeleteAll() { Container::DeleteAll(); }
72
73 Iterator Erase( Iterator a_It ) { return Container::Erase( a_It ); }
74
75 TBOOL IsEmpty() const { return Container::IsEmpty(); }
76 TUINT Size() const { return Container::Size(); }
77
78 Iterator Head() const { return Container::Head(); }
79 Iterator Tail() const { return Container::Tail(); }
80 Iterator Begin() const { return Container::Begin(); }
81 Iterator RBegin() const { return Container::RBegin(); }
82 Iterator End() const { return Container::End(); }
83 Iterator REnd() const { return Container::REnd(); }
84 Iterator Front() const { return Container::Front(); }
85 Iterator Back() const { return Container::Back(); }
86
87 Container* AccessContainer() { return TSTATICCAST( Container, this ); }
88 Container* operator->() { return TSTATICCAST( Container, this ); }
89};
90
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define T2_FOREACH(vecName, iteratorName)
Definition T2Iterator.h:4
unsigned int TUINT
Definition Typedefs.h:8
int TINT
Definition Typedefs.h:7
bool TBOOL
Definition Typedefs.h:6
TINT operator()(const T &a_rcVal1, const T &a_rcVal2) const
void DeleteAll()
Iterator End() const
Iterator Erase(Iterator a_It)
Iterator Begin() const
Iterator ReInsert(T *a_pValue)
Container::Node Node
Iterator FindInsertionPoint(const T &a_rcValue)
Iterator Push(T &a_rValue)
Iterator REnd() const
void Delete(Iterator a_It)
Container * AccessContainer()
Iterator Head() const
T2SortedList()=default
Container::Iterator Iterator
Iterator Front() const
~T2SortedList()=default
Iterator RBegin() const
Iterator Push(T *a_pValue)
TUINT Size() const
TBOOL IsEmpty() const
Iterator Tail() const
Iterator ReInsert(T &a_rValue)
Container * operator->()
Iterator Back() const