From 5f02dcb1779ed1b8b534345128f997d9a46e9e88 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Thu, 16 Feb 2017 16:06:23 +0100 Subject: [PATCH] small fixes + index naming --- src/index_base.cc | 20 ++++++++++++++++ src/index_base.h | 11 +++++---- src/multi_array.cc | 39 ++++++++++++++++++++++++++----- src/multi_array.h | 11 ++++++--- src/multi_range.cc | 28 +++++++++++++++++++++++ src/multi_range.h | 2 ++ src/name.cc | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/name.h | 34 +++++++++++++++++++++++++++ src/single_range.cc | 16 ++++++++++--- src/single_range.h | 7 +++--- 10 files changed, 204 insertions(+), 20 deletions(-) create mode 100644 src/name.cc create mode 100644 src/name.h diff --git a/src/index_base.cc b/src/index_base.cc index c5ab5a4..dcf6c42 100644 --- a/src/index_base.cc +++ b/src/index_base.cc @@ -7,6 +7,26 @@ namespace MultiArrayTools * IndefinitIndexBase * ************************/ + size_t IndefinitIndexBase::pos() const + { + return mPos; + } + + const std::string& IndefinitIndexBase::name() const + { + return mName; + } + + void name(const std::string& str) + { + mName = str; + } + + void IndefinitIndexBase::name(const Name& nm) + { + mName = nm.own(); + } + bool IndefinitIndexBase::link(IndefinitIndexBase* toLink) { if(toLink->rangeType() != rangeType() and toLink->name() == name()){ diff --git a/src/index_base.h b/src/index_base.h index 383fdd2..9e7143f 100644 --- a/src/index_base.h +++ b/src/index_base.h @@ -5,9 +5,11 @@ #include #include +#include #include "base_def.h" #include "range_base.h" +#include "name.h" namespace MultiArrayTools { @@ -27,11 +29,12 @@ namespace MultiArrayTools virtual bool operator!=(const IndefinitIndexBase& i) = 0; virtual size_t dim() const = 0; - virtual size_t pos() const = 0; // = mPos; implement !!! - - std::string& name(); - const std::string& name() const; + virtual size_t pos() const; // = mPos; implement !!! + virtual const std::string& name() const; + virtual void name(const std::string& str); + virtual void name(const Name& nm); + MultiRangeType rangeType() const = 0; virtual bool link(IndefinitIndexBase* toLink); diff --git a/src/multi_array.cc b/src/multi_array.cc index b084ded..c85e315 100644 --- a/src/multi_array.cc +++ b/src/multi_array.cc @@ -7,13 +7,40 @@ namespace MultiArrayTools * MultiArray * *******************/ - template - void giveNames(const std::string& name, /**/); - - template - void giveNames(const std::vector& names, /**/); + namespace + { + template + void giveNames(const std::string& name, /**/); + + template + void giveNames(const std::vector& names, /**/); + } /*!!!! giveNames(...) !!!!!*/ + + template + MultiArray(const Range& range) : mRange(&range), mCont(mRange.size()) + { + mInit = true; + } + + template + MultiArray(const Range& range, const std::vector& vec) : mRange(&range), mCont(vec) + { + mInit = true; + if(mCont.size() > mRange.size()){ + mCont.erase(mCont.begin() + mRange.size(), mCont.end()); + } + } + + template + MultiArray(const Range& range, std::vector&& vec) : mRange(&range), mCont(vec) + { + mInit = true; + if(mCont.size() > mRange.size()){ + mCont.erase(mCont.begin() + mRange.size(), mCont.end()); + } + } template T& MultiArray::operator()(const typename Range::indexType& i) @@ -32,7 +59,7 @@ namespace MultiArrayTools MultiArrayOperation& operator()(const NameTypes&... str) const { auto index = mRange->begin(); - // give names... !!! + index.name(Name("master", str...)); return MultiArrayOperation(*this, index); } diff --git a/src/multi_array.h b/src/multi_array.h index 5398fed..40b674b 100644 --- a/src/multi_array.h +++ b/src/multi_array.h @@ -1,23 +1,28 @@ + // -*- C++ -*- #ifndef __multi_array_h__ #define __multi_array_h__ #include +#include #include "base_def.h" #include "multi_array_operation.h" +#include "name.h" namespace MultiArrayTools { - + template class MultiArray { public: DEFAULT_MEMBERS(MultiArray); - MultiArray(const Range& range); // !!!! + MultiArray(const Range& range); + MultiArray(const Range& range, const std::vector& vec); + MultiArray(const Range& range, std::vector&& vec); template MultiArrayOperation& operator()(const NameTypes&... str) const; @@ -27,8 +32,8 @@ namespace MultiArrayTools private: bool mInit = false; - std::vector mCont; std::shared_ptr mRange; + std::vector mCont; }; } diff --git a/src/multi_range.cc b/src/multi_range.cc index aa6254d..710425c 100644 --- a/src/multi_range.cc +++ b/src/multi_range.cc @@ -51,6 +51,20 @@ namespace MultiArrayTools plus(index, digit, oor - max()); } } + + template + void nameTuple(IndexPack& iPack, Name& name) + { + std::get(iPack).name(name.get(N)); + nameTuple(iPack, name); + } + + template <> + void nameTuple<0>(IndexPack& iPack, Name& name) + { + std::get<0>(iPack).name(name.get(0)); + } + } @@ -92,6 +106,20 @@ namespace MultiArrayTools return evaluate_x(in); } + template + void MultiIndex::name(const Name& nm) + { + name(nm.own()); + if(nm.size() >= sizeof...(Indices)){ + nameTuple(mIPack, nm); + } + else { + Name nm2 = nm; + nm2.autoName(sizeof...(Indices)); + nameTuple(mIPack, nm); + } + } + template size_t MultiIndex::dim() const { diff --git a/src/multi_range.h b/src/multi_range.h index 3bbd1e7..b722092 100644 --- a/src/multi_range.h +++ b/src/multi_range.h @@ -36,6 +36,8 @@ namespace MultiArrayTools IndefinitIndexBase& getIndex(size_t n); const IndefinitIndexBase& getIndex(size_t n) const; + + virtual void name(const Name& nm) override; // dimension of MultiRange; includes ALL degrees of freedom virtual size_t dim() const override; diff --git a/src/name.cc b/src/name.cc new file mode 100644 index 0000000..2771986 --- /dev/null +++ b/src/name.cc @@ -0,0 +1,56 @@ + +#include "name.h" + +namespace MultiArrayTools +{ + + namespace + { + template + void giveNames(std::vector& nvec, const Name& name1, const Name& name2, const NameTypes&... names) + { + nvec.push_back(name1); + giveNames(nvec, name2, names...); + } + + void giveNames(std::vector& nvec, const Name& name) + { + nvec.push_back(name); + } + + void giveNames(std::vector& nvec) + { + nvec.clear(); + } + + } + + template + Name::Name(const std::string& mainName, const NameTypes&... names) : mMain(mainName) + { + mSub.reserve(sizeof...(NameTypes)); + giveNames(mSub, names...); + } + + const std::string& Name::own() const + { + return mMain; + } + + const Name& Name::get(size_t n) const + { + return mSub.at(n); + } + + void Name::autoName(size_t newSize) + { + size_t oldSize = mSub.size(); + mSub.resize(newSize); + if(oldSize < newSize){ + for(size_t i = oldSize; i != newSize; ++i){ + mSub[i] = mMain + to_string( i ); + } + } + } + +} diff --git a/src/name.h b/src/name.h new file mode 100644 index 0000000..ff28e7c --- /dev/null +++ b/src/name.h @@ -0,0 +1,34 @@ +// -*- C++ -*- + +#ifndef __name_h__ +#define __name_h__ + +#include +#include + +#include "base_def.h" + +namespace MultiArrayTools +{ + class Name + { + public: + + DEFAULT_MEMBERS(Name); + + template + Name(const std::string& mainName, const NameTypes&... names); + + const std::string& own() const; + const Name& get(size_t n) const; + + void autoName(size_t newSize); + + private: + std::string mMain; + std::vector mSub; + }; + +} + +#endif diff --git a/src/single_range.cc b/src/single_range.cc index 7013592..03a4d34 100644 --- a/src/single_range.cc +++ b/src/single_range.cc @@ -26,7 +26,11 @@ namespace MultiArrayTools return cnt; } - + template + size_t SingleRange::size() const + { + return mSpace.size(); + } /****************** * SingleIndex * @@ -39,7 +43,7 @@ namespace MultiArrayTools } template - size_t dim() const + size_t SingleIndexBase::dim() const { return 1; } @@ -51,9 +55,15 @@ namespace MultiArrayTools } template - void linkTo(IndefinitIndexBase* target) + void SingleIndexBase::linkTo(IndefinitIndexBase* target) { target->link(this); } + template + SingleIndex& SingleIndexBase::operator=(const U& upos) + { + setPos( dynamic_cast( mRange )->get(upos) ); + } + } diff --git a/src/single_range.h b/src/single_range.h index abee6d5..de6dbba 100644 --- a/src/single_range.h +++ b/src/single_range.h @@ -20,13 +20,10 @@ namespace MultiArrayTools DEFAULT_MEMBERS(SingleIndex); - virtual size_t size() const override; - virtual SingleIndexBase& operator=(size_t pos) override; - virtual SingleIndexBase& operator=(const U& upos); + virtual SingleIndex& operator=(const U& upos); virtual const U& getMetaPos() const; virtual size_t dim() const override; // = 1 - virtual void linkTo(IndefinitIndexBase* target) override; protected: @@ -38,6 +35,8 @@ namespace MultiArrayTools { public: DEFAULT_MEMBERS(SingleRange); + + virtual size_t size() const override; const U& get(size_t pos) const; size_t get(const U& metaPos) const;