00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __STRINGPOOL_HPP
00023 #define __STRINGPOOL_HPP
00024
00025 #include <xqilla/framework/XQillaExport.hpp>
00026 #include <xercesc/framework/MemoryManager.hpp>
00027 #include <memory>
00028 #include <cstring>
00029
00030 #include <string>
00031
00032 #include <memory.h>
00033
00034 class XQILLA_API StringPool
00035 {
00036 public:
00037 StringPool(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mm);
00038 ~StringPool();
00039
00041 const XMLCh* getPooledString(const XMLCh *src);
00043 const XMLCh* getPooledString(const XMLCh *src, unsigned int length);
00045 const XMLCh* getPooledString(const char *src);
00046
00047 unsigned int getCount() const { return _count; }
00048 unsigned int getHits() const { return _hits; }
00049 unsigned int getMisses() const { return _misses; }
00050 unsigned int getTooBig() const { return _toobig; }
00051 void dumpStatistics() const;
00052
00053 private:
00054 StringPool(const StringPool&);
00055 StringPool &operator=(const StringPool&);
00056
00057 static unsigned int hash(const XMLCh *v, unsigned int length);
00058 const XMLCh *replicate(const XMLCh *v, unsigned int length) const;
00059 void resize();
00060
00061 class Bucket
00062 {
00063 public:
00064 Bucket(const XMLCh *v, unsigned int l, unsigned int h, Bucket *n)
00065 : value(v), length(l), hashValue(h), next(n) {}
00066
00067 const XMLCh *value;
00068 unsigned int length;
00069 unsigned int hashValue;
00070 Bucket *next;
00071 };
00072
00073 XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *_mm;
00074 Bucket **_bucketList;
00075 unsigned int _modulus;
00076 unsigned int _count;
00077 unsigned int _hits;
00078 unsigned int _misses;
00079 unsigned int _toobig;
00080 };
00081
00082 inline unsigned int StringPool::hash(const XMLCh *v, unsigned int length)
00083 {
00084 unsigned int hashVal = 0;
00085 while(length) {
00086 hashVal += (hashVal * 37) + (hashVal >> 24) + (unsigned int)(*v);
00087 ++v;
00088 --length;
00089 }
00090 return hashVal;
00091 }
00092
00093 inline const XMLCh *StringPool::replicate(const XMLCh *v, unsigned int length) const
00094 {
00095 unsigned int size = length * sizeof(XMLCh);
00096 XMLCh *ret = (XMLCh*)_mm->allocate(size + sizeof(XMLCh));
00097 memcpy(ret, v, size);
00098 ret[length] = 0;
00099 return ret;
00100 }
00101
00102 #endif