Commit 69918f23 authored by Mark Olesen's avatar Mark Olesen
Browse files

consistency update

- OSspecific: chmod() -> chMod(), even although it's not used anywhere

- ListOps get subset() and inplaceSubset() templated on BoolListType

- added UList<bool>::operator[](..) const specialization.
  Returns false (actually pTraits<bool>::zero) for out-of-range elements.
  This lets us use List<bool> with lazy evaluation and no noticeable
  change in performance.

- use rcIndex() and fcIndex() wherever possible.
  Could check if branching or modulus is faster for fcIndex().

- UList and FixedList get 'const T* cdata() const' and 'T* data()' members.
  Similar to the STL front() and std::string::data() methods, they return a
  pointer to the first element without needing to write '&myList[0]', recast
  begin() or violate const-ness.
parent 603364e1
......@@ -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<bool> list4(4, true);
{
const List<bool>& 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;
......
......@@ -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]))
{
......
......@@ -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;
}
}
......
......@@ -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;
}
}
......
......@@ -156,7 +156,7 @@ class vtkPV3Foam
bool empty() const
{
return (size_ == 0);
return !size_;
}
void reset()
......
......@@ -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
......
......@@ -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;
}
......
......@@ -226,7 +226,7 @@ bool Foam::HashTable<T, Key, Hash>::set
const bool protect
)
{
if (tableSize_ == 0)
if (!tableSize_)
{
resize(2);
}
......@@ -556,7 +556,7 @@ void Foam::HashTable<T, Key, Hash>::operator=
}
// could be zero-sized from a previous transfer()
if (tableSize_ == 0)
if (!tableSize_)
{
resize(rhs.tableSize_);
}
......
......@@ -54,7 +54,7 @@ inline Foam::label Foam::HashTable<T, Key, Hash>::size() const
template<class T, class Key, class Hash>
inline bool Foam::HashTable<T, Key, Hash>::empty() const
{
return (nElmts_ == 0);
return !nElmts_;
}
......
......@@ -41,7 +41,7 @@ inline Foam::label Foam::StaticHashTable<T, Key, Hash>::size() const
template<class T, class Key, class Hash>
inline bool Foam::StaticHashTable<T, Key, Hash>::empty() const
{
return (nElmts_ == 0);
return !nElmts_;
}
......
......@@ -83,7 +83,7 @@ inline Foam::label Foam::DLListBase::size() const
inline bool Foam::DLListBase::empty() const
{
return (nElmts_ == 0);
return !nElmts_;
}
......
......@@ -73,7 +73,7 @@ inline Foam::label Foam::SLListBase::size() const
inline bool Foam::SLListBase::empty() const
{
return (nElmts_ == 0);
return !nElmts_;
}
......
......@@ -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.
......
......@@ -121,7 +121,7 @@ inline Foam::label Foam::FixedList<T, Size>::fcIndex(const label i) const
template<class T, Foam::label Size>
inline Foam::label Foam::FixedList<T, Size>::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<T, Size>::transfer(const FixedList<T, Size>& lst)
}
}
template<class T, Foam::label Size>
inline const T*
Foam::FixedList<T, Size>::cdata() const
{
return v_;
}
template<class T, Foam::label Size>
inline T*
Foam::FixedList<T, Size>::data()
{
return v_;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
// Return subscript-checked element access
// element access
template<class T, Foam::label Size>
inline T& Foam::FixedList<T, Size>::operator[](const label i)
{
......@@ -208,7 +225,7 @@ inline T& Foam::FixedList<T, Size>::operator[](const label i)
}
// Return subscript-checked const element access
// const element access
template<class T, Foam::label Size>
inline const T& Foam::FixedList<T, Size>::operator[](const label i) const
{
......
......@@ -119,7 +119,7 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList<T, Size>& L)
}
else
{
is.read(reinterpret_cast<char*>(L.begin()), Size*sizeof(T));
is.read(reinterpret_cast<char*>(L.data()), Size*sizeof(T));
is.fatalCheck
(
......@@ -231,7 +231,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList<T, Size>& L)
}
else
{
os.write(reinterpret_cast<const char*>(L.v_), Size*sizeof(T));
os.write(reinterpret_cast<const char*>(L.cdata()), Size*sizeof(T));
}
// Check state of IOstream
......
......@@ -117,7 +117,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
{
if (s)
{
is.read(reinterpret_cast<char*>(L.begin()), s*sizeof(T));
is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
is.fatalCheck
(
......
......@@ -90,17 +90,31 @@ void duplicateOrder(const UList<T>&, labelList& order);
template<class T>
void uniqueOrder(const UList<T>&, labelList& order);
//- Extract elements of List whose region is certain value.
// Use e.g. to extract all selected elements:
// subset<boolList, labelList>(selectedElems, true, lst);
//- Extract elements of List when select is a certain value.
// eg, to extract all selected elements:
// subset<bool, labelList>(selectedElems, true, lst);
template<class T, class ListType>
ListType subset(const UList<T>& regions, const T& region, const ListType&);
ListType subset(const UList<T>& 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<boolList, labelList>(selectedElems, true, lst);
//- Inplace extract elements of List when select is a certain value.
// eg, to extract all selected elements:
// inplaceSubset<bool, labelList>(selectedElems, true, lst);
template<class T, class ListType>
void inplaceSubset(const UList<T>& regions, const T& region, ListType&);
void inplaceSubset(const UList<T>& select, const T& value, ListType&);
//- Extract elements of List when select is true
// eg, to extract all selected elements:
// subset<boolList, labelList>(selectedElems, lst);
// Note a labelHashSet could also be used for the bool-list
template<class BoolListType, class ListType>
ListType subset(const BoolListType& select, const ListType&);
//- Inplace extract elements of List when select is true
// eg, to extract all selected elements:
// inplaceSubset<boolList, labelList>(selectedElems, lst);
// Note a labelHashSet could also be used for the bool-list
template<class BoolListType, class ListType>
void inplaceSubset(const BoolListType& select, ListType&);
//- Invert one-to-one map. Unmapped elements will be -1.
labelList invert(const label len, const UList<label>&);
......@@ -108,8 +122,9 @@ labelList invert(const label len, const UList<label>&);
//- Invert one-to-many map. Unmapped elements will be size 0.
labelListList invertOneToMany(const label len, const UList<label>&);
//- Invert many-to-many. Input and output types need to be inherited
// from List. E.g. faces to pointFaces.
//- Invert many-to-many.
// Input and output types need to be inherited from List.
// eg, faces to pointFaces.
template<class InList, class OutList>
void invertManyToMany(const label len, const UList<InList>&, List<OutList>&);
......
......@@ -243,16 +243,17 @@ void Foam::uniqueOrder
template<class T, class ListType>
ListType Foam::subset
(
const UList<T>& regions,
const T& region,
const UList<T>& select,
const T& value,
const ListType& lst
)
{
if (regions.size() < lst.size())
// select must at least cover the list range
if (select.size() < lst.size())
{
FatalErrorIn("subset(const UList<T>&, const T&, const ListType&)")
<< "Regions is of size " << regions.size()
<< "; list it is supposed to index is of size " << lst.size()
<< "select is of size " << select.size()
<< "; but it must index a list of size " << lst.size()
<< abort(FatalError);
}
......@@ -261,7 +262,7 @@ ListType Foam::subset
label nElem = 0;
forAll(lst, elemI)
{
if (regions[elemI] == region)
if (select[elemI] == value)
{
newLst[nElem++] = lst[elemI];
}
......@@ -275,23 +276,77 @@ ListType Foam::subset
template<class T, class ListType>
void Foam::inplaceSubset
(
const UList<T>& regions,
const T& region,
const UList<T>& select,
const T& value,
ListType& lst
)
{
if (regions.size() < lst.size())
// select must at least cover the list range
if (select.size() < lst.size())
{
FatalErrorIn("inplaceSubset(const UList<T>&, const T&, ListType&)")
<< "Regions is of size " << regions.size()
<< "; list it is supposed to index is of size " << lst.size()
<< "select is of size " << select.size()
<< "; but it must index a list of size " << lst.size()
<< abort(FatalError);
}
label nElem = 0;
forAll(lst, elemI)
{
if (regions[elemI] == region)
if (select[elemI] == value)
{
if (nElem != elemI)
{
lst[nElem] = lst[elemI];
}
++nElem;
}
}
lst.setSize(nElem);
}
template<class BoolListType, class ListType>
ListType Foam::subset
(
const BoolListType& select,
const ListType& lst
)
{
// select can have a different size
// eg, when it is a PackedBoolList or a labelHashSet
ListType newLst(lst.size());
label nElem = 0;
forAll(lst, elemI)
{
if (select[elemI])
{
newLst[nElem++] = lst[elemI];
}
}
newLst.setSize(nElem);
return newLst;
}
template<class BoolListType, class ListType>
void Foam::inplaceSubset
(
const BoolListType& select,
ListType& lst
)
{
// select can have a different size
// eg, when it is a PackedBoolList or a labelHashSet
label nElem = 0;
forAll(lst, elemI)
{
if (select[elemI])
{
if (nElem != elemI)
{
......
......@@ -185,7 +185,7 @@ public:
//- Number of entries.
inline label size() const;
//- Return true if the list is empty (i.e., size() == 0).
//- Return true if the list is empty (ie, size() is zero).
inline bool empty() const;
//- Get value at index I.
......
......@@ -158,7 +158,7 @@ public:
//- Return the number of elements in the PtrList
inline label size() const;
//- Return true if the PtrList is empty (i.e., if size() == 0).
//- Return true if the PtrList is empty (ie, size() is zero).
inline bool empty() const;
......