OpenBarnyard
 
Loading...
Searching...
No Matches
T2Map.h
Go to the documentation of this file.
1#pragma once
2#include "T2Pair.h"
3#include "T2RedBlackTree.h"
4#include "Toshi/TComparator.h"
5
7
8template <class KeyType, class ValueType, class Comparator = TComparator<KeyType>>
9class T2Map
10{
11public:
16
17public:
19 : m_RedBlackTree( a_pAllocator )
20 {
21 }
22
24 {
25 Clear();
26 }
27
28 void Clear()
29 {
30 m_RedBlackTree.DeleteAll();
31 }
32
33 TSIZE Size() const
34 {
35 return m_RedBlackTree.Size();
36 }
37
38 template <class... Args>
39 ValueType* Emplace( const KeyType& key, Args&&... args )
40 {
41 Pair pair{ key, ValueType( std::forward<Args>( args )... ) };
42 Iterator result = m_RedBlackTree.Insert( std::move( pair ) );
43 return &result.GetValue()->GetSecond();
44 }
45
46 ValueType* Insert( const KeyType& key, const ValueType& value )
47 {
48 Pair pair{ key, value };
49 Iterator result = m_RedBlackTree.Insert( std::move( pair ) );
50 return &result.GetValue()->GetSecond();
51 }
52
53 ValueType* Insert( const KeyType& key, ValueType&& value )
54 {
55 Pair pair{ key, std::move( value ) };
56 Iterator result = m_RedBlackTree.Insert( std::move( pair ) );
57 return &result.GetValue()->GetSecond();
58 }
59
60 void Remove( const KeyType& key )
61 {
62 Iterator result = m_RedBlackTree.Find( { key } );
63
64 TASSERT( result != End() );
65 m_RedBlackTree.Delete( result.GetNode() );
66 }
67
68 void Remove( Iterator& it )
69 {
70 TASSERT( it != End() );
71 m_RedBlackTree.Delete( it.GetNode() );
72 }
73
74 Iterator FindByValue( const ValueType& value )
75 {
76 for ( auto it = Begin(); it != End(); it++ )
77 {
78 if ( it.GetValue()->GetSecond() == value )
79 {
80 return it;
81 }
82 }
83
84 return End();
85 }
86
87 Iterator Find( const KeyType& key )
88 {
89 return m_RedBlackTree.Find( { key } );
90 }
91
92 Iterator FindNext( Iterator a_oIterator, const KeyType& a_rKey )
93 {
94 return m_RedBlackTree.FindNext( a_oIterator.GetNode(), { a_rKey } );
95 }
96
97 TBOOL IsValid( Iterator a_oIterator ) const
98 {
99 return a_oIterator != End();
100 }
101
103 {
104 return m_RedBlackTree.Begin();
105 }
106
108 {
109 return m_RedBlackTree.End();
110 }
111
112 const CIterator Begin() const
113 {
114 return m_RedBlackTree.Begin();
115 }
116
117 const CIterator End() const
118 {
119 return m_RedBlackTree.End();
120 }
121
123 {
124 return Begin() == End();
125 }
126
127 Iterator operator[]( const KeyType& key )
128 {
129 return m_RedBlackTree.Find( { key } );
130 }
131
133 {
134 return m_RedBlackTree.GetAllocator();
135 }
136
137private:
138 T2RedBlackTree<Pair> m_RedBlackTree;
139};
140
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
TFORCEINLINE T2Allocator * GetGlobalAllocator()
Definition T2Allocator.h:49
Red-black tree implementation for the Toshi engine.
size_t TSIZE
Definition Typedefs.h:9
bool TBOOL
Definition Typedefs.h:6
T2RedBlackTree< Pair >::Iterator Iterator
Definition T2Map.h:13
T2Map(T2Allocator *a_pAllocator=GetGlobalAllocator())
Definition T2Map.h:18
TBOOL IsValid(Iterator a_oIterator) const
Definition T2Map.h:97
T2RedBlackTree< Pair >::CIterator CIterator
Definition T2Map.h:14
void Remove(const KeyType &key)
Definition T2Map.h:60
TBOOL IsEmpty() const
Definition T2Map.h:122
Iterator Find(const KeyType &key)
Definition T2Map.h:87
Iterator FindNext(Iterator a_oIterator, const KeyType &a_rKey)
Definition T2Map.h:92
~T2Map()
Definition T2Map.h:23
Iterator End()
Definition T2Map.h:107
Iterator operator[](const KeyType &key)
Definition T2Map.h:127
ValueType * Emplace(const KeyType &key, Args &&... args)
Definition T2Map.h:39
T2Allocator * GetAllocator() const
Definition T2Map.h:132
Iterator FindByValue(const ValueType &value)
Definition T2Map.h:74
T2RedBlackTree< Pair >::Node Node
Definition T2Map.h:15
const CIterator End() const
Definition T2Map.h:117
void Remove(Iterator &it)
Definition T2Map.h:68
ValueType * Insert(const KeyType &key, ValueType &&value)
Definition T2Map.h:53
Iterator Begin()
Definition T2Map.h:102
TSIZE Size() const
Definition T2Map.h:33
void Clear()
Definition T2Map.h:28
ValueType * Insert(const KeyType &key, const ValueType &value)
Definition T2Map.h:46
T2Pair< KeyType, ValueType, Comparator > Pair
Definition T2Map.h:12
const CIterator Begin() const
Definition T2Map.h:112
Definition T2Pair.h:8
T2RedBlackTreeNode< T > Node
TFORCEINLINE Node * GetNode()
TFORCEINLINE T * GetValue()