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
This commit is contained in:
Mark Olesen
2009-02-26 15:32:47 +01:00
parent e562aecb73
commit 2aeee852e8
17 changed files with 159 additions and 34 deletions

View File

@ -52,16 +52,18 @@ int main(int argc, char *argv[])
list[1] = 2; list[1] = 2;
list[2] = 3; list[2] = 3;
list[3] = 4; list[3] = 4;
Info<< list << endl;
Info<< "list:" << list
<< " hash:" << FixedList<label, 4>::Hash<>()(list) << endl;
label a[4] = {0, 1, 2, 3}; label a[4] = {0, 1, 2, 3};
FixedList<label, 4> list2(a); 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); // FixedList<label, 3> hmm(Sin);
//Info<< hmm << endl; // Info<< hmm << endl;
if (Pstream::parRun()) if (Pstream::parRun())
{ {
@ -69,7 +71,12 @@ int main(int argc, char *argv[])
{ {
Serr<< "slave sending to master " Serr<< "slave sending to master "
<< Pstream::masterNo() << endl; << Pstream::masterNo() << endl;
OPstream toMaster(Pstream::masterNo(), IOstream::ASCII);
OPstream toMaster
(
Pstream::blocking, Pstream::masterNo(), IOstream::ASCII
);
FixedList<label, 2> list3; FixedList<label, 2> list3;
list3[0] = 0; list3[0] = 0;
list3[1] = 1; list3[1] = 1;
@ -79,13 +86,16 @@ int main(int argc, char *argv[])
{ {
for for
( (
int slave=Pstream::firstSlave(); int slave = Pstream::firstSlave();
slave<=Pstream::lastSlave(); slave <= Pstream::lastSlave();
slave++ slave++
) )
{ {
Serr << "master receiving from slave " << slave << endl; Serr << "master receiving from slave " << slave << endl;
IPstream fromSlave(slave, IOstream::ASCII); IPstream fromSlave
(
Pstream::blocking, slave, IOstream::ASCII
);
FixedList<label, 2> list3(fromSlave); FixedList<label, 2> list3(fromSlave);
Serr<< list3 << endl; Serr<< list3 << endl;

View File

@ -61,8 +61,20 @@ int main(int argc, char *argv[])
fullHash.insert(i); 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> > 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; cpuTime timer;
@ -252,6 +264,21 @@ int main(int argc, char *argv[])
<< " s" << endl; << " s" << endl;
Info<< " sum " << sum << 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; Info<< "Starting write tests" << endl;

View File

@ -38,10 +38,11 @@ int main(int argc, char *argv[])
{ {
string test("$HOME kjhkjhkjh \" \\$HOME/tyetyery ${FOAM_RUN} \n ; hkjh ;$"); 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 // test sub-strings via iterators
string::const_iterator iter = test.end(); string::const_iterator iter = test.end();
string::const_iterator iter2 = test.end(); string::const_iterator iter2 = test.end();
string::size_type fnd = test.find('\\'); 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<< "output string with " << s2.length() << " characters\n";
cout<< "ostream<< >" << s2 << "<\n"; cout<< "ostream<< >" << s2 << "<\n";
Info<< "Ostream<< >" << s2 << "<\n"; Info<< "Ostream<< >" << s2 << "<\n";
Info<< "hash:" << unsigned(string::hash()(s2)) << endl;
Info << "End\n" << endl; Info << "End\n" << endl;

View File

@ -83,7 +83,7 @@ public:
// Constructors // Constructors
//- Construct given initial table size //- Construct given initial table size
HashPtrTable(const label size = 100); HashPtrTable(const label size = 128);
//- Construct from Istream using given Istream constructor class //- Construct from Istream using given Istream constructor class
template<class INew> template<class INew>

View File

@ -72,7 +72,7 @@ public:
// Constructors // Constructors
//- Construct given initial size //- Construct given initial size
HashSet(const label size = 100) HashSet(const label size = 128)
: :
HashTable<nil, Key, Hash>(size) HashTable<nil, Key, Hash>(size)
{} {}

View File

@ -142,10 +142,10 @@ public:
// Constructors // Constructors
//- Construct given initial table size //- Construct given initial table size
HashTable(const label size = 100); HashTable(const label size = 128);
//- Construct from Istream //- Construct from Istream
HashTable(Istream&, const label size = 100); HashTable(Istream&, const label size = 128);
//- Construct as copy //- Construct as copy
HashTable(const HashTable<T, Key, Hash>&); HashTable(const HashTable<T, Key, Hash>&);
@ -183,6 +183,8 @@ public:
//- Return the table of contents //- Return the table of contents
List<Key> toc() const; List<Key> toc() const;
//- Print information
Ostream& printInfo(Ostream&) const;
// Edit // Edit

View File

@ -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 * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, class Key, class Hash> template<class T, class Key, class Hash>

View File

@ -63,7 +63,7 @@ public:
// Constructors // Constructors
//- Construct given initial size //- Construct given initial size
Map(const label size = 100) Map(const label size = 128)
: :
HashTable<T, label, Hash<label> >(size) HashTable<T, label, Hash<label> >(size)
{} {}

View File

@ -58,7 +58,7 @@ public:
// Constructors // Constructors
//- Construct given initial map size //- Construct given initial map size
PtrMap(const label size = 100) PtrMap(const label size = 128)
: :
HashPtrTable<T, label, Hash<label> >(size) HashPtrTable<T, label, Hash<label> >(size)
{} {}

View File

@ -141,10 +141,10 @@ public:
// Constructors // Constructors
//- Construct given initial table size //- Construct given initial table size
StaticHashTable(const label size = 100); StaticHashTable(const label size = 128);
//- Construct from Istream //- Construct from Istream
StaticHashTable(Istream&, const label size = 100); StaticHashTable(Istream&, const label size = 128);
//- Construct as copy //- Construct as copy
StaticHashTable(const StaticHashTable<T, Key, Hash>&); StaticHashTable(const StaticHashTable<T, Key, Hash>&);
@ -181,6 +181,9 @@ public:
//- Return the table of contents //- Return the table of contents
List<Key> toc() const; List<Key> toc() const;
//- Print information
Ostream& printInfo(Ostream&) const;
// Edit // Edit

View File

@ -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 * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, class Key, class Hash> template<class T, class Key, class Hash>

View File

@ -408,7 +408,8 @@ template<class HashT>
inline Foam::FixedList<T, Size>::Hash<HashT>::Hash() 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 T, Foam::label Size>
template<class HashT> template<class HashT>
inline Foam::label Foam::FixedList<T, Size>::Hash<HashT>::operator() 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++) 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; return val;
@ -436,7 +437,7 @@ inline Foam::label Foam::FixedList<T, Size>::Hash<HashT>::operator()
const label tableSize const label tableSize
) const ) const
{ {
return mag(operator()(lst)) % tableSize; return ::abs(operator()(lst)) % tableSize;
} }
#endif #endif

View File

@ -148,9 +148,11 @@ public:
//- Masking for all bits below the offset //- Masking for all bits below the offset
inline static unsigned int maskLower(unsigned offset); inline static unsigned int maskLower(unsigned offset);
// Forward declaration of iteratorBase // Forward declaration of iterators
class iteratorBase; class iteratorBase;
class iterator;
class const_iterator;
// Constructors // Constructors
@ -355,7 +357,10 @@ public:
public iteratorBase 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&); void operator=(const const_iterator&);
public: public:

View File

@ -63,7 +63,7 @@ public:
// Constructors // Constructors
//- Construct given an initial estimate for the number of entries //- 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 //- Construct from objectRegistry and instance path
IOobjectList IOobjectList

View File

@ -82,12 +82,12 @@ public:
// Constructors // Constructors
//- Construct the time objectRegistry given an initial estimate //- Construct the time objectRegistry given an initial estimate
// for the number of entries // for the number of entries
explicit objectRegistry explicit objectRegistry
( (
const Time& db, const Time& db,
const label nIoObjects = 100 const label nIoObjects = 128
); );
//- Construct a sub-registry given an IObject to describe the registry //- Construct a sub-registry given an IObject to describe the registry
@ -95,7 +95,7 @@ public:
explicit objectRegistry explicit objectRegistry
( (
const IOobject& io, const IOobject& io,
const label nIoObjects = 100 const label nIoObjects = 128
); );

View File

@ -56,7 +56,7 @@ public:
// Constructors // Constructors
//- Construct given initial map size //- Construct given initial map size
EdgeMap(label size = 100) EdgeMap(const label size = 128)
: :
HashTable<T, edge, Hash<edge> >(size) HashTable<T, edge, Hash<edge> >(size)
{} {}

View File

@ -133,7 +133,7 @@ inline bool Foam::string::meta(const string& str, const char quote)
template<class String> template<class String>
inline Foam::string inline Foam::string
Foam::string::quotemeta(const string& str, const char quote) Foam::string::quotemeta(const string& str, const char quote)
{ {
if (!quote) if (!quote)
@ -205,14 +205,14 @@ inline Foam::string::size_type Foam::string::hash::operator()
const string& key const string& key
) const ) const
{ {
register size_type hashVal = 0; register size_type val = 0;
for (string::const_iterator iter=key.begin(); iter!=key.end(); ++iter) for (string::const_iterator iter=key.begin(); iter!=key.end(); ++iter)
{ {
hashVal = hashVal<<1 ^ *iter; val = (val << 1) ^ *iter;
} }
return hashVal; return val;
} }