Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
group_by_iterator.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/api/group_by_iterator.hpp
3  *
4  * DIANode for a groupby operation. Performs the actual groupby operation
5  *
6  * Part of Project Thrill - http://project-thrill.org
7  *
8  * Copyright (C) 2015 Huyen Chau Nguyen <[email protected]>
9  *
10  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
11  ******************************************************************************/
12 
13 #pragma once
14 #ifndef THRILL_API_GROUP_BY_ITERATOR_HEADER
15 #define THRILL_API_GROUP_BY_ITERATOR_HEADER
16 
19 #include <thrill/common/logger.hpp>
21 #include <thrill/data/file.hpp>
22 
23 #include <algorithm>
24 #include <utility>
25 #include <vector>
26 
27 namespace thrill {
28 namespace api {
29 
30 //! \ingroup api_layer
31 //! \{
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 // forward declarations for friend classes
36 template <typename ValueType,
37  typename KeyExtractor, typename GroupFunction, typename HashFunction,
38  bool UseLocationDetection>
40 
41 template <typename ValueType,
42  typename KeyExtractor, typename GroupFunction>
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 
47 template <typename ValueType, typename KeyExtractor, typename Comparator>
49 {
50  template <typename T1,
51  typename T2,
52  typename T3,
53  typename T4,
54  bool T5>
55  friend class GroupByNode;
56 
57  template <typename T1,
58  typename T2,
59  typename T3>
60  friend class GroupToIndexNode;
61 
62 public:
63  static constexpr bool debug = false;
64  using ValueIn = ValueType;
65  using Key = typename common::FunctionTraits<KeyExtractor>::result_type;
66  using Reader = typename data::File::Reader;
67 
68  GroupByIterator(Reader& reader, const KeyExtractor& key_extractor)
69  : reader_(reader),
70  key_extractor_(key_extractor),
71  is_reader_empty_(false),
72  equal_key_(true),
73  elem_(reader_.template Next<ValueIn>()),
75 
76  //! non-copyable: delete copy-constructor
77  GroupByIterator(const GroupByIterator&) = delete;
78  //! non-copyable: delete assignment operator
80  //! move-constructor: default
81  GroupByIterator(GroupByIterator&&) = default;
82  //! move-assignment operator: default
84 
85  bool HasNext() {
86  return (!is_reader_empty_ && equal_key_);
87  }
88 
90  assert(!is_reader_empty_);
91  ValueIn elem = elem_;
92  GetNextElem();
93  return elem;
94  }
95 
96 private:
97  bool HasNextForReal() {
98  return !is_reader_empty_;
99  }
100 
101  const Key& GetNextKey() {
102  equal_key_ = true;
103  return key_;
104  }
105 
106 private:
108  const KeyExtractor& key_extractor_;
113 
114  void GetNextElem() {
115  if (reader_.HasNext()) {
116  elem_ = reader_.template Next<ValueIn>();
117  Key key = key_extractor_(elem_);
118  if (key != key_) {
119  key_ = std::move(key);
120  equal_key_ = false;
121  }
122  }
123  else {
124  is_reader_empty_ = true;
125  }
126  }
127 };
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 
131 template <typename ValueType, typename KeyExtractor, typename Comparator>
133 {
134  template <typename T1,
135  typename T2,
136  typename T3,
137  typename T4,
138  bool T5>
139  friend class GroupByNode;
140 
141  template <typename T1,
142  typename T2,
143  typename T3>
144  friend class GroupToIndexNode;
145 
146 public:
147  static constexpr bool debug = false;
148  using ValueIn = ValueType;
149  using Key = typename common::FunctionTraits<KeyExtractor>::result_type;
151  ValueIn, std::vector<data::File::Reader>::iterator, Comparator>;
152 
153  GroupByMultiwayMergeIterator(Puller& reader, const KeyExtractor& key_extractor)
154  : reader_(reader),
155  key_extractor_(key_extractor),
156  is_reader_empty_(false),
157  equal_key_(true),
158  elem_(reader_.Next()),
160 
161  //! non-copyable: delete copy-constructor
163  //! non-copyable: delete assignment operator
165  //! move-constructor: default
167  //! move-assignment operator: default
169 
170  bool HasNext() {
171  return (!is_reader_empty_ && equal_key_);
172  }
173 
175  assert(!is_reader_empty_);
176  ValueIn elem = elem_;
177  GetNextElem();
178  return elem;
179  }
180 
181 private:
182  bool HasNextForReal() {
183  return !is_reader_empty_;
184  }
185 
186  const Key& GetNextKey() {
187  equal_key_ = true;
188  return key_;
189  }
190 
191 private:
193  const KeyExtractor& key_extractor_;
198 
199  void GetNextElem() {
200  if (reader_.HasNext()) {
201  elem_ = reader_.Next();
202  Key next_key = key_extractor_(elem_);
203  if (next_key != key_) {
204  key_ = std::move(next_key);
205  equal_key_ = false;
206  }
207  }
208  else {
209  is_reader_empty_ = true;
210  }
211  }
212 };
213 
214 //! \}
215 
216 } // namespace api
217 } // namespace thrill
218 
219 #endif // !THRILL_API_GROUP_BY_ITERATOR_HEADER
220 
221 /******************************************************************************/
GroupByMultiwayMergeIterator(Puller &reader, const KeyExtractor &key_extractor)
typename common::FunctionTraits< KeyExtractor >::result_type Key
typename data::File::Reader Reader
const KeyExtractor & key_extractor_
GroupByIterator(Reader &reader, const KeyExtractor &key_extractor)
typename common::FunctionTraits< KeyExtractor >::result_type Key
GroupByIterator & operator=(const GroupByIterator &)=delete
non-copyable: delete assignment operator
GroupByMultiwayMergeIterator & operator=(const GroupByMultiwayMergeIterator &)=delete
non-copyable: delete assignment operator
DynBlockReader Reader
Definition: file.hpp:60