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