mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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}
This commit is contained in:
@ -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))
|
||||
{
|
||||
result += int(c) - zeroOffset;
|
||||
}
|
||||
else
|
||||
{
|
||||
result += toupper(c) - alphaOffset;
|
||||
}
|
||||
|
||||
// Get character for the lower part of the byte
|
||||
if (!nibble)
|
||||
{
|
||||
is.get(c);
|
||||
}
|
||||
}
|
||||
|
||||
stored |= result << (byte*CHAR_BIT);
|
||||
|
||||
if (!--nBytes)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
i += PackedList<1>::packing();
|
||||
}
|
||||
|
||||
// trim possible trailing junk
|
||||
// mask off the final partial segment
|
||||
{
|
||||
const unsigned int off = sz % PackedList<1>::packing();
|
||||
if (off)
|
||||
{
|
||||
const unsigned int seg = sz / PackedList<1>::packing();
|
||||
|
||||
lst.storage()[seg] &= PackedList<1>::maskLower(off);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// skip over all trailing whitespace and zeroes
|
||||
char c = 0;
|
||||
while (is.get(c) && (isspace(c) || c == '0'))
|
||||
{}
|
||||
|
||||
// put back for later readEndList() to deal with
|
||||
is.putback(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
const label val = readLabel(is);
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"operator>>(Istream&, PackedBoolList&) : "
|
||||
"reading the single entry"
|
||||
);
|
||||
|
||||
lst = val;
|
||||
}
|
||||
}
|
||||
|
||||
// Read end of contents
|
||||
is.readEndList("PackedList");
|
||||
}
|
||||
else if (firstTok.isPunctuation())
|
||||
{
|
||||
if (firstTok.pToken() != token::BEGIN_LIST)
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"operator>>(Istream&, PackedBoolList&)",
|
||||
is
|
||||
)
|
||||
<< "incorrect first token, expected '(', found "
|
||||
<< firstTok.info()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
char c = '(';
|
||||
|
||||
for (label storeI=0; c && c != ')'; ++storeI)
|
||||
{
|
||||
lst.resize((storeI+1)*PackedList<1>::packing());
|
||||
|
||||
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;
|
||||
c = 0;
|
||||
|
||||
// Get next non-whitespace character
|
||||
while (is.get(c) && isspace(c))
|
||||
{}
|
||||
|
||||
if (!c || c == ')')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
for (label nibble=0; nibble < 2; ++nibble)
|
||||
{
|
||||
if (!isxdigit(c))
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"operator>>(Istream&, PackedBoolList&)",
|
||||
is
|
||||
)
|
||||
<< "Illegal hex digit: '" << c << "'"
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
result *= 16;
|
||||
|
||||
if (isdigit(c))
|
||||
{
|
||||
result += int(c) - zeroOffset;
|
||||
}
|
||||
else
|
||||
{
|
||||
result += toupper(c) - alphaOffset;
|
||||
}
|
||||
|
||||
// Get character for the lower part of the byte
|
||||
if (!nibble)
|
||||
{
|
||||
is.get(c);
|
||||
}
|
||||
}
|
||||
|
||||
stored |= result << (byte*CHAR_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
// put back for later readEndList() to deal with
|
||||
is.putback(c);
|
||||
|
||||
// Read end of contents
|
||||
is.readEndList("PackedList");
|
||||
}
|
||||
else
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"operator>>(Istream&, PackedBoolList&)",
|
||||
is
|
||||
)
|
||||
<< "incorrect first token, expected <int> or '(', found "
|
||||
<< firstTok.info()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
|
||||
Foam::Ostream& Foam::operator<<
|
||||
(
|
||||
Ostream& os,
|
||||
const PackedBoolList& lst
|
||||
)
|
||||
{
|
||||
const label sz = lst.size();
|
||||
|
||||
unsigned int nBytes = packedBytes(sz);
|
||||
|
||||
os << sz << token::BEGIN_LIST;
|
||||
|
||||
for (label storeI=0; nBytes; ++storeI)
|
||||
{
|
||||
PackedList<1>::StorageType stored = lst.storage()[storeI];
|
||||
|
||||
for
|
||||
(
|
||||
unsigned byte=0;
|
||||
byte < sizeof(PackedList<1>::StorageType);
|
||||
++byte
|
||||
)
|
||||
{
|
||||
os << hexLookup[((stored & 0xFF) << 1)]
|
||||
<< hexLookup[((stored & 0xFF) << 1) + 1];
|
||||
|
||||
if (!--nBytes)
|
||||
{
|
||||
break;
|
||||
}
|
||||
stored >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
os << token::END_LIST;
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
|
||||
|
||||
|
||||
@ -25,32 +25,11 @@ Class
|
||||
Foam::PackedBoolList
|
||||
|
||||
Description
|
||||
A bit-packed bool list
|
||||
A bit-packed bool list.
|
||||
|
||||
Note
|
||||
The input/output format differs from that of Foam::PackedBoolList in
|
||||
that it is also packed. The input/output is always byte-wise, with
|
||||
the lowest bytes first.
|
||||
|
||||
Thus this label sequence
|
||||
@code
|
||||
0 1 2 3 12 13 14 15 17 19 20 21
|
||||
@endcode
|
||||
which has this bit representation:
|
||||
@code
|
||||
..........111-1-1111--------1111
|
||||
@endcode
|
||||
and this internal representation
|
||||
@code
|
||||
0x003AF00F
|
||||
@endcode
|
||||
would be read/written in one of these formats:
|
||||
@code
|
||||
22(0f f0 3a)
|
||||
22(0ff03a)
|
||||
@endcode
|
||||
Whitespace can be used to separate byte values, but must not appear
|
||||
within a byte itself.
|
||||
In addition to the obvious memory advantage over using a
|
||||
List\<bool\>, this class also provides a number of bit-like
|
||||
operations.
|
||||
|
||||
SourceFiles
|
||||
PackedBoolListI.H
|
||||
@ -72,16 +51,6 @@ SeeAlso
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
// Forward declaration of classes
|
||||
class Istream;
|
||||
class Ostream;
|
||||
|
||||
// Forward declaration of friend functions and operators
|
||||
class PackedBoolList;
|
||||
Istream& operator>>(Istream&, PackedBoolList&);
|
||||
Ostream& operator<<(Ostream&, const PackedBoolList&);
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class PackedBoolList Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
@ -235,13 +204,13 @@ public:
|
||||
PackedBoolList& operator^=(const PackedList<1>&);
|
||||
|
||||
|
||||
//- Synonymous with the or operator
|
||||
//- Add entries to this list, synonymous with the or operator
|
||||
inline PackedBoolList& operator+=(const PackedList<1>&);
|
||||
|
||||
//- Synonymous with the or operator
|
||||
//- Add entries to this list, synonymous with the or operator
|
||||
inline PackedBoolList& operator+=(const UList<label>& indices);
|
||||
|
||||
//- Synonymous with the or operator
|
||||
//- Add entries to this list, synonymous with the or operator
|
||||
inline PackedBoolList& operator+=(const UIndirectList<label>&);
|
||||
|
||||
//- Remove entries from this list
|
||||
@ -253,10 +222,6 @@ public:
|
||||
//- Remove entries from this list
|
||||
inline PackedBoolList& operator-=(const UIndirectList<label>&);
|
||||
|
||||
// IOstream Operators
|
||||
|
||||
friend Istream& operator>>(Istream&, PackedBoolList&);
|
||||
friend Ostream& operator<<(Ostream&, const PackedBoolList&);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -27,46 +27,14 @@ License
|
||||
#include "IOstreams.H"
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
template<unsigned nBits>
|
||||
Foam::PackedList<nBits>::PackedList(const label size, const unsigned int val)
|
||||
:
|
||||
StorageList(packedLength(size), 0u),
|
||||
size_(size)
|
||||
{
|
||||
if (val)
|
||||
{
|
||||
operator=(val);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
Foam::PackedList<nBits>::PackedList(Istream& is)
|
||||
:
|
||||
StorageList(),
|
||||
size_(0)
|
||||
{
|
||||
is >> *this;
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
Foam::PackedList<nBits>::PackedList(const UList<label>& lst)
|
||||
:
|
||||
StorageList(packedLength(lst.size()), 0u),
|
||||
size_(lst.size())
|
||||
{
|
||||
forAll(lst, i)
|
||||
{
|
||||
set(i, lst[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
template<unsigned nBits>
|
||||
inline Foam::label Foam::PackedList<nBits>::byteSize() const
|
||||
{
|
||||
return packedLength(this->size()) * sizeof(StorageType);
|
||||
}
|
||||
|
||||
|
||||
#if (UINT_MAX == 0xFFFFFFFF)
|
||||
// 32-bit counting, Hamming weight method
|
||||
@ -186,7 +154,10 @@ Foam::Xfer<Foam::labelList> Foam::PackedList<nBits>::values() const
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
Foam::Ostream& Foam::PackedList<nBits>::iteratorBase::print(Ostream& os) const
|
||||
Foam::Ostream& Foam::PackedList<nBits>::iteratorBase::printInfo
|
||||
(
|
||||
Ostream& os
|
||||
) const
|
||||
{
|
||||
os << "iterator<" << label(nBits) << "> ["
|
||||
<< this->index_ << "]"
|
||||
@ -200,7 +171,7 @@ Foam::Ostream& Foam::PackedList<nBits>::iteratorBase::print(Ostream& os) const
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
Foam::Ostream& Foam::PackedList<nBits>::print
|
||||
Foam::Ostream& Foam::PackedList<nBits>::printInfo
|
||||
(
|
||||
Ostream& os,
|
||||
const bool fullOutput
|
||||
@ -275,6 +246,278 @@ Foam::Ostream& Foam::PackedList<nBits>::print
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
Foam::Istream& Foam::PackedList<nBits>::read(Istream& is)
|
||||
{
|
||||
PackedList<nBits>& lst = *this;
|
||||
|
||||
lst.clear();
|
||||
is.fatalCheck("PackedList<nBits>::read(Istream&)");
|
||||
|
||||
token firstTok(is);
|
||||
is.fatalCheck
|
||||
(
|
||||
"PackedList<nBits>::read(Istream&) : "
|
||||
"reading first token"
|
||||
);
|
||||
|
||||
if (firstTok.isLabel())
|
||||
{
|
||||
const label sz = firstTok.labelToken();
|
||||
|
||||
// Set list length to that read
|
||||
lst.resize(sz);
|
||||
|
||||
// Read list contents depending on data format
|
||||
if (is.format() == IOstream::ASCII)
|
||||
{
|
||||
// Read beginning of contents
|
||||
const char delimiter = is.readBeginList("PackedList<nBits>");
|
||||
|
||||
if (sz)
|
||||
{
|
||||
if (delimiter == token::BEGIN_LIST)
|
||||
{
|
||||
for (register label i=0; i<sz; ++i)
|
||||
{
|
||||
lst[i] = lst.readValue(is);
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"PackedList<nBits>::read(Istream&) : "
|
||||
"reading entry"
|
||||
);
|
||||
}
|
||||
}
|
||||
else if (delimiter == token::BEGIN_BLOCK)
|
||||
{
|
||||
// assign for all entries
|
||||
lst = lst.readValue(is);
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"PackedList<nBits>::read(Istream&) : "
|
||||
"reading the single entry"
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"PackedList<nBits>::read(Istream&)",
|
||||
is
|
||||
)
|
||||
<< "incorrect list token, expected '(' or '{', found "
|
||||
<< firstTok.info()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
}
|
||||
|
||||
// Read end of contents
|
||||
is.readEndList("PackedList<nBits>");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sz)
|
||||
{
|
||||
is.read
|
||||
(
|
||||
reinterpret_cast<char*>(lst.storage().data()),
|
||||
lst.byteSize()
|
||||
);
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"PackedList<nBits>::read(Istream&) : "
|
||||
"reading the binary block"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (firstTok.isPunctuation())
|
||||
{
|
||||
if (firstTok.pToken() == token::BEGIN_LIST)
|
||||
{
|
||||
token nextTok(is);
|
||||
is.fatalCheck("PackedList<nBits>::read(Istream&)");
|
||||
|
||||
while
|
||||
(
|
||||
!( nextTok.isPunctuation()
|
||||
&& nextTok.pToken() == token::END_LIST
|
||||
)
|
||||
)
|
||||
{
|
||||
is.putBack(nextTok);
|
||||
lst.append(lst.readValue(is));
|
||||
|
||||
is >> nextTok;
|
||||
is.fatalCheck("PackedList<nBits>::read(Istream&)");
|
||||
}
|
||||
}
|
||||
else if (firstTok.pToken() == token::BEGIN_BLOCK)
|
||||
{
|
||||
token nextTok(is);
|
||||
is.fatalCheck("PackedList<nBits>::read(Istream&)");
|
||||
|
||||
while
|
||||
(
|
||||
!( nextTok.isPunctuation()
|
||||
&& nextTok.pToken() == token::END_BLOCK
|
||||
)
|
||||
)
|
||||
{
|
||||
is.putBack(nextTok);
|
||||
lst.setPair(is);
|
||||
|
||||
is >> nextTok;
|
||||
is.fatalCheck("PackedList<nBits>::read(Istream&)");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"PackedList<nBits>::read(Istream&)",
|
||||
is
|
||||
)
|
||||
<< "incorrect first token, expected '(', found "
|
||||
<< firstTok.info()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"PackedList<nBits>::read(Istream&)",
|
||||
is
|
||||
)
|
||||
<< "incorrect first token, expected <int>, '(' or '{', found "
|
||||
<< firstTok.info()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
Foam::Ostream& Foam::PackedList<nBits>::write
|
||||
(
|
||||
Ostream& os,
|
||||
const bool indexedOutput
|
||||
) const
|
||||
{
|
||||
const PackedList<nBits>& lst = *this;
|
||||
const label sz = lst.size();
|
||||
|
||||
// Write list contents depending on data format
|
||||
if (os.format() == IOstream::ASCII)
|
||||
{
|
||||
bool uniform = false;
|
||||
|
||||
if (sz > 1 && !indexedOutput)
|
||||
{
|
||||
uniform = true;
|
||||
|
||||
forAll(lst, i)
|
||||
{
|
||||
if (lst[i] != lst[0])
|
||||
{
|
||||
uniform = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (uniform)
|
||||
{
|
||||
// uniform values:
|
||||
os << sz << token::BEGIN_BLOCK << lst[0] << token::END_BLOCK;
|
||||
}
|
||||
else if (indexedOutput)
|
||||
{
|
||||
// indexed output
|
||||
os << nl << token::BEGIN_BLOCK << nl;
|
||||
|
||||
for
|
||||
(
|
||||
typename PackedList<nBits>::const_iterator iter = lst.cbegin();
|
||||
iter != lst.cend();
|
||||
++iter
|
||||
)
|
||||
{
|
||||
if (iter.writeIfSet(os))
|
||||
{
|
||||
os << nl;
|
||||
}
|
||||
}
|
||||
|
||||
os << token::END_BLOCK << nl;
|
||||
}
|
||||
else if (sz < 11)
|
||||
{
|
||||
// short list:
|
||||
os << sz << token::BEGIN_LIST;
|
||||
forAll(lst, i)
|
||||
{
|
||||
if (i)
|
||||
{
|
||||
os << token::SPACE;
|
||||
}
|
||||
os << lst[i];
|
||||
}
|
||||
os << token::END_LIST;
|
||||
}
|
||||
else
|
||||
{
|
||||
// longer list:
|
||||
os << nl << sz << nl << token::BEGIN_LIST;
|
||||
forAll(lst, i)
|
||||
{
|
||||
os << nl << lst[i];
|
||||
}
|
||||
os << nl << token::END_LIST << nl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
os << nl << sz << nl;
|
||||
if (sz)
|
||||
{
|
||||
os.write
|
||||
(
|
||||
reinterpret_cast<const char*>(lst.storage().cdata()),
|
||||
lst.byteSize()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
void Foam::PackedList<nBits>::writeEntry(Ostream& os) const
|
||||
{
|
||||
os << *this;
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
void Foam::PackedList<nBits>::writeEntry
|
||||
(
|
||||
const word& keyword,
|
||||
Ostream& os
|
||||
) const
|
||||
{
|
||||
os.writeKeyword(keyword);
|
||||
writeEntry(os);
|
||||
os << token::END_STATEMENT << endl;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||
|
||||
template<unsigned nBits>
|
||||
@ -305,193 +548,16 @@ Foam::PackedList<nBits>::operator=(const UList<label>& lst)
|
||||
// * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
|
||||
|
||||
template<unsigned nBits>
|
||||
Foam::Istream& Foam::operator>>
|
||||
(
|
||||
Istream& is,
|
||||
PackedList<nBits>& lst
|
||||
)
|
||||
Foam::Istream& Foam::operator>>(Istream& is, PackedList<nBits>& lst)
|
||||
{
|
||||
lst.clear();
|
||||
is.fatalCheck("operator>>(Istream&, PackedList<nBits>&)");
|
||||
|
||||
token firstTok(is);
|
||||
is.fatalCheck
|
||||
(
|
||||
"operator>>(Istream&, PackedList<nBits>&) : reading first token"
|
||||
);
|
||||
|
||||
if (firstTok.isLabel())
|
||||
{
|
||||
const label sz = firstTok.labelToken();
|
||||
|
||||
// Set list length to that read
|
||||
lst.setSize(sz);
|
||||
|
||||
{
|
||||
// Read beginning of contents
|
||||
const char delimiter = is.readBeginList("List");
|
||||
|
||||
if (sz)
|
||||
{
|
||||
unsigned int val;
|
||||
|
||||
if (delimiter == token::BEGIN_LIST)
|
||||
{
|
||||
for (register label i=0; i<sz; ++i)
|
||||
{
|
||||
is >> val;
|
||||
|
||||
if (val > lst.max_value())
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"operator>>(Istream&, PackedList<nBits>&)",
|
||||
is
|
||||
)
|
||||
<< "out-of-range value: "
|
||||
<< val << " > " << lst.max_value()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"operator>>(Istream&, PackedList<nBits>&) : "
|
||||
"reading entry"
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
is >> val;
|
||||
|
||||
is.fatalCheck
|
||||
(
|
||||
"operator>>(Istream&, PackedList<nBits>&) : "
|
||||
"reading the single entry"
|
||||
);
|
||||
|
||||
if (val > lst.max_value())
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"operator>>(Istream&, PackedList<nBits>&)",
|
||||
is
|
||||
)
|
||||
<< "out-of-range value: "
|
||||
<< val << " > " << lst.max_value()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
// assign for all entries
|
||||
lst = val;
|
||||
}
|
||||
}
|
||||
|
||||
// Read end of contents
|
||||
is.readEndList("PackedList<nBits>");
|
||||
}
|
||||
}
|
||||
else if (firstTok.isPunctuation())
|
||||
{
|
||||
if (firstTok.pToken() != token::BEGIN_LIST)
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"operator>>(Istream&, PackedList<nBits>&)",
|
||||
is
|
||||
)
|
||||
<< "incorrect first token, expected '(', found "
|
||||
<< firstTok.info()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
token nextTok(is);
|
||||
is.fatalCheck("operator>>(Istream&, PackedList<nBits>&)");
|
||||
|
||||
unsigned int val;
|
||||
|
||||
while
|
||||
(
|
||||
!(nextTok.isPunctuation() && nextTok.pToken() == token::END_LIST)
|
||||
)
|
||||
{
|
||||
is.putBack(nextTok);
|
||||
is >> val;
|
||||
lst.append(val);
|
||||
|
||||
if (val > lst.max_value())
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"operator>>(Istream&, PackedList<nBits>&)",
|
||||
is
|
||||
)
|
||||
<< "out-of-range value: "
|
||||
<< val << " > " << lst.max_value()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
is >> nextTok;
|
||||
is.fatalCheck("operator>>(Istream&, PackedList<nBits>&)");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"operator>>(Istream&, PackedList<nBits>&)",
|
||||
is
|
||||
)
|
||||
<< "incorrect first token, expected <int> or '(', found "
|
||||
<< firstTok.info()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
return is;
|
||||
return lst.read(is);
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
Foam::Ostream& Foam::operator<<
|
||||
(
|
||||
Ostream& os,
|
||||
PackedList<nBits>& lst
|
||||
)
|
||||
Foam::Ostream& Foam::operator<<(Ostream& os, const PackedList<nBits>& lst)
|
||||
{
|
||||
if (lst.size() < 11)
|
||||
{
|
||||
// Write size and start delimiter
|
||||
os << lst.size() << token::BEGIN_LIST;
|
||||
|
||||
// Write contents
|
||||
forAll(lst, i)
|
||||
{
|
||||
if (i)
|
||||
{
|
||||
os << token::SPACE;
|
||||
}
|
||||
os << lst[i];
|
||||
}
|
||||
|
||||
// Write end delimiter
|
||||
os << token::END_LIST;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Write size and start delimiter
|
||||
os << nl << lst.size() << nl << token::BEGIN_LIST;
|
||||
|
||||
// Write contents
|
||||
forAll(lst, i)
|
||||
{
|
||||
os << nl << lst[i];
|
||||
}
|
||||
|
||||
// Write end delimiter
|
||||
os << nl << token::END_LIST << nl;
|
||||
}
|
||||
|
||||
return os;
|
||||
return lst.write(os, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -76,6 +76,19 @@ Note
|
||||
always be bit-wise zero. This property must not be violated by any
|
||||
inheriting classes.
|
||||
|
||||
In addition to the normal output format, PackedList also supports a
|
||||
compact ASCII format that may be convenient for user input in some
|
||||
situations. The general format is a group of index/value pairs:
|
||||
@verbatim
|
||||
{ (index1 value1) (index2 value2) (index3 value3) }
|
||||
@endverbatim
|
||||
The bool specialization just uses the indices corresponding to
|
||||
non-zero entries instead of a index/value pair:
|
||||
@verbatim
|
||||
{ index1 index2 index3 }
|
||||
@endverbatim
|
||||
In both cases, the supplied indices can be randomly ordered.
|
||||
|
||||
SeeAlso
|
||||
Foam::DynamicList
|
||||
|
||||
@ -110,10 +123,20 @@ Ostream& operator<<(Ostream&, const PackedList<nBits>&);
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class PackedListName Declaration
|
||||
Class PackedListCore Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
TemplateName(PackedList);
|
||||
//- Template-invariant bits for PackedList
|
||||
struct PackedListCore
|
||||
{
|
||||
//- Construct null
|
||||
PackedListCore()
|
||||
{}
|
||||
|
||||
//- Define template name and debug
|
||||
ClassName("PackedList");
|
||||
};
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class PackedList Declaration
|
||||
@ -122,6 +145,7 @@ TemplateName(PackedList);
|
||||
template<unsigned nBits=1>
|
||||
class PackedList
|
||||
:
|
||||
public PackedListCore,
|
||||
private List<unsigned int>
|
||||
{
|
||||
protected:
|
||||
@ -129,6 +153,18 @@ protected:
|
||||
typedef unsigned int StorageType;
|
||||
typedef List<StorageType> StorageList;
|
||||
|
||||
// Protected Member Functions
|
||||
|
||||
//- Calculate the list length when packed
|
||||
inline static label packedLength(const label);
|
||||
|
||||
//- Read a list entry (allows for specialization)
|
||||
inline static unsigned int readValue(Istream&);
|
||||
|
||||
//- Read an index/value pair and set accordingly.
|
||||
// For bool specialization, read a single index value
|
||||
inline void setPair(Istream&);
|
||||
|
||||
private:
|
||||
|
||||
//- nBits must be positive (non-zero) and fit within the storage.
|
||||
@ -143,12 +179,6 @@ private:
|
||||
//- Number of nBits entries
|
||||
label size_;
|
||||
|
||||
// Private Member Functions
|
||||
|
||||
//- Calculate the list length when packed
|
||||
inline static label packedLength(const label);
|
||||
|
||||
|
||||
public:
|
||||
|
||||
// Public data
|
||||
@ -184,10 +214,10 @@ public:
|
||||
explicit inline PackedList(const label size);
|
||||
|
||||
//- Construct with given size and value for all elements
|
||||
PackedList(const label size, const unsigned val);
|
||||
inline PackedList(const label size, const unsigned val);
|
||||
|
||||
//- Construct from Istream
|
||||
PackedList(Istream&);
|
||||
inline PackedList(Istream&);
|
||||
|
||||
//- Copy constructor
|
||||
inline PackedList(const PackedList<nBits>&);
|
||||
@ -235,6 +265,10 @@ public:
|
||||
//- Return the underlying packed storage
|
||||
inline const List<unsigned int>& storage() const;
|
||||
|
||||
//- Return the binary size in number of characters
|
||||
// used in the underlying storage
|
||||
label byteSize() const;
|
||||
|
||||
//- Count number of bits set, O(log(n))
|
||||
// Uses the Hamming weight (population count) method
|
||||
// http://en.wikipedia.org/wiki/Hamming_weight
|
||||
@ -243,7 +277,7 @@ public:
|
||||
//- Return the values as a list of labels
|
||||
Xfer<labelList> values() const;
|
||||
|
||||
//- Print values and information, optionally output unused elements
|
||||
//- Print information and values, optionally output unused elements
|
||||
//
|
||||
// addressable bits:
|
||||
// on: '1', off: '-'
|
||||
@ -251,7 +285,8 @@ public:
|
||||
// non-addressable bits:
|
||||
// on: '!', off: '.'
|
||||
//
|
||||
Ostream& print(Ostream&, const bool fullOutput=false) const;
|
||||
Ostream& printInfo(Ostream&, const bool fullOutput=false) const;
|
||||
|
||||
|
||||
// Edit
|
||||
|
||||
@ -300,10 +335,43 @@ public:
|
||||
inline Xfer<PackedList<nBits> > xfer();
|
||||
|
||||
|
||||
// IO
|
||||
|
||||
//- Clear list and read from stream
|
||||
Istream& read(Istream&);
|
||||
|
||||
//- Write, optionally with indexedOutput
|
||||
//
|
||||
// The indexed output may be convenient in some situations.
|
||||
// The general format is a group of index/value pairs:
|
||||
// @verbatim
|
||||
// { (index1 value1) (index2 value2) (index3 value3) }
|
||||
// @endverbatim
|
||||
// The bool specialization just uses the indices corresponding to
|
||||
// non-zero entries instead of a index/value pair:
|
||||
// @verbatim
|
||||
// { index1 index2 index3 }
|
||||
// @endverbatim
|
||||
//
|
||||
// Note the indexed output is only supported for ASCII streams.
|
||||
Ostream& write
|
||||
(
|
||||
Ostream&,
|
||||
const bool indexedOutput=false
|
||||
) const;
|
||||
|
||||
|
||||
//- Write as a dictionary entry
|
||||
void writeEntry(Ostream&) const;
|
||||
|
||||
//- Write as a dictionary entry with keyword
|
||||
void writeEntry(const word& keyword, Ostream&) const;
|
||||
|
||||
|
||||
// Member operators
|
||||
|
||||
//- Append a value at the end of the list
|
||||
inline void append(const unsigned int val);
|
||||
inline PackedList<nBits>& append(const unsigned int val);
|
||||
|
||||
//- Remove and return the last element
|
||||
inline unsigned int remove();
|
||||
@ -373,6 +441,9 @@ public:
|
||||
//- Return the element index corresponding to the iterator
|
||||
inline label key() const;
|
||||
|
||||
//- Write index/value for a non-zero entry
|
||||
// The bool specialization writes the index only
|
||||
inline bool writeIfSet(Ostream&) const;
|
||||
|
||||
// Member Operators
|
||||
|
||||
@ -392,8 +463,8 @@ public:
|
||||
// Never auto-vivify entries.
|
||||
inline operator unsigned int () const;
|
||||
|
||||
//- Print value and information
|
||||
Ostream& print(Ostream&) const;
|
||||
//- Print information and values
|
||||
Ostream& printInfo(Ostream&) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -27,6 +27,7 @@ License
|
||||
|
||||
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
||||
|
||||
defineTypeNameAndDebug(Foam::PackedListName, 0);
|
||||
defineTypeNameAndDebug(Foam::PackedListCore, 0);
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
@ -67,11 +67,117 @@ inline Foam::label Foam::PackedList<nBits>::packedLength(const label nElem)
|
||||
}
|
||||
|
||||
|
||||
namespace Foam
|
||||
{
|
||||
// Template specialization for bool entries
|
||||
template<>
|
||||
inline unsigned int Foam::PackedList<1>::readValue(Istream& is)
|
||||
{
|
||||
return readBool(is);
|
||||
}
|
||||
|
||||
// Template specialization for bool entries
|
||||
template<>
|
||||
inline void Foam::PackedList<1>::setPair(Istream& is)
|
||||
{
|
||||
set(readLabel(is), true);
|
||||
}
|
||||
|
||||
// Template specialization for bool entries
|
||||
template<>
|
||||
inline bool Foam::PackedList<1>::iteratorBase::writeIfSet(Ostream& os) const
|
||||
{
|
||||
if (this->get())
|
||||
{
|
||||
os << index_;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
inline unsigned int Foam::PackedList<nBits>::readValue(Istream& is)
|
||||
{
|
||||
const unsigned int val = readLabel(is);
|
||||
|
||||
if (val > max_value())
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"PackedList<nBits>::readValue(Istream&)",
|
||||
is
|
||||
)
|
||||
<< "Out-of-range value " << val << " for PackedList<" << nBits
|
||||
<< ">. Maximum permitted value is " << max_value() << "."
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
inline void Foam::PackedList<nBits>::setPair(Istream& is)
|
||||
{
|
||||
is.readBegin("Tuple2<label, unsigned int>");
|
||||
|
||||
const label ind = readLabel(is);
|
||||
const unsigned int val = readLabel(is);
|
||||
|
||||
is.readEnd("Tuple2<label, unsigned int>");
|
||||
|
||||
if (val > max_value())
|
||||
{
|
||||
FatalIOErrorIn
|
||||
(
|
||||
"PackedList<nBits>::setPair(Istream&)",
|
||||
is
|
||||
)
|
||||
<< "Out-of-range value " << val << " for PackedList<" << nBits
|
||||
<< "> at index " << ind
|
||||
<< ". Maximum permitted value is " << max_value() << "."
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
set(ind, val);
|
||||
|
||||
// Check state of Istream
|
||||
is.check("PackedList<nBits>::setPair(Istream&)");
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
inline bool Foam::PackedList<nBits>::iteratorBase::writeIfSet(Ostream& os) const
|
||||
{
|
||||
const label val = this->get();
|
||||
|
||||
if (val)
|
||||
{
|
||||
os << token::BEGIN_LIST
|
||||
<< index_ << token::SPACE << val
|
||||
<< token::END_LIST;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
template<unsigned nBits>
|
||||
inline Foam::PackedList<nBits>::PackedList()
|
||||
:
|
||||
PackedListCore(),
|
||||
StorageList(),
|
||||
size_(0)
|
||||
{}
|
||||
@ -80,14 +186,45 @@ inline Foam::PackedList<nBits>::PackedList()
|
||||
template<unsigned nBits>
|
||||
inline Foam::PackedList<nBits>::PackedList(const label size)
|
||||
:
|
||||
PackedListCore(),
|
||||
StorageList(packedLength(size), 0u),
|
||||
size_(size)
|
||||
{}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
inline Foam::PackedList<nBits>::PackedList
|
||||
(
|
||||
const label size,
|
||||
const unsigned int val
|
||||
)
|
||||
:
|
||||
PackedListCore(),
|
||||
StorageList(packedLength(size), 0u),
|
||||
size_(size)
|
||||
{
|
||||
if (val)
|
||||
{
|
||||
operator=(val);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
inline Foam::PackedList<nBits>::PackedList(Istream& is)
|
||||
:
|
||||
PackedListCore(),
|
||||
StorageList(),
|
||||
size_(0)
|
||||
{
|
||||
read(is);
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
inline Foam::PackedList<nBits>::PackedList(const PackedList<nBits>& lst)
|
||||
:
|
||||
PackedListCore(),
|
||||
StorageList(lst),
|
||||
size_(lst.size_)
|
||||
{}
|
||||
@ -100,6 +237,20 @@ inline Foam::PackedList<nBits>::PackedList(const Xfer<PackedList<nBits> >& lst)
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
inline Foam::PackedList<nBits>::PackedList(const UList<label>& lst)
|
||||
:
|
||||
PackedListCore(),
|
||||
StorageList(packedLength(lst.size()), 0u),
|
||||
size_(lst.size())
|
||||
{
|
||||
forAll(lst, i)
|
||||
{
|
||||
set(i, lst[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
inline Foam::autoPtr<Foam::PackedList<nBits> >
|
||||
Foam::PackedList<nBits>::clone() const
|
||||
@ -797,23 +948,14 @@ inline Foam::Xfer<Foam::PackedList<nBits> > Foam::PackedList<nBits>::xfer()
|
||||
template<unsigned nBits>
|
||||
inline unsigned int Foam::PackedList<nBits>::get(const label i) const
|
||||
{
|
||||
# ifdef FULLDEBUG
|
||||
if (i < 0)
|
||||
{
|
||||
FatalErrorIn("PackedList<nBits>::get(const label)")
|
||||
<< "negative index " << i << " max=" << size_-1
|
||||
<< abort(FatalError);
|
||||
}
|
||||
# endif
|
||||
|
||||
// lazy evaluation - return 0 for out-of-range
|
||||
if (i < size_)
|
||||
if (i < 0 || i >= size_)
|
||||
{
|
||||
return iteratorBase(this, i).get();
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
return iteratorBase(this, i).get();
|
||||
}
|
||||
}
|
||||
|
||||
@ -822,13 +964,13 @@ template<unsigned nBits>
|
||||
inline unsigned int Foam::PackedList<nBits>::operator[](const label i) const
|
||||
{
|
||||
// lazy evaluation - return 0 for out-of-range
|
||||
if (i < size_)
|
||||
if (i < 0 || i >= size_)
|
||||
{
|
||||
return iteratorBase(this, i).get();
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
return iteratorBase(this, i).get();
|
||||
}
|
||||
}
|
||||
|
||||
@ -840,18 +982,14 @@ inline bool Foam::PackedList<nBits>::set
|
||||
const unsigned int val
|
||||
)
|
||||
{
|
||||
# ifdef FULLDEBUG
|
||||
if (i < 0)
|
||||
{
|
||||
FatalErrorIn("PackedList<nBits>::set(const label)")
|
||||
<< "negative index " << i << " max=" << size_-1
|
||||
<< abort(FatalError);
|
||||
// lazy evaluation - ignore out-of-bounds
|
||||
return false;
|
||||
}
|
||||
# endif
|
||||
|
||||
// lazy evaluation - increase size on assigment
|
||||
if (i >= size_)
|
||||
else if (i >= size_)
|
||||
{
|
||||
// lazy evaluation - increase size on assigment
|
||||
resize(i + 1);
|
||||
}
|
||||
|
||||
@ -862,24 +1000,28 @@ inline bool Foam::PackedList<nBits>::set
|
||||
template<unsigned nBits>
|
||||
inline bool Foam::PackedList<nBits>::unset(const label i)
|
||||
{
|
||||
// lazy - ignore out-of-bounds
|
||||
// lazy evaluation - ignore out-of-bounds
|
||||
if (i < 0 || i >= size_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return iteratorBase(this, i).set(0u);
|
||||
else
|
||||
{
|
||||
return iteratorBase(this, i).set(0u);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<unsigned nBits>
|
||||
inline void Foam::PackedList<nBits>::append(const unsigned int val)
|
||||
inline Foam::PackedList<nBits>&
|
||||
Foam::PackedList<nBits>::append(const unsigned int val)
|
||||
{
|
||||
const label elemI = size_;
|
||||
reserve(elemI + 1);
|
||||
size_++;
|
||||
|
||||
iteratorBase(this, elemI).set(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user