PackedList gets count() and trim() methods

- the bit counting is relatively fast:
  under 0.2 seconds for 1M bits counted 1000 times

- trim()'ing the final zero elements tested for a few cases,
  but might need more attention
This commit is contained in:
Mark Olesen
2009-01-28 16:32:47 +01:00
parent 22df173acb
commit 7c739978b1
7 changed files with 429 additions and 174 deletions

View File

@ -38,7 +38,6 @@ using namespace Foam;
int main(int argc, char *argv[])
{
bool changed;
Info<< "PackedList max_bits() = " << PackedList<0>::max_bits() << nl;
Info<< "\ntest allocation with value\n";
@ -46,7 +45,42 @@ int main(int argc, char *argv[])
list1.print(Info);
Info<< "\ntest assign uniform value\n";
list1 = 4;
list1 = 3;
list1.print(Info);
Info<< "\ntest assign uniform value (with overflow)\n";
list1 = -1;
list1.print(Info);
Info<< "\ntest assign between references - does not work as expected\n";
list1[2] = 3;
list1[4] = list1[2];
list1.print(Info);
{
const PackedList<3>& constLst = list1;
Info<< "\ntest operator[] const with out-of-range index\n";
constLst.print(Info);
if (!constLst[20])
{
Info<< "[20] is false, as expected\n";
}
constLst.print(Info);
Info<< "\ntest operator[] non-const with out-of-range index\n";
if (!list1[20])
{
Info<< "[20] is false, as expected but list was resized!! (non-const)\n";
}
list1.print(Info);
}
Info<< "\ntest operator[] with out-of-range index\n";
if (!list1[20])
{
Info<< "[20] is false, as expected\n";
}
list1.print(Info);
Info<< "\ntest resize with value (without reallocation)\n";
@ -111,6 +145,10 @@ int main(int argc, char *argv[])
list1.setCapacity(24);
list1.print(Info);
Info<< "\ntest trim\n";
list1.trim();
list1.print(Info);
// add in some misc values
list1[31] = 1;
list1[32] = 2;
@ -122,12 +160,10 @@ int main(int argc, char *argv[])
iter.print(Info) << "\n";
Info<< "\ntest iterator operator=\n";
changed = (iter = 5);
iter = 2;
Info<< "iterator:" << iter() << "\n";
Info<< "changed:" << changed << "\n";
changed = (iter = 5);
Info<< "changed:" << changed << "\n";
iter = 5;
list1.print(Info);
Info<< "\ntest get() method\n";
@ -137,12 +173,13 @@ int main(int argc, char *argv[])
Info<< "\ntest iterator indexing\n";
Info<< "cend() ";
list1.cend().print(Info) << "\n";
for
for
(
PackedList<3>::const_iterator cit = list1[30];
cit != list1.cend();
++cit)
++cit
)
{
cit.print(Info);
}
@ -167,7 +204,7 @@ int main(int argc, char *argv[])
Info<< "\ntest pattern that fills all bits\n";
PackedList<4> list3(8, 8);
label pos = list3.size() - 1;
list3[pos--] = list3.max_value();

View File

@ -54,7 +54,7 @@ int main(int argc, char *argv[])
std::vector<bool> stlVector(n, true);
labelHashSet emptyHash;
labelHashSet fullHash;
labelHashSet fullHash(1000);
for(label i = 0; i < n; i++)
{
fullHash.insert(i);
@ -70,6 +70,35 @@ int main(int argc, char *argv[])
}
Info<< "resize/shrink/resize:" << timer.cpuTimeIncrement() << " s\n\n";
// set every other bit on:
Info<< "set every other bit on and count\n";
packed.storage() = 0xAAAAAAAAu;
// Count packed
sum = 0;
for (label iter = 0; iter < nIters; iter++)
{
forAll(packed, i)
{
sum += packed[i];
}
}
Info<< "Counting brute-force:" << timer.cpuTimeIncrement()
<< " s" << endl;
Info<< " sum " << sum << endl;
// Count packed
sum = 0;
for (label iter = 0; iter < nIters; iter++)
{
sum += packed.count();
}
Info<< "Counting via count():" << timer.cpuTimeIncrement()
<< " s" << endl;
Info<< " sum " << sum << endl;
// Dummy addition
sum = 0;
for (label iter = 0; iter < nIters; iter++)
@ -182,7 +211,7 @@ int main(int argc, char *argv[])
sum = 0;
for (label iter = 0; iter < nIters; iter++)
{
forAll(packed, i)
forAll(unpacked, i)
{
sum += emptyHash.found(i);
}
@ -196,7 +225,7 @@ int main(int argc, char *argv[])
sum = 0;
for (label iter = 0; iter < nIters; iter++)
{
forAll(packed, i)
forAll(unpacked, i)
{
sum += fullHash.found(i);
}
@ -206,7 +235,6 @@ int main(int argc, char *argv[])
Info<< " sum " << sum << endl;
//
// Write
//
@ -326,8 +354,7 @@ int main(int argc, char *argv[])
Info<< "Writing packed<3> uniform 1:" << timer.cpuTimeIncrement()
<< " s" << endl;
Info << "End\n" << endl;
return 0;