DASH  0.3.0
Generate.h
1 #ifndef DASH__ALGORITHM__GENERATE_H__
2 #define DASH__ALGORITHM__GENERATE_H__
3 
4 #include <dash/algorithm/LocalRange.h>
5 #include <dash/algorithm/Operation.h>
6 #include <dash/iterator/GlobIter.h>
7 
9 
10 #include <algorithm>
11 
12 namespace dash {
13 
31 template <typename GlobInputIt, class UnaryFunction>
32 void generate(
34  GlobInputIt first,
36  GlobInputIt last,
38  UnaryFunction gen)
39 {
41  static_assert(
42  iterator_traits::is_global_iterator::value,
43  "must be a global iterator");
45  auto lrange = dash::local_range(first, last);
46  auto lfirst = lrange.begin;
47  auto llast = lrange.end;
48 
49  std::generate(lfirst, llast, gen);
50 }
51 
70 template <typename GlobInputIt, class UnaryFunction>
73  GlobInputIt first,
75  GlobInputIt last,
77  UnaryFunction gen)
78 {
80  static_assert(
81  iterator_traits::is_global_iterator::value,
82  "must be a global iterator");
84  auto index_range = dash::local_index_range(first, last);
85  auto lbegin_index = index_range.begin;
86  auto lend_index = index_range.end;
87 
88  if (lbegin_index != lend_index) {
89  // Pattern from global begin iterator:
90  auto& pattern = first.pattern();
91  auto first_offset = first.pos();
92  // Iterate local index range:
93  for (auto lindex = lbegin_index; lindex != lend_index; ++lindex) {
94  auto gindex = pattern.global(lindex);
95  auto element_it = first + (gindex - first_offset);
96  *element_it = gen(gindex);
97  }
98  }
99 }
100 
101 } // namespace dash
102 
103 #endif // DASH__ALGORITHM__GENERATE_H__
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
LocalRange< const typename GlobIterType::value_type > local_range(const GlobIterType &first, const GlobIterType &last)
Resolves the local address range between global iterators.
Definition: LocalRange.h:295
Returns second operand.
Definition: Operation.h:201
void generate_with_index(GlobInputIt first, GlobInputIt last, UnaryFunction gen)
Assigns each element in range [first, last) a value generated by the given function object g...
Definition: Generate.h:71
void generate(GlobInputIt first, GlobInputIt last, UnaryFunction gen)
Assigns each element in range [first, last) a value generated by the given function object g...
Definition: Generate.h:32
std::enable_if< !GlobInputIter::has_view::value, LocalIndexRange< typename GlobInputIter::pattern_type::index_type >>::type local_index_range(const GlobInputIter &first, const GlobInputIter &last)
Resolves the local index range between global iterators.
Definition: LocalRange.h:101