unified Index class

This commit is contained in:
Joachim Schoeberl 2024-12-31 12:54:25 +01:00
parent b7b168e265
commit 9ab086f819
2 changed files with 38 additions and 222 deletions

View File

@ -198,16 +198,18 @@ namespace netgen
constexpr Index (t_invalid inv) : i(long(BASE)-1) { ; }
// TIndex & operator= (const TIndex &ai) { i = ai.i; return *this; }
// private:
constexpr operator const int& () const { return i; }
explicit constexpr operator int& () { return i; }
constexpr operator T () const { return i; }
explicit constexpr operator T& () { return i; }
public:
constexpr operator TIndex() const { return TIndex(i); }
constexpr operator TIndex&() { return static_cast<TIndex&>(*this); }
TIndex operator++ (int) { TIndex hi(*this); i++; return hi; }
TIndex operator-- (int) { TIndex hi(*this); i--; return hi; }
TIndex & operator++ () { i++; return *this; }
TIndex operator-- () { i--; return *this; }
TIndex operator+= (int add) { i += add; return *this; }
constexpr TIndex & operator+= (T add) & { i += add; return *this; }
constexpr TIndex operator+= (T add) && { i += add; return *this; }
void Invalidate() { i = long(TIndex::BASE)-1; }
bool IsValid() const { return i+1 != TIndex::BASE; }
// operator bool() const { return IsValid(); }
@ -216,19 +218,17 @@ namespace netgen
};
/*
template <typename T, typename TIndex, int Base>
auto operator+ (Index<T,TIndex,Base> pi, int i) -> TIndex { return TIndex(pi.i+i); }
// constexpr auto operator+ (Index<T,TIndex,Base> ind, int i) { return TIndex(T(ind)+i); }
constexpr auto operator+ (Index<T,TIndex,Base> ind, int i) { return TIndex(ind)+=i; }
template <typename T, typename TIndex, int Base>
inline TIndex operator+ (Index<T,TIndex,Base> pi, size_t i) { return TIndex(pi.i+i); }
constexpr TIndex operator+ (Index<T,TIndex,Base> pi, size_t i) { return TIndex(pi.i+i); }
template <typename T, typename TIndex, int Base>
inline TIndex operator+ (int i, Index<T,TIndex,Base> pi) { return TIndex(pi.i+i); }
constexpr TIndex operator+ (int i, Index<T,TIndex,Base> pi) { return TIndex(pi.i+i); }
template <typename T, typename TIndex, int Base>
inline TIndex operator+ (size_t i, Index<T,TIndex,Base> pi) { return TIndex(pi.i+i); }
template <typename T, typename TIndex, int Base>
constexpr inline auto operator- (Index<T,TIndex,Base> pi, int i) -> TIndex { return TIndex(pi.i-i); }
template <typename T, typename TIndex, int Base>
inline int operator- (Index<T,TIndex,Base> pa, Index<T,TIndex,Base> pb) { return pa.i-pb.i; }
template <typename T, typename TIndex, int Base>
@ -243,143 +243,15 @@ namespace netgen
inline bool operator== (Index<T,TIndex,Base> a, Index<T,TIndex,Base> b) { return a.i == b.i; }
template <typename T, typename TIndex, int Base>
inline bool operator!= (Index<T,TIndex,Base> a, Index<T,TIndex,Base> b) { return a.i != b.i; }
*/
/*
class PointIndex
{
int i;
public:
class t_invalid { public: constexpr t_invalid() = default; };
static constexpr t_invalid INVALID{};
PointIndex () = default;
PointIndex (const PointIndex&) = default;
PointIndex (PointIndex &&) = default;
PointIndex & operator= (const PointIndex&) = default;
PointIndex & operator= (PointIndex&&) = default;
constexpr PointIndex (int ai) : i(ai)
{
#ifdef DEBUG
if (ai < PointIndex::BASE)
cout << "illegal PointIndex, use PointIndex::INVALID instead" << endl;
// throw Exception("illegal PointIndex, use PointIndex::INVALID instead");
#endif
}
constexpr PointIndex (t_invalid inv) : i(PointIndex::BASE-1) { ; }
// PointIndex & operator= (const PointIndex &ai) { i = ai.i; return *this; }
constexpr operator const int& () const { return i; }
explicit constexpr operator int& () { return i; }
PointIndex operator++ (int) { PointIndex hi(*this); i++; return hi; }
PointIndex operator-- (int) { PointIndex hi(*this); i--; return hi; }
PointIndex & operator++ () { i++; return *this; }
PointIndex operator-- () { i--; return *this; }
PointIndex operator+= (int add) { i += add; return *this; }
void Invalidate() { i = PointIndex::BASE-1; }
bool IsValid() const { return i != PointIndex::BASE-1; }
#ifdef BASE0
static constexpr size_t BASE = 0;
#else
static constexpr size_t BASE = 1;
#endif
void DoArchive (Archive & ar) { ar & i; }
}
*/
// #define BASE0
/*
class PointIndex
{
int i;
public:
class t_invalid { public: constexpr t_invalid() = default; };
static constexpr t_invalid INVALID{};
PointIndex () = default;
PointIndex (const PointIndex&) = default;
PointIndex (PointIndex &&) = default;
PointIndex & operator= (const PointIndex&) = default;
PointIndex & operator= (PointIndex&&) = default;
// private:
constexpr PointIndex (int ai) : i(ai)
{
#ifdef DEBUG
if (ai < PointIndex::BASE)
cout << "illegal PointIndex, use PointIndex::INVALID instead" << endl;
// throw Exception("illegal PointIndex, use PointIndex::INVALID instead");
#endif
}
friend constexpr netgen::PointIndex ngcore::IndexBASE<netgen::PointIndex> ();
template <int N> friend class PointIndices;
friend constexpr PointIndex operator+ (PointIndex, int);
friend constexpr PointIndex operator+ (PointIndex, size_t);
friend constexpr PointIndex operator+ (int, PointIndex);
friend constexpr PointIndex operator+ (size_t, PointIndex);
friend constexpr PointIndex operator- (PointIndex, int);
friend constexpr int operator- (PointIndex, PointIndex);
friend bool operator< (PointIndex a, PointIndex b);
friend bool operator> (PointIndex a, PointIndex b);
friend bool operator>= (PointIndex a, PointIndex b);
friend bool operator<= (PointIndex a, PointIndex b);
friend constexpr bool operator== (PointIndex a, PointIndex b);
friend constexpr bool operator!= (PointIndex a, PointIndex b);
public:
constexpr PointIndex (t_invalid inv) : i(long(PointIndex::BASE)-1) { ; }
// PointIndex & operator= (const PointIndex &ai) { i = ai.i; return *this; }
// private:
constexpr operator const int& () const { return i; }
explicit constexpr operator int& () { return i; }
public:
PointIndex operator++ (int) { PointIndex hi(*this); i++; return hi; }
PointIndex operator-- (int) { PointIndex hi(*this); i--; return hi; }
PointIndex & operator++ () { i++; return *this; }
PointIndex operator-- () { i--; return *this; }
PointIndex operator+= (int add) { i += add; return *this; }
void Invalidate() { i = long(PointIndex::BASE)-1; }
bool IsValid() const { return i+1 != PointIndex::BASE; }
// operator bool() const { return IsValid(); }
#ifdef BASE0
static constexpr size_t BASE = 0;
#else
static constexpr size_t BASE = 1;
#endif
void DoArchive (Archive & ar) { ar & i; }
};
*/
class PointIndex : public Index<int,PointIndex,1>
{
public:
using Index::Index;
};
constexpr inline PointIndex operator+ (PointIndex pi, int i) { return PointIndex(pi.i+i); }
constexpr inline PointIndex operator+ (PointIndex pi, size_t i) { return PointIndex(pi.i+i); }
constexpr inline PointIndex operator+ (int i, PointIndex pi) { return PointIndex(pi.i+i); }
constexpr inline PointIndex operator+ (size_t i, PointIndex pi) { return PointIndex(pi.i+i); }
constexpr inline PointIndex operator- (PointIndex pi, int i) { return PointIndex(pi.i-i); }
constexpr inline int operator- (PointIndex pa, PointIndex pb) { return pa.i-pb.i; }
inline bool operator< (PointIndex a, PointIndex b) { return a.i-b.i < 0; }
inline bool operator> (PointIndex a, PointIndex b) { return a.i-b.i > 0; }
inline bool operator>= (PointIndex a, PointIndex b) { return a.i-b.i >= 0; }
inline bool operator<= (PointIndex a, PointIndex b) { return a.i-b.i <= 0; }
inline constexpr bool operator== (PointIndex a, PointIndex b) { return a.i == b.i; }
inline constexpr bool operator!= (PointIndex a, PointIndex b) { return a.i != b.i; }
}
namespace ngcore
@ -396,16 +268,15 @@ namespace netgen
{
// int i; ist >> i; pi = PointIndex(i); return ist;
int i; ist >> i;
// pi = PointIndex(i);
pi = IndexBASE<PointIndex>()+i-1;
return ist;
}
inline ostream & operator<< (ostream & ost, const PointIndex & pi)
{
// return (ost << int(pi));
int intpi = pi - IndexBASE<PointIndex>() + 1;
return (ost << intpi);
// return (ost << int(pi));
}
template <int N> class PointIndices;
@ -571,25 +442,12 @@ namespace std
namespace netgen
{
class ElementIndex
class ElementIndex : public Index<int,ElementIndex,0>
{
int i;
public:
ElementIndex () = default;
constexpr /* explicit */ ElementIndex (int ai) : i(ai) { ; }
ElementIndex & operator= (const ElementIndex & ai) { i = ai.i; return *this; }
ElementIndex & operator= (int ai) { i = ai; return *this; }
constexpr /* explicit */ operator int () const { return i; }
ElementIndex operator++ (int) { return ElementIndex(i++); }
ElementIndex operator-- (int) { return ElementIndex(i--); }
ElementIndex & operator++ () { ++i; return *this; }
ElementIndex & operator-- () { --i; return *this; }
int operator- (ElementIndex ei2) const { return i-ei2.i; }
friend constexpr ElementIndex ngcore::IndexBASE<ElementIndex>();
using Index::Index;
};
inline istream & operator>> (istream & ist, ElementIndex & pi)
{
int i; ist >> i; pi = i; return ist;
@ -600,64 +458,31 @@ namespace netgen
return (ost << ei-IndexBASE<ElementIndex>());
}
inline ElementIndex operator+ (ElementIndex ei, int i) { return ElementIndex { int(ei) + i }; }
inline ElementIndex operator+ (size_t s, ElementIndex ei) { return ElementIndex(int(ei) + s); }
inline ElementIndex operator+ (ElementIndex ei, size_t s) { return ElementIndex(int(ei) + s); }
inline bool operator== (ElementIndex ei1, ElementIndex ei2) { return int(ei1) == int(ei2); };
inline bool operator!= (ElementIndex ei1, ElementIndex ei2) { return int(ei1) != int(ei2); };
inline bool operator< (ElementIndex ei1, ElementIndex ei2) { return int(ei1) < int(ei2); };
inline bool operator> (ElementIndex ei1, ElementIndex ei2) { return int(ei1) > int(ei2); };
inline bool operator>= (ElementIndex ei1, ElementIndex ei2) { return int(ei1) >= int(ei2); };
inline bool operator<= (ElementIndex ei1, ElementIndex ei2) { return int(ei1) <= int(ei2); };
// these should not be needed:
inline bool operator== (ElementIndex ei1, int ei2) { return int(ei1) == int(ei2); };
inline bool operator< (size_t s, ElementIndex ei2) { return int(s) < int(ei2); };
inline bool operator< (ElementIndex ei1, size_t s) { return int(ei1) < int(s); }; // should not need
inline bool operator< (ElementIndex ei1, int s) { return int(ei1) < int(s); }; // should not need
inline bool operator>= (size_t s, ElementIndex ei2) { return int(s) >= int(ei2); };
class SurfaceElementIndex
// these should not be needed soon
inline bool operator== (Index<int,ElementIndex,0> ei1, int ei2) { return int(ei1) == int(ei2); };
inline bool operator< (size_t s, Index<int,ElementIndex,0> ei2) { return int(s) < int(ei2); };
inline bool operator< ( Index<int,ElementIndex,0> ei1, size_t s) { return int(ei1) < int(s); }; // should not need
inline bool operator< ( Index<int,ElementIndex,0> ei1, int s) { return int(ei1) < int(s); }; // should not need
inline bool operator>= (size_t s, Index<int,ElementIndex,0> ei2) { return int(s) >= int(ei2); };
class SurfaceElementIndex : public Index<int,SurfaceElementIndex,0>
{
int i;
public:
SurfaceElementIndex () = default;
constexpr SurfaceElementIndex (int ai) : i(ai) { ; }
/*
SurfaceElementIndex & operator= (const SurfaceElementIndex & ai)
{ i = ai.i; return *this; }
*/
SurfaceElementIndex & operator= (const SurfaceElementIndex & ai) = default;
SurfaceElementIndex & operator= (int ai) { i = ai; return *this; }
constexpr operator int () const { return i; }
SurfaceElementIndex operator++ (int) { SurfaceElementIndex hi(*this); i++; return hi; }
SurfaceElementIndex operator-- (int) { SurfaceElementIndex hi(*this); i--; return hi; }
SurfaceElementIndex & operator++ () { ++i; return *this; }
SurfaceElementIndex & operator-- () { --i; return *this; }
SurfaceElementIndex & operator+= (int inc) { i+=inc; return *this; }
void DoArchive (Archive & ar) { ar & i; }
using Index::Index;
};
inline SurfaceElementIndex operator+ (SurfaceElementIndex ei, int i) { return SurfaceElementIndex { int(ei) + i }; }
inline SurfaceElementIndex operator+ (size_t s, SurfaceElementIndex ei) { return SurfaceElementIndex(int(ei) + s); }
inline SurfaceElementIndex operator+ (SurfaceElementIndex ei, size_t s) { return SurfaceElementIndex(int(ei) + s); }
inline bool operator== (SurfaceElementIndex ei1, SurfaceElementIndex ei2) { return int(ei1) == int(ei2); };
inline bool operator!= (SurfaceElementIndex ei1, SurfaceElementIndex ei2) { return int(ei1) != int(ei2); };
inline bool operator< (SurfaceElementIndex ei1, SurfaceElementIndex ei2) { return int(ei1) < int(ei2); };
inline bool operator> (SurfaceElementIndex ei1, SurfaceElementIndex ei2) { return int(ei1) > int(ei2); };
inline bool operator>= (SurfaceElementIndex ei1, SurfaceElementIndex ei2) { return int(ei1) >= int(ei2); };
inline bool operator<= (SurfaceElementIndex ei1, SurfaceElementIndex ei2) { return int(ei1) <= int(ei2); };
// these should not be needed:
inline bool operator== (SurfaceElementIndex ei1, int ei2) { return int(ei1) == int(ei2); };
inline bool operator== (int ei2, SurfaceElementIndex ei1) { return int(ei1) == int(ei2); };
inline bool operator!= (SurfaceElementIndex ei1, int ei2) { return int(ei1) != int(ei2); };
inline bool operator< (size_t s, SurfaceElementIndex ei2) { return int(s) < int(ei2); };
inline bool operator< (SurfaceElementIndex ei1, size_t s) { return int(ei1) < int(s); }; // should not need
inline bool operator< (SurfaceElementIndex ei1, int s) { return int(ei1) < int(s); }; // should not need
inline bool operator>= (size_t s, SurfaceElementIndex ei2) { return int(s) >= int(ei2); };
inline bool operator>= (SurfaceElementIndex ei1, int s) { return int(ei1) >= int(s); };
// these should not be needed soon
inline bool operator== (Index<int, SurfaceElementIndex,0> ei1, int ei2) { return int(ei1) == int(ei2); };
inline bool operator== (int ei2, Index<int, SurfaceElementIndex,0> ei1) { return int(ei1) == int(ei2); };
inline bool operator!= (Index<int, SurfaceElementIndex,0> ei1, int ei2) { return int(ei1) != int(ei2); };
inline bool operator< (size_t s, Index<int, SurfaceElementIndex,0> ei2) { return int(s) < int(ei2); };
inline bool operator< (Index<int, SurfaceElementIndex,0> ei1, size_t s) { return int(ei1) < int(s); }; // should not need
inline bool operator< (Index<int, SurfaceElementIndex,0> ei1, int s) { return int(ei1) < int(s); }; // should not need
inline bool operator>= (size_t s, Index<int, SurfaceElementIndex,0> ei2) { return int(s) >= int(ei2); };
inline bool operator>= (Index<int, SurfaceElementIndex,0> ei1, int s) { return int(ei1) >= int(s); };
inline void SetInvalid (SurfaceElementIndex & id) { id = -1; }
@ -673,20 +498,11 @@ namespace netgen
return (ost << int(pi));
}
class SegmentIndex
class SegmentIndex : public Index<int,SegmentIndex,0>
{
int i;
public:
SegmentIndex () = default;
constexpr SegmentIndex (int ai) : i(ai) { ; }
SegmentIndex & operator= (const SegmentIndex & ai)
{ i = ai.i; return *this; }
SegmentIndex & operator= (int ai) { i = ai; return *this; }
constexpr operator int () const { return i; }
SegmentIndex& operator++ () { ++i; return *this; }
SegmentIndex& operator-- () { --i; return *this; }
SegmentIndex operator++ (int) { return i++; }
SegmentIndex operator-- (int) { return i--; }
using Index::Index;
};
inline void SetInvalid (SegmentIndex & id) { id = -1; }

View File

@ -268,7 +268,7 @@ DLL_HEADER void ExportNetgenMeshing(py::module &m)
.def(py::init<int>())
.def("__repr__", &ToString<PointIndex>)
.def("__str__", &ToString<PointIndex>)
.def_property_readonly("nr", &PointIndex::operator const int&)
.def_property_readonly("nr", &PointIndex::operator int)
.def("__eq__" , FunctionPointer( [](PointIndex &self, PointIndex &other)
{ return static_cast<int>(self)==static_cast<int>(other); }) )
.def("__hash__" , FunctionPointer( [](PointIndex &self ) { return static_cast<int>(self); }) )