2017-02-16 11:20:40 +01:00
|
|
|
// -*- C++ -*-
|
|
|
|
|
|
|
|
#ifndef __index_base_h__
|
|
|
|
#define __index_base_h__
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <string>
|
2017-02-16 16:06:23 +01:00
|
|
|
#include <vector>
|
2017-02-16 11:20:40 +01:00
|
|
|
|
|
|
|
#include "base_def.h"
|
|
|
|
#include "range_base.h"
|
2017-10-30 18:00:07 +01:00
|
|
|
|
2017-02-16 11:20:40 +01:00
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
2017-08-25 22:03:20 +02:00
|
|
|
size_t indexId()
|
|
|
|
{
|
|
|
|
static size_t id = 0;
|
|
|
|
++id;
|
|
|
|
return id;
|
|
|
|
}
|
2017-08-28 18:28:43 +02:00
|
|
|
|
|
|
|
enum class IndexType{
|
|
|
|
SINGLE = 0,
|
|
|
|
MULTI = 1,
|
|
|
|
CONT = 2
|
|
|
|
};
|
2017-12-10 18:41:53 +01:00
|
|
|
|
|
|
|
class VirtualIndexWrapperBase
|
2017-02-16 11:20:40 +01:00
|
|
|
{
|
|
|
|
public:
|
2017-12-10 18:41:53 +01:00
|
|
|
DEFAULT_MEMBERS(VirtualIndexWrapperBase);
|
|
|
|
|
|
|
|
virtual IndexType type() const = 0;
|
|
|
|
virtual size_t dim() const = 0;
|
|
|
|
virtual size_t pos() const = 0;
|
|
|
|
virtual size_t max() const = 0;
|
|
|
|
virtual std::shared_ptr<RangeBase> rangePtr() const = 0;
|
2017-12-11 18:49:43 +01:00
|
|
|
virtual std::shared_ptr<VirtualIndexWrapperBase> getPtr(size_t n) const = 0;
|
2017-12-10 18:41:53 +01:00
|
|
|
virtual intptr_t getPtrNum() const = 0;
|
2017-12-11 18:49:43 +01:00
|
|
|
virtual size_t getStepSize(size_t n) const = 0;
|
2017-12-10 18:41:53 +01:00
|
|
|
};
|
2017-08-25 22:03:20 +02:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
template <class I>
|
|
|
|
std::shared_ptr<IndexWrapper<I> > make_viwb(std::shared_ptr<I> idxPtr)
|
|
|
|
{
|
|
|
|
return std::make_shared<IndexWrapper<I> >(idxPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class I>
|
|
|
|
std::shared_ptr<IndexWrapper<I> > make_viwb(const I& idxPtr)
|
|
|
|
{
|
|
|
|
return make_viwb( std::make_shared<I>(idxPtr) );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class I>
|
|
|
|
class IndexWrapper
|
|
|
|
{
|
|
|
|
public:
|
2017-08-25 22:03:20 +02:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
DEFAULT_MEMBERS(IndexWrapper);
|
2017-08-28 18:28:43 +02:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
IndexWrapper(std::shared_ptr<I>& idxPtr);
|
|
|
|
|
|
|
|
virtual IndexType type() const override { return mIdxPtr->type(); }
|
|
|
|
virtual size_t dim() const override { return mIdxPtr->dim(); }
|
|
|
|
virtual size_t pos() const override { return mIdxPtr->pos(); }
|
|
|
|
virtual size_t max() const override { return mIdxPtr->max(); }
|
|
|
|
virtual std::shared_ptr<RangeBase> rangePtr() const override { return mIdxPtr->rangePtr(); }
|
|
|
|
virtual std::shared_ptr<VirtualIndexWrapperBase> getPtr(size_t n) const override { return mIdxPtr->getv(n); }
|
2017-12-11 18:49:43 +01:00
|
|
|
virtual intptr_t getPtrNum() const override { return static_cast<intptr_t>( mIdxPtr.get() ); }
|
|
|
|
virtual size_t getStepSize(size_t n) const override { return mIdxPtr->getStepSize(n); }
|
2017-02-16 11:20:40 +01:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
private:
|
|
|
|
std::shared_ptr<I> mIdxPtr;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class I, typename MetaType>
|
|
|
|
class IndexInterface
|
|
|
|
{
|
|
|
|
public:
|
2017-12-11 18:49:43 +01:00
|
|
|
typedef typename I::RangeType RangeType;
|
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
//DEFAULT_MEMBERS(IndexInterface);
|
2017-08-30 17:56:38 +02:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
I* THIS() { return static_cast<I*>(this); }
|
2017-12-11 18:49:43 +01:00
|
|
|
I const* THIS() const { return static_cast<I const*>(this); }
|
2017-07-26 18:38:11 +02:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
~IndexInterface() = default;
|
|
|
|
|
|
|
|
IndexType type() const { return I::S_type(THIS()); }
|
2017-02-16 11:20:40 +01:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
IndexInterface& operator=(size_t pos) { return I::S_ass_op(THIS(), pos); }
|
|
|
|
IndexInterface& operator++() { return I::S_pp_op(THIS()); }
|
|
|
|
IndexInterface& operator--() { return I::S_mm_op(THIS()); }
|
2017-02-16 11:20:40 +01:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
int pp(std::shared_ptr<IndexInterface>& idxPtr) { return I::S_pp(THIS()); }
|
|
|
|
int mm(std::shared_ptr<IndexInterface>& idxPtr) { return I::S_mm(THIS()); }
|
|
|
|
|
|
|
|
bool operator==(const IndexInterface& in) const;
|
|
|
|
bool operator!=(const IndexInterface& in) const;
|
|
|
|
|
|
|
|
size_t dim() const { return I::S_dim(THIS()); }
|
|
|
|
size_t pos() const;
|
|
|
|
size_t max() const;
|
2017-09-12 18:36:05 +02:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
bool last() const { return I::S_last(THIS()); }
|
|
|
|
bool first() const { return I::S_first(THIS()); }
|
2017-08-30 19:41:49 +02:00
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
std::shared_ptr<RangeBase> vrange() const { return mRangePtr; }
|
|
|
|
std::shared_ptr<RangeType> range() const { return std::dynamic_pointer_cast<RangeType>(mRangePtr); }
|
2017-12-10 18:41:53 +01:00
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
auto getPtr() const -> decltype(I::S_get<N>(THIS())) { return I::S_get<N>(THIS()); }
|
2017-08-30 19:41:49 +02:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
std::shared_ptr<VIWB> getVPtr(size_t n) const { return I::S_getVPtr(THIS(),n); }
|
2017-05-22 18:21:14 +02:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
size_t getStepSize(size_t n) const { return I::S_getStepSize(THIS(),n); }
|
|
|
|
|
|
|
|
operator size_t() const;
|
|
|
|
|
|
|
|
std::string id() const { return I::S_id(THIS()); }
|
2017-08-25 22:03:20 +02:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
MetaType meta() const { return I::S_meta(THIS()); }
|
|
|
|
IndexInterface& at(const MetaType& meta) { return I::S_at(THIS(), meta); }
|
2017-12-11 18:49:43 +01:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
private:
|
2017-02-16 11:20:40 +01:00
|
|
|
|
2017-12-11 18:49:43 +01:00
|
|
|
friend I;
|
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
IndexInterface() { mId = indexId(); }
|
|
|
|
IndexInterface(IndexInterface&& in) = default;
|
|
|
|
IndexInterface& operator=(IndexInterface&& in) = default;
|
|
|
|
|
|
|
|
IndexInterface(const std::shared_ptr<RangeBase>& range, size_t pos);
|
|
|
|
|
2017-07-25 17:46:59 +02:00
|
|
|
std::shared_ptr<RangeBase> mRangePtr;
|
|
|
|
size_t mPos;
|
2017-08-25 22:03:20 +02:00
|
|
|
size_t mId;
|
2017-12-11 18:49:43 +01:00
|
|
|
size_t mMax;
|
2017-02-16 11:20:40 +01:00
|
|
|
};
|
2017-12-10 18:41:53 +01:00
|
|
|
|
2017-02-16 11:20:40 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:35:25 +01:00
|
|
|
/* ========================= *
|
|
|
|
* --- TEMPLATE CODE --- *
|
|
|
|
* ========================= */
|
|
|
|
|
|
|
|
namespace MultiArrayTools
|
|
|
|
{
|
2017-12-11 18:49:43 +01:00
|
|
|
/**********************
|
2017-12-10 18:41:53 +01:00
|
|
|
* IndexInterface *
|
2017-12-11 18:49:43 +01:00
|
|
|
**********************/
|
2017-11-20 21:35:25 +01:00
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
template <class I, typename MetaType>
|
|
|
|
IndexInterface<I,MetaType>::IndexInterface(const std::shared_ptr<RangeBase>& range,
|
2017-11-20 21:35:25 +01:00
|
|
|
size_t pos) : mRangePtr(range),
|
2017-12-11 18:49:43 +01:00
|
|
|
mPos(pos),
|
|
|
|
mMax(mRangePtr->size())
|
2017-11-20 21:35:25 +01:00
|
|
|
{
|
|
|
|
mId = indexId();
|
|
|
|
}
|
2017-12-10 18:41:53 +01:00
|
|
|
|
|
|
|
template <class I, typename MetaType>
|
|
|
|
bool IndexInterface<I,MetaType>::operator==(const IndexInterface& in) const
|
2017-11-20 21:35:25 +01:00
|
|
|
{
|
|
|
|
return in.mPos == mPos and in.mRangePtr.get() == mRangePtr.get();
|
|
|
|
}
|
2017-12-10 18:41:53 +01:00
|
|
|
|
|
|
|
template <class I, typename MetaType>
|
|
|
|
bool IndexInterface<I,MetaType>::operator!=(const IndexInterface& in) const
|
2017-11-20 21:35:25 +01:00
|
|
|
{
|
|
|
|
return in.mPos != mPos or in.mRangePtr.get() != mRangePtr.get();
|
|
|
|
}
|
2017-12-10 18:41:53 +01:00
|
|
|
|
|
|
|
template <class I, typename MetaType>
|
|
|
|
size_t IndexInterface<I,MetaType>::pos() const
|
2017-11-20 21:35:25 +01:00
|
|
|
{
|
|
|
|
return mPos;
|
|
|
|
}
|
|
|
|
|
2017-12-10 18:41:53 +01:00
|
|
|
template <class I, typename MetaType>
|
|
|
|
size_t IndexInterface<I,MetaType>::max() const
|
2017-11-20 21:35:25 +01:00
|
|
|
{
|
2017-12-11 18:49:43 +01:00
|
|
|
return mMax;
|
2017-11-20 21:35:25 +01:00
|
|
|
}
|
2017-12-10 18:41:53 +01:00
|
|
|
|
|
|
|
template <class I, typename MetaType>
|
|
|
|
IndexInterface<I,MetaType>::operator size_t() const
|
2017-11-20 21:35:25 +01:00
|
|
|
{
|
|
|
|
return pos();
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 11:20:40 +01:00
|
|
|
|
|
|
|
#endif
|