hstring.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #if !defined hString_H
  2. #define hString_H
  3. #pragma warning (push, 3) //go back down to 3 for the stl include
  4. #include <string>
  5. #include <set>
  6. #include <list>
  7. #include <map>
  8. #pragma warning (pop)
  9. using namespace std;
  10. class hstring
  11. {
  12. int mId;
  13. void Init(const char *str);
  14. public:
  15. hstring()
  16. {
  17. mId=0;
  18. }
  19. hstring(const char *str)
  20. {
  21. Init(str);
  22. }
  23. hstring(const string &str)
  24. {
  25. Init(str.c_str());
  26. }
  27. hstring(const hstring &str)
  28. {
  29. mId=str.mId;
  30. }
  31. operator string () const
  32. {
  33. return str();
  34. }
  35. const char *c_str(void) const;
  36. string str(void) const;
  37. hstring& operator= (const char *str)
  38. {
  39. Init(str);
  40. return *this;
  41. }
  42. hstring& operator= (const string &str)
  43. {
  44. Init(str.c_str());
  45. return *this;
  46. }
  47. hstring& operator= (const hstring &str)
  48. {
  49. mId=str.mId;
  50. return *this;
  51. }
  52. bool operator== (const hstring &str) const
  53. {
  54. return((mId==str.mId)?true:false);
  55. }
  56. int compare(const hstring &str) const
  57. {
  58. return strcmp(c_str(),str.c_str());
  59. }
  60. bool operator< (const hstring &str) const
  61. {
  62. return((mId<str.mId)?true:false);
  63. }
  64. int length() const
  65. {
  66. return strlen(c_str());
  67. }
  68. };
  69. void TouchStringPool(void);
  70. ////////////
  71. // MapPool
  72. ////////////
  73. #define MAP_NODE_SIZE (32)
  74. class CMapBlock;
  75. class CMapPoolLow
  76. {
  77. vector <CMapBlock *> mMapBlocks;
  78. vector <void *> mFreeList;
  79. int mLastBlockNum;
  80. public:
  81. CMapPoolLow();
  82. ~CMapPoolLow();
  83. void *Alloc();
  84. void Free(void *p);
  85. void TouchMem();
  86. };
  87. CMapPoolLow &GetMapPool();
  88. template<class T>
  89. class CMapPool
  90. {
  91. CMapPoolLow &mPool;
  92. public:
  93. CMapPool() : mPool(GetMapPool())
  94. {
  95. }
  96. template <class U>
  97. CMapPool(const U&) : mPool(GetMapPool())
  98. {
  99. }
  100. ~CMapPool()
  101. {
  102. }
  103. typedef T value_type;
  104. typedef T* pointer;
  105. typedef const T* const_pointer;
  106. typedef T& reference;
  107. typedef const T& const_reference;
  108. typedef size_t size_type;
  109. typedef ptrdiff_t difference_type;
  110. template <class U>
  111. struct rebind
  112. {
  113. typedef CMapPool<U> other;
  114. };
  115. // return address of values
  116. pointer address (reference value) const
  117. {
  118. return &value;
  119. }
  120. const_pointer address (const_reference value) const
  121. {
  122. return &value;
  123. }
  124. // return maximum number of elements that can be allocated
  125. size_type max_size () const
  126. {
  127. // return mMaxSize;
  128. return 0xfffffff; //uh, take a guess
  129. }
  130. // allocate but don't initialize num elements of type T
  131. pointer allocate (size_type num, const void* = 0)
  132. {
  133. assert(sizeof(T)<=(MAP_NODE_SIZE-2)); // to big for this pool
  134. assert(num==1); //allocator not design for this
  135. return (T*)mPool.Alloc();
  136. }
  137. void *_Charalloc(size_type size)
  138. {
  139. assert(size<=(MAP_NODE_SIZE-2)); // to big for this pool
  140. return mPool.Alloc();
  141. }
  142. // initialize elements of allocated storage p with value value
  143. void construct (pointer p, const T& value)
  144. {
  145. // initialize memory with placement new
  146. new((void*)p)T(value);
  147. }
  148. // destroy elements of initialized storage p
  149. void destroy (pointer p)
  150. {
  151. // destroy objects by calling their destructor
  152. p->~T();
  153. }
  154. // deallocate storage p of deleted elements
  155. template<class U>
  156. void deallocate (U *p, size_type num)
  157. {
  158. assert(num==1); //allocator not design for this
  159. mPool.Free(p);
  160. }
  161. };
  162. template <class T1,class T2>
  163. bool operator== (const CMapPool<T1>&,
  164. const CMapPool<T2>&)
  165. {
  166. return false;
  167. }
  168. template <class T1,class T2>
  169. bool operator!= (const CMapPool<T1>&,
  170. const CMapPool<T2>&)
  171. {
  172. return true;
  173. }
  174. template <class K,class V,class Compare = less<K> >
  175. class hmap : public map<K,V,Compare,CMapPool<V> >{};
  176. template <class K,class V,class Compare = less<K> >
  177. class hmultimap : public multimap<K,V,Compare,CMapPool<V> >{};
  178. template <class K,class Compare = less<K> >
  179. class hset : public set<K,Compare,CMapPool<K> >{};
  180. template <class K,class Compare = less<K> >
  181. class hmultiset : public multiset<K,Compare,CMapPool<K> >{};
  182. template <class K>
  183. class hlist : public list<K,CMapPool<K> >{};
  184. #endif // hString_H