Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __CS_STRHASH_H__
00020 #define __CS_STRHASH_H__
00021
00022 #include "csextern.h"
00023 #include "csutil/hash.h"
00024 #include "csutil/mempool.h"
00025 #include "iutil/strset.h"
00026
00031 namespace CS
00032 {
00033 namespace Utility
00034 {
00042 template<typename Tag>
00043 class CS_CRYSTALSPACE_EXPORT StringHash
00044 {
00045 private:
00046 typedef csHash<StringID<Tag>, char const*> HashType;
00047 HashType registry;
00048 csMemoryPool pool;
00049
00050 public:
00051 typedef typename HashType::ConstGlobalIterator GlobalIterator;
00052
00053 private:
00054 void Copy(StringHash const& h)
00055 {
00056 if (&h != this)
00057 {
00058 GlobalIterator it(h.GetIterator());
00059 while (it.HasNext())
00060 {
00061 char const* s;
00062 StringID<Tag> id = it.Next(s);
00063 this->Register(s, id);
00064 }
00065 }
00066 }
00067
00068 public:
00070 StringHash (size_t size = 23) : registry (size) {}
00072 StringHash (StringHash const& h) { Copy(h); }
00074 ~StringHash () { Empty(); }
00076 StringHash& operator=(StringHash const& h) { Copy(h); return *this; }
00077
00097 const char* Register (const char* s, StringID<Tag> id = 0)
00098 {
00099 char const* t = pool.Store(s);
00100 registry.PutUnique(t, id);
00101 return t;
00102 }
00103
00109 StringID<Tag> Request (const char* s) const
00110 {
00111 return registry.Get(s, CS::InvalidStringID<Tag> ());
00112 }
00113
00124 const char* Request (StringID<Tag> id) const
00125 {
00126 GlobalIterator it(GetIterator());
00127 while (it.HasNext())
00128 {
00129 char const* s;
00130 StringID<Tag> const x = it.Next(s);
00131 if (x == id)
00132 return s;
00133 }
00134 return 0;
00135 }
00136
00142 bool Contains(char const* s) const
00143 { return Request(s) != InvalidStringID<Tag> (); }
00144
00153 bool Contains(StringID<Tag> id) const
00154 { return Request(id) != 0; }
00155
00160 bool Delete(char const* s)
00161 {
00162 return registry.DeleteAll(s);
00163 }
00164
00171 bool Delete(StringID<Tag> id)
00172 {
00173 char const* s = Request(id);
00174 return s != 0 ? Delete(s) : false;
00175 }
00176
00180 void Empty ()
00181 {
00182 registry.Empty();
00183 pool.Empty();
00184 }
00185
00190
00191 void Clear ()
00192 { Empty(); }
00193
00195 size_t GetSize () const
00196 { return registry.GetSize (); }
00197
00203 bool IsEmpty() const
00204 { return GetSize() == 0; }
00205
00211 GlobalIterator GetIterator () const
00212 { return registry.GetIterator(); }
00213 };
00214 }
00215 }
00216
00220 typedef CS::Utility::StringHash<CS::StringSetTag::General> csStringHash;
00221
00222 #endif // __CS_STRHASH_H__