Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

Common/KWCommon/vtkHashMap.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Module:    $RCSfile: vtkHashMap.h,v $
00004 
00005   Copyright (c) Kitware, Inc.
00006   All rights reserved.
00007   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00008 
00009      This software is distributed WITHOUT ANY WARRANTY; without even
00010      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00011      PURPOSE.  See the above copyright notice for more information.
00012 
00013 =========================================================================*/
00028 #include "vtkAbstractMap.h"
00029 #include "vtkWindows.h" // used for windows.h
00030 #ifndef __vtkHashMap_h
00031 #define __vtkHashMap_h
00032 
00033 template<class DataType> class vtkVector;
00034 template<class KeyType, class DataType> class vtkHashMapIterator;
00035 
00036 template<class KeyType, class DataType>
00037 class vtkHashMap : public vtkAbstractMap<KeyType,DataType>
00038 {
00039   friend class vtkHashMapIterator<KeyType,DataType>;
00040   virtual const char* GetClassNameInternal() const {return "vtkHashMap";}
00041 public:
00042   typedef vtkAbstractMap<KeyType,DataType> Superclass;
00043   typedef vtkHashMapIterator<KeyType,DataType> IteratorType;
00044   
00045   // Cannot use this macro because of the comma in the type name.
00046   // The CPP splits that in two and we ae in trouble.
00047   //vtkContainerTypeMacro((vtkHashMap<KeyType,DataType>), vtkContainer);
00048 
00049   void PrintSelf(ostream& os, vtkIndent indent);
00050   static vtkHashMap<KeyType,DataType>* New(); 
00051   
00054   vtkHashMapIterator<KeyType,DataType>* NewIterator();
00055   
00059   int SetItem(const KeyType& key, const DataType& data);
00060   
00063   int RemoveItem(const KeyType& key);
00064   
00066   void RemoveAllItems();
00067   
00070   int GetItem(const KeyType& key, DataType& data);
00071 
00073   vtkIdType GetNumberOfItems() const;
00074   
00076 
00080   void SetMaximumLoadFactor(float factor);
00081   float GetMaximumLoadFactor() const;
00083   
00085 
00088   void SetNumberOfBuckets(vtkIdType n);
00089   vtkIdType GetNumberOfBuckets() const;  
00091   
00092 protected:
00093   vtkHashMap();
00094   virtual ~vtkHashMap();
00095   
00096   typedef vtkAbstractMapItem<KeyType,DataType> ItemType;
00097   typedef vtkVector<ItemType> BucketType;
00098   
00099   // Check if the load factor is okay.  Increase number of buckets and
00100   // re-hash if necessary.
00101   void CheckLoadFactor();
00102   
00103   // Change the number of buckets to the given number and re-hash the
00104   // items.
00105   void RehashItems(vtkIdType newNumberOfBuckets);
00106 
00108   vtkIdType HashKey(const KeyType& key, vtkIdType nbuckets);
00109   
00110   float MaximumLoadFactor;
00111   vtkIdType NumberOfItems;
00112   vtkIdType NumberOfBuckets;
00113   BucketType** Buckets;
00114   
00115 private:
00116   vtkHashMap(const vtkHashMap<KeyType,DataType>&); // Not implemented
00117   void operator=(const vtkHashMap<KeyType,DataType>&); // Not implemented
00118 };
00119 
00120 static inline unsigned long vtkHashMapHashMethod(int x) 
00121 { return static_cast<unsigned long>(x); }
00122 static inline unsigned long vtkHashMapHashMethod(const char* s)
00123 {
00124   unsigned long h = 0;
00125   for(;*s;++s)
00126     {
00127     h = 5*h + *s;
00128     }
00129   return h;
00130 }
00131 
00132 static inline unsigned long vtkHashMapHashMethod(vtkObjectBase* o) 
00133 { 
00134 #if defined ( _MSC_VER )
00135   return PtrToUlong(o);
00136 #else
00137   return reinterpret_cast<unsigned long>(o); 
00138 #endif
00139 }
00140 
00141 #ifdef VTK_NO_EXPLICIT_TEMPLATE_INSTANTIATION
00142 #include "vtkHashMap.txx"
00143 #endif 
00144 
00145 #endif
00146 
00147 
00148 

Generated on Tue May 30 12:31:37 2006 for ParaView by doxygen 1.3.5