TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Set.h
Go to the documentation of this file.
1 
12 #ifndef G3D_Set_h
13 #define G3D_Set_h
14 
15 #include "G3D/platform.h"
16 #include "G3D/Table.h"
17 #include "G3D/MemoryManager.h"
18 #include <assert.h>
19 #include <string>
20 
21 namespace G3D {
22 
31 // There is not copy constructor or assignment operator defined because
32 // the default ones are correct for Set.
33 template<class T, class HashFunc = HashTrait<T>, class EqualsFunc = EqualsTrait<T> >
34 class Set {
35 
41 
42 public:
43 
45  memberTable.clearAndSetMemoryManager(m);
46  }
47 
48  virtual ~Set() {}
49 
50  int size() const {
51  return (int)memberTable.size();
52  }
53 
54  bool contains(const T& member) const {
55  return memberTable.containsKey(member);
56  }
57 
62  bool insert(const T& member) {
63  bool isNew = false;
64  memberTable.getCreate(member, isNew) = true;
65  return isNew;
66  }
67 
72  bool remove(const T& member) {
73  return memberTable.remove(member);
74  }
75 
81  bool getRemove(const T& member, T& removed) {
82  bool ignore;
83  return memberTable.getRemove(member, removed, ignore);
84  }
85 
89  const T* getPointer(const T& member) const {
90  return memberTable.getKeyPointer(member);
91  }
92 
93  Array<T> getMembers() const {
94  return memberTable.getKeys();
95  }
96 
97  void getMembers(Array<T>& keyArray) const {
98  memberTable.getKeys(keyArray);
99  }
100 
101  void clear() {
102  memberTable.clear();
103  }
104 
105  void deleteAll() {
106  getMembers().deleteAll();
107  clear();
108  }
109 
113  class Iterator {
114  private:
115  friend class Set<T>;
116 
117  // Note: this is a Table iterator, we are currently defining
118  // Set iterator
120 
121  Iterator(const typename Table<T, bool>::Iterator& it) : it(it) {}
122 
123  public:
124  inline bool operator!=(const Iterator& other) const {
125  return !(*this == other);
126  }
127 
128  bool isValid() const {
129  return it.isValid();
130  }
131 
133  bool hasMore() const {
134  return it.isValid();
135  }
136 
137  bool operator==(const Iterator& other) const {
138  return it == other.it;
139  }
140 
145  ++it;
146  return *this;
147  }
148 
153  Iterator old = *this;
154  ++(*this);
155  return old;
156  }
157 
158  const T& operator*() const {
159  return it->key;
160  }
161 
162  T* operator->() const {
163  return &(it->key);
164  }
165 
166  operator T*() const {
167  return &(it->key);
168  }
169  };
170 
171 
177  Iterator begin() const {
178  return Iterator(memberTable.begin());
179  }
180 
181 
186  const Iterator end() const {
187  return Iterator(memberTable.end());
188  }
189 };
190 
191 }
192 
193 #endif
194 
bool isValid() const
Definition: Set.h:128
const Iterator end() const
Definition: Set.h:186
bool getRemove(const Key &key, Key &removedKey, Value &removedValue)
Definition: Table.h:660
bool contains(const T &member) const
Definition: Set.h:54
void deleteAll()
Definition: Set.h:105
void clear()
Definition: Table.h:578
size_t size() const
Definition: Table.h:589
void clear()
Definition: Set.h:101
Definition: AABox.h:25
Iterator begin() const
Definition: Table.h:562
shared_ptr< class MemoryManager > Ref
Definition: MemoryManager.h:31
Table< T, bool >::Iterator it
Definition: Set.h:119
T * operator->() const
Definition: Set.h:162
Iterator(const typename Table< T, bool >::Iterator &it)
Definition: Set.h:121
Table< T, bool, HashFunc, EqualsFunc > memberTable
Definition: Set.h:40
Iterator operator++(int)
Definition: Set.h:152
virtual ~Set()
Definition: Set.h:48
void getMembers(Array< T > &keyArray) const
Definition: Set.h:97
const T & operator*() const
Definition: Set.h:158
void G3D_DEPRECATED deleteAll()
Definition: Array.h:988
int size() const
Definition: Set.h:50
bool insert(const T &member)
Definition: Set.h:62
bool remove(const Key &key, Key &removedKey, Value &removedValue, bool updateRemoved)
Definition: Table.h:606
bool getRemove(const T &member, T &removed)
Definition: Set.h:81
bool hasMore() const
Definition: Set.h:133
Value & getCreate(const Key &key)
Definition: Table.h:861
Iterator & operator++()
Definition: Set.h:144
const T * getPointer(const T &member) const
Definition: Set.h:89
Definition: Set.h:34
const Iterator end() const
Definition: Table.h:570
bool operator!=(const Iterator &other) const
Definition: Set.h:124
Array< Key > getKeys() const
Definition: Table.h:907
const Key * getKeyPointer(const Key &key) const
Definition: Table.h:701
Array< T > getMembers() const
Definition: Set.h:93
Iterator begin() const
Definition: Set.h:177
bool operator==(const Iterator &other) const
Definition: Set.h:137
void clearAndSetMemoryManager(const MemoryManager::Ref &m)
Definition: Table.h:309
void clearAndSetMemoryManager(const MemoryManager::Ref &m)
Definition: Set.h:44
Definition: Set.h:113
bool containsKey(const Key &key) const
Definition: Table.h:874