small fixes + index naming

This commit is contained in:
Christian Zimmermann 2017-02-16 16:06:23 +01:00
parent c04863f93f
commit 5f02dcb177
10 changed files with 204 additions and 20 deletions

View file

@ -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()){

View file

@ -5,9 +5,11 @@
#include <cstdlib>
#include <string>
#include <vector>
#include "base_def.h"
#include "range_base.h"
#include "name.h"
namespace MultiArrayTools
{
@ -27,10 +29,11 @@ namespace MultiArrayTools
virtual bool operator!=(const IndefinitIndexBase& i) = 0;
virtual size_t dim() const = 0;
virtual size_t pos() const = 0; // = mPos; implement !!!
virtual size_t pos() const; // = mPos; implement !!!
std::string& name();
const std::string& name() const;
virtual const std::string& name() const;
virtual void name(const std::string& str);
virtual void name(const Name& nm);
MultiRangeType rangeType() const = 0;

View file

@ -7,14 +7,41 @@ namespace MultiArrayTools
* MultiArray *
*******************/
namespace
{
template <typename... Ranges>
void giveNames(const std::string& name, /**/);
template <typename... Ranges>
void giveNames(const std::vector<std::string>& names, /**/);
}
/*!!!! giveNames(...) !!!!!*/
template <typename T, class Range>
MultiArray(const Range& range) : mRange(&range), mCont(mRange.size())
{
mInit = true;
}
template <typename T, class Range>
MultiArray(const Range& range, const std::vector<T>& vec) : mRange(&range), mCont(vec)
{
mInit = true;
if(mCont.size() > mRange.size()){
mCont.erase(mCont.begin() + mRange.size(), mCont.end());
}
}
template <typename T, class Range>
MultiArray(const Range& range, std::vector<T>&& vec) : mRange(&range), mCont(vec)
{
mInit = true;
if(mCont.size() > mRange.size()){
mCont.erase(mCont.begin() + mRange.size(), mCont.end());
}
}
template <typename T, class Range>
T& MultiArray<T,Is...>::operator()(const typename Range::indexType& i)
{
@ -32,7 +59,7 @@ namespace MultiArrayTools
MultiArrayOperation<T,Range>& operator()(const NameTypes&... str) const
{
auto index = mRange->begin();
// give names... !!!
index.name(Name("master", str...));
return MultiArrayOperation<T,Range>(*this, index);
}

View file

@ -1,12 +1,15 @@
// -*- C++ -*-
#ifndef __multi_array_h__
#define __multi_array_h__
#include <cstdlib>
#include <vector>
#include "base_def.h"
#include "multi_array_operation.h"
#include "name.h"
namespace MultiArrayTools
{
@ -17,7 +20,9 @@ namespace MultiArrayTools
public:
DEFAULT_MEMBERS(MultiArray);
MultiArray(const Range& range); // !!!!
MultiArray(const Range& range);
MultiArray(const Range& range, const std::vector<T>& vec);
MultiArray(const Range& range, std::vector<T>&& vec);
template <class... NameTypes>
MultiArrayOperation<T,Range>& operator()(const NameTypes&... str) const;
@ -27,8 +32,8 @@ namespace MultiArrayTools
private:
bool mInit = false;
std::vector<T> mCont;
std::shared_ptr<Range> mRange;
std::vector<T> mCont;
};
}

View file

@ -51,6 +51,20 @@ namespace MultiArrayTools
plus(index, digit, oor - max());
}
}
template <size_t N>
void nameTuple(IndexPack& iPack, Name& name)
{
std::get<N>(iPack).name(name.get(N));
nameTuple<N-1>(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<sizeof...(Indices)-1>(in);
}
template <class... Indices>
void MultiIndex<Indices...>::name(const Name& nm)
{
name(nm.own());
if(nm.size() >= sizeof...(Indices)){
nameTuple<sizeof...(Indices)-1>(mIPack, nm);
}
else {
Name nm2 = nm;
nm2.autoName(sizeof...(Indices));
nameTuple<sizeof...(Indices)-1>(mIPack, nm);
}
}
template <class... Indices>
size_t MultiIndex<Indices...>::dim() const
{

View file

@ -37,6 +37,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;

56
src/name.cc Normal file
View file

@ -0,0 +1,56 @@
#include "name.h"
namespace MultiArrayTools
{
namespace
{
template <class... NameTypes>
void giveNames(std::vector<Name>& nvec, const Name& name1, const Name& name2, const NameTypes&... names)
{
nvec.push_back(name1);
giveNames(nvec, name2, names...);
}
void giveNames(std::vector<Name>& nvec, const Name& name)
{
nvec.push_back(name);
}
void giveNames(std::vector<Name>& nvec)
{
nvec.clear();
}
}
template <class... NameTypes>
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 );
}
}
}
}

34
src/name.h Normal file
View file

@ -0,0 +1,34 @@
// -*- C++ -*-
#ifndef __name_h__
#define __name_h__
#include <cstdlib>
#include <string>
#include "base_def.h"
namespace MultiArrayTools
{
class Name
{
public:
DEFAULT_MEMBERS(Name);
template <class... NameTypes>
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<Name> mSub;
};
}
#endif

View file

@ -26,7 +26,11 @@ namespace MultiArrayTools
return cnt;
}
template <typename U, IndexType TYPE>
size_t SingleRange<U,TYPE>::size() const
{
return mSpace.size();
}
/******************
* SingleIndex *
@ -39,7 +43,7 @@ namespace MultiArrayTools
}
template <typename U, IndexType TYPE>
size_t dim() const
size_t SingleIndexBase<U,TYPE>::dim() const
{
return 1;
}
@ -51,9 +55,15 @@ namespace MultiArrayTools
}
template <typename U, IndexType TYPE>
void linkTo(IndefinitIndexBase* target)
void SingleIndexBase<U,TYPE>::linkTo(IndefinitIndexBase* target)
{
target->link(this);
}
template <typename U, IndexType TYPE>
SingleIndex& SingleIndexBase<U,TYPE>::operator=(const U& upos)
{
setPos( dynamic_cast<SingleRange*>( mRange )->get(upos) );
}
}

View file

@ -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:
@ -39,6 +36,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;