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 #include "HashTable.H"
00028 #include "Istream.H"
00029 #include "Ostream.H"
00030
00031
00032
00033 template<class T, class Key, class Hash>
00034 Foam::HashTable<T, Key, Hash>::HashTable(Istream& is, const label size)
00035 :
00036 HashTableName(),
00037 nElmts_(0),
00038 tableSize_(canonicalSize(size)),
00039 table_(new hashedEntry*[tableSize_]),
00040 endIter_(*this, NULL, 0),
00041 endConstIter_(*this, NULL, 0)
00042 {
00043 for (label hashIdx = 0; hashIdx < tableSize_; hashIdx++)
00044 {
00045 table_[hashIdx] = 0;
00046 }
00047
00048 operator>>(is, *this);
00049 }
00050
00051
00052
00053
00054 template<class T, class Key, class Hash>
00055 Foam::Ostream&
00056 Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const
00057 {
00058 label used = 0;
00059 label maxChain = 0;
00060 unsigned avgChain = 0;
00061
00062 for (label hashIdx = 0; hashIdx < tableSize_; ++hashIdx)
00063 {
00064 label count = 0;
00065 for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
00066 {
00067 ++count;
00068 }
00069
00070 if (count)
00071 {
00072 ++used;
00073 avgChain += count;
00074
00075 if (maxChain < count)
00076 {
00077 maxChain = count;
00078 }
00079 }
00080 }
00081
00082 os << "HashTable<T,Key,Hash>"
00083 << " elements:" << size() << " slots:" << used << "/" << tableSize_
00084 << " chaining(avg/max):" << (used ? (float(avgChain)/used) : 0)
00085 << "/" << maxChain << endl;
00086
00087 return os;
00088 }
00089
00090
00091
00092
00093 template<class T, class Key, class Hash>
00094 Foam::Istream& Foam::operator>>
00095 (
00096 Istream& is,
00097 HashTable<T, Key, Hash>& L
00098 )
00099 {
00100 is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
00101
00102
00103 L.clear();
00104
00105 is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
00106
00107 token firstToken(is);
00108
00109 is.fatalCheck
00110 (
00111 "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
00112 "reading first token"
00113 );
00114
00115 if (firstToken.isLabel())
00116 {
00117 label s = firstToken.labelToken();
00118
00119
00120 char delimiter = is.readBeginList("HashTable<T, Key, Hash>");
00121
00122 if (s)
00123 {
00124 if (2*s > L.tableSize_)
00125 {
00126 L.resize(2*s);
00127 }
00128
00129 if (delimiter == token::BEGIN_LIST)
00130 {
00131 for (label i=0; i<s; i++)
00132 {
00133 Key key;
00134 is >> key;
00135 L.insert(key, pTraits<T>(is));
00136
00137 is.fatalCheck
00138 (
00139 "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
00140 "reading entry"
00141 );
00142 }
00143 }
00144 else
00145 {
00146 FatalIOErrorIn
00147 (
00148 "operator>>(Istream&, HashTable<T, Key, Hash>&)",
00149 is
00150 ) << "incorrect first token, '(', found " << firstToken.info()
00151 << exit(FatalIOError);
00152 }
00153 }
00154
00155
00156 is.readEndList("HashTable");
00157 }
00158 else if (firstToken.isPunctuation())
00159 {
00160 if (firstToken.pToken() != token::BEGIN_LIST)
00161 {
00162 FatalIOErrorIn
00163 (
00164 "operator>>(Istream&, HashTable<T, Key, Hash>&)",
00165 is
00166 ) << "incorrect first token, '(', found " << firstToken.info()
00167 << exit(FatalIOError);
00168 }
00169
00170 token lastToken(is);
00171 while
00172 (
00173 !(
00174 lastToken.isPunctuation()
00175 && lastToken.pToken() == token::END_LIST
00176 )
00177 )
00178 {
00179 is.putBack(lastToken);
00180
00181 Key key;
00182 is >> key;
00183
00184 T element;
00185 is >> element;
00186
00187 L.insert(key, element);
00188
00189 is.fatalCheck
00190 (
00191 "operator>>(Istream&, HashTable<T, Key, Hash>&) : "
00192 "reading entry"
00193 );
00194
00195 is >> lastToken;
00196 }
00197 }
00198 else
00199 {
00200 FatalIOErrorIn
00201 (
00202 "operator>>(Istream&, HashTable<T, Key, Hash>&)",
00203 is
00204 ) << "incorrect first token, expected <int> or '(', found "
00205 << firstToken.info()
00206 << exit(FatalIOError);
00207 }
00208
00209 is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
00210
00211 return is;
00212 }
00213
00214
00215 template<class T, class Key, class Hash>
00216 Foam::Ostream& Foam::operator<<
00217 (
00218 Ostream& os,
00219 const HashTable<T, Key, Hash>& L
00220 )
00221 {
00222
00223 os << nl << L.size() << nl << token::BEGIN_LIST << nl;
00224
00225
00226 for
00227 (
00228 typename HashTable<T, Key, Hash>::const_iterator iter = L.cbegin();
00229 iter != L.cend();
00230 ++iter
00231 )
00232 {
00233 os << iter.key() << token::SPACE << iter() << nl;
00234 }
00235
00236
00237 os << token::END_LIST;
00238
00239
00240 os.check("Ostream& operator<<(Ostream&, const HashTable&)");
00241
00242 return os;
00243 }
00244
00245
00246