cnorxz/src/index_base.h

194 lines
4.9 KiB
C
Raw Normal View History

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
{
size_t indexId()
{
static size_t id = 0;
++id;
return id;
}
enum class IndexType{
SINGLE = 0,
MULTI = 1,
CONT = 2
};
class VirtualIndexWrapperBase
2017-02-16 11:20:40 +01:00
{
public:
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;
virtual VirtualIndexWrapperBase getPtr(size_t n) const = 0;
virtual intptr_t getPtrNum() const = 0;
};
typedef VirtualIndexWrapperBase VIWB;
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:
DEFAULT_MEMBERS(IndexWrapper);
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); }
virtual intptr_t getPtrNum() const override { return static_cast<intptr_t>( mIdxPtr.get() ); };
2017-02-16 11:20:40 +01:00
private:
std::shared_ptr<I> mIdxPtr;
};
template <class I, typename MetaType>
class IndexInterface
{
public:
//DEFAULT_MEMBERS(IndexInterface);
2017-08-30 17:56:38 +02:00
I* THIS() { return static_cast<I*>(this); }
~IndexInterface() = default;
IndexType type() const { return I::S_type(THIS()); }
2017-02-16 11:20:40 +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
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;
bool last() const { return I::S_last(THIS()); }
bool first() const { return I::S_first(THIS()); }
std::shared_ptr<RangeBase> rangePtr() const;
template <size_t N>
auto getPtr() const -> decltype(I::S_get<N>(THIS())) { return I::S_get<N>(THIS()); }
std::shared_ptr<VIWB> getVPtr(size_t n) const { return I::S_getVPtr(THIS(),n); }
2017-05-22 18:21:14 +02: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()); }
MetaType meta() const { return I::S_meta(THIS()); }
IndexInterface& at(const MetaType& meta) { return I::S_at(THIS(), meta); }
2017-02-16 11:20:40 +01:00
protected:
IndexInterface() { mId = indexId(); }
IndexInterface(IndexInterface&& in) = default;
IndexInterface& operator=(IndexInterface&& in) = default;
IndexInterface(const std::shared_ptr<RangeBase>& range, size_t pos);
std::shared_ptr<RangeBase> mRangePtr;
size_t mPos;
size_t mId;
2017-02-16 11:20:40 +01:00
};
2017-02-16 11:20:40 +01:00
}
/* ========================= *
* --- TEMPLATE CODE --- *
* ========================= */
namespace MultiArrayTools
{
/*****************
* IndexInterface *
*****************/
template <class I, typename MetaType>
IndexInterface<I,MetaType>::IndexInterface(const std::shared_ptr<RangeBase>& range,
size_t pos) : mRangePtr(range),
mPos(pos)
{
mId = indexId();
}
template <class I, typename MetaType>
bool IndexInterface<I,MetaType>::operator==(const IndexInterface& in) const
{
return in.mPos == mPos and in.mRangePtr.get() == mRangePtr.get();
}
template <class I, typename MetaType>
bool IndexInterface<I,MetaType>::operator!=(const IndexInterface& in) const
{
return in.mPos != mPos or in.mRangePtr.get() != mRangePtr.get();
}
template <class I, typename MetaType>
size_t IndexInterface<I,MetaType>::pos() const
{
return mPos;
}
template <class I, typename MetaType>
size_t IndexInterface<I,MetaType>::max() const
{
return mRangePtr->size();
}
template <class I, typename MetaType>
std::shared_ptr<RangeBase> IndexInterface<I,MetaType>::rangePtr() const
{
return mRangePtr;
}
template <class I, typename MetaType>
IndexInterface<I,MetaType>::operator size_t() const
{
return pos();
}
}
2017-02-16 11:20:40 +01:00
#endif