Commit 48247a3d authored by Mark Olesen's avatar Mark Olesen
Browse files

consistency update

- DynamicList gets append methods as per List
- misc cosmetic changes
parent 6ce8e7d0
......@@ -160,18 +160,33 @@ int main(int argc, char *argv[])
<< " " << lstB.size() << endl;
Info<< "<dlD>" << dlD << "</dlD>" << nl << "sizes: "
<< " " << dlD.size() << "/" << dlD.capacity() << endl;
DynamicList<label,10> dlE1(10);
DynamicList<label> dlE2(dlE1);
DynamicList<label> dlE2(dlE1); // construct dissimilar
Info<< "<dlE1>" << dlE1 << "</dlE1>" << nl << "sizes: "
<< " " << dlE1.size() << "/" << dlE1.capacity() << endl;
Info<< "<dlE2>" << dlE2 << "</dlE2>" << nl << "sizes: "
<< " " << dlE2.size() << "/" << dlE2.capacity() << endl;
dlE2.append(100);
for (label elemI=0; elemI < 5; ++elemI)
{
dlE1.append(4 - elemI);
dlE2.append(elemI);
}
Info<< "<dlE2>" << dlE2 << "</dlE2>" << endl;
DynamicList<label> dlE3(dlE2); // construct identical
Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
dlE3 = dlE1; // assign dissimilar
Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
dlE3 = dlE2; // assign identical
Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
Info<< "\nEnd\n";
return 0;
......
......@@ -39,7 +39,7 @@ using namespace Foam;
int main()
{
HASHTABLE_CLASS<double> table1(100);
HASHTABLE_CLASS<double> table1(13);
table1.insert("aaa", 1.0);
table1.insert("aba", 2.0);
......@@ -56,7 +56,8 @@ int main()
table1.erase("abs");
Info<< "\ntable1 toc: " << table1.toc() << endl;
Info<< "\ntable1 [" << table1.size() << "] " << endl;
table1.printInfo(Info)
<< "table1 [" << table1.size() << "] " << endl;
forAllIter(HASHTABLE_CLASS<double>, table1, iter)
{
Info<< iter.key() << " => " << iter() << nl;
......@@ -97,7 +98,7 @@ int main()
<< "transfer table1 -> table3 via the xfer() method" << nl;
Info<< "\ntable1" << table1 << nl
<< "\ntable2" << table1 << nl
<< "\ntable2" << table2 << nl
<< "\ntable3" << table3 << nl;
Info<< "\nerase table2 by iterator" << nl;
......@@ -113,10 +114,14 @@ int main()
<< "\ntable3" << table3 << nl;
table3.resize(1);
Info<< "\nresize(1) table3" << table3 << nl;
Info<< "\nresize(1) table3" << nl;
table3.printInfo(Info)
<< table3 << nl;
table3.resize(10000);
Info<< "\nresize(10000) table3" << table3 << nl;
Info<< "\nresize(10000) table3" << nl;
table3.printInfo(Info)
<< table3 << nl;
HASHTABLE_CLASS<double> table4;
......@@ -134,26 +139,27 @@ int main()
table1.erase(table1.begin());
Info<< "removed an element - test table1 != table3 : "
<< (table1 != table3) << nl;
// insert a few things into table2
table2.set("ada", 14.0);
table2.set("aeq", 15.0);
table2.set("aaw", 16.0);
table2.set("abs", 17.0);
table2.set("adx", 20.0);
Info<< "\ntable1" << table1 << nl
<< "\ntable2" << table2 << nl;
label nErased = table1.erase(table2);
Info<< "\nerase table2 keys from table1 (removed "
<< nErased << " elements)" << nl
<< "\ntable1" << table1 << nl
<< "\ntable2" << table2 << nl;
Info<< "\nclearStorage table3 ... ";
Info<< "\ntable3" << table3
<< "\nclearStorage table3 ... ";
table3.clearStorage();
Info<< table3 << nl;
......
......@@ -39,7 +39,7 @@ using namespace Foam;
int main()
{
HASHTABLE_CLASS<double> table1(100);
HASHTABLE_CLASS<double> table1(13);
table1.insert("aaa", 1.0);
table1.insert("aba", 2.0);
......@@ -56,7 +56,8 @@ int main()
table1.erase("abs");
Info<< "\ntable1 toc: " << table1.toc() << endl;
Info<< "\ntable1 [" << table1.size() << "] " << endl;
table1.printInfo(Info)
<< "table1 [" << table1.size() << "] " << endl;
forAllIter(HASHTABLE_CLASS<double>, table1, iter)
{
Info<< iter.key() << " => " << iter() << nl;
......@@ -97,7 +98,7 @@ int main()
<< "transfer table1 -> table3 via the xfer() method" << nl;
Info<< "\ntable1" << table1 << nl
<< "\ntable2" << table1 << nl
<< "\ntable2" << table2 << nl
<< "\ntable3" << table3 << nl;
Info<< "\nerase table2 by iterator" << nl;
......@@ -113,10 +114,14 @@ int main()
<< "\ntable3" << table3 << nl;
table3.resize(1);
Info<< "\nresize(1) table3" << table3 << nl;
Info<< "\nresize(1) table3" << nl;
table3.printInfo(Info)
<< table3 << nl;
table3.resize(10000);
Info<< "\nresize(10000) table3" << table3 << nl;
Info<< "\nresize(10000) table3" << nl;
table3.printInfo(Info)
<< table3 << nl;
HASHTABLE_CLASS<double> table4;
......@@ -134,26 +139,27 @@ int main()
table1.erase(table1.begin());
Info<< "removed an element - test table1 != table3 : "
<< (table1 != table3) << nl;
// insert a few things into table2
table2.set("ada", 14.0);
table2.set("aeq", 15.0);
table2.set("aaw", 16.0);
table2.set("abs", 17.0);
table2.set("adx", 20.0);
Info<< "\ntable1" << table1 << nl
<< "\ntable2" << table2 << nl;
label nErased = table1.erase(table2);
Info<< "\nerase table2 keys from table1 (removed "
<< nErased << " elements)" << nl
<< "\ntable1" << table1 << nl
<< "\ntable2" << table2 << nl;
Info<< "\nclearStorage table3 ... ";
Info<< "\ntable3" << table3
<< "\nclearStorage table3 ... ";
table3.clearStorage();
Info<< table3 << nl;
......@@ -162,4 +168,5 @@ int main()
return 0;
}
// ************************************************************************* //
......@@ -27,6 +27,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "UIndirectList.H"
#include "DynamicList.H"
#include "IOstreams.H"
#include "ListOps.H"
#include "OFstream.H"
......@@ -58,11 +59,11 @@ int main(int argc, char *argv[])
idl[1] = -666;
Info<< "idl[1] changed:" << idl << endl;
Info<< "idl[1] changed: " << idl << endl;
idl = -999;
Info<< "idl changed:" << idl << endl;
Info<< "idl changed: " << idl << endl;
UIndirectList<double> idl2(idl);
......@@ -79,17 +80,26 @@ int main(int argc, char *argv[])
idl = ident;
}
Info<< "idl assigned from UList:" << idl << endl;
Info<< "idl assigned from UList: " << idl << endl;
List<double> realList = UIndirectList<double>(completeList, addresses);
// test List operations
Info<< "realList:" << realList << endl;
List<double> flatList = UIndirectList<double>(completeList, addresses);
Info<< "List assigned from UIndirectList: " << flatList << endl;
List<double> realList2(UIndirectList<double>(completeList, addresses));
List<double> flatList2(UIndirectList<double>(completeList, addresses));
Info<< "List constructed from UIndirectList: " << flatList2 << endl;
Info<< "realList2:" << realList2 << endl;
flatList.append(UIndirectList<double>(completeList, addresses));
Info<< "List::append(UIndirectList): " << flatList << endl;
DynamicList<double> dynList(UIndirectList<double>(completeList, addresses));
Info<< "DynamicList constructed from UIndirectList: " << dynList << endl;
dynList.append(UIndirectList<double>(completeList, addresses));
Info<< "DynamicList::append(UIndirectList): " << dynList << endl;
Info << "\nEnd\n" << endl;
return 0;
......
......@@ -50,7 +50,7 @@ void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
label s = firstToken.labelToken();
// Read beginning of contents
char listDelimiter = is.readBeginList("HashPtrTable<T, Key, Hash>");
char delimiter = is.readBeginList("HashPtrTable<T, Key, Hash>");
if (s)
{
......@@ -59,7 +59,7 @@ void Foam::HashPtrTable<T, Key, Hash>::read(Istream& is, const INew& inewt)
this->resize(2*s);
}
if (listDelimiter == token::BEGIN_LIST)
if (delimiter == token::BEGIN_LIST)
{
for (label i=0; i<s; i++)
{
......
......@@ -28,6 +28,13 @@ Class
Description
An STL-conforming hash table.
Note
Hashing index collisions are handled via chaining using a singly-linked
list with the colliding entry being added to the head of the linked
list. Thus copying the hash table (or indeed even resizing it) will
often result in a different hash order. Use a sorted table-of-contents
when the hash order is important.
SourceFiles
HashTableI.H
HashTable.C
......
......@@ -81,7 +81,7 @@ Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const
os << "HashTable<T,Key,Hash>"
<< " elements:" << size() << " slots:" << used << "/" << tableSize_
<< " chaining(avg/max):" << (used ? float(avgChain/used) : 0)
<< " chaining(avg/max):" << (used ? (float(avgChain)/used) : 0)
<< "/" << maxChain << endl;
return os;
......@@ -91,7 +91,11 @@ Foam::HashTable<T, Key, Hash>::printInfo(Ostream& os) const
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T, class Key, class Hash>
Foam::Istream& Foam::operator>>(Istream& is, HashTable<T, Key, Hash>& L)
Foam::Istream& Foam::operator>>
(
Istream& is,
HashTable<T, Key, Hash>& L
)
{
is.fatalCheck("operator>>(Istream&, HashTable<T, Key, Hash>&)");
......@@ -113,7 +117,7 @@ Foam::Istream& Foam::operator>>(Istream& is, HashTable<T, Key, Hash>& L)
label s = firstToken.labelToken();
// Read beginning of contents
char listDelimiter = is.readBeginList("HashTable<T, Key, Hash>");
char delimiter = is.readBeginList("HashTable<T, Key, Hash>");
if (s)
{
......@@ -122,7 +126,7 @@ Foam::Istream& Foam::operator>>(Istream& is, HashTable<T, Key, Hash>& L)
L.resize(2*s);
}
if (listDelimiter == token::BEGIN_LIST)
if (delimiter == token::BEGIN_LIST)
{
for (label i=0; i<s; i++)
{
......@@ -209,7 +213,11 @@ Foam::Istream& Foam::operator>>(Istream& is, HashTable<T, Key, Hash>& L)
template<class T, class Key, class Hash>
Foam::Ostream& Foam::operator<<(Ostream& os, const HashTable<T, Key, Hash>& L)
Foam::Ostream& Foam::operator<<
(
Ostream& os,
const HashTable<T, Key, Hash>& L
)
{
// Write size and start delimiter
os << nl << L.size() << nl << token::BEGIN_LIST << nl;
......
......@@ -117,7 +117,7 @@ Foam::Istream& Foam::operator>>(Istream& is, StaticHashTable<T, Key, Hash>& L)
label s = firstToken.labelToken();
// Read beginning of contents
char listDelimiter = is.readBeginList("StaticHashTable<T, Key, Hash>");
char delimiter = is.readBeginList("StaticHashTable<T, Key, Hash>");
if (s)
{
......@@ -126,7 +126,7 @@ Foam::Istream& Foam::operator>>(Istream& is, StaticHashTable<T, Key, Hash>& L)
L.resize(2*s);
}
if (listDelimiter == token::BEGIN_LIST)
if (delimiter == token::BEGIN_LIST)
{
for (label i=0; i<s; i++)
{
......
......@@ -50,11 +50,11 @@ void Foam::ILList<LListBase, T>::read(Istream& is, const INew& iNew)
label s = firstToken.labelToken();
// Read beginning of contents
char listDelimiter = is.readBeginList("ILList<LListBase, T>");
char delimiter = is.readBeginList("ILList<LListBase, T>");
if (s)
{
if (listDelimiter == token::BEGIN_LIST)
if (delimiter == token::BEGIN_LIST)
{
for (label i=0; i<s; i++)
{
......
......@@ -61,11 +61,11 @@ Foam::Istream& Foam::operator>>(Istream& is, LList<LListBase, T>& L)
label s = firstToken.labelToken();
// Read beginning of contents
char listDelimiter = is.readBeginList("LList<LListBase, T>");
char delimiter = is.readBeginList("LList<LListBase, T>");
if (s)
{
if (listDelimiter == token::BEGIN_LIST)
if (delimiter == token::BEGIN_LIST)
{
for (register label i=0; i<s; i++)
{
......
......@@ -53,11 +53,11 @@ void Foam::LPtrList<LListBase, T>::read(Istream& is, const INew& iNew)
label s = firstToken.labelToken();
// Read beginning of contents
char listDelimiter = is.readBeginList("LPtrList<LListBase, T>");
char delimiter = is.readBeginList("LPtrList<LListBase, T>");
if (s)
{
if (listDelimiter == token::BEGIN_LIST)
if (delimiter == token::BEGIN_LIST)
{
for (label i=0; i<s; i++)
{
......
......@@ -111,8 +111,11 @@ public:
// Also constructs from DynamicList with different sizing parameters.
explicit inline DynamicList(const UList<T>&);
//- Construct from UIndirectList. Size set to UIndirectList size.
explicit inline DynamicList(const UIndirectList<T>&);
//- Construct by transferring the parameter contents
explicit inline DynamicList(const Xfer<List<T> >&);
explicit inline DynamicList(const Xfer< List<T> >&);
//- Construct from Istream. Size set to size of read list.
explicit DynamicList(Istream&);
......@@ -173,16 +176,19 @@ public:
inline void transfer(DynamicList<T, SizeInc, SizeMult, SizeDiv>&);
//- Transfer contents to the Xfer container as a plain List
inline Xfer<List<T> > xfer();
inline Xfer< List<T> > xfer();
// Member Operators
//- Append an element at the end of the list
inline void append(const T& e);
inline void append(const T&);
//- Append a List at the end of this list
inline void append(const UList<T>&);
//- Append a UIndirectList at the end of this list
inline void append(const UIndirectList<T>&);
//- Remove and return the top element
inline T remove();
......@@ -198,7 +204,7 @@ public:
const DynamicList<T, SizeInc, SizeMult, SizeDiv>&
);
//- Assignment from List<T>.
//- Assignment from UList
inline void operator=(const UList<T>&);
// IOstream operators
......
......@@ -72,6 +72,17 @@ inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
{}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
(
const UIndirectList<T>& lst
)
:
List<T>(lst),
capacity_(lst.size())
{}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
(
......@@ -287,10 +298,10 @@ Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::Xfer<Foam::List<T> >
inline Foam::Xfer< Foam::List<T> >
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::xfer()
{
return xferMoveTo<List<T> >(*this);
return xferMoveTo< List<T> >(*this);
}
......@@ -313,8 +324,6 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
const UList<T>& lst
)
{
label nextFree = List<T>::size();
if (this == &lst)
{
FatalErrorIn
......@@ -324,6 +333,23 @@ inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
) << "attempted appending to self" << abort(FatalError);
}
label nextFree = List<T>::size();
setSize(nextFree + lst.size());
forAll(lst, elemI)
{
this->operator[](nextFree++) = lst[elemI];
}
}
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
(
const UIndirectList<T>& lst
)
{
label nextFree = List<T>::size();
setSize(nextFree + lst.size());
forAll(lst, elemI)
......
......@@ -82,9 +82,9 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L)
}
// Read beginning of contents
char listDelimiter = is.readBeginList("FixedList");
char delimiter = is.readBeginList("FixedList");
if (listDelimiter == token::BEGIN_LIST)
if (delimiter == token::BEGIN_LIST)
{
for (register unsigned i=0; i<Size; i++)
{
......
......@@ -404,61 +404,6 @@ void Foam::List<T>::clear()
}
template<class T>
void Foam::List<T>::append(const UList<T>& lst)
{
if (this == &lst)
{
FatalErrorIn
(
"List<T>::append(const UList<T>&)"
) << "attempted appending to self" << abort(FatalError);
}
label nextFree = this->size_;
setSize(nextFree + lst.size());
forAll(lst, elemI)
{
this->operator[](nextFree++) = lst[elemI];
}
}
template<class T>
void Foam::List<T>::append(const UIndirectList<T>& lst)
{
label nextFree = this->size_;
setSize(nextFree + lst.size());
forAll(lst, elemI)
{
this->operator[](nextFree++) = lst[elemI];
}
}
template<class T>
void Foam::List<T>::append(const SLList<T>& lst)
{
if (lst.size())
{
label nextFree = this->size_;
setSize(nextFree + lst.size());
for
(
typename SLList<T>::const_iterator iter = lst.begin();
iter != lst.end();
++iter
)
{
this->operator[](nextFree++) = iter();
}
}
}
// Transfer the contents of the argument List into this List
// and anull the argument list
template<class T>
......
......@@ -182,24 +182,21 @@ public:
void clear();
//- Append a List at the end of this list
void append(const UList<T>&);
inline void append(const UList<T>&);
//- Append a UIndirectList at the end of this list
void append(const UIndirectList<T>&);
inline void append(const UIndirectList<T>&);