use NETGEN_CHECK_RANGE macro in array

This commit is contained in:
Christopher Lackner 2019-07-30 13:38:42 +02:00
parent 8ae2475085
commit 73fe929811

View File

@ -9,27 +9,14 @@
#include "archive.hpp" #include "archive.hpp"
#include "exception.hpp"
#include "localheap.hpp" #include "localheap.hpp"
#include "utils.hpp" #include "utils.hpp"
#ifdef DEBUG
#define CHECK_RANGE
#endif
namespace ngcore namespace ngcore
{ {
using std::ostream; using std::ostream;
/**
Exception thrown by array range check.
Only thrown when compiled with RANGE_CHECK
*/
class ArrayRangeException : public Exception
{
public:
ArrayRangeException () : Exception("ArrayRangeException\n") { ; }
};
template <typename ... ARGS> class Tuple template <typename ... ARGS> class Tuple
{ {
public: public:
@ -392,7 +379,7 @@ namespace ngcore
Array represented by size and data-pointer. Array represented by size and data-pointer.
No memory allocation and deallocation, must be provided by user. No memory allocation and deallocation, must be provided by user.
Helper functions for printing. Helper functions for printing.
Optional range check by macro CHECK_RANGE Optional range check by macro NETGEN_CHECK_RANGE
*/ */
template <class T> template <class T>
class FlatArray : public BaseArrayObject<FlatArray<T> > class FlatArray : public BaseArrayObject<FlatArray<T> >
@ -485,13 +472,10 @@ namespace ngcore
return *this; return *this;
} }
/// Access array. range check by macro CHECK_RANGE /// Access array. range check by macro NETGEN_CHECK_RANGE
NETGEN_INLINE T & operator[] (size_t i) const NETGEN_INLINE T & operator[] (size_t i) const
{ {
#ifdef CHECK_RANGE NETGEN_CHECK_RANGE(i,0,size-1);
if (i < 0 || i >= size)
throw RangeException ("FlatArray::operator[]", i, 0, size-1);
#endif
return data[i]; return data[i];
} }
@ -509,13 +493,10 @@ namespace ngcore
// { return CArray<T> (data+pos); } // { return CArray<T> (data+pos); }
NETGEN_INLINE T * operator+ (size_t pos) const { return data+pos; } NETGEN_INLINE T * operator+ (size_t pos) const { return data+pos; }
/// access last element. check by macro CHECK_RANGE /// access last element. check by macro NETGEN_CHECK_RANGE
T & Last () const T & Last () const
{ {
#ifdef CHECK_RANGE NETGEN_CHECK_RANGE(0,size-1,size-1);
if (!size)
throw Exception ("Array should not be empty");
#endif
return data[size-1]; return data[size-1];
} }
@ -857,10 +838,7 @@ namespace ngcore
/// Delete element i. Move last element to position i. /// Delete element i. Move last element to position i.
NETGEN_INLINE void DeleteElement (size_t i) NETGEN_INLINE void DeleteElement (size_t i)
{ {
#ifdef CHECK_RANGE NETGEN_CHECK_RANGE(i,0,size-1);
// RangeCheck (i);
#endif
data[i] = std::move(data[size-1]); data[i] = std::move(data[size-1]);
size--; size--;
} }
@ -878,9 +856,7 @@ namespace ngcore
/// Delete last element. /// Delete last element.
NETGEN_INLINE void DeleteLast () NETGEN_INLINE void DeleteLast ()
{ {
#ifdef CHECK_RANGE NETGEN_CHECK_RANGE(0,size-1,size-1);
// CheckNonEmpty();
#endif
size--; size--;
} }