OpenBarnyard
 
Loading...
Searching...
No Matches
T2SortedVector.h
Go to the documentation of this file.
1#pragma once
2#include "T2Vector.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, typename SortResults = T2SortedVectorDefaultSortResults<T>>
23class T2SortedVector : private Container
24{
25public:
26 using Iterator = Container::Iterator;
27
28public:
29 template <class... Args>
30 T2SortedVector( Args&&... args )
31 : Container( std::forward<Args>( args )... )
32 {}
33
34 ~T2SortedVector() = default;
35
36 // With a help of binary search algorithm, looks for an insertion place of the specified value
37 Iterator FindInsertionPoint( const T& a_rcValue, TBOOL& a_rbNotUnique )
38 {
39 TINT iSearchStart = 0;
40 TINT iSearchEnd = Container::Size() - 1;
41 a_rbNotUnique = TFALSE;
42
43 while ( iSearchStart <= iSearchEnd )
44 {
45 TINT iMiddle = ( iSearchStart + iSearchEnd ) / 2;
46 TINT iSortResult = SortResults()( a_rcValue, Container::At( iMiddle ) );
47
48 if ( iSortResult < 0 )
49 {
50 // Should look for the insertion place at the left side
51 iSearchEnd = iMiddle - 1;
52 }
53 else if ( iSortResult > 0 )
54 {
55 // Should look for the insertion place at the right side
56 iSearchStart = iMiddle + 1;
57 }
58 else
59 {
60 // We found an item that is equal to this
61 // Now, skip all equal items to prevent order changes
62 iSearchStart += 1;
63
64 while ( iSearchStart <= iSearchEnd &&
65 SortResults()( a_rcValue, Container::At( iSearchStart ) ) == 0 )
66 {
67 iSearchStart += 1;
68 }
69
70 a_rbNotUnique = TTRUE;
71 break;
72 }
73 }
74
75 return Iterator( iSearchStart, this );
76 }
77
78 Iterator Push( const T& a_rValue )
79 {
80 TBOOL bNotUnique;
81 return Container::InsertBefore( FindInsertionPoint( a_rValue, bNotUnique ), a_rValue );
82 }
83
84 Iterator Push( const T* a_pValue ) { return Push( *a_pValue ); }
85
86 Iterator ReInsert( const T& a_rcItem )
87 {
88 FindAndErase( a_rcItem );
89
90 return Push( a_rcItem );
91 }
92
93 Iterator ReInsert( const T* a_pValue )
94 {
95 return ReInsert( *a_pValue );
96 }
97
98 void PopBack() { Container::PopBack(); }
99 void PopFront() { Container::PopFront(); }
100
101 void Clear() { Container::Clear(); }
102
103 Iterator Find( const T& a_rValue ) { return Container::Find( a_rValue ); }
104 void Erase( const Iterator& a_rIterator ) { Container::Erase( a_rIterator ); }
105 void FindAndErase( const T& a_rcItem ) { Container::FindAndErase( a_rcItem ); }
106 void EraseFast( const Iterator& a_rIterator ) { Container::EraseFast( a_rIterator ); }
107 void FindAndEraseFast( const T& a_rcItem ) { Container::FindAndEraseFast( a_rcItem ); }
108
109 TINT Size() const { return Container::Size(); }
110 TINT Capacity() const { return Container::Capacity(); }
111 TBOOL IsEmpty() const { return Container::IsEmpty(); }
112
113 Iterator Front() { return Container::Front(); }
114 Iterator Back() { return Container::Back(); }
115 Iterator Begin() { return Container::Begin(); }
116 Iterator End() { return Container::End(); }
117
118 T& At( TINT a_iIndex ) { return Container::At( a_iIndex ); }
119 const T& At( TINT a_iIndex ) const { return Container::At( a_iIndex ); }
120
121 T& operator[]( TINT a_iIndex ) { return Container::At( a_iIndex ); }
122 const T& operator[]( TINT a_iIndex ) const { return Container::At( a_iIndex ); }
123
124 Container* AccessContainer() { return TSTATICCAST( Container, this ); }
125};
126
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
TINT operator()(const T &a_rcVal1, const T &a_rcVal2) const
Iterator FindInsertionPoint(const T &a_rcValue, TBOOL &a_rbNotUnique)
TBOOL IsEmpty() const
Iterator Push(const T *a_pValue)
Container * AccessContainer()
Iterator Find(const T &a_rValue)
~T2SortedVector()=default
void FindAndEraseFast(const T &a_rcItem)
const T & At(TINT a_iIndex) const
void Erase(const Iterator &a_rIterator)
T2SortedVector(Args &&... args)
T & operator[](TINT a_iIndex)
Iterator ReInsert(const T *a_pValue)
TINT Size() const
Iterator ReInsert(const T &a_rcItem)
T & At(TINT a_iIndex)
void EraseFast(const Iterator &a_rIterator)
void FindAndErase(const T &a_rcItem)
Iterator Push(const T &a_rValue)
const T & operator[](TINT a_iIndex) const
Container::Iterator Iterator
TINT Capacity() const