Commit a2b24d07 authored by Mark Olesen's avatar Mark Olesen
Browse files

draft for having a resizing granularity in DynamicList

- nice idea, but need to convince compiler that division by zero doesn't occur
parent 81184573
......@@ -25,19 +25,13 @@ License
\*---------------------------------------------------------------------------*/
#include "error.H"
#include "HashPtrTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Construct given initial table size
template<class T, class Key, class Hash>
HashPtrTable<T, Key, Hash>::HashPtrTable(label size)
Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size)
:
HashTable<T*, Key, Hash>(size)
{}
......@@ -45,7 +39,10 @@ HashPtrTable<T, Key, Hash>::HashPtrTable(label size)
// Construct as copy
template<class T, class Key, class Hash>
HashPtrTable<T, Key, Hash>::HashPtrTable(const HashPtrTable<T, Key, Hash>& ht)
Foam::HashPtrTable<T, Key, Hash>::HashPtrTable
(
const HashPtrTable<T, Key, Hash>& ht
)
:
HashTable<T*, Key, Hash>()
{
......@@ -59,7 +56,7 @@ HashPtrTable<T, Key, Hash>::HashPtrTable(const HashPtrTable<T, Key, Hash>& ht)
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
HashPtrTable<T, Key, Hash>::~HashPtrTable()
Foam::HashPtrTable<T, Key, Hash>::~HashPtrTable()
{
clear();
}
......@@ -68,7 +65,7 @@ HashPtrTable<T, Key, Hash>::~HashPtrTable()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
T* HashPtrTable<T, Key, Hash>::remove(iterator& it)
T* Foam::HashPtrTable<T, Key, Hash>::remove(iterator& it)
{
T* elemPtr = *it;
HashTable<T*, Key, Hash>::erase(it);
......@@ -77,7 +74,7 @@ T* HashPtrTable<T, Key, Hash>::remove(iterator& it)
template<class T, class Key, class Hash>
bool HashPtrTable<T, Key, Hash>::erase(iterator& it)
bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& it)
{
T* elemPtr = *it;
......@@ -98,7 +95,7 @@ bool HashPtrTable<T, Key, Hash>::erase(iterator& it)
template<class T, class Key, class Hash>
void HashPtrTable<T, Key, Hash>::clear()
void Foam::HashPtrTable<T, Key, Hash>::clear()
{
for
(
......@@ -117,10 +114,13 @@ void HashPtrTable<T, Key, Hash>::clear()
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
void HashPtrTable<T, Key, Hash>::operator=(const HashPtrTable<T, Key, Hash>& ht)
void Foam::HashPtrTable<T, Key, Hash>::operator=
(
const HashPtrTable<T, Key, Hash>& rhs
)
{
// Check for assignment to self
if (this == &ht)
if (this == &rhs)
{
FatalErrorIn
(
......@@ -132,17 +132,12 @@ void HashPtrTable<T, Key, Hash>::operator=(const HashPtrTable<T, Key, Hash>& ht)
clear();
for(const_iterator iter = ht.begin(); iter != ht.end(); ++iter)
for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
{
insert(iter.key(), new T(**iter));
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
#include "HashPtrTableIO.C"
......
......@@ -83,7 +83,7 @@ public:
// Constructors
//- Construct given initial table size
HashPtrTable(label size = 100);
HashPtrTable(const label size = 100);
//- Construct from Istream using given Istream constructor class
template<class INew>
......
......@@ -29,16 +29,11 @@ License
#include "Ostream.H"
#include "INew.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
template<class T, class Key, class Hash>
template<class INew>
void HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
{
is.fatalCheck("HashPtrTable<T, Key, Hash>::read(Istream&, const INew&)");
......@@ -147,14 +142,14 @@ void HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
template<class T, class Key, class Hash>
template<class INew>
HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is, const INew& inewt)
Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is, const INew& inewt)
{
read(is, inewt);
}
template<class T, class Key, class Hash>
HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is)
Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is)
{
read(is, INew<T>());
}
......@@ -163,7 +158,7 @@ HashPtrTable<T, Key, Hash>::HashPtrTable(Istream& is)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, class Key, class Hash>
Istream& operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L)
Foam::Istream& Foam::operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L)
{
L.clear();
L.read(is, INew<T>());
......@@ -173,7 +168,11 @@ Istream& operator>>(Istream& is, HashPtrTable<T, Key, Hash>& L)
template<class T, class Key, class Hash>
Ostream& operator<<(Ostream& os, const HashPtrTable<T, Key, Hash>& L)
Foam::Ostream& Foam::operator<<
(
Ostream& os,
const HashPtrTable<T, Key, Hash>& L
)
{
// Write size and start delimiter
os << nl << L.size() << nl << token::BEGIN_LIST << nl;
......@@ -199,8 +198,4 @@ Ostream& operator<<(Ostream& os, const HashPtrTable<T, Key, Hash>& L)
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
......@@ -72,7 +72,7 @@ public:
// Constructors
//- Construct given initial size
HashSet(label size = 100)
HashSet(const label size = 100)
:
HashTable<nil, Key, Hash>(size)
{}
......
......@@ -63,7 +63,7 @@ public:
// Constructors
//- Construct given initial size
Map(label size = 100)
Map(const label size = 100)
:
HashTable<T, label, Hash<label> >(size)
{}
......
......@@ -58,7 +58,7 @@ public:
// Constructors
//- Construct given initial map size
PtrMap(label size = 100)
PtrMap(const label size = 100)
:
HashPtrTable<T, label, Hash<label> >(size)
{}
......
......@@ -45,6 +45,7 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
List<T>(nElem),
capacity_(nElem)
{
// we could also enforce SizeInc granularity when (!SizeMult || !SizeDiv)
List<T>::size(0);
}
......@@ -96,6 +97,8 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setCapacity
// truncate addressed sizes too
nextFree = capacity_;
}
// we could also enforce SizeInc granularity when (!SizeMult || !SizeDiv)
List<T>::setSize(capacity_);
List<T>::size(nextFree);
......@@ -111,11 +114,25 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::reserve
// allocate more capacity?
if (nElem > capacity_)
{
capacity_ = max
(
nElem,
label(SizeInc + capacity_ * SizeMult / SizeDiv)
);
// TODO: convince the compiler that division by zero does not occur
// if (SizeInc && (!SizeMult || !SizeDiv))
// {
// // resize with SizeInc as the granularity
// capacity_ = nElem;
// unsigned pad = SizeInc - (capacity_ % SizeInc);
// if (pad != SizeInc)
// {
// capacity_ += pad;
// }
// }
// else
{
capacity_ = max
(
nElem,
label(SizeInc + capacity_ * SizeMult / SizeDiv)
);
}
// adjust allocated size, leave addressed size untouched
label nextFree = List<T>::size();
......@@ -134,11 +151,25 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
// allocate more capacity?
if (nElem > capacity_)
{
capacity_ = max
(
nElem,
label(SizeInc + capacity_ * SizeMult / SizeDiv)
);
// TODO: convince the compiler that division by zero does not occur
// if (SizeInc && (!SizeMult || !SizeDiv))
// {
// // resize with SizeInc as the granularity
// capacity_ = nElem;
// unsigned pad = SizeInc - (capacity_ % SizeInc);
// if (pad != SizeInc)
// {
// capacity_ += pad;
// }
// }
// else
{
capacity_ = max
(
nElem,
label(SizeInc + capacity_ * SizeMult / SizeDiv)
);
}
List<T>::setSize(capacity_);
}
......@@ -190,6 +221,7 @@ Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::shrink()
{
// use the full list when resizing
List<T>::size(capacity_);
// the new size
capacity_ = nextFree;
List<T>::setSize(capacity_);
......
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