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 "StaticHashTable.H"
00028 #include "Istream.H"
00029 #include "Ostream.H"
00030
00031
00032
00033 template<class T, class Key, class Hash>
00034 Foam::StaticHashTable<T, Key, Hash>::StaticHashTable
00035 (
00036 Istream& is,
00037 const label size
00038 )
00039 :
00040 StaticHashTableName(),
00041 keys_(size),
00042 objects_(size),
00043 nElmts_(0),
00044 endIter_(*this, keys_.size(), 0),
00045 endConstIter_(*this, keys_.size(), 0)
00046 {
00047 if (size < 1)
00048 {
00049 FatalErrorIn
00050 (
00051 "StaticHashTable<T, Key, Hash>::StaticHashTable(const label size)"
00052 ) << "Illegal size " << size << " for StaticHashTable."
00053 << " Minimum size is 1" << abort(FatalError);
00054 }
00055
00056 operator>>(is, *this);
00057 }
00058
00059
00060
00061
00062 template<class T, class Key, class Hash>
00063 Foam::Ostream&
00064 Foam::StaticHashTable<T, Key, Hash>::printInfo(Ostream& os) const
00065 {
00066 label used = 0;
00067 label maxChain = 0;
00068 unsigned avgChain = 0;
00069
00070
00071 forAll(keys_, hashIdx)
00072 {
00073 const label count = keys_[hashIdx].size();
00074 if (count)
00075 {
00076 ++used;
00077 avgChain += count;
00078
00079 if (maxChain < count)
00080 {
00081 maxChain = count;
00082 }
00083 }
00084 }
00085
00086 os << "StaticHashTable<T,Key,Hash>"
00087 << " elements:" << size() << " slots:" << used << "/" << keys_.size()
00088 << " chaining(avg/max):" << (used ? float(avgChain/used) : 0)
00089 << "/" << maxChain << endl;
00090
00091 return os;
00092 }
00093
00094
00095
00096
00097 template<class T, class Key, class Hash>
00098 Foam::Istream& Foam::operator>>(Istream& is, StaticHashTable<T, Key, Hash>& L)
00099 {
00100 is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
00101
00102
00103 L.clear();
00104
00105 is.fatalCheck("operator>>(Istream&, StaticHashTable<T, Key, Hash>&)");
00106
00107 token firstToken(is);
00108
00109 is.fatalCheck
00110 (
00111 "operator>>(Istream&, StaticHashTable<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("StaticHashTable<T, Key, Hash>");
00121
00122 if (s)
00123 {
00124 if (2*s > L.keys_.size())
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&, StaticHashTable<T, Key, Hash>&)"
00140 " : reading entry"
00141 );
00142 }
00143 }
00144 else
00145 {
00146 FatalIOErrorIn
00147 (
00148 "operator>>(Istream&, StaticHashTable<T, Key, Hash>&)",
00149 is
00150 ) << "incorrect first token, '(', found " << firstToken.info()
00151 << exit(FatalIOError);
00152 }
00153 }
00154
00155
00156 is.readEndList("StaticHashTable");
00157 }
00158 else if (firstToken.isPunctuation())
00159 {
00160 if (firstToken.pToken() != token::BEGIN_LIST)
00161 {
00162 FatalIOErrorIn
00163 (
00164 "operator>>(Istream&, StaticHashTable<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&, StaticHashTable<T, Key, Hash>&) : "
00192 "reading entry"
00193 );
00194
00195 is >> lastToken;
00196 }
00197 }
00198 else
00199 {
00200 FatalIOErrorIn
00201 (
00202 "operator>>(Istream&, StaticHashTable<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&, StaticHashTable<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 StaticHashTable<T, Key, Hash>& L)
00220 {
00221
00222 os << nl << L.size() << nl << token::BEGIN_LIST << nl;
00223
00224
00225 for
00226 (
00227 typename StaticHashTable<T, Key, Hash>::const_iterator iter = L.begin();
00228 iter != L.end();
00229 ++iter
00230 )
00231 {
00232 os << iter.key() << token::SPACE << iter() << nl;
00233 }
00234
00235
00236 os << token::END_LIST;
00237
00238
00239 os.check("Ostream& operator<<(Ostream&, const StaticHashTable&)");
00240
00241 return os;
00242 }
00243
00244
00245