cnorxz/src/multi_array.h

266 lines
7 KiB
C
Raw Normal View History

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>
#include <memory>
#include <iterator>
#include <algorithm>
2017-02-16 11:20:40 +01:00
#include "base_def.h"
#include "multi_array_operation.h"
2017-03-07 23:14:57 +01:00
#include "manipulator.h"
2017-02-16 16:06:23 +01:00
#include "name.h"
2017-02-16 11:20:40 +01:00
namespace MultiArrayTools
{
2017-02-26 18:14:58 +01:00
template <typename T, class Range>
class MultiArrayBase
{
public:
typedef T value_type;
class const_iterator : public std::iterator<std::random_access_iterator_tag,T>
{
public:
DEFAULT_MEMBERS(const_iterator);
const_iterator(const MultiArrayBase& ma);
const_iterator(const MultiArrayBase& ma, const typename Range::IndexType& index);
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:
const typename Range::IndexType& index() const;
typename Range::IndexType& index();
protected:
MultiArrayBase const* mMAPtr = nullptr;
typename Range::IndexType mIndex;
};
2017-02-26 18:14:58 +01:00
DEFAULT_MEMBERS(MultiArrayBase);
MultiArrayBase(const Range& range);
2017-03-14 23:00:41 +01:00
virtual ~MultiArrayBase() = default;
// only relevant for slices... has no effect for usual multiarrays
virtual void link(IndefinitIndexBase* iibPtr) const;
2017-02-26 18:14:58 +01:00
virtual const T& operator[](const typename Range::IndexType& i) const = 0;
virtual size_t size() const;
virtual bool isSlice() const = 0;
virtual const_iterator begin() const;
virtual const_iterator end() const;
2017-02-26 18:14:58 +01:00
virtual auto beginIndex() const -> decltype(Range().begin());
virtual auto endIndex() const -> decltype(Range().end());
2017-02-26 18:14:58 +01:00
virtual const Range& range() const;
2017-02-27 11:23:40 +01:00
2017-03-20 11:21:33 +01:00
virtual bool isConst() const;
template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str) const;
template <class NameType>
ConstMultiArrayOperationRoot<T,Range> operator()(const NameType& name, bool master) const;
2017-03-09 19:59:11 +01:00
virtual bool isInit() const;
2017-02-26 18:14:58 +01:00
protected:
2017-03-09 19:59:11 +01:00
bool mInit = false;
2017-02-26 18:14:58 +01:00
std::shared_ptr<Range> mRange;
};
2017-03-20 11:21:33 +01:00
template <typename T, class Range>
class MutableMultiArrayBase : public MultiArrayBase<T,Range>
{
public:
typedef typename MultiArrayBase<T,Range>::const_iterator const_iterator;
typedef MultiArrayBase<T,Range> MAB;
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);
iterator(MutableMultiArrayBase& ma, const typename Range::IndexType& index);
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:
const typename Range::IndexType& index() const;
typename Range::IndexType& index();
protected:
MutableMultiArrayBase* mMAPtr = nullptr;
typename Range::IndexType mIndex;
};
2017-03-20 11:21:33 +01:00
DEFAULT_MEMBERS(MutableMultiArrayBase);
MutableMultiArrayBase(const Range& range);
virtual T& operator[](const typename Range::IndexType& i) = 0;
virtual iterator begin();
virtual iterator end();
2017-03-20 11:21:33 +01:00
virtual bool isConst() const override;
2017-03-22 11:44:33 +01:00
template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Range> operator()(bool x, const NameTypes&... str) const
{
return MAB::operator()(str...);
}
2017-03-22 21:51:54 +01:00
template <class... NameTypes>
ConstMultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str) const;
template <class NameType>
ConstMultiArrayOperationRoot<T,Range> operator()(const NameType& name, bool master) const;
2017-03-22 11:44:33 +01:00
2017-03-20 11:21:33 +01:00
template <class... NameTypes>
MultiArrayOperationRoot<T,Range> operator()(const NameTypes&... str);
template <class NameType>
MultiArrayOperationRoot<T,Range> operator()(const NameType& name, bool master);
};
2017-02-26 18:14:58 +01:00
2017-02-16 11:20:40 +01:00
template <typename T, class Range>
2017-03-20 11:21:33 +01:00
class MultiArray : public MutableMultiArrayBase<T,Range>
2017-02-16 11:20:40 +01:00
{
public:
2017-02-26 18:14:58 +01:00
typedef MultiArrayBase<T,Range> MAB;
typedef typename MultiArrayBase<T,Range>::const_iterator const_iterator;
typedef typename MutableMultiArrayBase<T,Range>::iterator iterator;
2017-02-16 11:20:40 +01:00
DEFAULT_MEMBERS(MultiArray);
2017-02-16 16:06:23 +01:00
MultiArray(const Range& range);
MultiArray(const Range& range, const std::vector<T>& vec);
MultiArray(const Range& range, std::vector<T>&& vec);
2017-03-08 20:10:11 +01:00
template <class Range2, class Range3>
MultiArray(const MultiArray<MultiArray<T,Range2>,Range3> in);
// implement contstructor using FunctionalMultiArray as Input !!!
2017-03-08 20:10:11 +01:00
template <class Range2, class Range3>
MultiArray& operator=(const MultiArray<MultiArray<T,Range2>,Range3> in);
2017-02-16 11:20:40 +01:00
2017-02-26 18:14:58 +01:00
T& operator[](const typename Range::IndexType& i) override;
const T& operator[](const typename Range::IndexType& i) const override;
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-03-29 17:05:50 +02:00
const T* data() const;
2017-03-08 20:10:11 +01:00
// virtual void manipulate(ManipulatorBase<T>& mb,
// const typename Range::IndexType& manBegin,
// const typename Range::IndexType& manEnd);
2017-03-07 23:14:57 +01:00
template <typename U, class RangeX>
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
};
template <typename T, class Range, class Function>
class FunctionalMultiArray : public MultiArrayBase<T,Range>
{
public:
typedef MultiArrayBase<T,Range> MAB;
typedef typename MultiArrayBase<T,Range>::const_iterator const_iterator;
DEFAULT_MEMBERS(FunctionalMultiArray);
2017-03-28 17:55:12 +02:00
//FunctionalMultiArray(const Range& range);
FunctionalMultiArray(const Range& range, const Function& func);
virtual const T& operator[](const typename Range::IndexType& i) const override;
virtual bool isConst() const override;
virtual bool isSlice() const override;
protected:
mutable T mVal;
Function mFunc;
};
2017-02-16 11:20:40 +01:00
}
#include "multi_array.cc"
#endif