diff --git a/applications/test/bitSet1/Test-bitSet1.C b/applications/test/bitSet1/Test-bitSet1.C index 111b3af820..e838f2b0a9 100644 --- a/applications/test/bitSet1/Test-bitSet1.C +++ b/applications/test/bitSet1/Test-bitSet1.C @@ -25,7 +25,7 @@ Application Test-bitSet1 Description - Test bitSet functionality + Basic bitSet characteristics \*---------------------------------------------------------------------------*/ @@ -64,8 +64,6 @@ inline Ostream& report // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -// Main program: - int main(int argc, char *argv[]) { bitSet set1(100); diff --git a/applications/test/bitSet2/Test-bitSet2.C b/applications/test/bitSet2/Test-bitSet2.C index 71b8992a68..a99c0bf1c9 100644 --- a/applications/test/bitSet2/Test-bitSet2.C +++ b/applications/test/bitSet2/Test-bitSet2.C @@ -38,8 +38,11 @@ Description #include "bitSet.H" #include "FlatOutput.H" -using namespace Foam; +// #define TEST_SFINAE +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +using namespace Foam; inline Ostream& extent(const bitSet& bitset) { @@ -99,7 +102,8 @@ inline Ostream& report inline Ostream& report(const UList& bools) { - return info(bools); + info(bools); + return Info; } @@ -167,6 +171,70 @@ int main(int argc, char *argv[]) Info<< "\nalternating bit pattern\n"; compare(list1, "..........1..1..1..1..1..1..1..1"); + // As boolList + { + boolList bools = list1.values(); + + Info<<"===============" << nl; + Info<<"bools: " << flatOutput(bools) << nl; + + for (int i : { -10, 0, 8, 15, 32}) + { + Info<< i << " is " << (bools.test(i) ? "set" : "unset") + << " = " << bools.get(i) << nl; + } + + Info<<"bools: " << flatOutput(bools) << nl; + + for (int i : { -10, 5, 24}) + { + Info<< "set(" << i << ") = " + << (bools.set(i) ? "true" : "false") + << nl; + } + Info<<"bools: " << flatOutput(bools) << nl; + + for (int i : { -10, 12, 32, 150}) + { + Info<< "unset(" << i << ") = " + << (bools.unset(i) ? "true" : "false") + << nl; + } + + Info<<"bools: " << flatOutput(bools) << nl; + + #if 0 + boolList bools2(6, false); + Info<<"other bools: " << flatOutput(bools2) << nl; + + bools2.set(4); + Info<<"other bools: " << flatOutput(bools2) << nl; + + bools2.clear(); + bools2.set(3); + bools2.resize(8); + Info<<"other bools: " << flatOutput(bools2) << nl; + #endif + Info<<"===============" << nl; + } + + #ifdef TEST_SFINAE + { + labelList labels = list1.toc(); + if (labels.test(0)) + { + Info<<"no" << endl; + } + + List ptrs(10, nullptr); + if (ptrs.get(0)) + { + Info<<"no" << endl; + } + } + #endif + + list1.unset(labelRange(13, 20)); // In range Info<< "\nafter clear [13,..]\n"; diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H index f629703a1f..52acb51131 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H @@ -579,8 +579,7 @@ inline unsigned int Foam::PackedList::get(const label i) const } #endif - // Lazy evaluation - return 0 for out-of-range - return 0u; + return 0u; // Out-of-bounds (lazy): return 0 (false) } return reference(const_cast*>(this), i).get(); @@ -603,19 +602,16 @@ inline bool Foam::PackedList::set << endl; #endif - // Lazy evaluation - ignore out-of-bounds - return false; + return false; // Out-of-bounds: ignore } else if (i >= size()) { - if (!val) + if (!val) // Unset out-of-bounds: ignore { - // Same as unset out-of-bounds = noop return false; } - // Lazy evaluation - increase size on assigment - resize(i + 1); + resize(i + 1); // Lazy evaluation: adjust size for assign } return reference(this, i).set(val); @@ -627,8 +623,7 @@ inline bool Foam::PackedList::unset(const label i) { if (i < 0 || i >= size()) { - // Unset out-of-bounds = noop - return false; + return false; // Unset out-of-bounds: ignore } return reference(this, i).set(0u); diff --git a/src/OpenFOAM/containers/Lists/List/List.C b/src/OpenFOAM/containers/Lists/List/List.C index 8897e32885..7114aabb19 100644 --- a/src/OpenFOAM/containers/Lists/List/List.C +++ b/src/OpenFOAM/containers/Lists/List/List.C @@ -35,330 +35,10 @@ License #include -// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // template -Foam::List::List(const label len) -: - UList(nullptr, len) -{ - if (len < 0) - { - FatalErrorInFunction - << "bad size " << len - << abort(FatalError); - } - - alloc(); -} - - -template -Foam::List::List(const label len, const T& val) -: - UList(nullptr, len) -{ - if (len < 0) - { - FatalErrorInFunction - << "bad size " << len - << abort(FatalError); - } - - if (len) - { - alloc(); - - List_ACCESS(T, (*this), vp); - for (label i=0; i < len; ++i) - { - vp[i] = val; - } - } -} - - -template -Foam::List::List(const label len, const zero) -: - UList(nullptr, len) -{ - if (len < 0) - { - FatalErrorInFunction - << "bad size " << len - << abort(FatalError); - } - - if (len) - { - alloc(); - - List_ACCESS(T, (*this), vp); - for (label i=0; i < len; ++i) - { - vp[i] = Zero; - } - } -} - - -template -Foam::List::List(const one, const T& val) -: - UList(new T[1], 1) -{ - this->v_[0] = val; -} - - -template -Foam::List::List(const one, T&& val) -: - UList(new T[1], 1) -{ - this->v_[0] = std::move(val); -} - - -template -Foam::List::List(const one, const zero) -: - UList(new T[1], 1) -{ - this->v_[0] = Zero; -} - - -template -Foam::List::List(const UList& a) -: - UList(nullptr, a.size_) -{ - if (this->size_) - { - alloc(); - - #ifdef USEMEMCPY - if (contiguous()) - { - memcpy(this->v_, a.v_, this->byteSize()); - } - else - #endif - { - List_ACCESS(T, (*this), vp); - List_CONST_ACCESS(T, a, ap); - List_FOR_ALL((*this), i) - { - vp[i] = ap[i]; - } - } - } -} - - -template -Foam::List::List(const List& a) -: - UList(nullptr, a.size_) -{ - if (this->size_) - { - alloc(); - - #ifdef USEMEMCPY - if (contiguous()) - { - memcpy(this->v_, a.v_, this->byteSize()); - } - else - #endif - { - List_ACCESS(T, (*this), vp); - List_CONST_ACCESS(T, a, ap); - List_FOR_ALL((*this), i) - { - vp[i] = ap[i]; - } - } - } -} - - -template -Foam::List::List(List& a, bool reuse) -: - UList(nullptr, a.size_) -{ - if (reuse) - { - // swap content - this->v_ = a.v_; - a.v_ = nullptr; - a.size_ = 0; - } - else if (this->size_) - { - alloc(); - - #ifdef USEMEMCPY - if (contiguous()) - { - memcpy(this->v_, a.v_, this->byteSize()); - } - else - #endif - { - List_ACCESS(T, (*this), vp); - List_CONST_ACCESS(T, a, ap); - List_FOR_ALL((*this), i) - { - vp[i] = ap[i]; - } - } - } -} - - -template -Foam::List::List(const UList& lst, const labelUList& mapAddressing) -: - UList(nullptr, mapAddressing.size()) -{ - const label len = mapAddressing.size(); - - if (len) - { - alloc(); - - List_ACCESS(T, (*this), vp); - - for (label i=0; i < len; ++i) - { - vp[i] = lst[mapAddressing[i]]; - } - } -} - - -template -template -Foam::List::List(InputIterator begIter, InputIterator endIter) -: - List(begIter, endIter, std::distance(begIter, endIter)) -{} - - -template -template -Foam::List::List(const FixedList& lst) -: - UList(nullptr, Size) -{ - alloc(); - copyList(lst); -} - - -template -Foam::List::List(const PtrList& lst) -: - UList(nullptr, lst.size()) -{ - alloc(); - copyList(lst); -} - - -template -Foam::List::List(const SLList& lst) -: - List(lst.begin(), lst.end(), lst.size()) -{} - - -template -Foam::List::List(const UIndirectList& lst) -: - UList(nullptr, lst.size()) -{ - alloc(); - copyList(lst); -} - - -template -Foam::List::List(const BiIndirectList& lst) -: - UList(nullptr, lst.size()) -{ - alloc(); - copyList(lst); -} - - -template -Foam::List::List(std::initializer_list lst) -: - List(lst.begin(), lst.end(), lst.size()) -{} - - -template -Foam::List::List(List&& lst) -: - UList(nullptr, 0) -{ - // Can use transfer or swap to manage content - transfer(lst); -} - - -template -template -Foam::List::List(DynamicList&& lst) -: - UList(nullptr, 0) -{ - transfer(lst); -} - - -template -Foam::List::List(SortableList&& lst) -: - UList(nullptr, 0) -{ - transfer(lst); -} - - -template -Foam::List::List(SLList&& lst) -: - UList(nullptr, 0) -{ - operator=(std::move(lst)); -} - - -// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * // - -template -Foam::List::~List() -{ - if (this->v_) - { - delete[] this->v_; - } -} - - -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -template -void Foam::List::setSize(const label newSize) +void Foam::List::doResize(const label newSize) { if (newSize < 0) { @@ -406,11 +86,333 @@ void Foam::List::setSize(const label newSize) } +// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // + template -void Foam::List::setSize(const label newSize, const T& val) +Foam::List::List(const label len) +: + UList(nullptr, len) +{ + if (len < 0) + { + FatalErrorInFunction + << "bad size " << len + << abort(FatalError); + } + + doAlloc(); +} + + +template +Foam::List::List(const label len, const T& val) +: + UList(nullptr, len) +{ + if (len < 0) + { + FatalErrorInFunction + << "bad size " << len + << abort(FatalError); + } + + if (len) + { + doAlloc(); + + List_ACCESS(T, (*this), vp); + for (label i=0; i < len; ++i) + { + vp[i] = val; + } + } +} + + +template +Foam::List::List(const label len, const zero) +: + UList(nullptr, len) +{ + if (len < 0) + { + FatalErrorInFunction + << "bad size " << len + << abort(FatalError); + } + + if (len) + { + doAlloc(); + + List_ACCESS(T, (*this), vp); + for (label i=0; i < len; ++i) + { + vp[i] = Zero; + } + } +} + + +template +Foam::List::List(const one, const T& val) +: + UList(new T[1], 1) +{ + this->v_[0] = val; +} + + +template +Foam::List::List(const one, T&& val) +: + UList(new T[1], 1) +{ + this->v_[0] = std::move(val); +} + + +template +Foam::List::List(const one, const zero) +: + UList(new T[1], 1) +{ + this->v_[0] = Zero; +} + + +template +Foam::List::List(const UList& a) +: + UList(nullptr, a.size_) +{ + if (this->size_) + { + doAlloc(); + + #ifdef USEMEMCPY + if (contiguous()) + { + memcpy(this->v_, a.v_, this->byteSize()); + } + else + #endif + { + List_ACCESS(T, (*this), vp); + List_CONST_ACCESS(T, a, ap); + List_FOR_ALL((*this), i) + { + vp[i] = ap[i]; + } + } + } +} + + +template +Foam::List::List(const List& a) +: + UList(nullptr, a.size_) +{ + if (this->size_) + { + doAlloc(); + + #ifdef USEMEMCPY + if (contiguous()) + { + memcpy(this->v_, a.v_, this->byteSize()); + } + else + #endif + { + List_ACCESS(T, (*this), vp); + List_CONST_ACCESS(T, a, ap); + List_FOR_ALL((*this), i) + { + vp[i] = ap[i]; + } + } + } +} + + +template +Foam::List::List(List& a, bool reuse) +: + UList(nullptr, a.size_) +{ + if (reuse) + { + // swap content + this->v_ = a.v_; + a.v_ = nullptr; + a.size_ = 0; + } + else if (this->size_) + { + doAlloc(); + + #ifdef USEMEMCPY + if (contiguous()) + { + memcpy(this->v_, a.v_, this->byteSize()); + } + else + #endif + { + List_ACCESS(T, (*this), vp); + List_CONST_ACCESS(T, a, ap); + List_FOR_ALL((*this), i) + { + vp[i] = ap[i]; + } + } + } +} + + +template +Foam::List::List(const UList& list, const labelUList& mapAddressing) +: + UList(nullptr, mapAddressing.size()) +{ + const label len = mapAddressing.size(); + + if (len) + { + doAlloc(); + + List_ACCESS(T, (*this), vp); + + for (label i=0; i < len; ++i) + { + vp[i] = list[mapAddressing[i]]; + } + } +} + + +template +template +Foam::List::List(InputIterator begIter, InputIterator endIter) +: + List(begIter, endIter, std::distance(begIter, endIter)) +{} + + +template +template +Foam::List::List(const FixedList& list) +: + UList(nullptr, Size) +{ + doAlloc(); + copyList(list); +} + + +template +Foam::List::List(const PtrList& list) +: + UList(nullptr, list.size()) +{ + doAlloc(); + copyList(list); +} + + +template +Foam::List::List(const SLList& list) +: + List(list.begin(), list.end(), list.size()) +{} + + +template +Foam::List::List(const UIndirectList& list) +: + UList(nullptr, list.size()) +{ + doAlloc(); + copyList(list); +} + + +template +Foam::List::List(const BiIndirectList& list) +: + UList(nullptr, list.size()) +{ + doAlloc(); + copyList(list); +} + + +template +Foam::List::List(std::initializer_list list) +: + List(list.begin(), list.end(), list.size()) +{} + + +template +Foam::List::List(List&& list) +: + UList(nullptr, 0) +{ + // Can use transfer or swap to manage content + transfer(list); +} + + +template +template +Foam::List::List(DynamicList&& list) +: + UList(nullptr, 0) +{ + transfer(list); +} + + +template +Foam::List::List(SortableList&& list) +: + UList(nullptr, 0) +{ + transfer(list); +} + + +template +Foam::List::List(SLList&& list) +: + UList(nullptr, 0) +{ + operator=(std::move(list)); +} + + +// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * // + +template +Foam::List::~List() +{ + if (this->v_) + { + delete[] this->v_; + } +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +void Foam::List::resize(const label newSize, const T& val) { const label oldSize = this->size_; - this->setSize(newSize); + this->doResize(newSize); List_ACCESS(T, *this, vp); for (label i = oldSize; i < newSize; ++i) @@ -421,37 +423,37 @@ void Foam::List::setSize(const label newSize, const T& val) template -void Foam::List::transfer(List& lst) +void Foam::List::transfer(List& list) { // Clear and swap - could also check for self assignment clear(); - this->size_ = lst.size_; - this->v_ = lst.v_; + this->size_ = list.size_; + this->v_ = list.v_; - lst.size_ = 0; - lst.v_ = nullptr; + list.size_ = 0; + list.v_ = nullptr; } template template -void Foam::List::transfer(DynamicList& lst) +void Foam::List::transfer(DynamicList& list) { // Shrink the allocated space to the number of elements used - lst.shrink(); - transfer(static_cast&>(lst)); + list.shrink(); + transfer(static_cast&>(list)); // Ensure DynamicList has proper capacity=0 too - lst.clearStorage(); + list.clearStorage(); } template -void Foam::List::transfer(SortableList& lst) +void Foam::List::transfer(SortableList& list) { // Shrink away the sort indices - lst.shrink(); - transfer(static_cast&>(lst)); + list.shrink(); + transfer(static_cast&>(list)); } @@ -484,23 +486,23 @@ void Foam::List::operator=(const UList& a) template -void Foam::List::operator=(const List& lst) +void Foam::List::operator=(const List& list) { - if (this == &lst) + if (this == &list) { FatalErrorInFunction << "attempted assignment to self" << abort(FatalError); } - operator=(static_cast&>(lst)); + operator=(static_cast&>(list)); } template -void Foam::List::operator=(const SLList& lst) +void Foam::List::operator=(const SLList& list) { - const label len = lst.size(); + const label len = list.size(); reAlloc(len); @@ -509,7 +511,7 @@ void Foam::List::operator=(const SLList& lst) List_ACCESS(T, (*this), vp); label i = 0; - for (auto iter = lst.cbegin(); iter != lst.cend(); ++iter) + for (auto iter = list.cbegin(); iter != list.cend(); ++iter) { vp[i] = *iter; ++i; @@ -519,9 +521,9 @@ void Foam::List::operator=(const SLList& lst) template -void Foam::List::operator=(const UIndirectList& lst) +void Foam::List::operator=(const UIndirectList& list) { - const label len = lst.size(); + const label len = list.size(); reAlloc(len); @@ -531,16 +533,16 @@ void Foam::List::operator=(const UIndirectList& lst) for (label i=0; i -void Foam::List::operator=(const BiIndirectList& lst) +void Foam::List::operator=(const BiIndirectList& list) { - const label len = lst.size(); + const label len = list.size(); reAlloc(len); @@ -550,16 +552,16 @@ void Foam::List::operator=(const BiIndirectList& lst) for (label i=0; i -void Foam::List::operator=(std::initializer_list lst) +void Foam::List::operator=(std::initializer_list list) { - const label len = lst.size(); + const label len = list.size(); reAlloc(len); @@ -568,7 +570,7 @@ void Foam::List::operator=(std::initializer_list lst) List_ACCESS(T, (*this), vp); label i = 0; - for (const auto& val : lst) + for (const auto& val : list) { vp[i] = val; ++i; @@ -578,38 +580,38 @@ void Foam::List::operator=(std::initializer_list lst) template -void Foam::List::operator=(List&& lst) +void Foam::List::operator=(List&& list) { - if (this == &lst) + if (this == &list) { FatalErrorInFunction << "attempted assignment to self" << abort(FatalError); } - transfer(lst); + transfer(list); } template template -void Foam::List::operator=(DynamicList&& lst) +void Foam::List::operator=(DynamicList&& list) { - transfer(lst); + transfer(list); } template -void Foam::List::operator=(SortableList&& lst) +void Foam::List::operator=(SortableList&& list) { - transfer(lst); + transfer(list); } template -void Foam::List::operator=(SLList&& lst) +void Foam::List::operator=(SLList&& list) { - const label len = lst.size(); + const label len = list.size(); reAlloc(len); @@ -619,11 +621,11 @@ void Foam::List::operator=(SLList&& lst) for (label i = 0; i < len; ++i) { - vp[i] = std::move(lst.removeHead()); + vp[i] = std::move(list.removeHead()); } } - lst.clear(); + list.clear(); } diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index c7830f50eb..96122a64d4 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -67,7 +67,7 @@ template class IndirectList; template class UIndirectList; template class BiIndirectList; -template Istream& operator>>(Istream& is, List& L); +template Istream& operator>>(Istream& is, List& list); // Commonly required list types typedef List charList; @@ -82,17 +82,20 @@ class List : public UList { - // Private member functions + // Private Member Functions //- Allocate list storage - inline void alloc(); + inline void doAlloc(); //- Reallocate list storage to the given size inline void reAlloc(const label len); //- Copy list of given type. template - inline void copyList(const List2& lst); + inline void copyList(const List2& list); + + //- Change allocation size of List. Backend for resize/setSize. + void doResize(const label newSize); //- Construct given begin/end iterators and number of elements // Since the size is provided, the end iterator is actually ignored. @@ -146,7 +149,7 @@ public: List(List& a, bool reuse); //- Construct as subset - List(const UList& lst, const labelUList& mapAddressing); + List(const UList& list, const labelUList& mapAddressing); //- Construct given begin/end iterators. // Uses std::distance for the size. @@ -155,35 +158,35 @@ public: //- Construct as copy of FixedList template - explicit List(const FixedList& lst); + explicit List(const FixedList& list); //- Construct as copy of PtrList - explicit List(const PtrList& lst); + explicit List(const PtrList& list); //- Construct as copy of SLList - explicit List(const SLList& lst); + explicit List(const SLList& list); //- Construct as copy of UIndirectList - explicit List(const UIndirectList& lst); + explicit List(const UIndirectList& list); //- Construct as copy of BiIndirectList - explicit List(const BiIndirectList& lst); + explicit List(const BiIndirectList& list); //- Construct from an initializer list - List(std::initializer_list lst); + List(std::initializer_list list); //- Move construct from List - List(List&& lst); + List(List&& list); //- Move construct from DynamicList template - List(DynamicList&& lst); + List(DynamicList&& list); //- Move construct from SortableList - List(SortableList&& lst); + List(SortableList&& list); //- Move construct from SLList - List(SLList&& lst); + List(SLList&& list); //- Construct from Istream List(Istream& is); @@ -206,17 +209,18 @@ public: // Edit - //- Alias for setSize(const label) + //- Adjust allocated size of list. + // The boolList version fills new memory with false. inline void resize(const label newSize); - //- Alias for setSize(const label, const T&) - inline void resize(const label newSize, const T& val); + //- Adjust allocated size of list and set val for new elements + void resize(const label newSize, const T& val); - //- Reset size of List - void setSize(const label newSize); + //- Alias for resize(const label) + inline void setSize(const label newSize); - //- Reset size of List and value for new elements - void setSize(const label newSize, const T& val); + //- Alias for resize(const label, const T&) + inline void setSize(const label newSize, const T& val); //- Clear the list, i.e. set size to zero inline void clear(); @@ -228,23 +232,23 @@ public: inline void append(T&& val); //- Append a List to the end of this list - inline void append(const UList& lst); + inline void append(const UList& list); //- Append a UIndirectList at the end of this list - inline void append(const UIndirectList& lst); + inline void append(const UIndirectList& list); //- Transfer the contents of the argument List into this list //- and annul the argument list - void transfer(List& lst); + void transfer(List& list); //- Transfer the contents of the argument List into this list //- and annul the argument list template - void transfer(DynamicList& lst); + void transfer(DynamicList& list); //- Transfer the contents of the argument List into this list //- and annul the argument list - void transfer(SortableList& lst); + void transfer(SortableList& list); //- Return subscript-checked element of UList and resizing the list //- if required. @@ -257,19 +261,19 @@ public: void operator=(const UList& a); //- Assignment operator. Takes linear time - void operator=(const List& lst); + void operator=(const List& list); //- Assignment to SLList operator. Takes linear time - void operator=(const SLList& lst); + void operator=(const SLList& list); //- Assignment to UIndirectList operator. Takes linear time - void operator=(const UIndirectList& lst); + void operator=(const UIndirectList& list); //- Assignment to BiIndirectList operator. Takes linear time - void operator=(const BiIndirectList& lst); + void operator=(const BiIndirectList& list); //- Assignment to an initializer list - void operator=(std::initializer_list lst); + void operator=(std::initializer_list list); //- Assignment of all entries to the given value inline void operator=(const T& val); @@ -278,17 +282,17 @@ public: inline void operator=(const zero); //- Move assignment. Takes constant time - void operator=(List&& lst); + void operator=(List&& list); //- Move assignment. Takes constant time. template - void operator=(DynamicList&& lst); + void operator=(DynamicList&& list); //- Move assignment. Takes constant time. - void operator=(SortableList&& lst); + void operator=(SortableList&& list); //- Move assignment. Takes constant time - void operator=(SLList&& lst); + void operator=(SLList&& list); // Istream Operator @@ -297,7 +301,7 @@ public: friend Istream& operator>> ( Istream& is, - List& L + List& list ); @@ -305,6 +309,34 @@ public: //- No shallowCopy permitted void shallowCopy(const UList&) = delete; + + + // Special Methods + + //- A bitSet::set() method for a list of bool + // Increases size when setting an out-of-bounds value. + // + // \return True if value changed. + template + typename std::enable_if::value, bool>::type + inline set(const label i, bool val = true) + { + if (i < 0) + { + return false; // Out-of-bounds: ignore + } + else if (i >= this->size()) + { + if (!val) // Unset out-of-bounds: ignore + { + return false; + } + this->resize(i+1, false); // Adjust size for assign, fill 0 + } + + (*this)[i] = val; + return true; + } }; diff --git a/src/OpenFOAM/containers/Lists/List/ListI.H b/src/OpenFOAM/containers/Lists/List/ListI.H index 9579e3612d..3936ebfe88 100644 --- a/src/OpenFOAM/containers/Lists/List/ListI.H +++ b/src/OpenFOAM/containers/Lists/List/ListI.H @@ -26,7 +26,7 @@ License // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // template -inline void Foam::List::alloc() +inline void Foam::List::doAlloc() { if (this->size_) { @@ -42,20 +42,20 @@ inline void Foam::List::reAlloc(const label len) { clear(); this->size_ = len; - alloc(); + doAlloc(); } } template template -inline void Foam::List::copyList(const List2& lst) +inline void Foam::List::copyList(const List2& list) { const label len = this->size_; for (label i=0; ioperator[](i) = lst[i]; + this->operator[](i) = list[i]; } } @@ -73,7 +73,7 @@ inline Foam::List::List { if (this->size_) { - alloc(); + doAlloc(); InputIterator iter = begIter; for (label i = 0; i < len; ++i) @@ -121,17 +121,35 @@ inline void Foam::List::clear() } -template -inline void Foam::List::resize(const label len) +namespace Foam { - this->setSize(len); + // Template specialization for bool. Fills with false + template<> + inline void List::resize(const label newSize) + { + this->resize(newSize, false); + } } template -inline void Foam::List::resize(const label len, const T& val) +inline void Foam::List::resize(const label len) { - this->setSize(len, val); + this->doResize(len); +} + + +template +inline void Foam::List::setSize(const label len) +{ + this->resize(len); +} + + +template +inline void Foam::List::setSize(const label len, const T& val) +{ + this->resize(len, val); } @@ -142,7 +160,7 @@ inline T& Foam::List::newElmt(const label i) if (i >= n) { - setSize(2*n); + resize(2*n); } return UList::operator[](i); @@ -152,7 +170,7 @@ inline T& Foam::List::newElmt(const label i) template inline void Foam::List::append(const T& val) { - setSize(this->size() + 1, val); // copy element + resize(this->size() + 1, val); // copy element } @@ -160,44 +178,44 @@ template inline void Foam::List::append(T&& val) { const label idx = this->size(); - setSize(idx + 1); + resize(idx + 1); this->operator[](idx) = std::move(val); // move assign element } template -inline void Foam::List::append(const UList& lst) +inline void Foam::List::append(const UList& list) { - if (this == &lst) + if (this == &list) { FatalErrorInFunction << "attempted appending to self" << abort(FatalError); } label idx = this->size(); - const label n = lst.size(); + const label n = list.size(); - setSize(idx + n); + resize(idx + n); for (label i=0; ioperator[](idx++) = lst[i]; // copy element + this->operator[](idx++) = list[i]; // copy element } } template -inline void Foam::List::append(const UIndirectList& lst) +inline void Foam::List::append(const UIndirectList& list) { label idx = this->size(); - const label n = lst.size(); + const label n = list.size(); - setSize(idx + n); + resize(idx + n); for (label i=0; ioperator[](idx++) = lst[i]; // copy element + this->operator[](idx++) = list[i]; // copy element } } diff --git a/src/OpenFOAM/containers/Lists/List/ListIO.C b/src/OpenFOAM/containers/Lists/List/ListIO.C index 55d11dfb52..d421a1f060 100644 --- a/src/OpenFOAM/containers/Lists/List/ListIO.C +++ b/src/OpenFOAM/containers/Lists/List/ListIO.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -41,10 +41,10 @@ Foam::List::List(Istream& is) template -Foam::Istream& Foam::operator>>(Istream& is, List& lst) +Foam::Istream& Foam::operator>>(Istream& is, List& list) { // Anull list - lst.setSize(0); + list.resize(0); is.fatalCheck(FUNCTION_NAME); @@ -55,7 +55,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List& lst) // Compound: simply transfer contents if (firstToken.isCompound()) { - lst.transfer + list.transfer ( dynamicCast>> ( @@ -72,8 +72,8 @@ Foam::Istream& Foam::operator>>(Istream& is, List& lst) { const label len = firstToken.labelToken(); - // Set list length to that read - lst.setSize(len); + // Resize to length read + list.resize(len); // Read list contents depending on data format @@ -88,11 +88,12 @@ Foam::Istream& Foam::operator>>(Istream& is, List& lst) { for (label i=0; i> lst[i]; + is >> list[i]; is.fatalCheck ( - "operator>>(Istream&, List&) : reading entry" + "operator>>(Istream&, List&) : " + "reading entry" ); } } @@ -111,7 +112,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List& lst) for (label i=0; i>(Istream& is, List& lst) { // Non-empty, binary, contiguous - is.read(reinterpret_cast(lst.data()), len*sizeof(T)); + is.read(reinterpret_cast(list.data()), len*sizeof(T)); is.fatalCheck ( - "operator>>(Istream&, List&) : reading the binary block" + "operator>>(Istream&, List&) : " + "reading the binary block" ); } @@ -151,7 +153,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List& lst) SLList sll(is); // Read as singly-linked list // Reallocate and move assign list elements - lst = std::move(sll); + list = std::move(sll); return is; } diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index 7bba6587a4..9170d1aea3 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.H @@ -490,6 +490,44 @@ public: Istream& os, UList& L ); + + + // Special Methods + + //- A bitSet::test() method for a list of bool + // + // \return The element value, or false for out-of-range access + template + typename std::enable_if::value, bool>::type + inline test(const label i) const + { + return (i >= 0 && i < size() && v_[i]); + } + + //- A bitSet::get() method for a list of bool + // + // \return The element value, or false for out-of-range access + template + typename std::enable_if::value, bool>::type + inline get(const label i) const + { + return (i >= 0 && i < size_ && v_[i]); + } + + //- A bitSet::unset() method for a list of bool + // + // \return True if value changed and was not out-of-range + template + typename std::enable_if::value, bool>::type + inline unset(const label i) + { + if (i >= 0 && i < size_ && v_[i]) + { + v_[i] = false; + return true; + } + return false; + } }; diff --git a/src/OpenFOAM/containers/Lists/UList/UListI.H b/src/OpenFOAM/containers/Lists/UList/UListI.H index f17852673a..663d8d571d 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListI.H +++ b/src/OpenFOAM/containers/Lists/UList/UListI.H @@ -225,16 +225,6 @@ inline void Foam::UList::shallowCopy(const UList& list) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -template -inline T& Foam::UList::operator[](const label i) -{ - #ifdef FULLDEBUG - checkIndex(i); - #endif - return v_[i]; -} - - namespace Foam { // Template specialization for bool @@ -252,6 +242,16 @@ namespace Foam } +template +inline T& Foam::UList::operator[](const label i) +{ + #ifdef FULLDEBUG + checkIndex(i); + #endif + return v_[i]; +} + + template inline const T& Foam::UList::operator[](const label i) const {