astrotypes  0.0
SliceIterator.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018 PulsarSearchSoft
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef PSS_ASTROTYPES_MULTIARRAY_SLICEITERATOR_H
25 #define PSS_ASTROTYPES_MULTIARRAY_SLICEITERATOR_H
26 
27 #include "Slice.h"
28 #include "OverlaySliceIterator.h"
29 #include "detail/SlicePosition.h"
30 #include <utility>
31 #include <iterator>
32 
33 namespace pss {
34 namespace astrotypes {
35  // pre declarations
36  template<bool is_const, typename ParentT, template<typename> class SliceMixin, typename Dimension, typename... Dimensions>
37  class Slice;
38 
39  template<typename SliceType, bool is_const> class SliceIterator;
40 
41  namespace multiarray {
42  template<typename T, typename D, std::size_t> class ReducedRankSlice;
43  } // namespace multiarray
44 
45 } // namespace pss
46 } // namespace astrotypes
47 
48 namespace pss {
49 namespace astrotypes {
50 
56 template<typename DerivedType, typename SliceType, bool is_const, int Rank>
57 class SliceIteratorBase : public SliceIteratorBase<DerivedType, SliceType, is_const, 1>
58 {
59  private:
60  template<typename D, typename S, bool, int>
61  friend class SliceIteratorBase;
62 
65  typedef typename std::conditional<is_const, typename SliceType::Parent::const_iterator, typename SliceType::Parent::iterator>::type parent_iterator;
66 
67  protected:
68  typedef typename std::conditional<is_const, SliceType const, SliceType>::type SliceT;
69  typedef typename BaseT::ImplT ImplT; // to allow friend decalariont
70 
71  public:
72  typedef typename std::iterator_traits<parent_iterator>::value_type value_type;
73  typedef typename std::iterator_traits<parent_iterator>::reference reference;
74  typedef typename std::iterator_traits<parent_iterator>::pointer pointer;
75  typedef typename std::iterator_traits<parent_iterator>::difference_type difference_type;
76  typedef typename std::forward_iterator_tag iterator_category;
77 
78  public:
79  SliceIteratorBase(SliceT&);
81 
82  DerivedType& operator++();
83 
84  difference_type operator-(SelfType const&) const;
85 
86  SliceT const& slice() const;
87 
88  protected:
89  SliceT& _slice;
91 };
92 
93 template<typename DerivedType, typename SliceType, bool is_const>
94 class SliceIteratorBase<DerivedType, SliceType, is_const, 1>
95 {
96  template<typename D, typename S, bool, int>
97  friend class SliceIteratorBase;
98 
100  typedef typename std::conditional<is_const, typename SliceType::Parent::const_iterator, typename SliceType::Parent::iterator>::type parent_iterator;
101 
102  protected:
103  typedef typename std::conditional<is_const, SliceType const, SliceType>::type SliceT;
104  typedef SelfType ImplT; // to allow friend decalariont
105 
106  public:
107  typedef typename std::iterator_traits<parent_iterator>::value_type value_type;
108  typedef typename std::iterator_traits<parent_iterator>::reference reference;
109  typedef typename std::iterator_traits<parent_iterator>::pointer pointer;
110  typedef typename std::iterator_traits<parent_iterator>::difference_type difference_type;
111  typedef typename std::iterator_traits<parent_iterator>::iterator_category iterator_category;
112 
113  public:
114  SliceIteratorBase(SliceT&);
116 
117  DerivedType& operator++();
118  DerivedType& operator++(int);
119 
120  template<typename D, bool const_val>
122 
123  template<typename D, bool const_val>
125 
127  const reference operator*() const;
128 
142  template<typename DataT>
143  inline OverlaySliceIterator<DataT, DerivedType> operator()(DataT&) const;
144 
146  static DerivedType create_end(SliceT& slice);
147 
148  difference_type operator-(SelfType const&) const;
149 
150  protected:
151  parent_iterator _current;
152 };
153 
154 template<typename SliceType, bool is_const>
155 class SliceIterator;
156 
157 template<bool is_const, typename ParentT, template<typename> class SliceMixin, typename Dimension, typename... Dimensions, bool is_const2>
158 class SliceIterator<Slice<is_const2, ParentT, SliceMixin, Dimension, Dimensions...>, is_const>
159  : public SliceIteratorBase<SliceIterator<Slice<is_const2, ParentT, SliceMixin, Dimension, Dimensions...>, is_const>
160  , Slice<is_const2, ParentT, SliceMixin, Dimension, Dimensions...>, is_const
161  , Slice<is_const2, ParentT, SliceMixin, Dimension, Dimensions...>::rank>
162 {
163  protected:
164  typedef Slice<is_const2, ParentT, SliceMixin, Dimension, Dimensions...> SliceType;
165  typedef typename std::conditional<is_const, SliceType const, SliceType>::type SliceT;
166 
167  public:
168  // made available for Fried definitions in Slice class
170  typedef SliceIteratorBase<SliceIterator<SliceType, is_const>, SliceType, is_const, 1> ImplT;
171 
172  public:
173  SliceIterator(SliceT& s) : BaseT(s) {}
174  SliceIterator(BaseT const& b) : BaseT(b) {}
175  SliceIterator(BaseT&& b) : BaseT(std::forward<BaseT>(b)) {}
176 };
177 
178 // handle mixin types
179 template<bool is_const, typename ParentT, template<typename> class SliceMixin, typename Dimension, typename... Dimensions, bool is_const2>
180 class SliceIterator<SliceMixin<Slice<is_const, ParentT, SliceMixin, Dimension, Dimensions...>>, is_const2> : public SliceIteratorBase<SliceIterator<SliceMixin<Slice<is_const, ParentT, SliceMixin, Dimension, Dimensions...>>, is_const2>, Slice<is_const, ParentT, SliceMixin, Dimension, Dimensions...>, is_const2, Slice<is_const, ParentT, SliceMixin, Dimension, Dimensions...>::rank>
181 {
182  protected:
183  typedef Slice<is_const, ParentT, SliceMixin, Dimension, Dimensions...> SliceType;
184  typedef typename std::conditional<is_const2, SliceType const, SliceType>::type SliceT;
185 
186  public:
187  typedef SliceIteratorBase<SliceIterator<SliceMixin<SliceType>, is_const2>, SliceType, is_const2, SliceType::rank> BaseT;
188  typedef SliceIteratorBase<SliceIterator<SliceMixin<SliceType>, is_const2>, SliceType, is_const2, 1> ImplT;
189 
190  public:
191  SliceIterator(SliceT& s) : BaseT(s) {}
192 };
193 
194 template<typename T, std::size_t B, typename SliceType, bool is_const>
195 class SliceIterator<multiarray::ReducedRankSlice<SliceType, T, B>, is_const> : public SliceIterator<SliceType, is_const>
196 {
198 
199  protected:
200  typedef typename ActualBaseT::SliceT SliceT; // Inner level of mixin
201 
202  public:
203  typedef typename ActualBaseT::BaseT BaseT; // required for friend access
204  typedef typename ActualBaseT::ImplT ImplT; // required for friend aceess
205 
206  public:
207  SliceIterator(SliceT& s);
208  SliceIterator(BaseT const& b) : ActualBaseT(b) {}
209  SliceIterator(BaseT&& b) : ActualBaseT(std::forward<BaseT>(b)) {}
210 
211  SliceIterator& operator++();
212  SliceIterator& operator++(int);
213 
214  static SliceIterator create_end(SliceT& slice) { return SliceIterator(ActualBaseT::create_end(slice)); }
215 };
216 
217 // This is a wrapper class that ensures we return the types we expect to see
218 template<template<typename, typename...> class Mixin, typename... Ts, typename SliceType, bool is_const>
219 class SliceIterator<Mixin<SliceType, Ts...>, is_const> : public SliceIterator<SliceType, is_const>
220 {
222 
223  protected:
224  typedef typename ActualBaseT::SliceT SliceT; // Inner level of mixin
225 
226  public:
227  typedef typename ActualBaseT::BaseT BaseT; // required for friend access
228  typedef typename ActualBaseT::ImplT ImplT; // required for friend aceess
229 
230  public:
231  SliceIterator(SliceT& s) : ActualBaseT(s) {};
232  SliceIterator(BaseT const& b) : ActualBaseT(b) {}
233  SliceIterator(BaseT&& b) : ActualBaseT(std::forward<BaseT>(b)) {}
234 
235  SliceIterator& operator++();
236  SliceIterator& operator++(int);
237 
238  static SliceIterator create_end(SliceT& slice) { return SliceIterator(ActualBaseT::create_end(slice)); }
239 };
240 
241 /*
242  * @brief remove any mixins from the tyeps list
243  */
244 template<typename SliceType>
246 {
247  typedef SliceType type;
248 };
249 
250 template<typename SliceType, typename D, std::size_t Rank>
251 struct SliceMixinRemover<multiarray::ReducedRankSlice<SliceType, D, Rank>>
252 {
254 };
255 
256 template<template<typename> class Mixin, typename SliceType>
257 struct SliceMixinRemover<Mixin<SliceType>>
258 {
260 };
261 
262 /*
263  * @brief add layers in the mixin structure of the SliceType
264  */
265 template<typename ReplacementSliceType, typename SliceIterator>
267 
268 template<typename ReplacementSliceType
269  , typename SliceType
270  , bool is_const>
271 struct SliceIteratorHelper<ReplacementSliceType, SliceIterator<SliceType, is_const>>
272 {
274 };
275 
276 
277 // remove the SliceMixin
278 template<typename ReplacementSliceType
279  , template<typename> class SliceMixin
280  , typename SliceType
281  , bool is_const>
282 struct SliceIteratorHelper<ReplacementSliceType, SliceIterator<SliceMixin<SliceType>, is_const>>
283 {
285 };
286 
287 } // namespace astrotypes
288 } // namespace pss
289 #include "detail/SliceIterator.cpp"
290 
291 #endif // PSS_ASTROTYPES_MULTIARRAY_SLICEITERATOR_H
std::iterator_traits< parent_iterator >::value_type value_type
std::iterator_traits< parent_iterator >::reference reference
Definition: SliceIterator.h:73
difference_type operator-(SelfType const &) const
std::forward_iterator_tag iterator_category
Definition: SliceIterator.h:76
SliceIteratorBase< SliceIterator< SliceType, is_const >, SliceType, is_const, SliceType::rank > BaseT
SliceIteratorBase< SliceIterator< SliceType, is_const >, SliceType, is_const, 1 > ImplT
SliceIteratorBase< SliceIterator< SliceMixin< SliceType >, is_const2 >, SliceType, is_const2, SliceType::rank > BaseT
std::iterator_traits< parent_iterator >::difference_type difference_type
std::iterator_traits< parent_iterator >::pointer pointer
Definition: SliceIterator.h:74
SliceT const & slice() const
multiarray::ReducedRankSlice< typename SliceMixinRemover< SliceType >::type, D, Rank > type
std::conditional< is_const, SliceType const, SliceType >::type SliceT
SliceIteratorBase< SliceIterator< SliceMixin< SliceType >, is_const2 >, SliceType, is_const2, 1 > ImplT
STL namespace.
Representation of a Slice through a Data Array.
Definition: Slice.h:105
std::iterator_traits< parent_iterator >::difference_type difference_type
Definition: SliceIterator.h:75
std::iterator_traits< parent_iterator >::reference reference
Overlays an iterator from one data structure onto that of another.
bool operator!=(PointerAllocator< FirstType > const &, PointerAllocator< SecondType > const &) noexcept
Allows to test for diversity of PointerAllocator objects.
std::iterator_traits< parent_iterator >::iterator_category iterator_category
bool operator==(PointerAllocator< FirstType > const &, PointerAllocator< SecondType > const &) noexcept
Allows to test for equivalence of PointerAllocator objects.
std::iterator_traits< parent_iterator >::value_type value_type
Definition: SliceIterator.h:72
std::iterator_traits< parent_iterator >::pointer pointer
SlicePosition< SliceT::rank > _pos
Definition: SliceIterator.h:90
Class to Iterate over a Slice.
Definition: SliceIterator.h:57
std::conditional< is_const, SliceType const, SliceType >::type SliceT
Definition: SliceIterator.h:68