00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef HashTable_H
00046 #define HashTable_H
00047
00048 #include "label.H"
00049 #include "uLabel.H"
00050 #include "word.H"
00051 #include "Xfer.H"
00052 #include "className.H"
00053
00054
00055
00056 namespace Foam
00057 {
00058
00059
00060
00061 template<class T> class List;
00062 template<class T> class UList;
00063 template<class T, class Key, class Hash> class HashTable;
00064 template<class T, class Key, class Hash> class HashPtrTable;
00065
00066 template<class T, class Key, class Hash>
00067 Istream& operator>>(Istream&, HashTable<T, Key, Hash>&);
00068
00069 template<class T, class Key, class Hash>
00070 Ostream& operator<<(Ostream&, const HashTable<T, Key, Hash>&);
00071
00072
00073
00074
00075
00076
00077 TemplateName(HashTable);
00078
00079
00080
00081
00082
00083
00084 template<class T, class Key=word, class Hash=string::hash>
00085 class HashTable
00086 :
00087 public HashTableName
00088 {
00089
00090
00091 struct hashedEntry
00092 {
00093
00094 Key key_;
00095
00096
00097 hashedEntry* next_;
00098
00099
00100 T obj_;
00101
00102
00103
00104
00105 inline hashedEntry
00106 (
00107 const Key&,
00108 hashedEntry* next,
00109 const T& newEntry
00110 );
00111
00112
00113 hashedEntry(const hashedEntry&);
00114 };
00115
00116
00117
00118
00119
00120 label nElmts_;
00121
00122
00123 label tableSize_;
00124
00125
00126 hashedEntry** table_;
00127
00128
00129
00130
00131
00132 static label canonicalSize(const label);
00133
00134
00135
00136 inline label hashKeyIndex(const Key&) const;
00137
00138
00139 bool set(const Key&, const T& newElmt, bool protect);
00140
00141 public:
00142
00143
00144 template<class T2, class Key2, class Hash2>
00145 friend class HashPtrTable;
00146
00147
00148
00149
00150 class iterator;
00151 friend class iterator;
00152
00153 class const_iterator;
00154 friend class const_iterator;
00155
00156
00157
00158
00159
00160 HashTable(const label size = 128);
00161
00162
00163 HashTable(Istream&, const label size = 128);
00164
00165
00166 HashTable(const HashTable<T, Key, Hash>&);
00167
00168
00169 HashTable(const Xfer<HashTable<T, Key, Hash> >&);
00170
00171
00172
00173
00174 ~HashTable();
00175
00176
00177
00178
00179
00180
00181
00182 inline label size() const;
00183
00184
00185 inline bool empty() const;
00186
00187
00188 bool found(const Key&) const;
00189
00190
00191
00192 iterator find(const Key&);
00193
00194
00195
00196 const_iterator find(const Key&) const;
00197
00198
00199 List<Key> toc() const;
00200
00201
00202 Ostream& printInfo(Ostream&) const;
00203
00204
00205
00206
00207 inline bool insert(const Key&, const T& newElmt);
00208
00209
00210 inline bool set(const Key&, const T& newElmt);
00211
00212
00213 bool erase(const iterator&);
00214
00215
00216 bool erase(const Key&);
00217
00218
00219
00220 label erase(const UList<Key>&);
00221
00222
00223
00224
00225
00226 template<class AnyType>
00227 label erase(const HashTable<AnyType, Key, Hash>&);
00228
00229
00230 void resize(const label newSize);
00231
00232
00233 void clear();
00234
00235
00236
00237 void clearStorage();
00238
00239
00240
00241 void transfer(HashTable<T, Key, Hash>&);
00242
00243
00244 inline Xfer<HashTable<T, Key, Hash> > xfer();
00245
00246
00247
00248
00249
00250 inline T& operator[](const Key&);
00251
00252
00253 inline const T& operator[](const Key&) const;
00254
00255
00256 inline T& operator()(const Key&);
00257
00258
00259 void operator=(const HashTable<T, Key, Hash>&);
00260
00261
00262
00263
00264 bool operator==(const HashTable<T, Key, Hash>&) const;
00265
00266
00267 bool operator!=(const HashTable<T, Key, Hash>&) const;
00268
00269
00270
00271
00272
00273
00274 typedef T value_type;
00275
00276
00277
00278 typedef T& reference;
00279
00280
00281
00282
00283 typedef const T& const_reference;
00284
00285
00286 typedef label size_type;
00287
00288
00289
00290
00291
00292 class iterator
00293 {
00294 friend class HashTable;
00295 friend class const_iterator;
00296
00297
00298
00299
00300 HashTable<T, Key, Hash>& hashTable_;
00301
00302
00303 hashedEntry* elmtPtr_;
00304
00305
00306 label hashIndex_;
00307
00308 public:
00309
00310
00311
00312
00313 inline iterator
00314 (
00315 HashTable<T, Key, Hash>& curHashTable,
00316 hashedEntry* elmt,
00317 label hashIndex
00318 );
00319
00320
00321
00322 inline void operator=(const iterator&);
00323
00324 inline bool operator==(const iterator&) const;
00325 inline bool operator!=(const iterator&) const;
00326
00327 inline bool operator==(const const_iterator&) const;
00328 inline bool operator!=(const const_iterator&) const;
00329
00330 inline T& operator*();
00331 inline T& operator()();
00332
00333 inline const T& operator*() const;
00334 inline const T& operator()() const;
00335
00336 inline iterator& operator++();
00337 inline iterator operator++(int);
00338
00339 inline const Key& key() const;
00340 };
00341
00342
00343
00344 inline iterator begin();
00345
00346
00347 inline const iterator& end();
00348
00349
00350
00351
00352
00353 class const_iterator
00354 {
00355 friend class iterator;
00356
00357
00358
00359
00360 const HashTable<T, Key, Hash>& hashTable_;
00361
00362
00363 const hashedEntry* elmtPtr_;
00364
00365
00366 label hashIndex_;
00367
00368
00369 public:
00370
00371
00372
00373
00374 inline const_iterator
00375 (
00376 const HashTable<T, Key, Hash>& curHashTable,
00377 const hashedEntry* elmt,
00378 label hashIndex
00379 );
00380
00381
00382 inline const_iterator(const iterator&);
00383
00384
00385
00386
00387 inline void operator=(const const_iterator&);
00388
00389 inline bool operator==(const const_iterator&) const;
00390 inline bool operator!=(const const_iterator&) const;
00391
00392 inline bool operator==(const iterator&) const;
00393 inline bool operator!=(const iterator&) const;
00394
00395 inline const T& operator*() const;
00396 inline const T& operator()() const;
00397
00398 inline const_iterator& operator++();
00399 inline const_iterator operator++(int);
00400
00401 inline const Key& key() const;
00402 };
00403
00404
00405
00406 inline const_iterator cbegin() const;
00407
00408
00409 inline const const_iterator& cend() const;
00410
00411
00412 inline const_iterator begin() const;
00413
00414
00415 inline const const_iterator& end() const;
00416
00417
00418
00419
00420 friend Istream& operator>> <T, Key, Hash>
00421 (
00422 Istream&,
00423 HashTable<T, Key, Hash>&
00424 );
00425
00426 friend Ostream& operator<< <T, Key, Hash>
00427 (
00428 Ostream&,
00429 const HashTable<T, Key, Hash>&
00430 );
00431
00432
00433 private:
00434
00435
00436 iterator endIter_;
00437
00438
00439 const_iterator endConstIter_;
00440 };
00441
00442
00443
00444
00445 }
00446
00447
00448
00449 # include "HashTableI.H"
00450
00451
00452
00453 #ifndef NoHashTableC
00454 #ifdef NoRepository
00455 # include "HashTable.C"
00456 #endif
00457 #endif
00458
00459
00460
00461 #endif
00462
00463