Commit 2aeee852 authored by Mark Olesen's avatar Mark Olesen
Browse files

PackedList bugfix, HashTable tweak

- it was possible to create a PackedList::iterator from a
  PackedList::const_iterator and violate const-ness

- added HashTable::printInfo for emitting some information

- changed default table sizes from 100 -> 128 in preparation for future
  2^n table sizes
parent e562aecb
......@@ -52,16 +52,18 @@ int main(int argc, char *argv[])
list[1] = 2;
list[2] = 3;
list[3] = 4;
Info<< list << endl;
Info<< "list:" << list
<< " hash:" << FixedList<label, 4>::Hash<>()(list) << endl;
label a[4] = {0, 1, 2, 3};
FixedList<label, 4> list2(a);
Info<< list2 << endl;
Info<< FixedList<label, 4>::Hash<>()(list2) << endl;
Info<< "list:" << list2
<< " hash:" << FixedList<label, 4>::Hash<>()(list2) << endl;
//FixedList<label, 3> hmm(Sin);
//Info<< hmm << endl;
// FixedList<label, 3> hmm(Sin);
// Info<< hmm << endl;
if (Pstream::parRun())
{
......@@ -69,7 +71,12 @@ int main(int argc, char *argv[])
{
Serr<< "slave sending to master "
<< Pstream::masterNo() << endl;
OPstream toMaster(Pstream::masterNo(), IOstream::ASCII);
OPstream toMaster
(
Pstream::blocking, Pstream::masterNo(), IOstream::ASCII
);
FixedList<label, 2> list3;
list3[0] = 0;
list3[1] = 1;
......@@ -79,13 +86,16 @@ int main(int argc, char *argv[])
{
for
(
int slave=Pstream::firstSlave();
slave<=Pstream::lastSlave();
int slave = Pstream::firstSlave();
slave <= Pstream::lastSlave();
slave++
)
{
Serr << "master receiving from slave " << slave << endl;
IPstream fromSlave(slave, IOstream::ASCII);
IPstream fromSlave
(
Pstream::blocking, slave, IOstream::ASCII
);
FixedList<label, 2> list3(fromSlave);
Serr<< list3 << endl;
......
......@@ -61,8 +61,20 @@ int main(int argc, char *argv[])
fullHash.insert(i);
}
// don't use fullStaticHash, it's too slow
// fullStaticHash is really slow
// give it lots of slots to help
StaticHashTable<nil, label, Hash<label> > emptyStaticHash;
StaticHashTable<nil, label, Hash<label> > fullStaticHash(100000);
for (label i = 0; i < n; i++)
{
fullStaticHash.insert(i, nil());
}
emptyHash.printInfo(Info);
fullHash.printInfo(Info);
emptyStaticHash.printInfo(Info);
fullStaticHash.printInfo(Info);
cpuTime timer;
......@@ -252,6 +264,21 @@ int main(int argc, char *argv[])
<< " s" << endl;
Info<< " sum " << sum << endl;
#if 0
// we can skip this test - it is usually quite slow
// Read full static hash
sum = 0;
for (label iter = 0; iter < nIters; ++iter)
{
forAll(unpacked, i)
{
sum += fullStaticHash.found(i);
}
}
Info<< "Reading full StaticHash:" << timer.cpuTimeIncrement()
<< " s" << endl;
Info<< " sum " << sum << endl;
#endif
Info<< "Starting write tests" << endl;
......
......@@ -38,10 +38,11 @@ int main(int argc, char *argv[])
{
string test("$HOME kjhkjhkjh \" \\$HOME/tyetyery ${FOAM_RUN} \n ; hkjh ;$");
Info<< test << endl;
Info<< "string:" << test << nl << "hash:"
<< unsigned(string::hash()(test)) << endl;
// test sub-strings via iterators
string::const_iterator iter = test.end();
string::const_iterator iter = test.end();
string::const_iterator iter2 = test.end();
string::size_type fnd = test.find('\\');
......@@ -77,6 +78,8 @@ int main(int argc, char *argv[])
cout<< "output string with " << s2.length() << " characters\n";
cout<< "ostream<< >" << s2 << "<\n";
Info<< "Ostream<< >" << s2 << "<\n";
Info<< "hash:" << unsigned(string::hash()(s2)) << endl;
Info << "End\n" << endl;
......
......@@ -83,7 +83,7 @@ public:
// Constructors
//- Construct given initial table size
HashPtrTable(const label size = 100);
HashPtrTable(const label size = 128);
//- Construct from Istream using given Istream constructor class
template<class INew>
......
......@@ -72,7 +72,7 @@ public:
// Constructors
//- Construct given initial size
HashSet(const label size = 100)
HashSet(const label size = 128)
:
HashTable<nil, Key, Hash>(size)
{}
......
......@@ -142,10 +142,10 @@ public:
// Constructors
//- Construct given initial table size
HashTable(const label size = 100);
HashTable(const label size = 128);
//- Construct from Istream
HashTable(Istream&, const label size = 100);
HashTable(Istream&, const label size = 128);
//- Construct as copy
HashTable(const HashTable<T, Key, Hash>&);
......@@ -183,6 +183,8 @@ public:
//- Return the table of contents
List<Key> toc() const;
//- Print information
Ostream& printInfo(Ostream&) const;
// Edit
......
......@@ -48,6 +48,45 @@ Foam::HashTable<T, Key, Hash>::HashTable(Istream& is, const label size)
}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
Foam::Ostream&
Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const
{
label used = 0;
label maxChain = 0;
unsigned avgChain = 0;
for (label hashIdx = 0; hashIdx < tableSize_; ++hashIdx)
{
label count = 0;
for (hashedEntry* ep = table_[hashIdx]; ep; ep = ep->next_)
{
++count;
}
if (count)
{
++used;
avgChain += count;
if (maxChain < count)
{
maxChain = count;
}
}
}
os << "HashTable<T,Key,Hash>"
<< " elements:" << size() << " slots:" << used << "/" << tableSize_
<< " chaining(avg/max):" << (used ? float(avgChain/used) : 0)
<< "/" << maxChain << endl;
return os;
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, class Key, class Hash>
......
......@@ -63,7 +63,7 @@ public:
// Constructors
//- Construct given initial size
Map(const label size = 100)
Map(const label size = 128)
:
HashTable<T, label, Hash<label> >(size)
{}
......
......@@ -58,7 +58,7 @@ public:
// Constructors
//- Construct given initial map size
PtrMap(const label size = 100)
PtrMap(const label size = 128)
:
HashPtrTable<T, label, Hash<label> >(size)
{}
......
......@@ -141,10 +141,10 @@ public:
// Constructors
//- Construct given initial table size
StaticHashTable(const label size = 100);
StaticHashTable(const label size = 128);
//- Construct from Istream
StaticHashTable(Istream&, const label size = 100);
StaticHashTable(Istream&, const label size = 128);
//- Construct as copy
StaticHashTable(const StaticHashTable<T, Key, Hash>&);
......@@ -181,6 +181,9 @@ public:
//- Return the table of contents
List<Key> toc() const;
//- Print information
Ostream& printInfo(Ostream&) const;
// Edit
......
......@@ -57,6 +57,41 @@ Foam::StaticHashTable<T, Key, Hash>::StaticHashTable
}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
Foam::Ostream&
Foam::StaticHashTable<T, Key, Hash>::printInfo(Ostream& os) const
{
label used = 0;
label maxChain = 0;
unsigned avgChain = 0;
// Find first non-empty entry
forAll(keys_, hashIdx)
{
const label count = keys_[hashIdx].size();
if (count)
{
++used;
avgChain += count;
if (maxChain < count)
{
maxChain = count;
}
}
}
os << "StaticHashTable<T,Key,Hash>"
<< " elements:" << size() << " slots:" << used << "/" << keys_.size()
<< " chaining(avg/max):" << (used ? float(avgChain/used) : 0)
<< "/" << maxChain << endl;
return os;
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, class Key, class Hash>
......
......@@ -408,7 +408,8 @@ template<class HashT>
inline Foam::FixedList<T, Size>::Hash<HashT>::Hash()
{}
//- Rotating Hash. From http://burtleburtle.net/bob/hash/doobs.html
// Rotating Hash
template<class T, Foam::label Size>
template<class HashT>
inline Foam::label Foam::FixedList<T, Size>::Hash<HashT>::operator()
......@@ -422,7 +423,7 @@ inline Foam::label Foam::FixedList<T, Size>::Hash<HashT>::operator()
for (register int i=0; i<Size; i++)
{
val = (val<<4)^(val>>farbit)^HashT()(lst[i]);
val = (val << 4) ^ (val >> farbit) ^ HashT()(lst[i]);
}
return val;
......@@ -436,7 +437,7 @@ inline Foam::label Foam::FixedList<T, Size>::Hash<HashT>::operator()
const label tableSize
) const
{
return mag(operator()(lst)) % tableSize;
return ::abs(operator()(lst)) % tableSize;
}
#endif
......
......@@ -148,9 +148,11 @@ public:
//- Masking for all bits below the offset
inline static unsigned int maskLower(unsigned offset);
// Forward declaration of iteratorBase
// Forward declaration of iterators
class iteratorBase;
class iterator;
class const_iterator;
// Constructors
......@@ -355,7 +357,10 @@ public:
public iteratorBase
{
//- Should never violate const-ness!
//- Disallow copy constructor from const_iterator - violates const-ness!
iterator(const const_iterator&);
//- Disallow assignment from const_iterator - violates const-ness!
void operator=(const const_iterator&);
public:
......
......@@ -63,7 +63,7 @@ public:
// Constructors
//- Construct given an initial estimate for the number of entries
explicit IOobjectList(const label nIoObjects = 100);
explicit IOobjectList(const label nIoObjects = 128);
//- Construct from objectRegistry and instance path
IOobjectList
......
......@@ -82,12 +82,12 @@ public:
// Constructors
//- Construct the time objectRegistry given an initial estimate
//- Construct the time objectRegistry given an initial estimate
// for the number of entries
explicit objectRegistry
(
const Time& db,
const label nIoObjects = 100
const label nIoObjects = 128
);
//- Construct a sub-registry given an IObject to describe the registry
......@@ -95,7 +95,7 @@ public:
explicit objectRegistry
(
const IOobject& io,
const label nIoObjects = 100
const label nIoObjects = 128
);
......
......@@ -56,7 +56,7 @@ public:
// Constructors
//- Construct given initial map size
EdgeMap(label size = 100)
EdgeMap(const label size = 128)
:
HashTable<T, edge, Hash<edge> >(size)
{}
......
......@@ -133,7 +133,7 @@ inline bool Foam::string::meta(const string& str, const char quote)
template<class String>
inline Foam::string
inline Foam::string
Foam::string::quotemeta(const string& str, const char quote)
{
if (!quote)
......@@ -205,14 +205,14 @@ inline Foam::string::size_type Foam::string::hash::operator()
const string& key
) const
{
register size_type hashVal = 0;
register size_type val = 0;
for (string::const_iterator iter=key.begin(); iter!=key.end(); ++iter)
{
hashVal = hashVal<<1 ^ *iter;
val = (val << 1) ^ *iter;
}
return hashVal;
return val;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment