diff --git a/applications/test/PackedList/PackedListTest.C b/applications/test/PackedList/PackedListTest.C index a8488eb183..41796562ba 100644 --- a/applications/test/PackedList/PackedListTest.C +++ b/applications/test/PackedList/PackedListTest.C @@ -77,14 +77,22 @@ int main(int argc, char *argv[]) const PackedList<3>& constLst = list1; Info<< "\ntest operator[] const with out-of-range index\n"; constLst.print(Info); - if (!constLst[20]) + if (constLst[20]) + { + Info<< "[20] is true (unexpected)\n"; + } + else { Info<< "[20] is false (expected) list size should be unchanged (const)\n"; } constLst.print(Info); Info<< "\ntest operator[] non-const with out-of-range index\n"; - if (!list1[20]) + if (list1[20]) + { + Info<< "[20] is true (unexpected)\n"; + } + else { Info<< "[20] is false (expected) but list was resized?? (non-const)\n"; } @@ -268,6 +276,23 @@ int main(int argc, char *argv[]) Info<< "removed final value: " << list3.remove() << endl; list3.print(Info); + + List list4(4, true); + { + const List& constLst = list4; + Info<< "\ntest operator[] const with out-of-range index\n"; + Info<< constLst << endl; + if (constLst[20]) + { + Info<< "[20] is true (unexpected)\n"; + } + else + { + Info<< "[20] is false (expected) list size should be unchanged (const)\n"; + } + Info<< constLst << endl; + } + Info<< "\n\nDone.\n"; return 0; diff --git a/applications/test/primitivePatch/testPrimitivePatch.C b/applications/test/primitivePatch/testPrimitivePatch.C index 7130e90628..e19b7d0512 100644 --- a/applications/test/primitivePatch/testPrimitivePatch.C +++ b/applications/test/primitivePatch/testPrimitivePatch.C @@ -69,7 +69,7 @@ void checkFaceEdges forAll(f, fp) { - label fp1 = (fp + 1) % f.size(); + label fp1 = f.fcIndex(fp); if (edges[myEdges[fp]] != edge(f[fp], f[fp1])) { diff --git a/applications/utilities/mesh/advanced/collapseEdges/collapseEdges.C b/applications/utilities/mesh/advanced/collapseEdges/collapseEdges.C index c92418dc80..1dc59edb7c 100644 --- a/applications/utilities/mesh/advanced/collapseEdges/collapseEdges.C +++ b/applications/utilities/mesh/advanced/collapseEdges/collapseEdges.C @@ -74,20 +74,17 @@ labelList getSortedEdges const edge& e = edges[edgeI]; label fp = findIndex(f, e[0]); - - label fp1 = (fp+1) % f.size(); + label fp1 = f.fcIndex(fp); if (f[fp1] == e[1]) { - // Edgei in fp-fp1 order + // EdgeI between fp -> fp1 faceEdges[fp] = edgeI; } else { - // Edgei between fp-1 and fp - label fpMin1 = (fp == 0 ? f.size()-1 : fp-1); - - faceEdges[fpMin1] = edgeI; + // EdgeI between fp-1 -> fp + faceEdges[f.rcIndex(fp)] = edgeI; } } diff --git a/applications/utilities/mesh/advanced/modifyMesh/cellSplitter.C b/applications/utilities/mesh/advanced/modifyMesh/cellSplitter.C index ce532b979f..3ae39131a1 100644 --- a/applications/utilities/mesh/advanced/modifyMesh/cellSplitter.C +++ b/applications/utilities/mesh/advanced/modifyMesh/cellSplitter.C @@ -211,7 +211,7 @@ void Foam::cellSplitter::setRefinement // Add other pyramids for (label i = 1; i < cFaces.size(); i++) - { + { label addedCellI = meshMod.setAction ( @@ -277,7 +277,7 @@ void Foam::cellSplitter::setRefinement label index = findIndex(f0, e[0]); - bool edgeInFaceOrder = (f0[(index+1) % f0.size()] == e[1]); + bool edgeInFaceOrder = (f0[f0.fcIndex(index)] == e[1]); // Check if cellI is the face owner @@ -323,7 +323,7 @@ void Foam::cellSplitter::setRefinement label index = findIndex(f1, e[0]); - bool edgeInFaceOrder = (f1[(index+1) % f1.size()] == e[1]); + bool edgeInFaceOrder = (f1[f1.fcIndex(index)] == e[1]); // Check if cellI is the face owner @@ -362,7 +362,7 @@ void Foam::cellSplitter::setRefinement } } } - + // // Update all existing faces for split owner or neighbour. @@ -441,7 +441,7 @@ void Foam::cellSplitter::setRefinement label patchID, zoneID, zoneFlip; getFaceInfo(faceI, patchID, zoneID, zoneFlip); - + meshMod.setAction ( polyModifyFace @@ -458,7 +458,7 @@ void Foam::cellSplitter::setRefinement ) ); } - + faceUpToDate[faceI] = true; } } diff --git a/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.H b/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.H index cb25ac5089..af0fb849a4 100644 --- a/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.H +++ b/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.H @@ -156,7 +156,7 @@ class vtkPV3Foam bool empty() const { - return (size_ == 0); + return !size_; } void reset() diff --git a/applications/utilities/surface/surfaceSplitNonManifolds/surfaceSplitNonManifolds.C b/applications/utilities/surface/surfaceSplitNonManifolds/surfaceSplitNonManifolds.C index 8968831e64..f7131d87db 100644 --- a/applications/utilities/surface/surfaceSplitNonManifolds/surfaceSplitNonManifolds.C +++ b/applications/utilities/surface/surfaceSplitNonManifolds/surfaceSplitNonManifolds.C @@ -351,7 +351,7 @@ label otherEdge return -1; } - + // Starting from startPoint on startEdge on startFace walk along border // and insert faces along the way. Walk keeps always one point or one edge @@ -461,18 +461,8 @@ label sharedFace label startIndex = findIndex(f, e.start()); - bool edgeOrder; - - if (f[(startIndex + 1) % f.size()] == e.end()) - { - // points in face in same order as edge - edgeOrder = true; - } - else - { - // points in face in reverse order as edge - edgeOrder = false; - } + // points in face in same order as edge + bool edgeOrder = (f[f.fcIndex(startIndex)] == e.end()); // Get faces using edge in sorted order. (sorted such that walking // around them in anti-clockwise order corresponds to edge vector @@ -485,25 +475,18 @@ label sharedFace if (edgeOrder) { // Get face before firstFaceI - if (faceIndex == 0) - { - return eFaces[eFaces.size() - 1]; - } - else - { - return eFaces[faceIndex - 1]; - } + return eFaces[eFaces.rcIndex(faceIndex)]; } else { // Get face after firstFaceI - return eFaces[(faceIndex+1) % eFaces.size()]; + return eFaces[eFaces.fcIndex(faceIndex)]; } } // Calculate (inward pointing) normals on edges shared by faces in faceToEdge and -// averages them to pointNormals. +// averages them to pointNormals. void calcPointVecs ( const triSurface& surf, @@ -602,7 +585,7 @@ void calcPointVecs } scalar magMidVec = mag(midVec); - + if (magMidVec > SMALL) { midVec /= magMidVec; @@ -925,7 +908,7 @@ int main(int argc, char *argv[]) newPoints[newPointI] = newPoints[pointI] + 0.1 * minLen * n; } } - + // // Renumber all faces in connectedFaces diff --git a/src/OSspecific/Unix/Unix.C b/src/OSspecific/Unix/Unix.C index f243c48ed4..9378a21c7c 100644 --- a/src/OSspecific/Unix/Unix.C +++ b/src/OSspecific/Unix/Unix.C @@ -432,7 +432,7 @@ bool Foam::mkDir(const fileName& pathName, mode_t mode) // Set the file mode -bool Foam::chmod(const fileName& name, const mode_t m) +bool Foam::chMod(const fileName& name, const mode_t m) { return ::chmod(name.c_str(), m) == 0; } diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index ae9bcc2718..3ed474b32d 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C @@ -226,7 +226,7 @@ bool Foam::HashTable::set const bool protect ) { - if (tableSize_ == 0) + if (!tableSize_) { resize(2); } @@ -556,7 +556,7 @@ void Foam::HashTable::operator= } // could be zero-sized from a previous transfer() - if (tableSize_ == 0) + if (!tableSize_) { resize(rhs.tableSize_); } diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H index 79ff374145..aea5f3043a 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableI.H @@ -54,7 +54,7 @@ inline Foam::label Foam::HashTable::size() const template inline bool Foam::HashTable::empty() const { - return (nElmts_ == 0); + return !nElmts_; } diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H index 329437a07a..5cd380357a 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTableI.H @@ -41,7 +41,7 @@ inline Foam::label Foam::StaticHashTable::size() const template inline bool Foam::StaticHashTable::empty() const { - return (nElmts_ == 0); + return !nElmts_; } diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H index 9b80941bf9..bcc20289ca 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBaseI.H @@ -83,7 +83,7 @@ inline Foam::label Foam::DLListBase::size() const inline bool Foam::DLListBase::empty() const { - return (nElmts_ == 0); + return !nElmts_; } diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H index de34257d34..339963da5e 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBaseI.H @@ -73,7 +73,7 @@ inline Foam::label Foam::SLListBase::size() const inline bool Foam::SLListBase::empty() const { - return (nElmts_ == 0); + return !nElmts_; } diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 2e3ddf408b..0079f13a02 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -135,10 +135,21 @@ public: inline label fcIndex(const label i) const; //- Return the reverse circular index, i.e. the previous index - // which returns to the last at the begining of the list + // which returns to the last at the beginning of the list inline label rcIndex(const label i) const; + //- Return a const pointer to the first data element, + // similar to the STL front() method and the string::data() method + // This can be used (with caution) when interfacing with C code. + inline const T* cdata() const; + + //- Return a pointer to the first data element, + // similar to the STL front() method and the string::data() method + // This can be used (with caution) when interfacing with C code. + inline T* data(); + + // Check //- Check start is within valid range (0 ... size-1). @@ -174,10 +185,10 @@ public: // Member operators - //- Return subscript-checked element of FixedList. + //- Return element of FixedList. inline T& operator[](const label); - //- Return subscript-checked element of constant FixedList. + //- Return element of constant FixedList. inline const T& operator[](const label) const; //- Assignment from array operator. Takes linear time. @@ -282,7 +293,7 @@ public: //- Return size of the largest possible FixedList. inline label max_size() const; - //- Return true if the FixedList is empty (i.e., if size() == 0). + //- Return true if the FixedList is empty (ie, size() is zero). inline bool empty() const; //- Swap two FixedLists of the same type in constant time. diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index d618cb6e38..a800b04b1d 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -121,7 +121,7 @@ inline Foam::label Foam::FixedList::fcIndex(const label i) const template inline Foam::label Foam::FixedList::rcIndex(const label i) const { - return (i == 0 ? Size-1 : i-1); + return (i ? i-1 : Size-1); } @@ -195,9 +195,26 @@ inline void Foam::FixedList::transfer(const FixedList& lst) } } + +template +inline const T* +Foam::FixedList::cdata() const +{ + return v_; +} + + +template +inline T* +Foam::FixedList::data() +{ + return v_; +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -// Return subscript-checked element access +// element access template inline T& Foam::FixedList::operator[](const label i) { @@ -208,7 +225,7 @@ inline T& Foam::FixedList::operator[](const label i) } -// Return subscript-checked const element access +// const element access template inline const T& Foam::FixedList::operator[](const label i) const { diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C index b76bf3be24..49caad2a0a 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C @@ -119,7 +119,7 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList& L) } else { - is.read(reinterpret_cast(L.begin()), Size*sizeof(T)); + is.read(reinterpret_cast(L.data()), Size*sizeof(T)); is.fatalCheck ( @@ -231,7 +231,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList& L) } else { - os.write(reinterpret_cast(L.v_), Size*sizeof(T)); + os.write(reinterpret_cast(L.cdata()), Size*sizeof(T)); } // Check state of IOstream diff --git a/src/OpenFOAM/containers/Lists/List/ListIO.C b/src/OpenFOAM/containers/Lists/List/ListIO.C index b7cb86c4ee..7cfdfb0397 100644 --- a/src/OpenFOAM/containers/Lists/List/ListIO.C +++ b/src/OpenFOAM/containers/Lists/List/ListIO.C @@ -117,7 +117,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List& L) { if (s) { - is.read(reinterpret_cast(L.begin()), s*sizeof(T)); + is.read(reinterpret_cast(L.data()), s*sizeof(T)); is.fatalCheck ( diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H index b7e90d53d1..7de9239d04 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H @@ -90,17 +90,31 @@ void duplicateOrder(const UList&, labelList& order); template void uniqueOrder(const UList&, labelList& order); -//- Extract elements of List whose region is certain value. -// Use e.g. to extract all selected elements: -// subset(selectedElems, true, lst); +//- Extract elements of List when select is a certain value. +// eg, to extract all selected elements: +// subset(selectedElems, true, lst); template -ListType subset(const UList& regions, const T& region, const ListType&); +ListType subset(const UList& select, const T& value, const ListType&); -//- Inplace extract elements of List whose region is certain value. Use e.g. -// to extract all selected elements: -// inplaceSubset(selectedElems, true, lst); +//- Inplace extract elements of List when select is a certain value. +// eg, to extract all selected elements: +// inplaceSubset(selectedElems, true, lst); template -void inplaceSubset(const UList& regions, const T& region, ListType&); +void inplaceSubset(const UList& select, const T& value, ListType&); + +//- Extract elements of List when select is true +// eg, to extract all selected elements: +// subset(selectedElems, lst); +// Note a labelHashSet could also be used for the bool-list +template +ListType subset(const BoolListType& select, const ListType&); + +//- Inplace extract elements of List when select is true +// eg, to extract all selected elements: +// inplaceSubset(selectedElems, lst); +// Note a labelHashSet could also be used for the bool-list +template +void inplaceSubset(const BoolListType& select, ListType&); //- Invert one-to-one map. Unmapped elements will be -1. labelList invert(const label len, const UList