mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: ListOp::inplaceMapValue using a Map<label> for the mapping.
For example, with some HashTable or Map container of models
{ model0 => 1, model1 => 4, model2 => 5, model3 => 12, model4 => 15, }
specify the remapping
Map<label> mapper({{1, 3}, {2, 6}, {3, 12}, {5, 8}});
inplaceMapValue(mapper, models) then yields
{ model0 => 3, model1 => 4, model2 => 8, model3 => 12, model4 => 15, }
--
ENH: extend bitSet::count() to optionally count unset bits instead.
--
ENH: BitOps compatibility methods for boolList.
- These ease coding that uses a boolList instead of bitSet and use
short-circuit logic when possible.
Eg, when 'bitset' and 'bools' contain the same information
bitset.count() <-> BitOps::count(bools)
bitset.all() <-> BitOps::all(bools)
bitset.any() <-> BitOps::any(bools)
bitset.none() <-> BitOps::none(bools)
These methods can then be used directly in parameters or in logic.
Eg,
returnReduce(bitset.any(), orOp<bool>());
returnReduce(BitOps::any(bools), orOp<bool>());
if (BitOps::any(bools)) ...
This commit is contained in:
@ -35,9 +35,82 @@ Description
|
||||
#include "SubList.H"
|
||||
#include "ListOps.H"
|
||||
#include "FlatOutput.H"
|
||||
#include "UPtrList.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
|
||||
// Proof-of-concept for sorted HashTable output
|
||||
// .. but yet not really convincing
|
||||
|
||||
|
||||
// Forward declarations
|
||||
template<class T, class Key, class Hash> class HashSorter;
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
Ostream& operator<<(Ostream& os, const HashSorter<T, Key, Hash>& sorter);
|
||||
|
||||
|
||||
template<class T, class Key, class Hash>
|
||||
class HashSorter
|
||||
{
|
||||
const HashTable<T,Key,Hash>& table;
|
||||
|
||||
public:
|
||||
|
||||
HashSorter(const HashTable<T,Key,Hash>& ht)
|
||||
:
|
||||
table(ht)
|
||||
{}
|
||||
|
||||
friend Ostream& operator<<
|
||||
(
|
||||
Ostream& os,
|
||||
const HashSorter<T, Key, Hash>& sorter
|
||||
)
|
||||
{
|
||||
const auto& tbl = sorter.table;
|
||||
const label len = tbl.size();
|
||||
|
||||
// Should actually be able to get the flat entries or iterators
|
||||
// and sort that instead.
|
||||
|
||||
UPtrList<const Key> keys(len);
|
||||
label count = 0;
|
||||
|
||||
for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
|
||||
{
|
||||
keys.set(count, &(iter.key()));
|
||||
++count;
|
||||
}
|
||||
|
||||
labelList order(identity(len));
|
||||
std::sort
|
||||
(
|
||||
order.begin(),
|
||||
order.end(),
|
||||
ListOps::less<UPtrList<const Key>>(keys)
|
||||
);
|
||||
|
||||
// Size and start list delimiter
|
||||
os << nl << len << nl << token::BEGIN_LIST << nl;
|
||||
|
||||
// Contents
|
||||
for (const label idx : order)
|
||||
{
|
||||
const auto& k = keys[idx];
|
||||
|
||||
os << k << token::SPACE << tbl[k] << nl;
|
||||
}
|
||||
|
||||
os << token::END_LIST; // End list delimiter
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
template<class ListType>
|
||||
@ -135,6 +208,42 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
|
||||
// Test remapping
|
||||
{
|
||||
Info<< nl << "Test inplaceMapValue" << nl << nl;
|
||||
|
||||
HashTable<label> input;
|
||||
typedef HashSorter<label, label, Hash<label>> Mapper;
|
||||
typedef HashSorter<label, word, string::hash> Sorter;
|
||||
|
||||
for (label i=0; i < 10; ++i)
|
||||
{
|
||||
input.insert(word::printf("word%d", i), i);
|
||||
}
|
||||
|
||||
Map<label> mapper;
|
||||
{
|
||||
// A mapping that does some, but not all values
|
||||
|
||||
labelList rndList(identity(16)); // larger range
|
||||
shuffle(rndList);
|
||||
|
||||
for (label i=0; i < 8; ++i) // smaller sample
|
||||
{
|
||||
mapper.insert(rndList[i], 100*i);
|
||||
}
|
||||
}
|
||||
|
||||
Info<< nl
|
||||
<< "input: " << Sorter(input) << nl
|
||||
<< "mapper: " << Mapper(mapper) << nl << nl;
|
||||
|
||||
inplaceMapValue(mapper, input);
|
||||
|
||||
Info<< nl << "output: " << Sorter(input) << nl;
|
||||
}
|
||||
|
||||
|
||||
Info<< "\nEnd\n" << endl;
|
||||
|
||||
return 0;
|
||||
|
||||
@ -49,6 +49,7 @@ inline Ostream& report
|
||||
{
|
||||
Info<< "size=" << bitset.size() << "/" << bitset.capacity()
|
||||
<< " count=" << bitset.count()
|
||||
<< " !count=" << bitset.count(false)
|
||||
<< " all:" << bitset.all()
|
||||
<< " any:" << bitset.any()
|
||||
<< " none:" << bitset.none() << nl;
|
||||
@ -63,6 +64,19 @@ inline Ostream& report
|
||||
}
|
||||
|
||||
|
||||
inline Ostream& report(const UList<bool>& bools)
|
||||
{
|
||||
Info<< "size=" << bools.size()
|
||||
<< " count=" << BitOps::count(bools)
|
||||
<< " !count=" << BitOps::count(bools, false)
|
||||
<< " all:" << BitOps::all(bools)
|
||||
<< " any:" << BitOps::any(bools)
|
||||
<< " none:" << BitOps::none(bools) << nl;
|
||||
|
||||
return Info;
|
||||
}
|
||||
|
||||
|
||||
template<class UIntType>
|
||||
std::string toString(UIntType value, char off='.', char on='1')
|
||||
{
|
||||
@ -304,6 +318,35 @@ int main(int argc, char *argv[])
|
||||
<< flatOutput(bools1.toc()) << endl;
|
||||
}
|
||||
|
||||
|
||||
// Check bitSet vs boolList
|
||||
{
|
||||
boolList bools(list4.values());
|
||||
|
||||
Info<< nl << "Check BitOps on boolList" << nl << nl;
|
||||
|
||||
Info<<"bitSet ";
|
||||
report(list4);
|
||||
|
||||
list4.shrink();
|
||||
Info<<"shrunk ";
|
||||
report(list4);
|
||||
|
||||
Info<< nl;
|
||||
|
||||
Info<<"bools ";
|
||||
report(bools);
|
||||
|
||||
bools = false;
|
||||
Info<<"bools (all unset) ";
|
||||
report(bools);
|
||||
|
||||
bools = true;
|
||||
Info<<"bools (all set) ";
|
||||
report(bools);
|
||||
}
|
||||
|
||||
Info<< "\nDone" << nl << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -184,9 +184,10 @@ int main(int argc, char *argv[])
|
||||
Info<< BitOps::bitInfo<unsigned>(BitOps::repeat_value<unsigned, 3>(1u))
|
||||
<< nl;
|
||||
|
||||
Info<< BitOps::bitInfo<unsigned>(BitOps::repeat_value<unsigned>(1u))
|
||||
Info<< BitOps::bitInfo<unsigned>(BitOps::repeat_value<unsigned, 1>(1u))
|
||||
<< nl;
|
||||
|
||||
|
||||
Info << "---\nEnd\n" << endl;
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user