From 0e616f8370e7d9712ebd93153365f48810f0d2b3 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 29 Oct 2023 22:03:45 +0100 Subject: [PATCH] add operator- Index --- src/include/ranges/crange.h | 1 + src/include/ranges/dindex.h | 1 + src/include/ranges/mrange.cc.h | 6 ++++++ src/include/ranges/mrange.h | 1 + src/include/ranges/prange.cc.h | 6 ++++++ src/include/ranges/prange.h | 1 + src/include/ranges/srange.cc.h | 6 ++++++ src/include/ranges/srange.h | 1 + src/include/ranges/urange.cc.h | 6 ++++++ src/include/ranges/urange.h | 4 +--- src/include/ranges/xindex.cc.h | 8 ++++++++ src/include/ranges/xindex.h | 2 ++ src/include/ranges/yrange.h | 1 + src/lib/ranges/crange.cc | 5 +++++ src/lib/ranges/dindex.cc | 5 +++++ src/lib/ranges/yrange.cc | 5 +++++ 16 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/include/ranges/crange.h b/src/include/ranges/crange.h index 3252c54..b813ded 100644 --- a/src/include/ranges/crange.h +++ b/src/include/ranges/crange.h @@ -26,6 +26,7 @@ namespace CNORXZ CIndex& operator--(); CIndex operator+(Int n) const; CIndex operator-(Int n) const; + SizeT operator-(const CIndex& i) const; CIndex& operator+=(Int n); CIndex& operator-=(Int n); diff --git a/src/include/ranges/dindex.h b/src/include/ranges/dindex.h index c40ab33..84613fb 100644 --- a/src/include/ranges/dindex.h +++ b/src/include/ranges/dindex.h @@ -32,6 +32,7 @@ namespace CNORXZ DIndex& operator--(); DIndex operator+(Int n) const; DIndex operator-(Int n) const; + SizeT operator-(const DIndex& i) const; DIndex& operator+=(Int n); DIndex& operator-=(Int n); diff --git a/src/include/ranges/mrange.cc.h b/src/include/ranges/mrange.cc.h index ebad77d..445782b 100644 --- a/src/include/ranges/mrange.cc.h +++ b/src/include/ranges/mrange.cc.h @@ -299,6 +299,12 @@ namespace CNORXZ return o -= n; } + template + SizeT GMIndex::operator-(const GMIndex& i) const + { + return lex() - i.lex(); + } + template GMIndex& GMIndex::operator+=(Int n) { diff --git a/src/include/ranges/mrange.h b/src/include/ranges/mrange.h index 17abf67..738af9d 100644 --- a/src/include/ranges/mrange.h +++ b/src/include/ranges/mrange.h @@ -47,6 +47,7 @@ namespace CNORXZ GMIndex& operator--(); GMIndex operator+(Int n) const; GMIndex operator-(Int n) const; + SizeT operator-(const GMIndex& i) const; GMIndex& operator+=(Int n); GMIndex& operator-=(Int n); diff --git a/src/include/ranges/prange.cc.h b/src/include/ranges/prange.cc.h index fc41d7f..efa1ed7 100644 --- a/src/include/ranges/prange.cc.h +++ b/src/include/ranges/prange.cc.h @@ -51,6 +51,12 @@ namespace CNORXZ { return PIndex(mRangePtr, IB::mPos + n); } + + template + SizeT PIndex::operator-(const PIndex& i) const + { + return lex() - i.lex(); + } template PIndex PIndex::operator-(Int n) const diff --git a/src/include/ranges/prange.h b/src/include/ranges/prange.h index a8d6ee1..e064056 100644 --- a/src/include/ranges/prange.h +++ b/src/include/ranges/prange.h @@ -26,6 +26,7 @@ namespace CNORXZ PIndex& operator--(); PIndex operator+(Int n) const; PIndex operator-(Int n) const; + SizeT operator-(const PIndex& i) const; PIndex& operator+=(Int n); PIndex& operator-=(Int n); diff --git a/src/include/ranges/srange.cc.h b/src/include/ranges/srange.cc.h index a974b50..5c8312c 100644 --- a/src/include/ranges/srange.cc.h +++ b/src/include/ranges/srange.cc.h @@ -54,6 +54,12 @@ namespace CNORXZ return SIndex(mRangePtr, IB::mPos - n); } + template + SizeT SIndex::operator-(const SIndex& i) const + { + return lex() - i.lex(); + } + template SIndex& SIndex::operator+=(Int n) { diff --git a/src/include/ranges/srange.h b/src/include/ranges/srange.h index 1ab85d1..ea04686 100644 --- a/src/include/ranges/srange.h +++ b/src/include/ranges/srange.h @@ -27,6 +27,7 @@ namespace CNORXZ SIndex& operator--(); SIndex operator+(Int n) const; SIndex operator-(Int n) const; + SizeT operator-(const SIndex& i) const; SIndex& operator+=(Int n); SIndex& operator-=(Int n); diff --git a/src/include/ranges/urange.cc.h b/src/include/ranges/urange.cc.h index 94744c1..4303c44 100644 --- a/src/include/ranges/urange.cc.h +++ b/src/include/ranges/urange.cc.h @@ -57,6 +57,12 @@ namespace CNORXZ { return UIndex(mRangePtr, IB::mPos - n); } + + template + SizeT UIndex::operator-(const UIndex& i) const + { + return lex() - i.lex(); + } template UIndex& UIndex::operator+=(Int n) diff --git a/src/include/ranges/urange.h b/src/include/ranges/urange.h index 2eeb6a3..dc68a84 100644 --- a/src/include/ranges/urange.h +++ b/src/include/ranges/urange.h @@ -26,14 +26,12 @@ namespace CNORXZ DEFAULT_MEMBERS(UIndex); UIndex(const RangePtr& range, SizeT pos = 0); - void swap(UIndex& i) {}; // !!! - UIndex& operator=(SizeT lexpos); UIndex& operator++(); UIndex& operator--(); UIndex operator+(Int n) const; UIndex operator-(Int n) const; - SizeT operator-(const UIndex& i) const { return lex() - i.lex(); } // !!! + SizeT operator-(const UIndex& i) const; UIndex& operator+=(Int n); UIndex& operator-=(Int n); diff --git a/src/include/ranges/xindex.cc.h b/src/include/ranges/xindex.cc.h index 5695349..8fc4e0a 100644 --- a/src/include/ranges/xindex.cc.h +++ b/src/include/ranges/xindex.cc.h @@ -65,6 +65,14 @@ namespace CNORXZ return std::make_shared>(*mI - n); } + template + SizeT XIndex::operator-(const XIndexBase& i) const + { + const XIndex* ip = dynamic_cast*>(&i); + CXZ_ASSERT(ip != nullptr, "bad index type"); + return (*mI) - (*ip->mI); + } + template XIndex& XIndex::operator+=(Int n) { diff --git a/src/include/ranges/xindex.h b/src/include/ranges/xindex.h index fad8459..81c303f 100644 --- a/src/include/ranges/xindex.h +++ b/src/include/ranges/xindex.h @@ -25,6 +25,7 @@ namespace CNORXZ virtual XIndexBase& operator--() = 0; virtual XIndexPtr operator+(Int n) const = 0; virtual XIndexPtr operator-(Int n) const = 0; + virtual SizeT operator-(const XIndexBase& i) const = 0; virtual XIndexBase& operator+=(Int n) = 0; virtual XIndexBase& operator-=(Int n) = 0; @@ -80,6 +81,7 @@ namespace CNORXZ virtual XIndex& operator--() override final; virtual XIndexPtr operator+(Int n) const override final; virtual XIndexPtr operator-(Int n) const override final; + virtual SizeT operator-(const XIndexBase& i) const override final; virtual XIndex& operator+=(Int n) override final; virtual XIndex& operator-=(Int n) override final; diff --git a/src/include/ranges/yrange.h b/src/include/ranges/yrange.h index 5e50206..355b080 100644 --- a/src/include/ranges/yrange.h +++ b/src/include/ranges/yrange.h @@ -38,6 +38,7 @@ namespace CNORXZ YIndex& operator--(); YIndex operator+(Int n) const; // equivalent to applying n times ++ YIndex operator-(Int n) const; + SizeT operator-(const YIndex& i) const; YIndex& operator+=(Int n); YIndex& operator-=(Int n); diff --git a/src/lib/ranges/crange.cc b/src/lib/ranges/crange.cc index 015bf67..af4557f 100644 --- a/src/lib/ranges/crange.cc +++ b/src/lib/ranges/crange.cc @@ -40,6 +40,11 @@ namespace CNORXZ { return CIndex(mRangePtr, IB::mPos - n); } + + SizeT CIndex::operator-(const CIndex& i) const + { + return lex() - i.lex(); + } CIndex& CIndex::operator+=(Int n) { diff --git a/src/lib/ranges/dindex.cc b/src/lib/ranges/dindex.cc index 9188c53..9b059ee 100644 --- a/src/lib/ranges/dindex.cc +++ b/src/lib/ranges/dindex.cc @@ -74,6 +74,11 @@ namespace CNORXZ { return (*mI) - n; } + + SizeT DIndex::operator-(const DIndex& i) const + { + return (*mI) - (*i.mI); + } DIndex& DIndex::operator+=(Int n) { diff --git a/src/lib/ranges/yrange.cc b/src/lib/ranges/yrange.cc index 176e600..029dc60 100644 --- a/src/lib/ranges/yrange.cc +++ b/src/lib/ranges/yrange.cc @@ -246,6 +246,11 @@ namespace CNORXZ YIndex o(*this); return o -= n; } + + SizeT YIndex::operator-(const YIndex& i) const + { + return lex() - i.lex(); + } YIndex& YIndex::operator+=(Int n) {