StringPool.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2006
00003  *     DecisionSoft Limited. All rights reserved.
00004  * Copyright (c) 2004-2006
00005  *     Progress Software Corporation. All rights reserved.
00006  * Copyright (c) 2004-2006
00007  *     Oracle. All rights reserved.
00008  *
00009  * See the file LICENSE for redistribution information.
00010  *
00011  * $Id: StringPool.hpp,v 1.8 2006/11/01 16:37:12 jpcs Exp $
00012  */
00013 
00014 #ifndef __STRINGPOOL_HPP
00015 #define __STRINGPOOL_HPP
00016 
00017 #include <xqilla/framework/XQillaExport.hpp>
00018 #include <xercesc/framework/MemoryManager.hpp>
00019 #include <memory>
00020 #include <string>
00021 
00022 class XQILLA_API StringPool
00023 {
00024 public:
00025   StringPool(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mm);
00026   ~StringPool();
00027 
00029   const XMLCh* getPooledString(const XMLCh *src);
00031   const XMLCh* getPooledString(const char *src);
00032 
00033   unsigned int getCount() const { return _count; }
00034   unsigned int getHits() const { return _hits; }
00035   unsigned int getMisses() const { return _misses; }
00036   unsigned int getTooBig() const { return _toobig; }
00037   void dumpStatistics() const;
00038 
00039 private:
00040   StringPool(const StringPool&);
00041   StringPool &operator=(const StringPool&);
00042 
00043   static unsigned int hash(const XMLCh *v);
00044   const XMLCh *replicate(const XMLCh *v, unsigned int length) const;
00045   void resize();
00046 
00047   class Bucket
00048   {
00049   public:
00050     Bucket(const XMLCh *v, unsigned int l, unsigned int h, Bucket *n)
00051       : value(v), length(l), hashValue(h), next(n) {}
00052 
00053     const XMLCh *value;
00054     unsigned int length;
00055     unsigned int hashValue;
00056     Bucket *next;
00057   };
00058 
00059   XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *_mm;
00060   Bucket **_bucketList;
00061   unsigned int _modulus;
00062   unsigned int _count;
00063   unsigned int _hits;
00064   unsigned int _misses;
00065   unsigned int _toobig;
00066 };
00067 
00068 inline unsigned int StringPool::hash(const XMLCh *v)
00069 {
00070   unsigned int top;
00071   unsigned int hashVal = 0;
00072   while(*v) {
00073     top = hashVal >> 24;
00074     hashVal += (hashVal * 37) + top + (unsigned int)(*v);
00075     ++v;
00076   }
00077   return hashVal;
00078 }
00079 
00080 inline const XMLCh *StringPool::replicate(const XMLCh *v, unsigned int length) const
00081 {
00082   unsigned int size = (length+1) * sizeof(XMLCh);
00083   XMLCh *ret = (XMLCh*)_mm->allocate(size);
00084   memcpy(ret, v, size);
00085   return ret;
00086 }
00087 
00088 #endif

Generated on Mon Feb 12 16:52:06 2007 for XQilla Simple API by  doxygen 1.4.6