CPARK 1.0
A light-weighted, distributed computing framework for C++ that offers a fast and general-purpose large data processing solution.
Loading...
Searching...
No Matches
generator_rdd.h
1#ifndef CPARK_GENERATOR_RDD_H
2#define CPARK_GENERATOR_RDD_H
3
4#include "base_rdd.h"
5
6namespace cpark {
7
24template <typename Num, typename Func, typename T = std::invoke_result_t<Func, Num>>
25requires std::is_arithmetic_v<Num>&& std::invocable<Func, Num>&&
26 std::convertible_to<std::invoke_result_t<Func, Num>, T> class GeneratorRdd
27 : public BaseRdd<GeneratorRdd<Num, Func, T>> {
28public:
30 friend Base;
31
35 class Iterator : std::forward_iterator_tag {
36 public:
37 using difference_type = std::ptrdiff_t;
38 using value_type = T;
39
40 Iterator() = default;
41
43 Iterator(const Func* func, Num i) : func_{func}, i_{i} {}
44
46 // Not sure if returning value_type would satisfy input_range requirements.
47 // Works for now.
48 value_type operator*() const { return (*func_)(i_); }
49
52 ++i_;
53 return *this;
54 }
55
58 auto old = *this;
59 ++i_;
60 return old;
61 }
62
64 bool operator==(const Iterator& other) const { return func_ == other.func_ && i_ == other.i_; }
65
66 bool operator!=(const Iterator& other) const { return !(*this == other); }
67
68 private:
69 const Func* func_;
70 Num i_{};
71 };
72
73public:
74 constexpr GeneratorRdd(Num begin, Num end, Func func, ExecutionContext* context)
75 : Base{context}, func_{std::move(func)}, begin_{begin}, end_{end} {
76 static_assert(concepts::Rdd<GeneratorRdd<Num, Func, T>>,
77 "Instance of GeneratorRdd does not satisfy Rdd concept.");
78
79 // Creates the splits for this Rdd. Each split contains two iterators that can be used to
80 // compute the corresponding value.
81 for (size_t i : std::views::iota(size_t{0}, Base::splits_num_)) {
82 size_t total_size = static_cast<size_t>(end_ - begin_);
83 size_t split_size = (total_size + Base::splits_num_ - 1) / Base::splits_num_;
84 auto b = Iterator{&func, begin_ + static_cast<Num>(std::min(total_size, i * split_size))};
85 auto e =
86 Iterator{&func, begin_ + static_cast<Num>(std::min(total_size, (i + 1) * split_size))};
87 splits_.emplace_back(std::ranges::subrange{b, e}, Base::context_);
88 }
89 }
90
91 // Explicitly define default copy constrictor and assignment operator,
92 // because some linters or compilers can not define implicit copy constructors for this class,
93 // though they are supposed to do so.
94 // TODO: find out why.
95 constexpr GeneratorRdd(const GeneratorRdd&) = default;
96 GeneratorRdd& operator=(const GeneratorRdd&) = default;
97
98private:
99 constexpr auto beginImpl() const { return std::ranges::begin(splits_); }
100
101 constexpr auto endImpl() const { return std::ranges::end(splits_); }
102
103private:
104 Func func_;
105 Num begin_, end_;
106 // A vector holding the splits for this rdd.
107 std::vector<ViewSplit<std::ranges::subrange<Iterator>>> splits_{};
108};
109
// end of c_Generator
111
112} // namespace cpark
113
114#endif //CPARK_GENERATOR_RDD_H
Definition base_rdd.h:522
auto end() const
Definition base_rdd.h:579
auto begin() const
Definition base_rdd.h:571
Definition base_rdd.h:94
Definition generator_rdd.h:35
Iterator(const Func *func, Num i)
Definition generator_rdd.h:43
Iterator operator++(int)
Definition generator_rdd.h:57
value_type operator*() const
Definition generator_rdd.h:48
Iterator & operator++()
Definition generator_rdd.h:51
bool operator==(const Iterator &other) const
Definition generator_rdd.h:64
Definition generator_rdd.h:27