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

ENH: added binary IO for PackedList and compact ASCII format

The compact ASCII format is a block of index/value tuples for the
non-zero entries:

    { (index1 value1) (index2 value2) (index3 value3) }

For PackedList<1>, and thus PackedBoolList, the compact ASCII format is
a block of indices for the non-zero entries:

    { index1 index2 index3 }

Thus either of the following could be used - for PackedList<2>:

  - a list of all values:
        16(0 3 0 2 0 0 3 1 0 0 0 0 0 0 0 1)

  - a block of index/value tuples:
        {(1 3) (3 2) (7 3) (8 1) (15 1)}

For PackedList<1> and PackedBoolList, either of the following could be
used:

  - a list of all values, using any valid bool representation:
        16(0 1 0 true 0 0 t 1 0 n n 0 0 0 0 yes)

  - a block of the indices for non-zero entries:
        {1 3 7 8 15}
parent d18695d7
......@@ -166,7 +166,7 @@ int main(int argc, char *argv[])
if (args.optionFound("info"))
{
packLst.print(Info);
packLst.printInfo(Info);
}
Info<< nl;
......
......@@ -42,42 +42,42 @@ int main(int argc, char *argv[])
Info<< "\ntest allocation with value\n";
PackedList<3> list1(5,1);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest assign uniform value\n";
list1 = 3;
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest assign uniform value (with overflow)\n";
list1 = -1;
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest zero\n";
list1 = 0;
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest set() with default argument (max_value)\n";
list1.set(1);
list1.set(3);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest unset() with in-range and out-of-range\n";
list1.unset(3);
list1.unset(100000);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest assign between references\n";
list1[2] = 3;
list1[4] = list1[2];
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest assign between references, with chaining\n";
list1[0] = list1[4] = 1;
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest assign between references, with chaining and auto-vivify\n";
list1[1] = list1[8] = list1[10] = list1[14] = 2;
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest operator== between references\n";
......@@ -126,7 +126,7 @@ int main(int argc, char *argv[])
{
const PackedList<3>& constLst = list1;
Info<< "\ntest operator[] const with out-of-range index\n";
constLst.print(Info, true);
constLst.printInfo(Info, true);
if (constLst[20])
{
Info<< "[20] is true (unexpected)\n";
......@@ -136,7 +136,7 @@ int main(int argc, char *argv[])
Info<< "[20] is false (expected) list size should be unchanged "
<< "(const)\n";
}
constLst.print(Info, true);
constLst.printInfo(Info, true);
Info<< "\ntest operator[] non-const with out-of-range index\n";
if (list1[20])
......@@ -148,7 +148,7 @@ int main(int argc, char *argv[])
Info<< "[20] is false (expected) but list was resized?? "
<< "(non-const)\n";
}
list1.print(Info, true);
list1.printInfo(Info, true);
}
......@@ -157,85 +157,85 @@ int main(int argc, char *argv[])
{
Info<< "[20] is false, as expected\n";
}
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest resize with value (without reallocation)\n";
list1.resize(8, list1.max_value());
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest flip() function\n";
list1.flip();
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\nre-flip()\n";
list1.flip();
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest set() function\n";
list1.set(1, 5);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest assign bool\n";
list1 = false;
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest assign bool\n";
list1 = true;
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest resize without value (with reallocation)\n";
list1.resize(12);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest resize with value (with reallocation)\n";
list1.resize(25, list1.max_value());
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest resize smaller (should not touch allocation)\n";
list1.resize(8);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest append() operation\n";
list1.append(2);
list1.append(3);
list1.append(4);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest reserve() operation\n";
list1.reserve(32);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest shrink() operation\n";
list1.shrink();
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest setCapacity() operation\n";
list1.setCapacity(15);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest setCapacity() operation\n";
list1.setCapacity(100);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest operator[] assignment\n";
list1[16] = 5;
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest operator[] assignment with auto-vivify\n";
list1[36] = list1.max_value();
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest setCapacity smaller\n";
list1.setCapacity(24);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest resize much smaller\n";
list1.resize(150);
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest trim\n";
list1.trim();
list1.print(Info, true);
list1.printInfo(Info, true);
// add in some misc values
list1[31] = 1;
......@@ -245,40 +245,40 @@ int main(int argc, char *argv[])
Info<< "\ntest iterator\n";
PackedList<3>::iterator iter = list1.begin();
Info<< "begin():";
iter.print(Info) << "\n";
iter.printInfo(Info) << "\n";
Info<< "iterator:" << iter() << "\n";
iter() = 5;
iter.print(Info);
list1.print(Info, true);
iter.printInfo(Info);
list1.printInfo(Info, true);
iter = list1[31];
Info<< "iterator:" << iter() << "\n";
iter.print(Info);
iter.printInfo(Info);
Info<< "\ntest get() method\n";
Info<< "get(10):" << list1.get(10) << " and list[10]:" << list1[10] << "\n";
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest iterator indexing\n";
Info<< "cend() ";
list1.cend().print(Info) << "\n";
list1.cend().printInfo(Info) << "\n";
{
Info<< "\ntest assignment of iterator\n";
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "cend()\n";
list1.end().print(Info);
list1.end().printInfo(Info);
PackedList<3>::iterator cit = list1[100];
Info<< "out-of-range: ";
cit.print(Info);
cit.printInfo(Info);
cit = list1[15];
Info<< "in-range: ";
cit.print(Info);
cit.printInfo(Info);
Info<< "out-of-range: ";
cit = list1[1000];
cit.print(Info);
cit.printInfo(Info);
}
......@@ -289,7 +289,7 @@ int main(int argc, char *argv[])
++cit
)
{
cit.print(Info);
cit.printInfo(Info);
}
Info<< "\ntest operator[] auto-vivify\n";
......@@ -304,16 +304,16 @@ int main(int argc, char *argv[])
Info<< "size after write:" << list1.size() << "\n";
Info<< "list[45]:" << list1[45] << "\n";
list1[49] = list1[100];
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "\ntest copy constructor + append\n";
PackedList<3> list2(list1);
list2.append(4);
Info<< "source list:\n";
list1.print(Info, true);
list1.printInfo(Info, true);
Info<< "destination list:\n";
list2.print(Info, true);
list2.printInfo(Info, true);
Info<< "\ntest pattern that fills all bits\n";
PackedList<4> list3(8, 8);
......@@ -323,10 +323,10 @@ int main(int argc, char *argv[])
list3[pos--] = list3.max_value();
list3[pos--] = 0;
list3[pos--] = list3.max_value();
list3.print(Info, true);
list3.printInfo(Info, true);
Info<< "removed final value: " << list3.remove() << endl;
list3.print(Info, true);
list3.printInfo(Info, true);
Info<<"list: " << list3 << endl;
......@@ -358,7 +358,7 @@ int main(int argc, char *argv[])
PackedBoolList listb(list4);
Info<< "copied from bool list " << endl;
listb.print(Info, true);
listb.printInfo(Info, true);
{
labelList indices = listb.used();
......
......@@ -47,12 +47,12 @@ int main(int argc, char *argv[])
}
Info<< "\nalternating bit pattern\n";
list1.print(Info, true);
list1.printInfo(Info, true);
PackedBoolList list2 = ~list1;
Info<< "\ncomplementary bit pattern\n";
list2.print(Info, true);
list2.printInfo(Info, true);
list2.resize(24, true);
list2.resize(28, false);
......@@ -62,74 +62,73 @@ int main(int argc, char *argv[])
}
Info<< "\nresized with 4 true + 4 false, bottom 4 bits true\n";
list2.print(Info, true);
list2.printInfo(Info, true);
labelList list2Labels = list2.used();
Info<< "\noperator|\n";
(list1 | list2).print(Info, true);
(list1 | list2).printInfo(Info, true);
Info<< "\noperator& : does trim\n";
(list1 & list2).print(Info, true);
(list1 & list2).printInfo(Info, true);
Info<< "\noperator^\n";
(list1 ^ list2).print(Info, true);
(list1 ^ list2).printInfo(Info, true);
Info<< "\noperator|=\n";
{
PackedBoolList list3 = list1;
(list3 |= list2).print(Info, true);
(list3 |= list2).printInfo(Info, true);
}
Info<< "\noperator|= with UList<label>\n";
{
PackedBoolList list3 = list1;
(list3 |= list2Labels).print(Info, true);
(list3 |= list2Labels).printInfo(Info, true);
}
Info<< "\noperator&=\n";
{
PackedBoolList list3 = list1;
(list3 &= list2).print(Info, true);
(list3 &= list2).printInfo(Info, true);
}
Info<< "\noperator+=\n";
{
PackedBoolList list3 = list1;
(list3 += list2).print(Info, true);
(list3 += list2).printInfo(Info, true);
}
Info<< "\noperator+= with UList<label>\n";
{
PackedBoolList list3 = list1;
(list3 += list2Labels).print(Info, true);
(list3 += list2Labels).printInfo(Info, true);
}
Info<< "\noperator-=\n";
{
PackedBoolList list3 = list1;
(list3 -= list2).print(Info, true);
(list3 -= list2).printInfo(Info, true);
}
Info<< "\noperator-= with UList<label>\n";
{
PackedBoolList list3 = list1;
(list3 -= list2Labels).print(Info, true);
(list3 -= list2Labels).printInfo(Info, true);
}
PackedBoolList list4
(
IStringStream
(
"(AB 05 10 0F F0)"
"(1 n 1 n 1 n 1 1 off 0 0 f f 0 y yes y true y false on t)"
)()
);
Info<< "\ntest Istream constructor\n";
list4.print(Info, true);
list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used()() <<endl;
Info<< "\nassign from labelList\n";
......@@ -137,19 +136,55 @@ int main(int argc, char *argv[])
(
IStringStream
(
"(0 1 2 3 12 13 14 15 17 19 20 21)"
"(0 1 2 3 12 13 14 19 20 21)"
)()
);
list4.print(Info, true);
list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used()() <<endl;
Info<< "\nread from Istream\n";
IStringStream("(0f 3a)")() >> list4;
Info<< "\nassign from indices\n";
list4.read
(
IStringStream
(
"{0 1 2 3 12 13 14 19 20 21}"
)()
);
list4.print(Info, true);
list4.printInfo(Info, true);
Info<< list4 << " indices: " << list4.used()() <<endl;
List<bool> boolLst(list4.size());
forAll(list4, i)
{
boolLst[i] = list4[i];
}
Info<< "List<bool>: " << boolLst <<endl;
// check roundabout assignments
PackedList<2> pl2
(
IStringStream
(
"{(0 3)(1 3)(2 3)(3 3)(12 3)(13 3)(14 3)(19 3)(20 3)(21 3)}"
)()
);
Info<< "roundabout assignment: " << pl2 << endl;
list4.clear();
forAll(pl2, i)
{
list4[i] = pl2[i];
}
list4.write(Info, true) << endl;
list4.writeEntry("PackedBoolList", Info);
return 0;
}
......
......@@ -66,7 +66,7 @@ primitives/random/Random.C
containers/HashTables/HashTable/HashTableCore.C
containers/HashTables/StaticHashTable/StaticHashTableCore.C
containers/Lists/SortableList/ParSortableListName.C
containers/Lists/PackedList/PackedListName.C
containers/Lists/PackedList/PackedListCore.C
containers/Lists/PackedList/PackedBoolList.C
containers/Lists/ListOps/ListOps.C
containers/LinkedLists/linkTypes/SLListBase/SLListBase.C
......
......@@ -25,44 +25,10 @@ License
#include "PackedBoolList.H"
#include "IOstreams.H"
#include <cctype>
// * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * * //
// table-lookup instead of printf("%02x", char)
//! @cond localScope
static const char hexLookup[] =
{
"000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
"202122232425262728292a2b2c2d2e2f"
"303132333435363738393a3b3c3d3e3f"
"404142434445464748494a4b4c4d4e4f"
"505152535455565758595a5b5c5d5e5f"
"606162636465666768696a6b6c6d6e6f"
"707172737475767778797a7b7c7d7e7f"
"808182838485868788898a8b8c8d8e8f"
"909192939495969798999a9b9c9d9e9f"
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
};
// number of bytes required to pack nElem
static inline unsigned int packedBytes(const Foam::label nElem)
{
return (nElem + CHAR_BIT - 1) / CHAR_BIT;
}
//! @endcond
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::PackedBoolList::PackedBoolList(Istream &is)
Foam::PackedBoolList::PackedBoolList(Istream& is)
:
PackedList<1>()
{
......@@ -160,7 +126,7 @@ void Foam::PackedBoolList::modulo(const PackedList<1>& lst)
StorageList& lhs = this->storage();
const StorageList& rhs = lst.storage();
const label len = min(rhs.size(), lhs.size());
const label len = min(lhs.size(), rhs.size());
for (label i=0; i < len; ++i)
{
......@@ -274,286 +240,6 @@ Foam::PackedBoolList::operator^=(const PackedList<1>& lst)
return *this;
}
// * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>
(
Istream& istr,
PackedBoolList& lst
)
{
// Takes into account that 'a' (or 'A') is 10
static const label alphaOffset = toupper('A') - 10;
// Takes into account that '0' is 0
static const label zeroOffset = int('0');
ISstream& is = dynamicCast<ISstream>(istr);
lst.clear();
is.fatalCheck("operator>>(Istream&, PackedBoolList&)");
token firstTok(is);
is.fatalCheck
(
"operator>>(Istream&, PackedBoolList&) : reading first token"
);
if (firstTok.isLabel())
{
const label sz = firstTok.labelToken();
// Set list length to that read
lst.resize(sz);
// Read list contents as ASCII
// Read beginning of contents
const char delimiter = is.readBeginList("PackedBoolList");
if (sz)
{
if (delimiter == token::BEGIN_LIST)
{
// number of bytes when packed:
unsigned int nBytes = packedBytes(lst.size());
for (label i=0, storeI=0; i < sz; ++storeI)
{
PackedList<1>::StorageType& stored = lst.storage()[storeI];
// byte-wise read
for
(
unsigned byte=0;
byte < sizeof(PackedList<1>::StorageType);
++byte
)
{
PackedList<1>::StorageType result = 0;
char c = 0;
// Get next non-whitespace character
while (is.get(c) && isspace(c))
{}
for (label nibble=0; nibble < 2; ++nibble)
{
if (!isxdigit(c))
{
FatalIOErrorIn
(
"operator>>(Istream&, PackedBoolList&) : "
"reading first token",
is
)
<< "Illegal hex digit: '" << c << "'"
<< exit(FatalIOError);
}
result <<= 4;
if (isdigit(c))