2017-02-16 11:20:40 +01:00
|
|
|
// -*- C++ -*-
|
|
|
|
|
|
|
|
#ifndef __multi_array_h__
|
|
|
|
#define __multi_array_h__
|
|
|
|
|
|
|
|
#include <cstdlib>
|
2017-02-16 16:06:23 +01:00
|
|
|
#include <vector>
|
2017-02-17 18:10:03 +01:00
|
|
|
#include <memory>
|
2017-03-20 18:53:30 +01:00
|
|
|
#include <iterator>
|
|
|
|
#include <algorithm>
|
2017-02-16 11:20:40 +01:00
|
|
|
|
|
|
|
#include "base_def.h"
|
2017-05-22 18:21:14 +02:00
|
|
|
#include "multi_range.h"
|
2017-08-08 18:07:58 +02:00
|
|
|
//#include "multi_array_operation.h"
|
|
|
|
//#include "manipulator.h"
|
2017-10-30 18:00:07 +01:00
|
|
|
//
|
2017-02-16 11:20:40 +01:00
|
|
|
|
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
2017-08-11 15:26:10 +02:00
|
|
|
|
|
|
|
// Explicitely specify subranges in template argument !!!
|
2017-08-25 17:02:23 +02:00
|
|
|
template <typename T, class... SRanges>
|
2017-02-26 18:14:58 +01:00
|
|
|
class MultiArrayBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-03-27 19:29:51 +02:00
|
|
|
typedef T value_type;
|
2017-08-25 17:02:23 +02:00
|
|
|
typedef ContainerRange<SRanges...> CRange;
|
|
|
|
typedef typename CRange::IndexType IndexType;
|
2017-03-27 19:29:51 +02:00
|
|
|
|
2017-03-20 18:53:30 +01:00
|
|
|
class const_iterator : public std::iterator<std::random_access_iterator_tag,T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
DEFAULT_MEMBERS(const_iterator);
|
|
|
|
|
|
|
|
const_iterator(const MultiArrayBase& ma);
|
2017-08-08 18:07:58 +02:00
|
|
|
const_iterator(const MultiArrayBase& ma, const typename CRange::IndexType& index);
|
2017-03-20 18:53:30 +01:00
|
|
|
virtual ~const_iterator() = default;
|
|
|
|
|
|
|
|
// Requirements:
|
|
|
|
bool operator==(const const_iterator& it) const;
|
|
|
|
bool operator!=(const const_iterator& it) const;
|
|
|
|
|
|
|
|
const T& operator*() const;
|
|
|
|
T const* operator->() const;
|
|
|
|
|
|
|
|
const_iterator& operator++();
|
|
|
|
const_iterator operator++(int);
|
|
|
|
const_iterator& operator--();
|
|
|
|
const_iterator operator--(int);
|
|
|
|
|
|
|
|
const_iterator& operator+=(int diff);
|
|
|
|
const_iterator& operator-=(int diff);
|
|
|
|
const_iterator operator+(int num) const;
|
|
|
|
const_iterator operator-(int num) const;
|
|
|
|
|
|
|
|
int operator-(const const_iterator& it) const;
|
|
|
|
|
|
|
|
const T& operator[](int num) const;
|
|
|
|
|
|
|
|
bool operator<(const const_iterator& it) const;
|
|
|
|
bool operator>(const const_iterator& it) const;
|
|
|
|
bool operator<=(const const_iterator& it) const;
|
|
|
|
bool operator>=(const const_iterator& it) const;
|
|
|
|
|
|
|
|
// Multi Array specific:
|
2017-08-25 17:02:23 +02:00
|
|
|
typename ContainerRange<SRanges...>::IndexType index() const;
|
|
|
|
|
2017-03-20 18:53:30 +01:00
|
|
|
protected:
|
|
|
|
MultiArrayBase const* mMAPtr = nullptr;
|
2017-08-08 18:07:58 +02:00
|
|
|
size_t mPos;
|
2017-03-20 18:53:30 +01:00
|
|
|
};
|
|
|
|
|
2017-02-26 18:14:58 +01:00
|
|
|
DEFAULT_MEMBERS(MultiArrayBase);
|
2017-08-25 17:02:23 +02:00
|
|
|
MultiArrayBase(const std::shared_ptr<SRanges>&... ranges);
|
2017-03-14 23:00:41 +01:00
|
|
|
|
|
|
|
virtual ~MultiArrayBase() = default;
|
2017-02-26 18:14:58 +01:00
|
|
|
|
2017-08-08 18:07:58 +02:00
|
|
|
virtual const T& operator[](const typename CRange::IndexType& i) const = 0;
|
2017-08-09 16:58:38 +02:00
|
|
|
virtual const T& at(const typename CRange::IndexType::MetaType& meta) const = 0;
|
2017-02-26 18:14:58 +01:00
|
|
|
|
2017-08-08 18:07:58 +02:00
|
|
|
virtual const T* data() const = 0;
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual const std::vector<T>& datav() const = 0;
|
2017-08-08 18:07:58 +02:00
|
|
|
|
2017-02-26 18:14:58 +01:00
|
|
|
virtual size_t size() const;
|
|
|
|
virtual bool isSlice() const = 0;
|
2017-03-20 18:53:30 +01:00
|
|
|
|
|
|
|
virtual const_iterator begin() const;
|
|
|
|
virtual const_iterator end() const;
|
2017-02-26 18:14:58 +01:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
virtual IndexType beginIndex() const;
|
|
|
|
virtual IndexType endIndex() const;
|
2017-02-26 18:14:58 +01:00
|
|
|
|
2017-08-11 15:26:10 +02:00
|
|
|
virtual const std::shared_ptr<CRange>& range() const;
|
2017-02-27 11:23:40 +01:00
|
|
|
|
2017-03-20 11:21:33 +01:00
|
|
|
virtual bool isConst() const;
|
2017-03-16 19:30:43 +01:00
|
|
|
|
2017-09-11 12:54:24 +02:00
|
|
|
virtual ConstOperationRoot<T,SRanges...>
|
|
|
|
operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) const;
|
2017-08-11 15:26:10 +02:00
|
|
|
|
2017-03-09 19:59:11 +01:00
|
|
|
virtual bool isInit() const;
|
2017-08-08 18:07:58 +02:00
|
|
|
|
2017-02-26 18:14:58 +01:00
|
|
|
protected:
|
2017-03-09 19:59:11 +01:00
|
|
|
bool mInit = false;
|
2017-08-08 18:07:58 +02:00
|
|
|
std::shared_ptr<CRange> mRange;
|
2017-02-26 18:14:58 +01:00
|
|
|
|
|
|
|
};
|
2017-03-20 11:21:33 +01:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
class MutableMultiArrayBase : public MultiArrayBase<T,SRanges...>
|
2017-03-20 11:21:33 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
typedef ContainerRange<SRanges...> CRange;
|
|
|
|
typedef typename MultiArrayBase<T,SRanges...>::const_iterator const_iterator;
|
|
|
|
typedef MultiArrayBase<T,SRanges...> MAB;
|
2017-08-08 18:07:58 +02:00
|
|
|
typedef typename CRange::IndexType IndexType;
|
2017-03-20 18:53:30 +01:00
|
|
|
|
|
|
|
class iterator : public std::iterator<std::random_access_iterator_tag,T>,
|
|
|
|
public std::iterator<std::output_iterator_tag,T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
DEFAULT_MEMBERS(iterator);
|
|
|
|
|
|
|
|
iterator(MutableMultiArrayBase& ma);
|
2017-05-22 18:21:14 +02:00
|
|
|
iterator(MutableMultiArrayBase& ma, const IndexType& index);
|
2017-03-20 18:53:30 +01:00
|
|
|
virtual ~iterator() = default;
|
|
|
|
|
|
|
|
// Requirements:
|
|
|
|
bool operator==(const iterator& it) const;
|
|
|
|
bool operator!=(const iterator& it) const;
|
|
|
|
|
|
|
|
const T& operator*() const;
|
|
|
|
T const* operator->() const;
|
|
|
|
T& operator*();
|
|
|
|
T* operator->();
|
|
|
|
|
|
|
|
iterator& operator++();
|
|
|
|
iterator operator++(int);
|
|
|
|
iterator& operator--();
|
|
|
|
iterator operator--(int);
|
|
|
|
|
|
|
|
iterator& operator+=(int diff);
|
|
|
|
iterator& operator-=(int diff);
|
|
|
|
iterator operator+(int num) const;
|
|
|
|
iterator operator-(int num) const;
|
|
|
|
|
|
|
|
int operator-(const iterator& it) const;
|
|
|
|
|
|
|
|
const T& operator[](int num) const;
|
|
|
|
T& operator[](int num);
|
|
|
|
|
|
|
|
bool operator<(const iterator& it) const;
|
|
|
|
bool operator>(const iterator& it) const;
|
|
|
|
bool operator<=(const iterator& it) const;
|
|
|
|
bool operator>=(const iterator& it) const;
|
|
|
|
|
|
|
|
// Multi Array specific:
|
2017-08-08 18:07:58 +02:00
|
|
|
typename CRange::IndexType index() const;
|
|
|
|
|
2017-03-20 18:53:30 +01:00
|
|
|
protected:
|
|
|
|
MutableMultiArrayBase* mMAPtr = nullptr;
|
2017-08-08 18:07:58 +02:00
|
|
|
size_t mPos;
|
2017-03-20 18:53:30 +01:00
|
|
|
};
|
|
|
|
|
2017-03-20 11:21:33 +01:00
|
|
|
|
|
|
|
DEFAULT_MEMBERS(MutableMultiArrayBase);
|
2017-08-25 17:02:23 +02:00
|
|
|
MutableMultiArrayBase(const std::shared_ptr<SRanges>&... ranges);
|
2017-03-20 11:21:33 +01:00
|
|
|
|
2017-05-22 18:21:14 +02:00
|
|
|
virtual T& operator[](const IndexType& i) = 0;
|
2017-08-09 16:58:38 +02:00
|
|
|
virtual T& at(const typename CRange::IndexType::MetaType& meta) = 0;
|
|
|
|
|
2017-08-08 18:07:58 +02:00
|
|
|
virtual T* data() = 0;
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual std::vector<T>& datav() = 0;
|
2017-03-20 18:53:30 +01:00
|
|
|
|
|
|
|
virtual iterator begin();
|
|
|
|
virtual iterator end();
|
2017-03-20 11:21:33 +01:00
|
|
|
|
|
|
|
virtual bool isConst() const override;
|
|
|
|
|
2017-09-11 12:54:24 +02:00
|
|
|
virtual ConstOperationRoot<T,SRanges...>
|
|
|
|
operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) const override;
|
|
|
|
virtual OperationRoot<T,SRanges...> operator()(std::shared_ptr<typename SRanges::IndexType>&... inds);
|
2017-03-20 11:21:33 +01:00
|
|
|
};
|
2017-02-26 18:14:58 +01:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
template <typename T, class... SRanges>
|
|
|
|
class MultiArray : public MutableMultiArrayBase<T,SRanges...>
|
2017-02-16 11:20:40 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
typedef ContainerRange<SRanges...> CRange;
|
|
|
|
typedef MultiArrayBase<T,SRanges...> MAB;
|
|
|
|
typedef typename MultiArrayBase<T,SRanges...>::const_iterator const_iterator;
|
|
|
|
typedef typename MutableMultiArrayBase<T,SRanges...>::iterator iterator;
|
2017-08-08 18:07:58 +02:00
|
|
|
typedef typename CRange::IndexType IndexType;
|
2017-03-20 18:53:30 +01:00
|
|
|
|
2017-02-16 11:20:40 +01:00
|
|
|
DEFAULT_MEMBERS(MultiArray);
|
2017-08-25 17:02:23 +02:00
|
|
|
MultiArray(const std::shared_ptr<SRanges>&... ranges);
|
|
|
|
MultiArray(const std::shared_ptr<SRanges>&... ranges, const std::vector<T>& vec);
|
|
|
|
MultiArray(const std::shared_ptr<SRanges>&... ranges, std::vector<T>&& vec);
|
2017-03-08 20:10:11 +01:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
// template <class Range2, class Range3>
|
|
|
|
// MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in);
|
2017-03-08 20:10:11 +01:00
|
|
|
|
2017-03-26 16:55:52 +02:00
|
|
|
// implement contstructor using FunctionalMultiArray as Input !!!
|
|
|
|
|
2017-08-08 18:07:58 +02:00
|
|
|
//template <class Range2, class Range3>
|
|
|
|
//MultiArray& operator=(const MultiArray<MultiArray<T,Range2>,Range3> in);
|
2017-02-16 11:20:40 +01:00
|
|
|
|
2017-08-09 16:58:38 +02:00
|
|
|
virtual T& operator[](const IndexType& i) override;
|
|
|
|
virtual const T& operator[](const IndexType& i) const override;
|
|
|
|
virtual T& at(const typename CRange::IndexType::MetaType& meta) override;
|
|
|
|
virtual const T& at(const typename CRange::IndexType::MetaType& meta) const override;
|
|
|
|
|
2017-03-16 22:42:55 +01:00
|
|
|
virtual bool isConst() const override;
|
2017-02-26 18:14:58 +01:00
|
|
|
virtual bool isSlice() const override;
|
2017-03-07 23:14:57 +01:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
template <class... SRanges2>
|
|
|
|
MultiArray<T,SRanges2...> format(const std::shared_ptr<SRanges2>&... nrs); // reformat array using 'nr' which in
|
2017-08-09 11:29:41 +02:00
|
|
|
// total must have the same size as mRange
|
2017-08-08 18:07:58 +02:00
|
|
|
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual const T* data() const override;
|
|
|
|
virtual T* data() override;
|
2017-03-29 17:05:50 +02:00
|
|
|
|
2017-09-09 19:59:09 +02:00
|
|
|
virtual const std::vector<T>& datav() const override;
|
|
|
|
virtual std::vector<T>& datav() override;
|
|
|
|
|
2017-03-08 20:10:11 +01:00
|
|
|
// virtual void manipulate(ManipulatorBase<T>& mb,
|
2017-08-08 18:07:58 +02:00
|
|
|
// const typename CRange::IndexType& manBegin,
|
|
|
|
// const typename CRange::IndexType& manEnd);
|
2017-03-07 23:14:57 +01:00
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
template <typename U, class... SRanges2>
|
2017-03-07 23:14:57 +01:00
|
|
|
friend class MultiArray;
|
2017-02-16 11:20:40 +01:00
|
|
|
|
|
|
|
private:
|
2017-02-16 16:06:23 +01:00
|
|
|
std::vector<T> mCont;
|
2017-02-16 11:20:40 +01:00
|
|
|
};
|
|
|
|
|
2017-08-25 17:02:23 +02:00
|
|
|
template <typename T, class Function, class... SRanges>
|
|
|
|
class FunctionalMultiArray : public MultiArrayBase<T,SRanges...>
|
2017-03-26 16:55:52 +02:00
|
|
|
{
|
|
|
|
public:
|
2017-08-25 17:02:23 +02:00
|
|
|
|
|
|
|
typedef ContainerRange<SRanges...> CRange;
|
2017-08-08 18:07:58 +02:00
|
|
|
typedef MultiArrayBase<T,CRange> MAB;
|
|
|
|
typedef typename MultiArrayBase<T,CRange>::const_iterator const_iterator;
|
|
|
|
typedef typename CRange::IndexType IndexType;
|
2017-05-22 18:21:14 +02:00
|
|
|
|
2017-03-26 16:55:52 +02:00
|
|
|
DEFAULT_MEMBERS(FunctionalMultiArray);
|
2017-08-08 18:07:58 +02:00
|
|
|
//FunctionalMultiArray(const CRange& range);
|
2017-08-25 17:02:23 +02:00
|
|
|
FunctionalMultiArray(const std::shared_ptr<SRanges>&... ranges, const Function& func);
|
2017-03-26 16:55:52 +02:00
|
|
|
|
2017-05-22 18:21:14 +02:00
|
|
|
virtual const T& operator[](const IndexType& i) const override;
|
2017-03-26 16:55:52 +02:00
|
|
|
|
|
|
|
virtual bool isConst() const override;
|
|
|
|
virtual bool isSlice() const override;
|
|
|
|
|
|
|
|
protected:
|
2017-03-27 11:33:40 +02:00
|
|
|
mutable T mVal;
|
|
|
|
Function mFunc;
|
2017-03-26 16:55:52 +02:00
|
|
|
};
|
|
|
|
|
2017-02-16 11:20:40 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:35:25 +01:00
|
|
|
/* ========================= *
|
|
|
|
* --- TEMPLATE CODE --- *
|
|
|
|
* ========================= */
|
|
|
|
|
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
* MultiArrayBase::const_iterator *
|
|
|
|
**************************************/
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
MultiArrayBase<T,SRanges...>::const_iterator::const_iterator(const MultiArrayBase<T,SRanges...>& ma):
|
|
|
|
mMAPtr(&ma), mPos(0) { }
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
MultiArrayBase<T,SRanges...>::const_iterator::const_iterator(const MultiArrayBase<T,SRanges...>& ma,
|
|
|
|
const typename CRange::IndexType& index):
|
|
|
|
mMAPtr(&ma), mPos(index.pos()) { }
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MultiArrayBase<T,SRanges...>::const_iterator::operator==(const const_iterator& it) const
|
|
|
|
{
|
|
|
|
return mMAPtr == it.mMAPtr and mPos == it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MultiArrayBase<T,SRanges...>::const_iterator::operator!=(const const_iterator& it) const
|
|
|
|
{
|
|
|
|
return mMAPtr != it.mMAPtr or mPos != it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T& MultiArrayBase<T,SRanges...>::const_iterator::operator*() const
|
|
|
|
{
|
|
|
|
return mMAPtr->data()[mPos];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
T const* MultiArrayBase<T,SRanges...>::const_iterator::operator->() const
|
|
|
|
{
|
|
|
|
return &mMAPtr->data()[mPos];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::const_iterator& MultiArrayBase<T,SRanges...>::const_iterator::operator++()
|
|
|
|
{
|
|
|
|
++mPos;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::const_iterator::operator++(int)
|
|
|
|
{
|
|
|
|
const_iterator tmp(*this);
|
|
|
|
++mPos;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::const_iterator& MultiArrayBase<T,SRanges...>::const_iterator::operator--()
|
|
|
|
{
|
|
|
|
--mPos;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::const_iterator::operator--(int)
|
|
|
|
{
|
|
|
|
const_iterator tmp(*this);
|
|
|
|
--mPos;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::const_iterator& MultiArrayBase<T,SRanges...>::const_iterator::operator+=(int diff)
|
|
|
|
{
|
|
|
|
mPos += diff;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::const_iterator& MultiArrayBase<T,SRanges...>::const_iterator::operator-=(int diff)
|
|
|
|
{
|
|
|
|
mPos -= diff;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::const_iterator::operator+(int num) const
|
|
|
|
{
|
|
|
|
const_iterator tmp(*this);
|
|
|
|
tmp += num;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::const_iterator::operator-(int num) const
|
|
|
|
{
|
|
|
|
const_iterator tmp(*this);
|
|
|
|
tmp -= num;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
int MultiArrayBase<T,SRanges...>::const_iterator::operator-(const const_iterator& it) const
|
|
|
|
{
|
|
|
|
return mPos - it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T& MultiArrayBase<T,SRanges...>::const_iterator::operator[](int num) const
|
|
|
|
{
|
|
|
|
return *(operator+(num));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MultiArrayBase<T,SRanges...>::const_iterator::operator<(const const_iterator& it) const
|
|
|
|
{
|
|
|
|
return mPos < it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MultiArrayBase<T,SRanges...>::const_iterator::operator>(const const_iterator& it) const
|
|
|
|
{
|
|
|
|
return mPos > it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MultiArrayBase<T,SRanges...>::const_iterator::operator<=(const const_iterator& it) const
|
|
|
|
{
|
|
|
|
return mPos <= it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MultiArrayBase<T,SRanges...>::const_iterator::operator>=(const const_iterator& it) const
|
|
|
|
{
|
|
|
|
return mPos >= it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::IndexType
|
|
|
|
MultiArrayBase<T,SRanges...>::const_iterator::index() const
|
|
|
|
{
|
|
|
|
auto i = mMAPtr->beginIndex();
|
|
|
|
i = mPos;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
* MultiArrayBase *
|
|
|
|
**********************/
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
MultiArrayBase<T,SRanges...>::MultiArrayBase(const std::shared_ptr<SRanges>&... ranges)
|
|
|
|
{
|
|
|
|
ContainerRangeFactory<SRanges...> crf(ranges...);
|
|
|
|
mRange = std::dynamic_pointer_cast<ContainerRange<SRanges...> >( crf.create() );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
size_t MultiArrayBase<T,SRanges...>::size() const
|
|
|
|
{
|
|
|
|
return mRange->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::begin() const
|
|
|
|
{
|
|
|
|
return const_iterator(*this, beginIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::const_iterator MultiArrayBase<T,SRanges...>::end() const
|
|
|
|
{
|
|
|
|
return const_iterator(*this, endIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::IndexType
|
|
|
|
MultiArrayBase<T,SRanges...>::beginIndex() const
|
|
|
|
{
|
|
|
|
return mRange->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MultiArrayBase<T,SRanges...>::IndexType
|
|
|
|
MultiArrayBase<T,SRanges...>::endIndex() const
|
|
|
|
{
|
|
|
|
return mRange->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const std::shared_ptr<typename MultiArrayBase<T,SRanges...>::CRange>&
|
|
|
|
MultiArrayBase<T,SRanges...>::range() const
|
|
|
|
{
|
|
|
|
return mRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MultiArrayBase<T,SRanges...>::isConst() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
ConstOperationRoot<T,SRanges...>
|
|
|
|
MultiArrayBase<T,SRanges...>::operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) const
|
|
|
|
{
|
|
|
|
return ConstOperationRoot<T,SRanges...>(*this, inds...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MultiArrayBase<T,SRanges...>::isInit() const
|
|
|
|
{
|
|
|
|
return mInit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************
|
|
|
|
* MutableMultiArrayBase::iterator *
|
|
|
|
****************************************/
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
MutableMultiArrayBase<T,SRanges...>::iterator::iterator(MutableMultiArrayBase<T,SRanges...>& ma):
|
|
|
|
mMAPtr(&ma), mPos(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
MutableMultiArrayBase<T,SRanges...>::iterator::iterator(MutableMultiArrayBase<T,SRanges...>& ma,
|
|
|
|
const typename CRange::IndexType& index):
|
|
|
|
mMAPtr(&ma), mPos(index.pos())
|
|
|
|
{ }
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator==(const iterator& it) const
|
|
|
|
{
|
|
|
|
return mMAPtr == it.mMAPtr and mPos == it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator!=(const iterator& it) const
|
|
|
|
{
|
|
|
|
return mMAPtr != it.mMAPtr or mPos != it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T& MutableMultiArrayBase<T,SRanges...>::iterator::operator*() const
|
|
|
|
{
|
|
|
|
return mMAPtr->data()[mPos];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
T const* MutableMultiArrayBase<T,SRanges...>::iterator::operator->() const
|
|
|
|
{
|
|
|
|
return &mMAPtr->data()[mPos];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
T& MutableMultiArrayBase<T,SRanges...>::iterator::operator*()
|
|
|
|
{
|
|
|
|
return mMAPtr->data()[mPos];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
T* MutableMultiArrayBase<T,SRanges...>::iterator::operator->()
|
|
|
|
{
|
|
|
|
return &mMAPtr->data()[mPos];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::iterator& MutableMultiArrayBase<T,SRanges...>::iterator::operator++()
|
|
|
|
{
|
|
|
|
++mPos;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::iterator::operator++(int)
|
|
|
|
{
|
|
|
|
iterator tmp(*this);
|
|
|
|
++mPos;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::iterator& MutableMultiArrayBase<T,SRanges...>::iterator::operator--()
|
|
|
|
{
|
|
|
|
--mPos;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::iterator::operator--(int)
|
|
|
|
{
|
|
|
|
iterator tmp(*this);
|
|
|
|
--mPos;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::iterator& MutableMultiArrayBase<T,SRanges...>::iterator::operator+=(int diff)
|
|
|
|
{
|
|
|
|
mPos += diff;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::iterator& MutableMultiArrayBase<T,SRanges...>::iterator::operator-=(int diff)
|
|
|
|
{
|
|
|
|
mPos -= diff;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::iterator::operator+(int num) const
|
|
|
|
{
|
|
|
|
iterator tmp(*this);
|
|
|
|
tmp += num;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::iterator::operator-(int num) const
|
|
|
|
{
|
|
|
|
iterator tmp(*this);
|
|
|
|
tmp -= num;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
int MutableMultiArrayBase<T,SRanges...>::iterator::operator-(const iterator& it) const
|
|
|
|
{
|
|
|
|
return mPos - it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T& MutableMultiArrayBase<T,SRanges...>::iterator::operator[](int num) const
|
|
|
|
{
|
|
|
|
return *(operator+(num));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
T& MutableMultiArrayBase<T,SRanges...>::iterator::operator[](int num)
|
|
|
|
{
|
|
|
|
return *(operator+(num));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator<(const iterator& it) const
|
|
|
|
{
|
|
|
|
return mPos < it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator>(const iterator& it) const
|
|
|
|
{
|
|
|
|
return mPos > it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator<=(const iterator& it) const
|
|
|
|
{
|
|
|
|
return mPos <= it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MutableMultiArrayBase<T,SRanges...>::iterator::operator>=(const iterator& it) const
|
|
|
|
{
|
|
|
|
return mPos >= it.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::IndexType
|
|
|
|
MutableMultiArrayBase<T,SRanges...>::iterator::index() const
|
|
|
|
{
|
|
|
|
auto i = mMAPtr->beginIndex();
|
|
|
|
i = mPos;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************
|
|
|
|
* MutableMultiArrayBase *
|
|
|
|
******************************/
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
MutableMultiArrayBase<T,SRanges...>::MutableMultiArrayBase(const std::shared_ptr<SRanges>&... ranges) :
|
|
|
|
MultiArrayBase<T,SRanges...>(ranges...) {}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::begin()
|
|
|
|
{
|
|
|
|
return iterator(*this, MAB::beginIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
typename MutableMultiArrayBase<T,SRanges...>::iterator MutableMultiArrayBase<T,SRanges...>::end()
|
|
|
|
{
|
|
|
|
return iterator(*this, MAB::endIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MutableMultiArrayBase<T,SRanges...>::isConst() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
OperationRoot<T,SRanges...>
|
|
|
|
MutableMultiArrayBase<T,SRanges...>::operator()(std::shared_ptr<typename SRanges::IndexType>&... inds)
|
|
|
|
{
|
|
|
|
return OperationRoot<T,SRanges...>(*this, inds...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
ConstOperationRoot<T,SRanges...>
|
|
|
|
MutableMultiArrayBase<T,SRanges...>::operator()(std::shared_ptr<typename SRanges::IndexType>&... inds) const
|
|
|
|
{
|
|
|
|
return ConstOperationRoot<T,SRanges...>(*this, inds...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************
|
|
|
|
* MultiArray *
|
|
|
|
*******************/
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
MultiArray<T,SRanges...>::MultiArray(const std::shared_ptr<SRanges>&... ranges) :
|
|
|
|
MutableMultiArrayBase<T,SRanges...>(ranges...),
|
|
|
|
mCont(MAB::mRange->size())
|
|
|
|
{
|
|
|
|
MAB::mInit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
MultiArray<T,SRanges...>::MultiArray(const std::shared_ptr<SRanges>&... ranges, const std::vector<T>& vec) :
|
|
|
|
MutableMultiArrayBase<T,SRanges...>(ranges...),
|
|
|
|
mCont(vec)
|
|
|
|
{
|
|
|
|
MAB::mInit = true;
|
|
|
|
if(mCont.size() > MAB::mRange->size()){
|
|
|
|
mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
MultiArray<T,SRanges...>::MultiArray(const std::shared_ptr<SRanges>&... ranges, std::vector<T>&& vec) :
|
|
|
|
MutableMultiArrayBase<T,SRanges...>(ranges...),
|
|
|
|
mCont(vec)
|
|
|
|
{
|
|
|
|
MAB::mInit = true;
|
|
|
|
if(mCont.size() > MAB::mRange->size()){
|
|
|
|
mCont.erase(mCont.begin() + MAB::mRange->size(), mCont.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
template <class Range2, class Range3>
|
|
|
|
MultiArray<T,SRanges...>::MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in) :
|
|
|
|
MutableMultiArrayBase<T,SRanges...>(merge(in.range(), in[ in.beginIndex() ].range()))
|
|
|
|
// assert that Range2 has always same extension
|
|
|
|
{
|
|
|
|
MAB::mInit = true;
|
|
|
|
mCont.clear();
|
|
|
|
for(auto i = in.beginIndex(); i != in.endIndex(); ++i){
|
|
|
|
mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end());
|
|
|
|
}
|
|
|
|
assert(mCont.size() == MAB::mRange->size());
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
template <class Range2, class Range3>
|
|
|
|
MultiArray<T,SRanges...>& MultiArray<T,SRanges...>::operator=(const MultiArray<MultiArray<T,Range2>,Range3> in)
|
|
|
|
{
|
|
|
|
MAB::mRange.reset(new Range(merge(in.range(), in[ in.beginIndex() ].range())));
|
|
|
|
// assert that Range2 has always same extension
|
|
|
|
mCont.clear();
|
|
|
|
for(auto i = in.beginIndex(); i != in.endIndex(); ++i){
|
|
|
|
mCont.insert(mCont.end(), in[i].mCont.begin(), in[i].mCont.end());
|
|
|
|
}
|
|
|
|
assert(mCont.size() == MAB::mRange->size());
|
|
|
|
return *this;
|
|
|
|
} */
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
T& MultiArray<T,SRanges...>::operator[](const typename CRange::IndexType& i)
|
|
|
|
{
|
|
|
|
return mCont[ i.pos() ];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T& MultiArray<T,SRanges...>::operator[](const typename CRange::IndexType& i) const
|
|
|
|
{
|
|
|
|
return mCont[ i.pos() ];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
T& MultiArray<T,SRanges...>::at(const typename CRange::IndexType::MetaType& meta)
|
|
|
|
{
|
|
|
|
return mCont[ MAB::beginIndex().at(meta).pos() ];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T& MultiArray<T,SRanges...>::at(const typename CRange::IndexType::MetaType& meta) const
|
|
|
|
{
|
|
|
|
return mCont[ MAB::beginIndex().at(meta).pos() ];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MultiArray<T,SRanges...>::isConst() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
bool MultiArray<T,SRanges...>::isSlice() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
template <class... SRanges2>
|
|
|
|
MultiArray<T,SRanges2...> MultiArray<T,SRanges...>::format(const std::shared_ptr<SRanges2>&... nrs)
|
|
|
|
{
|
|
|
|
return MultiArray<T,SRanges2...>( nrs... , std::move(mCont) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const T* MultiArray<T,SRanges...>::data() const
|
|
|
|
{
|
|
|
|
return mCont.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
T* MultiArray<T,SRanges...>::data()
|
|
|
|
{
|
|
|
|
return mCont.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
const std::vector<T>& MultiArray<T,SRanges...>::datav() const
|
|
|
|
{
|
|
|
|
return mCont;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
std::vector<T>& MultiArray<T,SRanges...>::datav()
|
|
|
|
{
|
|
|
|
return mCont;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
template <typename T, class... SRanges>
|
|
|
|
void MultiArray<T,SRanges...>::manipulate(ManipulatorBase<T>& mb,
|
|
|
|
const typename Range::IndexType& manBegin,
|
|
|
|
const typename Range::IndexType& manEnd)
|
|
|
|
{
|
|
|
|
mb.setup(mCont, manBegin.pos(), manEnd.pos());
|
|
|
|
mb.execute();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
* FunctionalMultiArray *
|
|
|
|
****************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
template <typename T, class Range, class Function>
|
|
|
|
FunctionalMultiArray<T,Range,Function>::FunctionalMultiArray(const Range& range) :
|
|
|
|
MultiArrayBase<T,SRanges...>(range), mFunc() {}
|
|
|
|
*/
|
|
|
|
template <typename T, class Function, class... SRanges>
|
|
|
|
FunctionalMultiArray<T,Function,SRanges...>::FunctionalMultiArray(const std::shared_ptr<SRanges>&... ranges,
|
|
|
|
const Function& func) :
|
|
|
|
MultiArrayBase<T,SRanges...>(ranges...), mFunc(func) {}
|
|
|
|
|
|
|
|
template <typename T, class Function, class... SRanges>
|
|
|
|
const T& FunctionalMultiArray<T,Function,SRanges...>::operator[](const typename CRange::IndexType& i) const
|
|
|
|
{
|
|
|
|
mVal = mFunc(i);
|
|
|
|
return mVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class Function, class... SRanges>
|
|
|
|
bool FunctionalMultiArray<T,Function,SRanges...>::isConst() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class Function, class... SRanges>
|
|
|
|
bool FunctionalMultiArray<T,Function,SRanges...>::isSlice() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-02-16 11:20:40 +01:00
|
|
|
|
|
|
|
#endif
|