diff --git a/src/include/ranges/anonymous_range.h b/src/include/ranges/anonymous_range.h index c820138..d8b17b3 100644 --- a/src/include/ranges/anonymous_range.h +++ b/src/include/ranges/anonymous_range.h @@ -97,6 +97,8 @@ namespace MultiArrayTools std::shared_ptr sreplace(const std::shared_ptr in, size_t num) const; std::shared_ptr sreplace(const vector>& in, size_t num) const; + std::shared_ptr sreplace(const std::shared_ptr& in, + const vector& num) const; bool isEmpty() const; diff --git a/src/lib/ranges/anonymous_range.cc b/src/lib/ranges/anonymous_range.cc index 1583cc2..f0408b5 100644 --- a/src/lib/ranges/anonymous_range.cc +++ b/src/lib/ranges/anonymous_range.cc @@ -210,6 +210,26 @@ namespace MultiArrayTools return std::dynamic_pointer_cast(arf.create()); } + std::shared_ptr AnonymousRange::sreplace(const std::shared_ptr& in, + const vector& num) const + { + size_t cnt = num[0]; + size_t rep_size = 1; + // assert continuous ordering or replaced ranges: + for(auto& x: num){ + assert(cnt++ == x); + rep_size *= mOrig[x]->size(); + } + assert(rep_size == in->size()); + vector> norig; + norig.reserve(mOrig.size()-num.size()+1); + norig.insert(norig.end(),mOrig.begin(),mOrig.begin()+num[0]); + norig.push_back(in); + norig.insert(norig.end(),mOrig.begin()+num.back()+1,mOrig.end()); + AnonymousRangeFactory arf(norig); + return std::dynamic_pointer_cast(arf.create()); + } + const vector >& AnonymousRange::orig() const { return mOrig;