cnorxz/src/ranges/index_base.h

142 lines
3.5 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
2017-12-17 17:40:55 +01:00
#include "rbase_def.h"
2017-02-16 11:20:40 +01:00
#include "range_base.h"
2017-12-17 17:40:55 +01:00
#include "index_type.h"
#include "vindex_wrapper.h"
2017-10-30 18:00:07 +01:00
2017-02-16 11:20:40 +01:00
namespace MultiArrayTools
{
template <class I, typename MetaType>
class IndexInterface
{
public:
2017-12-12 11:15:39 +01:00
//typedef typename I::RangeType RangeType;
//DEFAULT_MEMBERS(IndexInterface);
2017-08-30 17:56:38 +02:00
2017-12-17 14:16:37 +01:00
I& THIS() { return static_cast<I&>(*this); }
I const& THIS() const { return static_cast<I const&>(*this); }
~IndexInterface() = default;
2017-12-17 14:16:37 +01:00
IndexType type() const { return THIS().type(); }
2017-02-16 11:20:40 +01:00
2017-12-17 14:16:37 +01:00
I& operator=(size_t pos) { return THIS() = pos; }
I& operator++() { return THIS()++; }
I& operator--() { return THIS()--;}
2017-02-16 11:20:40 +01:00
2017-12-17 14:16:37 +01:00
int pp(std::intptr_t idxPtrNum) { return THIS().pp(idxPtrNum); }
int mm(std::intptr_t idxPtrNum) { return THIS().mm(idxPtrNum); }
bool operator==(const IndexInterface& in) const;
bool operator!=(const IndexInterface& in) const;
2017-12-17 14:16:37 +01:00
size_t dim() const { return THIS().dim(); }
size_t pos() const;
size_t max() const;
2017-12-17 14:16:37 +01:00
bool last() const { return THIS().last(); }
bool first() const { return THIS().first(); }
2017-12-12 11:15:39 +01:00
std::shared_ptr<RangeBase> vrange() const { return mRangePtr; }
2017-12-12 11:15:39 +01:00
/*auto range() const -> decltype( I::S_range(THIS()) ) { return I::S_range(THIS()); }
template <size_t N>
2017-12-12 11:15:39 +01:00
auto getPtr() const -> decltype(I::template S_get<N>(THIS()))
{ return I::template S_get<N>(THIS()); }
*/
2017-12-17 14:16:37 +01:00
std::shared_ptr<VIWB> getVPtr(size_t n) const { return THIS().getVPtr(n); }
2017-05-22 18:21:14 +02:00
2017-12-17 14:16:37 +01:00
size_t getStepSize(size_t n) const { return THIS().getStepSize(n); }
operator size_t() const;
2017-12-17 14:16:37 +01:00
std::string id() const { return THIS().id(); }
2017-12-17 14:16:37 +01:00
MetaType meta() const { return THIS().meta(); }
I& at(const MetaType& meta) { return THIS().at(meta); }
2017-12-17 14:16:37 +01:00
void print(size_t offset = 0) const { THIS().print(offset); }
private:
2017-02-16 11:20:40 +01:00
friend I;
IndexInterface() { mId = indexId(); }
IndexInterface(const IndexInterface& in) = default;
IndexInterface& operator=(const IndexInterface& in) = default;
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;
size_t mMax;
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),
mMax(mRangePtr->size())
{
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 mMax;
}
template <class I, typename MetaType>
IndexInterface<I,MetaType>::operator size_t() const
{
return pos();
}
}
2017-02-16 11:20:40 +01:00
#endif