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

@ -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>

View File

@ -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)
{}

View File

@ -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

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

View File

@ -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)
{}

View File

@ -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)
{}

View File

@ -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

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

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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
);

View File

@ -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)
{}

View File

@ -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;
}