oaHashSet.h

Go to the documentation of this file.
00001 // *****************************************************************************
00002 // *****************************************************************************
00003 // oaHashSet.h
00004 //
00005 // This file contains the definitions of the oaHashSet class. This class
00006 // implements a hash set, optimized according to the size of the key type. If
00007 // the key is no larger than an oaUInt4, then the oaHashSet stores the keys
00008 // directly in an underlying oaHashTbl. If the key is larger than this, then
00009 // the keys are stored in a separate array and indices into that array are
00010 // stored in the underlying oaHashTbl. (This saves the space that would
00011 // otherwise be consumed by a lot of empty key slots.)
00012 //
00013 //  oaHashSet
00014 //      This class implements a base class that can be used for all types of
00015 //      hash sets. It is used by deriving from it for a particular key type
00016 //      and then overriding the virtual functions hash() and compare() for
00017 //      that particular key type.
00018 //
00019 //  oaHashSetIter
00020 //      This class implements an iterator over all objects in a hash set.
00021 //
00022 // The following classes are private implementation classes used by the public
00023 // classes described above.
00024 //
00025 //  oaHashSetTbl
00026 //      This class implements a hash table that stores indices into a separate
00027 //      array of keys that is stored in a separate, owning class.
00028 //
00029 //  oaKeySizedHashSet
00030 //      This class implements the hash set, with two separate templatized
00031 //      implementations based on whether they is is larger than an oaUInt4.
00032 //
00033 // *****************************************************************************
00034 // Except as specified in the OpenAccess terms of use of Cadence or Silicon
00035 // Integration Initiative, this material may not be copied, modified,
00036 // re-published, uploaded, executed, or distributed in any way, in any medium,
00037 // in whole or in part, without prior written permission from Cadence.
00038 //
00039 //                Copyright 2002-2005 Cadence Design Systems, Inc.
00040 //                           All Rights Reserved.
00041 //
00042 //  $Author: icftcm $
00043 //  $Revision: #1 $
00044 //  $Date: 2010/08/09 $
00045 //  $State: Exp $
00046 // *****************************************************************************
00047 // *****************************************************************************
00048 
00049 
00050 
00051 #if !defined(oaHashSet_P)
00052 #define oaHashSet_P
00053 
00054 
00055 
00056 // *****************************************************************************
00057 // Nested includes
00058 // *****************************************************************************
00059 #include "oaArray.h"
00060 #include "oaHashTbl.h"
00061 
00062 
00063 
00064 // *****************************************************************************
00065 // Declare and define types in the OpenAccess namespace.
00066 // *****************************************************************************
00067 BEGIN_OA_NAMESPACE
00068 
00069 
00070 
00071 // *****************************************************************************
00072 // Forward declarations.
00073 // *****************************************************************************
00074 template<class K, bool KeyIsSmall>
00075 class oaKeySizedHashSet;
00076 template<class K, bool KeyIsSmall>
00077 class oaKeySizedHashSetIter;
00078 
00079 
00080 
00081 // *****************************************************************************
00082 // oaHashSetTbl
00083 //
00084 // This class implements a hash table that stores indices into an array of keys
00085 // that is stored in an owning class.
00086 // *****************************************************************************
00087 template<class K>
00088 class oaHashSetTbl : public oaHashTbl<oaUInt4> {
00089 protected:
00090                                 oaHashSetTbl(oaUInt4 sizeIn = 32);
00091 
00092     virtual void                hash(oaUInt4    index,
00093                                      oaUInt4    &start,
00094                                      oaUInt4    &stride) const;
00095     virtual oaBoolean           compare(oaUInt4     index,
00096                                         const void  *data) const;
00097 
00098     void                        setOwner(oaKeySizedHashSet<K, false> *ownerIn);
00099 
00100     oaKeySizedHashSet<K, false> *owner;
00101 
00102     friend class oaKeySizedHashSet<K, false>;
00103 };
00104 
00105 
00106 
00107 // *****************************************************************************
00108 // oaKeySizedHashSet<K, false>
00109 //
00110 // This class implements the hash set for key types larger than 4 bytes.
00111 // *****************************************************************************
00112 template<class K>
00113 class oaKeySizedHashSet<K, false> {
00114 public:
00115     void                    add(const K &key);
00116     oaBoolean               find(const K &key) const;
00117     void                    remove(const K &key);
00118 
00119     oaUInt4                 getNumEntries() const;
00120 
00121 protected:
00122                             oaKeySizedHashSet(oaUInt4 sizeIn = 32);
00123 
00124     virtual void            hash(K          key,
00125                                  oaUInt4    &start,
00126                                  oaUInt4    &stride) const = 0;
00127     virtual oaBoolean       compare(K   key1,
00128                                     K   key2) const = 0;
00129 
00130     void                    hashIndex(oaUInt4   index,
00131                                       oaUInt4   &start,
00132                                       oaUInt4   &stride) const;
00133     oaBoolean               compareIndex(oaUInt4    index,
00134                                          const K    &key) const;
00135 
00136     const oaHashSetTbl<K>   *getHashTbl() const;
00137 
00138     oaHashSetTbl<K>         tbl;
00139 
00140 private:
00141     oaArray<K>              keys;
00142 
00143     friend class oaHashSetTbl<K>;
00144     friend class oaKeySizedHashSetIter<K, false>;
00145 };
00146 
00147 
00148 
00149 // *****************************************************************************
00150 // oaKeySizedHashSetIter<K, false>
00151 //
00152 // This class definition defines the hash set iterator for keys larger than
00153 // 4 bytes.
00154 // *****************************************************************************
00155 template<class K>
00156 class oaKeySizedHashSetIter<K, false> {
00157 public:
00158                                 oaKeySizedHashSetIter(oaKeySizedHashSet<K, false> &setIn);
00159 
00160     void                        reset();
00161 
00162     oaBoolean                   getNext(K &key);
00163 
00164 protected:
00165     oaKeySizedHashSet<K, false> *set;
00166     oaUInt4                     index;
00167 };
00168 
00169 
00170 
00171 // *****************************************************************************
00172 // oaKeySizedHashSet<K, true>
00173 //
00174 // This class implements the hash set for key types 4 bytes in size or smaller.
00175 // *****************************************************************************
00176 template<class K>
00177 class oaKeySizedHashSet<K, true> : public oaHashTbl<K> {
00178 public:
00179                             oaKeySizedHashSet(oaUInt4 sizeIn = 32);
00180 
00181     oaBoolean               find(const K &key) const;
00182 
00183 protected:
00184     virtual oaBoolean       compare(K           key,
00185                                     const void  *data) const;
00186     virtual oaBoolean       compare(K   key1,
00187                                     K   key2) const = 0;
00188 
00189     oaHashTbl<K>            *getHashTbl() const;
00190 
00191     friend class oaKeySizedHashSetIter<K, true>;
00192 };
00193 
00194 
00195 
00196 // *****************************************************************************
00197 // oaKeySizedHashSetIter<K, true>
00198 //
00199 // This class definition defines the hash set iterator for keys not larger than
00200 // 4 bytes.
00201 // *****************************************************************************
00202 template<class K>
00203 class oaKeySizedHashSetIter<K, true> {
00204 public:
00205                                 oaKeySizedHashSetIter(oaKeySizedHashSet<K, true> &setIn);
00206 
00207     void                        reset();
00208 
00209     oaBoolean                   getNext(K &key);
00210 
00211 protected:
00212     oaKeySizedHashSet<K, true>  &set;
00213     oaUInt4                     index;
00214 };
00215 
00216 
00217 
00218 // *****************************************************************************
00219 // oaHashSet<K>
00220 //
00221 // This class definition defines oaHashSet<K> according to one of two different
00222 // implementations, depending on whether K is larger than 4 bytes.
00223 // *****************************************************************************
00224 template<class K>
00225 class oaHashSet : public oaKeySizedHashSet<K, sizeof(K) <= sizeof(oaUInt4)> {
00226 public:
00227                             oaHashSet(oaUInt4 sizeIn = 32);
00228 };
00229 
00230 
00231 
00232 // *****************************************************************************
00233 // Explicit template instantiations
00234 // *****************************************************************************
00235 #if defined(OA_BASE_DLL_EXTERN)
00236 OA_BASE_DLL_EXTERN template
00237 class OA_BASE_DLL_PVT oaHashSet<oaUInt4>;
00238 #endif
00239 
00240 
00241 
00242 // *****************************************************************************
00243 // oaHashSetIter<K>
00244 //
00245 // This class definition defines the hash set iterator according to one of two
00246 // different implementations, depending on whether K is larger than 4 bytes.
00247 // *****************************************************************************
00248 template<class K>
00249 class oaHashSetIter : public oaKeySizedHashSetIter<K, sizeof(K) <= sizeof(oaUInt4)> {
00250 public:
00251                             oaHashSetIter(oaHashSet<K> &setIn);
00252 };
00253 
00254 
00255 
00256 // *****************************************************************************
00257 // Explicit template instantiations
00258 // *****************************************************************************
00259 #if defined(OA_BASE_DLL_EXTERN)
00260 OA_BASE_DLL_EXTERN template
00261 class OA_BASE_DLL_PVT oaHashSetIter<oaUInt4>;
00262 #endif
00263 
00264 
00265 
00266 END_OA_NAMESPACE
00267 
00268 #endif

Return to top of page