SourceMod SDK  1.7
sm_namehashset.h
Go to the documentation of this file.
1 
32 #ifndef _include_sourcemod_namehashset_h_
33 #define _include_sourcemod_namehashset_h_
34 
41 #include <am-allocator-policies.h>
42 #include <am-hashmap.h>
43 #include <am-string.h>
44 #include "sm_stringhashmap.h"
45 
46 namespace SourceMod
47 {
48 
49 // The HashPolicy type must have this method:
50 // static bool matches(const char *key, const T &value);
51 //
52 // Depending on what lookup types are used.
53 //
54 // If these members are available on T, then the HashPolicy type can be left
55 // default. It is okay to use |T *|, the functions will still be looked up
56 // on |T|.
57 template <typename T, typename KeyPolicy = T>
58 class NameHashSet : public ke::SystemAllocatorPolicy
59 {
61 
62  // Default policy type: the two types are different. Use them directly.
63  template <typename KeyType, typename KeyPolicyType>
64  struct Policy
65  {
66  typedef KeyType Payload;
67 
68  static uint32_t hash(const CharsAndLength &key)
69  {
70  return key.hash();
71  }
72 
73  static bool matches(const CharsAndLength &key, const KeyType &value)
74  {
75  return KeyPolicyType::matches(key.chars(), value);
76  }
77  };
78 
79  // Specialization: the types are equal, and T is a pointer. Strip the
80  // pointer off so we can access T:: for match functions.
81  template <typename KeyType>
82  struct Policy<KeyType *, KeyType *>
83  {
84  typedef KeyType *Payload;
85 
86  static uint32_t hash(const detail::CharsAndLength &key)
87  {
88  return key.hash();
89  }
90 
91  static bool matches(const CharsAndLength &key, const KeyType *value)
92  {
93  return KeyType::matches(key.chars(), value);
94  }
95  };
96 
97  typedef ke::HashTable<Policy<T, KeyPolicy>, ke::SystemAllocatorPolicy> Internal;
98 
99 public:
100  NameHashSet()
101  {
102  if (!table_.init())
103  this->reportOutOfMemory();
104  }
105 
106  typedef typename Internal::Result Result;
107  typedef typename Internal::Insert Insert;
108  typedef typename Internal::iterator iterator;
109 
110  Result find(const char *aKey)
111  {
112  return table_.find(aKey);
113  }
114 
115  Insert findForAdd(const char *aKey)
116  {
117  return table_.findForAdd(aKey);
118  }
119 
120  void add(Insert &i, const T &value)
121  {
122  return table_.add(i, value);
123  }
124 
125  void add(Insert &i, ke::Moveable<T> value)
126  {
127  return table_.add(i, value);
128  }
129 
130  bool retrieve(const char *aKey, T *value)
131  {
132  CharsAndLength key(aKey);
133  Result r = table_.find(aKey);
134  if (!r.found())
135  return false;
136  *value = *r;
137  return true;
138  }
139 
140  bool insert(const char *aKey, const T &value)
141  {
142  CharsAndLength key(aKey);
143  Insert i = table_.findForAdd(key);
144  if (i.found())
145  return false;
146  return table_.add(i, value);
147  }
148 
149  bool insert(const char *aKey, ke::Moveable<T> value)
150  {
151  CharsAndLength key(aKey);
152  Insert i = table_.findForAdd(key);
153  if (i.found())
154  return false;
155  return table_.add(i, value);
156  }
157 
158  bool contains(const char *aKey)
159  {
160  CharsAndLength key(aKey);
161  Result r = table_.find(aKey);
162  return r.found();
163  }
164 
165  bool remove(const char *aKey)
166  {
167  CharsAndLength key(aKey);
168  Result r = table_.find(key);
169  if (!r.found())
170  return false;
171  table_.remove(r);
172  return true;
173  }
174 
175  void remove(Result &r)
176  {
177  table_.remove(r);
178  }
179 
180  void clear()
181  {
182  table_.clear();
183  }
184 
185  size_t mem_usage() const
186  {
187  return table_.estimateMemoryUse();
188  }
189 
190  iterator iter()
191  {
192  return iterator(&table_);
193  }
194 
195 private:
196  Internal table_;
197 };
198 
199 }
200 
201 #endif // _include_sourcemod_namehashset_h_
Definition: sm_stringhashmap.h:58
Definition: sm_namehashset.h:58
Generic Key -> Value map class, based on a hash table. The Key, in this case, is always an ASCII stri...
Definition: IAdminSystem.h:63