diff --git a/applications/test/bitSet1/Test-bitSet1.C b/applications/test/bitSet1/Test-bitSet1.C index 111b3af8204d184154538f5b8af62b89e8ecfef3..e838f2b0a968e30ef90329de4eb9a9f65b115361 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 71b8992a685d0ead8b58968a3e346c21b3514a50..a99c0bf1c95660ab44e32d23d087023b49d1a3e6 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<bool>& 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<double*> 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 f629703a1f32cc8e8cd20865cb336ef3881fd2fa..52acb511310b8fd7a164f036a5b6ce14ff64209f 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<Width>::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<PackedList<Width>*>(this), i).get(); @@ -603,19 +602,16 @@ inline bool Foam::PackedList<Width>::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<Width>::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 8897e328853fed2e1da35f8e31283bdcb08fa44f..7114aabb198874d41e954744faeed0b5c125d20a 100644 --- a/src/OpenFOAM/containers/Lists/List/List.C +++ b/src/OpenFOAM/containers/Lists/List/List.C @@ -35,6 +35,57 @@ License #include <utility> +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +template<class T> +void Foam::List<T>::doResize(const label newSize) +{ + if (newSize < 0) + { + FatalErrorInFunction + << "bad size " << newSize + << abort(FatalError); + } + + if (newSize != this->size_) + { + if (newSize > 0) + { + T* nv = new T[newSize]; + + const label overlap = min(this->size_, newSize); + + if (overlap) + { + #ifdef USEMEMCPY + if (contiguous<T>()) + { + memcpy(nv, this->v_, overlap*sizeof(T)); + } + else + #endif + { + // No speedup observed for copy assignment on simple types + List_ACCESS(T, *this, vp); + for (label i = 0; i < overlap; ++i) + { + nv[i] = std::move(vp[i]); + } + } + } + + clear(); + this->size_ = newSize; + this->v_ = nv; + } + else + { + clear(); + } + } +} + + // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // template<class T> @@ -49,7 +100,7 @@ Foam::List<T>::List(const label len) << abort(FatalError); } - alloc(); + doAlloc(); } @@ -67,7 +118,7 @@ Foam::List<T>::List(const label len, const T& val) if (len) { - alloc(); + doAlloc(); List_ACCESS(T, (*this), vp); for (label i=0; i < len; ++i) @@ -92,7 +143,7 @@ Foam::List<T>::List(const label len, const zero) if (len) { - alloc(); + doAlloc(); List_ACCESS(T, (*this), vp); for (label i=0; i < len; ++i) @@ -137,7 +188,7 @@ Foam::List<T>::List(const UList<T>& a) { if (this->size_) { - alloc(); + doAlloc(); #ifdef USEMEMCPY if (contiguous<T>()) @@ -165,7 +216,7 @@ Foam::List<T>::List(const List<T>& a) { if (this->size_) { - alloc(); + doAlloc(); #ifdef USEMEMCPY if (contiguous<T>()) @@ -200,7 +251,7 @@ Foam::List<T>::List(List<T>& a, bool reuse) } else if (this->size_) { - alloc(); + doAlloc(); #ifdef USEMEMCPY if (contiguous<T>()) @@ -222,7 +273,7 @@ Foam::List<T>::List(List<T>& a, bool reuse) template<class T> -Foam::List<T>::List(const UList<T>& lst, const labelUList& mapAddressing) +Foam::List<T>::List(const UList<T>& list, const labelUList& mapAddressing) : UList<T>(nullptr, mapAddressing.size()) { @@ -230,13 +281,13 @@ Foam::List<T>::List(const UList<T>& lst, const labelUList& mapAddressing) if (len) { - alloc(); + doAlloc(); List_ACCESS(T, (*this), vp); for (label i=0; i < len; ++i) { - vp[i] = lst[mapAddressing[i]]; + vp[i] = list[mapAddressing[i]]; } } } @@ -252,94 +303,94 @@ Foam::List<T>::List(InputIterator begIter, InputIterator endIter) template<class T> template<unsigned Size> -Foam::List<T>::List(const FixedList<T, Size>& lst) +Foam::List<T>::List(const FixedList<T, Size>& list) : UList<T>(nullptr, Size) { - alloc(); - copyList(lst); + doAlloc(); + copyList(list); } template<class T> -Foam::List<T>::List(const PtrList<T>& lst) +Foam::List<T>::List(const PtrList<T>& list) : - UList<T>(nullptr, lst.size()) + UList<T>(nullptr, list.size()) { - alloc(); - copyList(lst); + doAlloc(); + copyList(list); } template<class T> -Foam::List<T>::List(const SLList<T>& lst) +Foam::List<T>::List(const SLList<T>& list) : - List<T>(lst.begin(), lst.end(), lst.size()) + List<T>(list.begin(), list.end(), list.size()) {} template<class T> -Foam::List<T>::List(const UIndirectList<T>& lst) +Foam::List<T>::List(const UIndirectList<T>& list) : - UList<T>(nullptr, lst.size()) + UList<T>(nullptr, list.size()) { - alloc(); - copyList(lst); + doAlloc(); + copyList(list); } template<class T> -Foam::List<T>::List(const BiIndirectList<T>& lst) +Foam::List<T>::List(const BiIndirectList<T>& list) : - UList<T>(nullptr, lst.size()) + UList<T>(nullptr, list.size()) { - alloc(); - copyList(lst); + doAlloc(); + copyList(list); } template<class T> -Foam::List<T>::List(std::initializer_list<T> lst) +Foam::List<T>::List(std::initializer_list<T> list) : - List<T>(lst.begin(), lst.end(), lst.size()) + List<T>(list.begin(), list.end(), list.size()) {} template<class T> -Foam::List<T>::List(List<T>&& lst) +Foam::List<T>::List(List<T>&& list) : UList<T>(nullptr, 0) { // Can use transfer or swap to manage content - transfer(lst); + transfer(list); } template<class T> template<int SizeMin> -Foam::List<T>::List(DynamicList<T, SizeMin>&& lst) +Foam::List<T>::List(DynamicList<T, SizeMin>&& list) : UList<T>(nullptr, 0) { - transfer(lst); + transfer(list); } template<class T> -Foam::List<T>::List(SortableList<T>&& lst) +Foam::List<T>::List(SortableList<T>&& list) : UList<T>(nullptr, 0) { - transfer(lst); + transfer(list); } template<class T> -Foam::List<T>::List(SLList<T>&& lst) +Foam::List<T>::List(SLList<T>&& list) : UList<T>(nullptr, 0) { - operator=(std::move(lst)); + operator=(std::move(list)); } @@ -358,59 +409,10 @@ Foam::List<T>::~List() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class T> -void Foam::List<T>::setSize(const label newSize) -{ - if (newSize < 0) - { - FatalErrorInFunction - << "bad size " << newSize - << abort(FatalError); - } - - if (newSize != this->size_) - { - if (newSize > 0) - { - T* nv = new T[newSize]; - - const label overlap = min(this->size_, newSize); - - if (overlap) - { - #ifdef USEMEMCPY - if (contiguous<T>()) - { - memcpy(nv, this->v_, overlap*sizeof(T)); - } - else - #endif - { - // No speedup observed for copy assignment on simple types - List_ACCESS(T, *this, vp); - for (label i = 0; i < overlap; ++i) - { - nv[i] = std::move(vp[i]); - } - } - } - - clear(); - this->size_ = newSize; - this->v_ = nv; - } - else - { - clear(); - } - } -} - - -template<class T> -void Foam::List<T>::setSize(const label newSize, const T& val) +void Foam::List<T>::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<T>::setSize(const label newSize, const T& val) template<class T> -void Foam::List<T>::transfer(List<T>& lst) +void Foam::List<T>::transfer(List<T>& 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<class T> template<int SizeMin> -void Foam::List<T>::transfer(DynamicList<T, SizeMin>& lst) +void Foam::List<T>::transfer(DynamicList<T, SizeMin>& list) { // Shrink the allocated space to the number of elements used - lst.shrink(); - transfer(static_cast<List<T>&>(lst)); + list.shrink(); + transfer(static_cast<List<T>&>(list)); // Ensure DynamicList has proper capacity=0 too - lst.clearStorage(); + list.clearStorage(); } template<class T> -void Foam::List<T>::transfer(SortableList<T>& lst) +void Foam::List<T>::transfer(SortableList<T>& list) { // Shrink away the sort indices - lst.shrink(); - transfer(static_cast<List<T>&>(lst)); + list.shrink(); + transfer(static_cast<List<T>&>(list)); } @@ -484,23 +486,23 @@ void Foam::List<T>::operator=(const UList<T>& a) template<class T> -void Foam::List<T>::operator=(const List<T>& lst) +void Foam::List<T>::operator=(const List<T>& list) { - if (this == &lst) + if (this == &list) { FatalErrorInFunction << "attempted assignment to self" << abort(FatalError); } - operator=(static_cast<const UList<T>&>(lst)); + operator=(static_cast<const UList<T>&>(list)); } template<class T> -void Foam::List<T>::operator=(const SLList<T>& lst) +void Foam::List<T>::operator=(const SLList<T>& list) { - const label len = lst.size(); + const label len = list.size(); reAlloc(len); @@ -509,7 +511,7 @@ void Foam::List<T>::operator=(const SLList<T>& 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<T>::operator=(const SLList<T>& lst) template<class T> -void Foam::List<T>::operator=(const UIndirectList<T>& lst) +void Foam::List<T>::operator=(const UIndirectList<T>& list) { - const label len = lst.size(); + const label len = list.size(); reAlloc(len); @@ -531,16 +533,16 @@ void Foam::List<T>::operator=(const UIndirectList<T>& lst) for (label i=0; i<len; ++i) { - vp[i] = lst[i]; + vp[i] = list[i]; } } } template<class T> -void Foam::List<T>::operator=(const BiIndirectList<T>& lst) +void Foam::List<T>::operator=(const BiIndirectList<T>& list) { - const label len = lst.size(); + const label len = list.size(); reAlloc(len); @@ -550,16 +552,16 @@ void Foam::List<T>::operator=(const BiIndirectList<T>& lst) for (label i=0; i<len; ++i) { - vp[i] = lst[i]; + vp[i] = list[i]; } } } template<class T> -void Foam::List<T>::operator=(std::initializer_list<T> lst) +void Foam::List<T>::operator=(std::initializer_list<T> list) { - const label len = lst.size(); + const label len = list.size(); reAlloc(len); @@ -568,7 +570,7 @@ void Foam::List<T>::operator=(std::initializer_list<T> 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<T>::operator=(std::initializer_list<T> lst) template<class T> -void Foam::List<T>::operator=(List<T>&& lst) +void Foam::List<T>::operator=(List<T>&& list) { - if (this == &lst) + if (this == &list) { FatalErrorInFunction << "attempted assignment to self" << abort(FatalError); } - transfer(lst); + transfer(list); } template<class T> template<int SizeMin> -void Foam::List<T>::operator=(DynamicList<T, SizeMin>&& lst) +void Foam::List<T>::operator=(DynamicList<T, SizeMin>&& list) { - transfer(lst); + transfer(list); } template<class T> -void Foam::List<T>::operator=(SortableList<T>&& lst) +void Foam::List<T>::operator=(SortableList<T>&& list) { - transfer(lst); + transfer(list); } template<class T> -void Foam::List<T>::operator=(SLList<T>&& lst) +void Foam::List<T>::operator=(SLList<T>&& list) { - const label len = lst.size(); + const label len = list.size(); reAlloc(len); @@ -619,11 +621,11 @@ void Foam::List<T>::operator=(SLList<T>&& 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 c7830f50ebec4cdeb6974e2ecc609aabed0029ef..96122a64d4dd29e48cddf37be95f3773e2862b5c 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -67,7 +67,7 @@ template<class T> class IndirectList; template<class T> class UIndirectList; template<class T> class BiIndirectList; -template<class T> Istream& operator>>(Istream& is, List<T>& L); +template<class T> Istream& operator>>(Istream& is, List<T>& list); // Commonly required list types typedef List<char> charList; @@ -82,17 +82,20 @@ class List : public UList<T> { - // 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<class List2> - 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<T>& a, bool reuse); //- Construct as subset - List(const UList<T>& lst, const labelUList& mapAddressing); + List(const UList<T>& 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<T, Size> template<unsigned Size> - explicit List(const FixedList<T, Size>& lst); + explicit List(const FixedList<T, Size>& list); //- Construct as copy of PtrList<T> - explicit List(const PtrList<T>& lst); + explicit List(const PtrList<T>& list); //- Construct as copy of SLList<T> - explicit List(const SLList<T>& lst); + explicit List(const SLList<T>& list); //- Construct as copy of UIndirectList<T> - explicit List(const UIndirectList<T>& lst); + explicit List(const UIndirectList<T>& list); //- Construct as copy of BiIndirectList<T> - explicit List(const BiIndirectList<T>& lst); + explicit List(const BiIndirectList<T>& list); //- Construct from an initializer list - List(std::initializer_list<T> lst); + List(std::initializer_list<T> list); //- Move construct from List - List(List<T>&& lst); + List(List<T>&& list); //- Move construct from DynamicList template<int SizeMin> - List(DynamicList<T, SizeMin>&& lst); + List(DynamicList<T, SizeMin>&& list); //- Move construct from SortableList - List(SortableList<T>&& lst); + List(SortableList<T>&& list); //- Move construct from SLList - List(SLList<T>&& lst); + List(SLList<T>&& 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<T>& lst); + inline void append(const UList<T>& list); //- Append a UIndirectList at the end of this list - inline void append(const UIndirectList<T>& lst); + inline void append(const UIndirectList<T>& list); //- Transfer the contents of the argument List into this list //- and annul the argument list - void transfer(List<T>& lst); + void transfer(List<T>& list); //- Transfer the contents of the argument List into this list //- and annul the argument list template<int SizeMin> - void transfer(DynamicList<T, SizeMin>& lst); + void transfer(DynamicList<T, SizeMin>& list); //- Transfer the contents of the argument List into this list //- and annul the argument list - void transfer(SortableList<T>& lst); + void transfer(SortableList<T>& list); //- Return subscript-checked element of UList and resizing the list //- if required. @@ -257,19 +261,19 @@ public: void operator=(const UList<T>& a); //- Assignment operator. Takes linear time - void operator=(const List<T>& lst); + void operator=(const List<T>& list); //- Assignment to SLList operator. Takes linear time - void operator=(const SLList<T>& lst); + void operator=(const SLList<T>& list); //- Assignment to UIndirectList operator. Takes linear time - void operator=(const UIndirectList<T>& lst); + void operator=(const UIndirectList<T>& list); //- Assignment to BiIndirectList operator. Takes linear time - void operator=(const BiIndirectList<T>& lst); + void operator=(const BiIndirectList<T>& list); //- Assignment to an initializer list - void operator=(std::initializer_list<T> lst); + void operator=(std::initializer_list<T> 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<T>&& lst); + void operator=(List<T>&& list); //- Move assignment. Takes constant time. template<int SizeMin> - void operator=(DynamicList<T, SizeMin>&& lst); + void operator=(DynamicList<T, SizeMin>&& list); //- Move assignment. Takes constant time. - void operator=(SortableList<T>&& lst); + void operator=(SortableList<T>&& list); //- Move assignment. Takes constant time - void operator=(SLList<T>&& lst); + void operator=(SLList<T>&& list); // Istream Operator @@ -297,7 +301,7 @@ public: friend Istream& operator>> <T> ( Istream& is, - List<T>& L + List<T>& list ); @@ -305,6 +309,34 @@ public: //- No shallowCopy permitted void shallowCopy(const UList<T>&) = 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<class TypeT = T> + typename std::enable_if<std::is_same<TypeT, bool>::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 9579e3612d53440601a67d9c8dc165be240f1015..3936ebfe88cb5f8bcfd7ff83d4b8bb776a94c4e7 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<class T> -inline void Foam::List<T>::alloc() +inline void Foam::List<T>::doAlloc() { if (this->size_) { @@ -42,20 +42,20 @@ inline void Foam::List<T>::reAlloc(const label len) { clear(); this->size_ = len; - alloc(); + doAlloc(); } } template<class T> template<class List2> -inline void Foam::List<T>::copyList(const List2& lst) +inline void Foam::List<T>::copyList(const List2& list) { const label len = this->size_; for (label i=0; i<len; ++i) { - this->operator[](i) = lst[i]; + this->operator[](i) = list[i]; } } @@ -73,7 +73,7 @@ inline Foam::List<T>::List { if (this->size_) { - alloc(); + doAlloc(); InputIterator iter = begIter; for (label i = 0; i < len; ++i) @@ -121,17 +121,35 @@ inline void Foam::List<T>::clear() } +namespace Foam +{ + // Template specialization for bool. Fills with false + template<> + inline void List<bool>::resize(const label newSize) + { + this->resize(newSize, false); + } +} + + template<class T> inline void Foam::List<T>::resize(const label len) { - this->setSize(len); + this->doResize(len); +} + + +template<class T> +inline void Foam::List<T>::setSize(const label len) +{ + this->resize(len); } template<class T> -inline void Foam::List<T>::resize(const label len, const T& val) +inline void Foam::List<T>::setSize(const label len, const T& val) { - this->setSize(len, val); + this->resize(len, val); } @@ -142,7 +160,7 @@ inline T& Foam::List<T>::newElmt(const label i) if (i >= n) { - setSize(2*n); + resize(2*n); } return UList<T>::operator[](i); @@ -152,7 +170,7 @@ inline T& Foam::List<T>::newElmt(const label i) template<class T> inline void Foam::List<T>::append(const T& val) { - setSize(this->size() + 1, val); // copy element + resize(this->size() + 1, val); // copy element } @@ -160,44 +178,44 @@ template<class T> inline void Foam::List<T>::append(T&& val) { const label idx = this->size(); - setSize(idx + 1); + resize(idx + 1); this->operator[](idx) = std::move(val); // move assign element } template<class T> -inline void Foam::List<T>::append(const UList<T>& lst) +inline void Foam::List<T>::append(const UList<T>& 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; i<n; ++i) { - this->operator[](idx++) = lst[i]; // copy element + this->operator[](idx++) = list[i]; // copy element } } template<class T> -inline void Foam::List<T>::append(const UIndirectList<T>& lst) +inline void Foam::List<T>::append(const UIndirectList<T>& 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; i<n; ++i) { - this->operator[](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 55d11dfb52f575ef53816027ffe57b38f76d9dd8..d421a1f060b3b1b05d416980d41e28934ffcff4d 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<T>::List(Istream& is) template<class T> -Foam::Istream& Foam::operator>>(Istream& is, List<T>& lst) +Foam::Istream& Foam::operator>>(Istream& is, List<T>& list) { // Anull list - lst.setSize(0); + list.resize(0); is.fatalCheck(FUNCTION_NAME); @@ -55,7 +55,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& lst) // Compound: simply transfer contents if (firstToken.isCompound()) { - lst.transfer + list.transfer ( dynamicCast<token::Compound<List<T>>> ( @@ -72,8 +72,8 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& 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<T>& lst) { for (label i=0; i<len; ++i) { - is >> lst[i]; + is >> list[i]; is.fatalCheck ( - "operator>>(Istream&, List<T>&) : reading entry" + "operator>>(Istream&, List<T>&) : " + "reading entry" ); } } @@ -111,7 +112,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& lst) for (label i=0; i<len; ++i) { - lst[i] = element; // Copy the value + list[i] = element; // Copy the value } } } @@ -123,11 +124,12 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& lst) { // Non-empty, binary, contiguous - is.read(reinterpret_cast<char*>(lst.data()), len*sizeof(T)); + is.read(reinterpret_cast<char*>(list.data()), len*sizeof(T)); is.fatalCheck ( - "operator>>(Istream&, List<T>&) : reading the binary block" + "operator>>(Istream&, List<T>&) : " + "reading the binary block" ); } @@ -151,7 +153,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& lst) SLList<T> 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 7bba6587a4f55554c0476534bd5dd4a002685209..9170d1aea31b5fedda1a9f444e9614691f812652 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<T>& L ); + + + // Special Methods + + //- A bitSet::test() method for a list of bool + // + // \return The element value, or false for out-of-range access + template<class TypeT = T> + typename std::enable_if<std::is_same<TypeT, bool>::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<class TypeT = T> + typename std::enable_if<std::is_same<TypeT, bool>::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<class TypeT = T> + typename std::enable_if<std::is_same<TypeT, bool>::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 f17852673a62aefd4dfcdb64cfa0c92fb14d773e..663d8d571db21091fd01cfa9f258b68e6cac1273 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<T>::shallowCopy(const UList<T>& list) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -template<class T> -inline T& Foam::UList<T>::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<class T> +inline T& Foam::UList<T>::operator[](const label i) +{ + #ifdef FULLDEBUG + checkIndex(i); + #endif + return v_[i]; +} + + template<class T> inline const T& Foam::UList<T>::operator[](const label i) const {