OpenClonk
C4Set< T > Class Template Reference

#include <C4StringTable.h>

Inheritance diagram for C4Set< T >:
[legend]

Public Member Functions

 C4Set ()
 
 ~C4Set ()
 
 C4Set (const C4Set &b)
 
C4Setoperator= (const C4Set &b)
 
void CompileFunc (class StdCompiler *pComp, class C4ValueNumbers *)
 
void Clear ()
 
template<typename H >
T & Get (H e) const
 
template<typename H >
bool Has (H e) const
 
unsigned int GetSize () const
 
T * Add (T const &e)
 
T * Add (T &&e)
 
template<typename H >
void Remove (H e)
 
T const * First () const
 
T const * Next (T const *p) const
 
void Swap (C4Set< T > *S2)
 
std::list< const T * > GetSortedListOfElementPointers () const
 
template<>
unsigned int Hash (int const &e)
 
template<>
unsigned int Hash (C4PropList *const &e)
 
template<>
unsigned int Hash (C4PropListNumbered *const &e)
 
template<>
bool Equals (C4PropListNumbered *const &a, int const &b)
 
template<>
bool Equals (C4PropListNumbered *const &a, C4PropList *const &b)
 
template<>
unsigned int Hash (C4PropList *const &e)
 
template<>
unsigned int Hash (C4PropListScript *const &e)
 
template<>
unsigned int Hash (const char *const &s)
 
template<>
bool Equals (C4String *const &a, const char *const &b)
 
template<>
unsigned int Hash (const C4String *const &e)
 
template<>
unsigned int Hash (C4String *const &e)
 

Static Public Member Functions

template<typename H >
static unsigned int Hash (const H &)
 
template<typename H >
static bool Equals (const T &, const H &)
 
static bool Equals (const T &a, const T &b)
 
static bool SortFunc (const T *p1, const T *p2)
 

Detailed Description

template<typename T>
class C4Set< T >

Definition at line 118 of file C4StringTable.h.

Constructor & Destructor Documentation

◆ C4Set() [1/2]

template<typename T>
C4Set< T >::C4Set ( )
inline

Definition at line 171 of file C4StringTable.h.

171  : Table(new T[Capacity])
172  {
173  ClearTable();
174  }

◆ ~C4Set()

template<typename T>
C4Set< T >::~C4Set ( )
inline

Definition at line 175 of file C4StringTable.h.

176  {
177  delete[] Table;
178  }

◆ C4Set() [2/2]

template<typename T>
C4Set< T >::C4Set ( const C4Set< T > &  b)
inline

Definition at line 179 of file C4StringTable.h.

179  : Capacity(0), Size(0), Table(0)
180  {
181  *this = b;
182  }
#define b

Member Function Documentation

◆ Add() [1/2]

template<typename T>
T* C4Set< T >::Add ( T const &  e)
inline

Definition at line 220 of file C4StringTable.h.

Referenced by C4PropListNumbered::AcquireNumber(), C4PropListNumbered::CompileFunc(), C4Set< C4String *>::Equals(), and C4String::~C4String().

221  {
222  MaintainCapacity();
223  T * r = AddInternal(e);
224  ++Size;
225  return r;
226  }
Here is the caller graph for this function:

◆ Add() [2/2]

template<typename T>
T* C4Set< T >::Add ( T &&  e)
inline

Definition at line 227 of file C4StringTable.h.

228  {
229  MaintainCapacity();
230  T * r = AddInternal(std::move(e));
231  ++Size;
232  return r;
233  }

◆ Clear()

template<typename T>
void C4Set< T >::Clear ( )
inline

Definition at line 194 of file C4StringTable.h.

Referenced by C4ScriptHost::Clear(), and C4ScriptHost::Preparse().

195  {
196  ClearTable();
197  Size = 0;
198  }
Here is the caller graph for this function:

◆ CompileFunc()

template<typename T >
void C4Set< T >::CompileFunc ( class StdCompiler pComp,
class C4ValueNumbers numbers 
)

Definition at line 449 of file C4PropList.cpp.

450 {
451  bool fNaming = pComp->hasNaming();
452  if (pComp->isDeserializer())
453  {
454  // Compiling: Empty previous
455  Clear();
456  // Read size (binary only)
457  uint32_t iSize;
458  if (!pComp->hasNaming()) pComp->Value(iSize);
459  // Read new
460  do
461  {
462  // No entries left to read?
463  if (!fNaming && !iSize--)
464  break;
465  // Read entries
466  try
467  {
468  T e;
469  // This could use the same technique StdArrayAdapt uses
470  // instead of hardcoding mkParAdapt here
471  pComp->Value(mkParAdapt(e, numbers));
472  Add(e);
473  }
474  catch (StdCompiler::NotFoundException *pEx)
475  {
476  // No value found: Stop reading loop
477  delete pEx;
478  break;
479  }
480  }
481  while (pComp->Separator(StdCompiler::SEP_SEP));
482  }
483  else
484  {
485  // Write size (binary only)
486  if (!fNaming)
487  {
488  int32_t iSize = GetSize();
489  pComp->Value(iSize);
490  }
491  // Write all entries
492  const T * p = First();
493  while (p)
494  {
495  pComp->Value(mkParAdapt(*const_cast<T *>(p), numbers));
496  p = Next(p);
497  if (p) pComp->Separator(StdCompiler::SEP_SEP);
498  }
499  }
500 }
T * Add(T const &e)
T const * Next(T const *p) const
void Clear()
T const * First() const
unsigned int GetSize() const
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:476
int iSize
Definition: TstC4NetIO.cpp:32

◆ Equals() [1/5]

template<>
bool C4Set< C4String * >::Equals< const char *> ( C4String *const &  a,
const char *const &  b 
)

Definition at line 35 of file C4StringTable.cpp.

36 {
37  return a->GetData() == b;
38 }
#define b
StdStrBuf GetData() const
Definition: C4StringTable.h:50

◆ Equals() [2/5]

template<typename T>
template<typename H >
static bool C4Set< T >::Equals ( const T &  ,
const H &   
)
static

◆ Equals() [3/5]

template<typename T>
static bool C4Set< T >::Equals ( const T &  a,
const T &  b 
)
inlinestatic

Definition at line 170 of file C4StringTable.h.

170 { return a == b; }
#define b
#define a

◆ Equals() [4/5]

template<>
bool C4Set< C4PropListNumbered * >::Equals< int > ( C4PropListNumbered *const &  a,
int const &  b 
)

Definition at line 1082 of file C4PropList.cpp.

1083 {
1084  return a->Number == b;
1085 }
#define b

◆ Equals() [5/5]

template<>
bool C4Set< C4PropListNumbered * >::Equals< C4PropList *> ( C4PropListNumbered *const &  a,
C4PropList *const &  b 
)

Definition at line 1088 of file C4PropList.cpp.

1089 {
1090  return a == b;
1091 }
#define b

◆ First()

template<typename T>
T const* C4Set< T >::First ( ) const
inline

Definition at line 253 of file C4StringTable.h.

Referenced by C4PropListScript::ClearScriptPropLists(), C4ScriptHost::CopyPropList(), C4PropList::Denumerate(), C4PropList::GetSortedLocalProperties(), C4PropList::GetUnsortedProperties(), and C4PropList::operator==().

253 { return Next(Table - 1); }
T const * Next(T const *p) const
Here is the caller graph for this function:

◆ Get()

template<typename T>
template<typename H >
T& C4Set< T >::Get ( e) const
inline

Definition at line 199 of file C4StringTable.h.

Referenced by C4PropListNumbered::AcquireNumber(), C4PropList::CompileFunc(), C4PropListNumbered::CompileFunc(), and C4PropList::operator==().

200  {
201  unsigned int h = Hash(e);
202  T * r = &Table[h % Capacity];
203  while (*r && !Equals(*r, e))
204  {
205  r = &Table[++h % Capacity];
206  }
207  return *r;
208  }
static unsigned int Hash(const H &)
static bool Equals(const T &, const H &)
Here is the caller graph for this function:

◆ GetSize()

template<typename T>
unsigned int C4Set< T >::GetSize ( ) const
inline

Definition at line 219 of file C4StringTable.h.

Referenced by C4PropList::AppendDataString(), C4PropListScript::ClearScriptPropLists(), C4PropList::operator==(), and C4PropList::ToJSON().

219 { return Size; }
Here is the caller graph for this function:

◆ GetSortedListOfElementPointers()

template<typename T>
std::list<const T *> C4Set< T >::GetSortedListOfElementPointers ( ) const
inline

Definition at line 279 of file C4StringTable.h.

Referenced by C4PropList::AppendDataString(), C4PropList::GetSortedLocalProperties(), and C4PropList::ToJSON().

280  {
281  // return a list of pointers to all elements in this set sorted by the standard less-than operation
282  // of the elements
283  // elements of resulting lists are guarantueed to be non-nullptr
284  // list remains valid as long as this set is not changed
285  std::list<const T *> result;
286  for (const T *p = First(); p; p = Next(p)) result.push_back(p);
287  result.sort(C4Set<T>::SortFunc);
288  return result;
289  }
T const * Next(T const *p) const
T const * First() const
Here is the caller graph for this function:

◆ Has()

template<typename T>
template<typename H >
bool C4Set< T >::Has ( e) const
inline

Definition at line 209 of file C4StringTable.h.

Referenced by C4PropList::CompileFunc().

210  {
211  unsigned int h = Hash(e);
212  T * r = &Table[h % Capacity];
213  while (*r && !Equals(*r, e))
214  {
215  r = &Table[++h % Capacity];
216  }
217  return !!*r;
218  }
static unsigned int Hash(const H &)
static bool Equals(const T &, const H &)
Here is the caller graph for this function:

◆ Hash() [1/9]

template<>
unsigned int C4Set< C4String * >::Hash< const char *> ( const char *const &  s)

Definition at line 24 of file C4StringTable.cpp.

25 {
26  // Fowler/Noll/Vo hash
27  unsigned int h = 2166136261u;
28  const char * p = s;
29  while (*p)
30  h = (h ^ *(p++)) * 16777619;
31  return h;
32 }
#define s

◆ Hash() [2/9]

template<typename T>
template<typename H >
static unsigned int C4Set< T >::Hash ( const H &  )
static

Referenced by C4Set< C4String *>::Equals(), C4PropList::GetEffect(), C4Set< C4String *>::Hash(), and C4String::~C4String().

Here is the caller graph for this function:

◆ Hash() [3/9]

template<>
unsigned int C4Set< C4String * >::Hash< const C4String *> ( const C4String *const &  e)
inline

Definition at line 293 of file C4StringTable.h.

294 {
295  return e->Hash;
296 }
unsigned int Hash
Definition: C4StringTable.h:37

◆ Hash() [4/9]

template<>
unsigned int C4Set< C4String * >::Hash< C4String *> ( C4String *const &  e)
inline

Definition at line 298 of file C4StringTable.h.

299 {
300  return e->Hash;
301 }
unsigned int Hash
Definition: C4StringTable.h:37

◆ Hash() [5/9]

template<>
unsigned int C4Set< C4PropListNumbered * >::Hash< int > ( int const &  e)

Definition at line 1053 of file C4PropList.cpp.

1054 {
1055  unsigned int hash = 4, tmp;
1056  hash += e >> 16;
1057  tmp = ((e & 0xffff) << 11) ^ hash;
1058  hash = (hash << 16) ^ tmp;
1059  hash += hash >> 11;
1060  hash ^= hash << 3;
1061  hash += hash >> 5;
1062  hash ^= hash << 4;
1063  hash += hash >> 17;
1064  hash ^= hash << 25;
1065  hash += hash >> 6;
1066  return hash;
1067 }

◆ Hash() [6/9]

template<>
unsigned int C4Set< C4PropListNumbered * >::Hash< C4PropList *> ( C4PropList *const &  e)

Definition at line 1070 of file C4PropList.cpp.

1071 {
1072  return Hash(e->GetPropListNumbered()->Number);
1073 }
static unsigned int Hash(const H &)
virtual C4PropListNumbered * GetPropListNumbered()
Definition: C4PropList.cpp:703

◆ Hash() [7/9]

template<>
unsigned int C4Set< C4PropListNumbered * >::Hash< C4PropListNumbered *> ( C4PropListNumbered *const &  e)

Definition at line 1076 of file C4PropList.cpp.

1077 {
1078  return Hash(e->Number);
1079 }
static unsigned int Hash(const H &)

◆ Hash() [8/9]

template<>
unsigned int C4Set< C4PropList * >::Hash< C4PropList *> ( C4PropList *const &  e)

Definition at line 1094 of file C4PropList.cpp.

1095 {
1096  return C4Set<C4PropListNumbered *>::Hash(static_cast<int>(reinterpret_cast<intptr_t>(e)));
1097 }
static unsigned int Hash(const H &)

◆ Hash() [9/9]

template<>
unsigned int C4Set< C4PropListScript * >::Hash< C4PropListScript *> ( C4PropListScript *const &  e)

Definition at line 1100 of file C4PropList.cpp.

1101 {
1102  // since script prop lists are only put in the set for reference keeping, just hash by pointer
1103  // but use only some of the more significant bits because
1104  uintptr_t hash = reinterpret_cast<uintptr_t>(e);
1105  return (unsigned int)(hash / 63);
1106 }

◆ Next()

template<typename T>
T const* C4Set< T >::Next ( T const *  p) const
inline

Definition at line 254 of file C4StringTable.h.

Referenced by C4PropListScript::ClearScriptPropLists(), C4ScriptHost::CopyPropList(), C4PropList::Denumerate(), C4PropList::GetSortedLocalProperties(), C4PropList::GetUnsortedProperties(), and C4PropList::operator==().

255  {
256  while (++p != &Table[Capacity])
257  {
258  if (*p) return p;
259  }
260  return nullptr;
261  }
Here is the caller graph for this function:

◆ operator=()

template<typename T>
C4Set& C4Set< T >::operator= ( const C4Set< T > &  b)
inline

Definition at line 183 of file C4StringTable.h.

184  {
185  Capacity = b.Capacity;
186  Size = b.Size;
187  delete[] Table;
188  Table = new T[Capacity];
189  for (unsigned int i = 0; i < Capacity; ++i)
190  Table[i] = b.Table[i];
191  return *this;
192  }

◆ Remove()

template<typename T>
template<typename H >
void C4Set< T >::Remove ( e)
inline

Definition at line 234 of file C4StringTable.h.

Referenced by C4PropListNumbered::ClearNumber(), C4PropList::CompileFunc(), C4PropListNumbered::~C4PropListNumbered(), and C4String::~C4String().

235  {
236  unsigned int h = Hash(e);
237  T * r = &Table[h % Capacity];
238  while (*r && !Equals(*r, e))
239  {
240  r = &Table[++h % Capacity];
241  }
242  assert(*r);
243  *r = nullptr;
244  --Size;
245  // Move entries which might have collided with e
246  while (*(r = &Table[++h % Capacity]))
247  {
248  T m = *r;
249  *r = nullptr;
250  AddInternal(std::move(m));
251  }
252  }
static unsigned int Hash(const H &)
static bool Equals(const T &, const H &)
Here is the caller graph for this function:

◆ SortFunc()

template<typename T>
static bool C4Set< T >::SortFunc ( const T *  p1,
const T *  p2 
)
inlinestatic

Definition at line 274 of file C4StringTable.h.

275  {
276  // elements are guarantueed to be non-nullptr
277  return *p1<*p2;
278  }

◆ Swap()

template<typename T>
void C4Set< T >::Swap ( C4Set< T > *  S2)
inline

Definition at line 262 of file C4StringTable.h.

263  {
264  unsigned int Capacity2 = S2->Capacity;
265  unsigned int Size2 = S2->Size;
266  T * Table2 = S2->Table;
267  S2->Capacity = Capacity;
268  S2->Size = Size;
269  S2->Table = Table;
270  Capacity = Capacity2;
271  Size = Size2;
272  Table = Table2;
273  }

The documentation for this class was generated from the following files: