Line data Source code
1 : /////////////////////////////////////////////////////////////////////////// 2 : // Spetabaru - Berenger Bramas MPCDF - 2017 3 : // Under LGPL Licence, please you must read the LICENCE file. 4 : /////////////////////////////////////////////////////////////////////////// 5 : #ifndef SPARRAYACCESSOR_HPP 6 : #define SPARRAYACCESSOR_HPP 7 : 8 : #include <type_traits> 9 : #include <vector> 10 : #include <cassert> 11 : #include "small_vector.hpp" 12 : 13 : template <class ObjectType> 14 : class SpArrayAccessor { 15 : small_vector<ObjectType*> dataPtr; 16 : small_vector<long int> dataIdx; 17 : 18 : public: 19 : class ConstIterator{ 20 : const SpArrayAccessor<ObjectType>& view; 21 : long int currentIndex; 22 : 23 132 : explicit ConstIterator(const SpArrayAccessor<ObjectType>& inView, const long int inStartIndex = 0) 24 132 : : view(inView), currentIndex(inStartIndex){ 25 132 : } 26 : public: 27 446 : bool operator!=(const ConstIterator& other) const { 28 446 : return &this->view != &other.view || this->currentIndex != other.currentIndex; 29 : } 30 : 31 380 : const ObjectType* operator*() const { 32 380 : return view.dataPtr[currentIndex]; 33 : } 34 : 35 380 : ConstIterator& operator++() { 36 380 : currentIndex += 1; 37 380 : return *this; 38 : } 39 : 40 : friend SpArrayAccessor<ObjectType>; 41 : }; 42 : 43 : class Iterator{ 44 : SpArrayAccessor<ObjectType>& view; 45 : long int currentIndex; 46 : 47 : explicit Iterator(SpArrayAccessor<ObjectType>& inView, const long int inStartIndex = 0) 48 : : view(inView), currentIndex(inStartIndex){ 49 : } 50 : public: 51 : bool operator!=(const Iterator& other) const { 52 : return &this->view != &other.view || this->currentIndex != other.currentIndex; 53 : } 54 : 55 : const ObjectType* operator*() const { 56 : return view.dataPtr[currentIndex]; 57 : } 58 : 59 : ObjectType* operator*() { 60 : return view.dataPtr[currentIndex]; 61 : } 62 : 63 : Iterator& operator++() { 64 : currentIndex += 1; 65 : return *this; 66 : } 67 : 68 : friend SpArrayAccessor<ObjectType>; 69 : }; 70 : 71 : template <class VHC> 72 66 : SpArrayAccessor(ObjectType* inHandle, VHC&& inView){ 73 446 : for(const long int idx : inView){ 74 380 : ObjectType* ptr = &inHandle[idx]; 75 380 : dataPtr.push_back(ptr); 76 380 : dataIdx.push_back(idx); 77 : } 78 66 : } 79 : 80 232 : SpArrayAccessor(const SpArrayAccessor&) = default; 81 : SpArrayAccessor(SpArrayAccessor&&) = default; 82 : SpArrayAccessor& operator=(const SpArrayAccessor&) = delete; 83 : SpArrayAccessor& operator=(SpArrayAccessor&&) = delete; 84 : 85 110 : void updatePtr(const long int position, ObjectType* ptr){ 86 110 : assert(position < static_cast<long int>(dataPtr.size())); 87 110 : dataPtr[position] = ptr; 88 110 : } 89 : 90 : template<typename ArrayTypeSFINAE = ObjectType> 91 : typename std::enable_if<!std::is_const<ArrayTypeSFINAE>::value, ArrayTypeSFINAE&>::type 92 : getAt(const long int inIndex){ 93 : return *dataPtr[inIndex]; 94 : } 95 : 96 560 : const ObjectType& getAt(const long int inIndex) const{ 97 560 : return *dataPtr[inIndex]; 98 : } 99 : 100 : long int getIndexAt(const long int inIndex) const{ 101 : return dataIdx[inIndex]; 102 : } 103 : 104 782 : long int getSize() const{ 105 782 : return static_cast<long int>(dataPtr.size()); 106 : } 107 : 108 66 : ConstIterator begin() const { 109 66 : return ConstIterator(*this); 110 : } 111 : 112 66 : ConstIterator end() const { 113 66 : return ConstIterator(*this, dataPtr.size()); 114 : } 115 : 116 : template<typename ArrayTypeSFINAE = ObjectType> 117 : typename std::enable_if<!std::is_const<ArrayTypeSFINAE>::value, Iterator>::type 118 : begin() { 119 : return Iterator(*this); 120 : } 121 : 122 : template<typename ArrayTypeSFINAE = ObjectType> 123 : typename std::enable_if<!std::is_const<ArrayTypeSFINAE>::value, Iterator>::type 124 : end() { 125 : return Iterator(*this, dataPtr.size()); 126 : } 127 : }; 128 : 129 : #endif // SPARRAYVIEW_HPP