Commit a843054b authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: more generous range check for UList<bool>::operator[] specialization

- now also handles negative indices without issue.
  This increases its robustness for predicate type of use.
parent 799924e7
......@@ -240,13 +240,13 @@ public:
// Check
//- Check start is within valid range (0 ... size-1)
//- Check start is within valid range [0,size)
inline void checkStart(const label start) const;
//- Check size is within valid range (0 ... size)
//- Check size is identical to Size template parameter
inline void checkSize(const label size) const;
//- Check index i is within valid range (0 ... size-1)
//- Check index is within valid range [0,size)
inline void checkIndex(const label i) const;
......
......@@ -195,8 +195,9 @@ inline void Foam::FixedList<T, Size>::checkStart(const label start) const
{
if (start < 0 || (start && unsigned(start) >= Size))
{
// Note: always accept start=0, even for zero-sized lists
FatalErrorInFunction
<< "start " << start << " out of range 0 ... " << (Size-1)
<< "start " << start << " out of range [0," << Size << ")"
<< abort(FatalError);
}
}
......@@ -220,7 +221,7 @@ inline void Foam::FixedList<T, Size>::checkIndex(const label i) const
if (i < 0 || unsigned(i) >= Size)
{
FatalErrorInFunction
<< "index " << i << " out of range 0 ... " << (Size-1)
<< "index " << i << " out of range [0," << Size << ")"
<< abort(FatalError);
}
}
......
......@@ -275,13 +275,13 @@ public:
// Check
//- Check start is within valid range (0 ... size-1)
//- Check start is within valid range [0,size)
inline void checkStart(const label start) const;
//- Check size is within valid range (0 ... size)
//- Check size is within valid range [0,size]
inline void checkSize(const label size) const;
//- Check index i is within valid range (0 ... size-1)
//- Check index is within valid range [0,size)
inline void checkIndex(const label i) const;
......
......@@ -101,8 +101,9 @@ inline void Foam::UList<T>::checkStart(const label start) const
{
if (start < 0 || (start && start >= size_))
{
// Note: accept start=0 for zero-sized lists
FatalErrorInFunction
<< "start " << start << " out of range 0 ... " << max(size_-1, 0)
<< "start " << start << " out of range [0," << size_ << ")"
<< abort(FatalError);
}
}
......@@ -114,7 +115,7 @@ inline void Foam::UList<T>::checkSize(const label size) const
if (size < 0 || size > size_)
{
FatalErrorInFunction
<< "size " << size << " out of range 0 ... " << size_
<< "size " << size << " out of range [0," << size_ << "]"
<< abort(FatalError);
}
}
......@@ -129,10 +130,10 @@ inline void Foam::UList<T>::checkIndex(const label i) const
<< "attempt to access element " << i << " from zero sized list"
<< abort(FatalError);
}
else if (i<0 || i>=size_)
else if (i < 0 || i >= size_)
{
FatalErrorInFunction
<< "index " << i << " out of range 0 ... " << size_-1
<< "index " << i << " out of range [0," << size_ << ")"
<< abort(FatalError);
}
}
......@@ -206,14 +207,15 @@ inline T& Foam::UList<T>::operator[](const label i)
return v_[i];
}
namespace Foam
{
// Template specialization for bool
template<>
inline const bool& Foam::UList<bool>::operator[](const label i) const
{
// lazy evaluation - return false for out-of-range
if (i < size_)
// Lazy evaluation - return false for out-of-range
if (i >= 0 && i < size_)
{
return v_[i];
}
......@@ -222,6 +224,7 @@ namespace Foam
}
}
template<class T>
inline const T& Foam::UList<T>::operator[](const label i) const
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment