3 #if !defined(CPPLINQ_LINQ_GROUPBY_HPP) 4 #define CPPLINQ_LINQ_GROUPBY_HPP 10 template <
class Iter,
class Key>
22 group(
const Key& key) : key(key)
57 template <
class Collection,
class KeyFn,
class Compare = default_less>
60 typedef typename Collection::cursor
63 typedef typename util::result_of<KeyFn(typename inner_cursor::element_type)>::type
66 typedef std::list<typename inner_cursor::element_type>
72 typedef std::list<group_type>
82 element_list_type elements;
83 std::list<group_type> groups;
84 std::map<key_type, group_type*, Compare> groupIndex;
91 impl_t(inner_cursor cur,
93 Compare comp = Compare())
94 : keySelector(keySelector)
98 insert_all(std::move(cur));
101 void insert_all(inner_cursor cur)
103 while(!cur.empty()) {
108 void insert(
typename inner_cursor::reference_type element)
110 key_type
key = keySelector(element);
111 auto groupPos = groupIndex.find(key);
112 if(groupPos == groupIndex.end()) {
114 bool firstGroup = groups.empty();
116 elements.push_back(element);
123 groups.push_back(group_type(key));
124 group_type& newGroup = groups.back();
126 groupIndex.insert( std::make_pair(key, &newGroup) );
128 newGroup.
fin = elements.end();
132 elements.insert(groupPos->second->end(), element);
150 Compare comp = Compare())
152 impl.reset(
new impl_t(cur, keyFn, comp));
153 inner = impl->groups.begin();
154 fin = impl->groups.end();
163 throw std::logic_error(
"attempt to iterate past end of range");
172 std::shared_ptr<impl_t> impl;
173 typename std::list<group_type>::iterator inner;
174 typename std::list<group_type>::iterator
fin;
179 Compare comp = Compare())
180 : c(c), keyFn(keyFn), comp(comp)
194 #endif // !defined(CPPLINQ_LINQ_GROUPBY_HPP) Iter fin
Definition: linq_groupby.hpp:15
bool operator()(const T &a, const T &b) const
Definition: linq_groupby.hpp:33
bool empty() const
Definition: linq_groupby.hpp:158
Iter start
Definition: linq_groupby.hpp:14
Iter begin() const
Definition: linq_groupby.hpp:26
Iter iterator
Definition: linq_groupby.hpp:17
cursor get_cursor() const
Definition: linq_groupby.hpp:184
forward_cursor_tag cursor_category
Definition: linq_groupby.hpp:146
group_type element_type
Definition: linq_groupby.hpp:140
Key key
Definition: linq_groupby.hpp:13
Definition: linq_groupby.hpp:37
linq_groupby(Collection c, KeyFn keyFn, Compare comp=Compare())
Definition: linq_groupby.hpp:177
Definition: linq_groupby.hpp:138
Iter end() const
Definition: linq_groupby.hpp:27
Definition: linq_cursor.hpp:64
void inc()
Definition: linq_groupby.hpp:161
group(const Key &key)
Definition: linq_groupby.hpp:22
element_type reference_type
Definition: linq_groupby.hpp:143
Definition: linq_groupby.hpp:11
group()
Definition: linq_groupby.hpp:20
Definition: linq_groupby.hpp:30
cursor(inner_cursor cur, KeyFn keyFn, Compare comp=Compare())
Definition: linq_groupby.hpp:148
void forget()
Definition: linq_groupby.hpp:157
Definition: linq_groupby.hpp:58
bool operator()(const T &a, const T &b) const
Definition: linq_groupby.hpp:40
Iter const_iterator
Definition: linq_groupby.hpp:18