DASH  0.3.0
Range.h
1 #ifndef DASH__RANGES_H__INCLUDED
2 #define DASH__RANGES_H__INCLUDED
3 
49 #include <dash/Types.h>
50 #include <dash/Meta.h>
51 
52 #include <type_traits>
53 
54 
55 namespace dash {
56 
57 
58 #ifndef DOXYGEN
59 
60 
61 // Related: boost::range
62 //
63 // https://github.com/boostorg/range/tree/develop/include/boost/range
64 //
65 
66 template <typename ViewT>
67 struct view_traits;
68 
69 // Forward-declaration
70 template <typename ViewType>
71 class IndexSetIdentity;
72 
73 // Forward-declaration
74 template <typename Iterator, typename Sentinel = Iterator>
75 class IteratorRange;
76 
77 // Forward-declaration
78 template <typename Iterator, typename Sentinel>
79 class IteratorRange<Iterator *, Sentinel *>;
80 
81 #endif
82 
83 
84 
88 template <typename RangeType>
89 constexpr auto begin(RangeType && range)
90  -> decltype(std::forward<RangeType>(range).begin()) {
91  return std::forward<RangeType>(range).begin();
92 }
93 
97 template <class RangeType>
98 constexpr auto end(RangeType && range)
99  -> decltype(std::forward<RangeType>(range).end()) {
100  return std::forward<RangeType>(range).end();
101 }
102 
106 template <class RangeType>
107 constexpr auto
108 size(RangeType && r)
109  -> decltype(std::forward<RangeType>(r).size()) {
110  return std::forward<RangeType>(r).size();
111 }
112 
113 
114 namespace detail {
115 
116 template<typename T>
117 struct _is_range_type
118 {
119 private:
120  typedef char yes;
121  typedef long no;
122 
123  typedef typename std::remove_reference<
124  typename std::remove_const<T>::type
125  >::type
126  ValueT;
127 
128 #ifdef __TODO__
129 private:
130  // Test if dash::begin(x) is valid expression:
131  template <typename C> static yes has_dash_begin(
132  decltype(
133  dash::begin(
134  std::move(std::declval<T>())
135  )
136  ) * );
137  template <typename C> static no has_dash_begin(...);
138 
139  // Test if dash::end(x) is valid expression:
140  template <typename C> static yes has_dash_end(
141  decltype(
142  dash::end(
143  std::move(std::declval<T>())
144  )
145  ) * );
146  template <typename C> static no has_dash_end(...);
147 
148 public:
149  enum { value = (
150  sizeof(has_dash_begin(static_cast<ValueT*>(nullptr))) == sizeof(yes)
151  && sizeof(has_dash_end(static_cast<ValueT*>(nullptr))) == sizeof(yes)
152  ) };
153 
154  //template<typename C, typename begin_decl =
155  // decltype(
156  // dash::begin(
157  // std::move(std::declval<T>())
158  // )) >
159  //static yes has_dash_begin(C *);
160 #endif
161  // Test if x.begin() is valid expression and type x::iterator is
162  // defined:
163  template<typename C, typename C::iterator (C::*)() = &C::begin >
164  static yes has_begin(C *);
165  static no has_begin(...);
166 
167  template<typename C, typename C::iterator (C::*)() const = &C::begin >
168  static yes has_const_begin(C *);
169  static no has_const_begin(...);
170 
171  // Test if x.end() is valid expression and type x::iterator is
172  // defined:
173  template<typename C, typename C::iterator (C::*)() = &C::end >
174  static yes has_end(C *);
175  static no has_end(...);
176 
177  template<typename C, typename C::iterator (C::*)() const = &C::end >
178  static yes has_const_end(C *);
179  static no has_const_end(...);
180 
181 public:
182  enum { value = (
183  ( sizeof(has_begin(static_cast<ValueT*>(nullptr)))
184  == sizeof(yes)
185  || sizeof(has_const_begin(static_cast<ValueT*>(nullptr)))
186  == sizeof(yes) )
187  &&
188  ( sizeof(has_end(static_cast<ValueT*>(nullptr)))
189  == sizeof(yes)
190  || sizeof(has_const_end(static_cast<ValueT*>(nullptr)))
191  == sizeof(yes) )
192  ) };
193 };
194 
195 } // namespace detail
196 
242 template <class RangeType>
243 struct is_range : dash::detail::_is_range_type<RangeType> { };
244 
245 template <
246  typename RangeType,
247  typename Iterator,
248  typename Sentinel = Iterator >
249 class RangeBase {
250 public:
251  typedef Iterator iterator;
252  typedef Sentinel sentinel;
253  typedef dash::default_index_t index_type;
254 protected:
255  RangeType & derived() {
256  return static_cast<RangeType &>(*this);
257  }
258  const RangeType & derived() const {
259  return static_cast<const RangeType &>(*this);
260  }
261 };
262 
263 
267 template <
268  typename IteratorT,
269  typename SentinelT >
270 struct view_traits<dash::IteratorRange<IteratorT, SentinelT> > {
271 private:
273 public:
274  typedef RangeT origin_type;
275  typedef RangeT domain_type;
276  typedef RangeT image_type;
277  typedef RangeT global_type;
278  typedef typename RangeT::local_type local_type;
279  typedef typename RangeT::index_type index_type;
280  typedef typename RangeT::index_set_type index_set_type;
281 
284  typedef std::integral_constant<bool, false> is_projection;
285  typedef std::integral_constant<bool, false> is_view;
287  typedef std::integral_constant<bool, true> is_origin;
291  typedef std::integral_constant<bool, std::is_same<
292  RangeT,
293  typename RangeT::local_type
294  >::value > is_local;
295 };
296 
301 template <
302  typename Iterator,
303  typename Sentinel >
305 : public RangeBase< IteratorRange<Iterator, Sentinel>,
306  Iterator,
307  Sentinel >
308 {
310 
311  Iterator & _begin;
312  Sentinel & _end;
313 
314 public:
315  typedef Iterator iterator;
316  typedef Sentinel sentinel;
317  typedef dash::default_index_t index_type;
318  typedef typename iterator::pattern_type pattern_type;
319  typedef dash::IndexSetIdentity<self_t> index_set_type;
320  typedef typename iterator::value_type value_type;
321 
322  typedef typename
323  std::conditional<
324  std::is_pointer<iterator>::value,
325  iterator,
326  typename iterator::local_type
327  >::type
328  local_iterator;
329 
330  typedef typename
331  std::conditional<
332  std::is_pointer<sentinel>::value,
333  iterator,
334  typename sentinel::local_type
335  >::type
336  local_sentinel;
337 
339 
340 
341 public:
342  template <class Container>
343  constexpr explicit IteratorRange(Container && c)
344  : _begin(c.begin())
345  , _end(c.end())
346  { }
347 
348  constexpr IteratorRange(iterator & begin, sentinel & end)
349  : _begin(begin)
350  , _end(end)
351  { }
352 
353  constexpr iterator begin() const { return _begin; }
354  constexpr iterator end() const { return _end; }
355 
356  constexpr const local_type local() const {
357  return local_type(
358  _begin.local(),
359  _end.local()
360  );
361  }
362 
363  constexpr const pattern_type & pattern() const {
364  return _begin.pattern();
365  }
366 
367  constexpr index_set_type index_set() const {
368  return index_set_type(*this);
369  }
370 };
371 
372 
377 template <
378  typename LocalIterator,
379  typename LocalSentinel >
380 class IteratorRange<LocalIterator *, LocalSentinel *>
381 : public RangeBase<
383  LocalIterator *,
384  LocalSentinel * >
385 {
387 
388  LocalIterator * _begin;
389  LocalSentinel * _end;
390 
391 public:
392  typedef LocalIterator * iterator;
393  typedef LocalSentinel * sentinel;
394  typedef dash::default_index_t index_type;
395  typedef dash::IndexSetIdentity<self_t> index_set_type;
396  typedef LocalIterator value_type;
397 
398  typedef iterator local_iterator;
399  typedef sentinel local_sentinel;
400 
402 
403 public:
404  template <class Container>
405  constexpr explicit IteratorRange(Container && c)
406  : _begin(c.begin())
407  , _end(c.end())
408  { }
409 
410  constexpr IteratorRange(iterator & begin, sentinel & end)
411  : _begin(begin)
412  , _end(end)
413  { }
414 
415  constexpr iterator begin() const { return _begin; }
416  constexpr iterator end() const { return _end; }
417 
418  constexpr const local_type & local() const {
419  return *this;
420  }
421 
422  constexpr index_set_type index_set() const {
423  return index_set_type(*this);
424  }
425 };
426 
431 template <class Iterator, class Sentinel>
434  const Iterator & begin,
435  const Sentinel & end) {
437  begin,
438  end);
439 }
440 
445 template <class Iterator, class Sentinel>
448  Iterator * begin,
449  Sentinel * end) {
451  begin,
452  end);
453 }
454 
459 template <class Iterator, class Sentinel>
462  Iterator & begin,
463  Sentinel & end) {
465  begin,
466  end);
467 }
468 
469 } // namespace dash
470 
471 #endif // DASH__RANGES_H__INCLUDED
constexpr dash::IteratorRange< const Iterator, const Sentinel > make_range(const Iterator &begin, const Sentinel &end)
Adapter utility function.
Definition: Range.h:433
size_t size()
Return the number of units in the global team.
constexpr auto end(RangeType &&range) -> decltype(std::forward< RangeType >(range).end())
Definition: Range.h:98
constexpr auto local(ViewType &v) -> typename std::enable_if<(std::is_pointer< typename ViewType::iterator >::value||(dash::view_traits< ViewValueT >::is_local::value)), ViewType &>::type
Definition: Local.h:28
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
std::integral_constant< bool, false > is_projection
Whether the view type is a projection (has less dimensions than the view&#39;s domain type)...
Definition: Range.h:284
constexpr auto begin(RangeType &&range) -> decltype(std::forward< RangeType >(range).begin())
Definition: Range.h:89
internal::default_signed_index default_index_t
Signed integer type used as default for index values.
Definition: Types.h:59
View type traits.
Definition: ViewTraits.h:31
Adapter template for range concept, wraps begin and end iterators in range type.
Definition: Range.h:304
std::integral_constant< bool, std::is_same< RangeT, typename RangeT::local_type >::value > is_local
Whether the view / container type is a local view.
Definition: Range.h:294
Definition of type trait dash::is_range<T> with static member value indicating whether type T is a mo...
Definition: Range.h:243
std::integral_constant< bool, true > is_origin
Whether the view is the origin domain.
Definition: Range.h:287
Specialization of adapter template for range concept, wraps begin and end pointers in range type...
Definition: Range.h:380