128 #if !defined(CPPLINQ_LINQ_HPP) 129 #define CPPLINQ_LINQ_HPP 132 #pragma push_macro("min") 133 #pragma push_macro("max") 137 #include <functional> 146 #include <type_traits> 153 #if _MSC_VER > 1600 || __cplusplus > 199711L 154 #define LINQ_USE_RVALUEREF 1 157 #if (defined(_MSC_VER) && _CPPRTTI) || !defined(_MSC_VER) 158 #define LINQ_USE_RTTI 1 161 #if defined(__clang__) 162 #if __has_feature(cxx_rvalue_references) 163 #define LINQ_USE_RVALUEREF 1 165 #if __has_feature(cxx_rtti) 166 #define LINQ_USE_RTTI 1 194 not1_(Pred p) : pred(p)
197 bool operator()(
const T& value)
204 not1_<Pred> not1(Pred p) {
return not1_<Pred>(p); }
211 U operator()(
const T& value)
const {
212 return static_cast<U
>(value);
217 template <
class Collection>
220 typedef typename Collection::cursor::element_type
222 typedef typename Collection::cursor::reference_type
233 template <
class KeyFn>
243 template <
class Selector>
255 template <
class Fn,
class Fn2>
261 template <
class Predicate>
274 return element_type();
277 reference_type
first = *it;
281 template <
class T,
class Fn>
287 bool any()
const {
auto cur = c.get_cursor();
return !cur.empty(); }
289 template <
class Predicate>
290 bool any(Predicate p)
const {
291 auto it = std::find_if(begin(), end(), p);
295 template <
class Predicate>
296 bool all(Predicate p)
const {
297 auto it = std::find_if(begin(), end(), detail::not1(p));
303 #if !defined(__clang__) 308 -> decltype(static_cast<linq_driver*>(0)->select(detail::cast_to<U>()))
310 return this->select(detail::cast_to<U>());
316 bool contains(
const typename Collection::cursor::element_type& value)
const {
317 return std::find(begin(), end(), value) != end();
320 typename std::iterator_traits<iterator>::difference_type
count()
const {
321 return std::distance(begin(), end());
324 template <
class Predicate>
325 typename std::iterator_traits<iterator>::difference_type
count(Predicate p)
const {
326 auto filtered = this->where(p);
327 return std::distance(begin(filtered), end(filtered));
336 auto cur = c.get_cursor();
337 while(ix && !cur.empty()) {
341 if (cur.empty()) {
throw std::logic_error(
"index out of bounds"); }
342 else {
return cur.get(); }
346 auto cur = c.get_cursor();
347 while(ix && !cur.empty()) {
351 if (cur.empty()) {
return element_type(); }
352 else {
return cur.get(); }
363 auto cur = c.get_cursor();
364 if (cur.empty()) {
throw std::logic_error(
"index out of bounds"); }
365 else {
return cur.get(); }
368 template <
class Predicate>
369 reference_type
first(Predicate pred)
const {
370 auto cur = c.get_cursor();
371 while (!cur.empty() && !pred(cur.get())) {
374 if (cur.empty()) {
throw std::logic_error(
"index out of bounds"); }
375 else {
return cur.get(); }
379 auto cur = c.get_cursor();
380 if (cur.empty()) {
return element_type(); }
381 else {
return cur.get(); }
384 template <
class Predicate>
386 auto cur = c.get_cursor();
387 while (!cur.empty() && !pred(cur.get())) {
390 if (cur.empty()) {
return element_type(); }
391 else {
return cur.get(); }
398 typename std::conditional<
400 typename Collection::cursor::cursor_category,
406 return linq_last_(c.get_cursor(),
typename Collection::cursor::cursor_category());
409 template <
class Predicate>
410 reference_type
last(Predicate pred)
const 412 auto cur = c.where(pred).get_cursor();
413 return linq_last_(cur,
typename decltype(cur)::cursor_category());
421 template <
class Predicate>
424 auto cur = c.where(pred).get_cursor();
428 reference_type
max()
const 430 return max(std::less<element_type>());
433 template <
class Compare>
434 reference_type
max(Compare less)
const 436 auto it = std::max_element(begin(), end(), less);
438 throw std::logic_error(
"max performed on empty range");
443 reference_type
min()
const 445 return min(std::less<element_type>());
448 template <
class Compare>
449 reference_type
min(Compare less)
const 451 auto it = std::min_element(begin(), end(), less);
453 throw std::logic_error(
"max performed on empty range");
474 template<
typename ITEM =
typename element_type>
475 typename std::enable_if<std::is_default_constructible<ITEM>::value, ITEM>::type
sum()
const {
480 typename element_type
sum(
typename element_type seed)
const {
484 template <typename Selector, typename Result = std::result_of<Selector(typename element_type)>::type>
485 typename std::enable_if<std::is_default_constructible<Result>::value, Result>::type
sum(Selector sel)
const {
486 return from(begin(), end()).select(sel).sum();
489 template <typename Selector, typename Result = std::result_of<Selector(typename element_type)>::type>
490 Result
sum(Selector sel, Result seed)
const {
491 return from(begin(), end()).select(sel).sum(seed);
511 std::vector<typename Collection::cursor::element_type>
to_vector()
const 513 return std::vector<typename Collection::cursor::element_type>(begin(), end());
516 std::list<typename Collection::cursor::element_type>
to_list()
const 518 return std::list<typename Collection::cursor::element_type>(begin(), end());
521 std::set<typename Collection::cursor::element_type>
to_set()
const 523 return std::set<typename Collection::cursor::element_type>(begin(), end());
534 typename std::iterator_traits<iterator>::reference
536 return *(begin()+=ix);
541 typedef typename Collection::cursor
cursor;
555 template <
class TContainer>
566 template <
class Iter>
572 template <
class TContainer>
580 #pragma pop_macro("min") 581 #pragma pop_macro("max") 583 #endif // defined(CPPLINQ_LINQ_HPP) reference_type max() const
Definition: linq.hpp:428
Definition: linq_select.hpp:12
linq_driver< linq_select_many< Collection, Fn, detail::default_select_many_selector > > select_many(Fn fn) const
Definition: linq.hpp:250
reference_type max(Compare less) const
Definition: linq.hpp:434
auto any(AN &&...an) -> operator_factory< any_tag, AN... >
Returns an Observable that emits true if any item emitted by the source Observable satisfies a specif...
Definition: rx-any.hpp:121
Definition: linq_iterators.hpp:116
linq_driver< linq_take< Collection > > take(std::size_t n) const
Definition: linq.hpp:494
cursor_iterator< typename Collection::cursor > iterator
Definition: linq.hpp:226
iterator begin() const
Definition: linq.hpp:528
element_type last_or_default() const
Definition: linq.hpp:416
std::set< typename Collection::cursor::element_type > to_set() const
Definition: linq.hpp:521
Definition: linq_selectmany.hpp:72
auto max() -> operator_factory< max_tag >
For each item from this observable reduce it by taking the max value of the previous items...
Definition: rx-reduce.hpp:496
linq_driver< linq_groupby< Collection, KeyFn > > groupby(KeyFn fn)
Definition: linq.hpp:234
reference_type last(Predicate pred) const
Definition: linq.hpp:410
bool contains(const typename Collection::cursor::element_type &value) const
Definition: linq.hpp:316
Definition: linq_cursor.hpp:143
std::vector< typename Collection::cursor::element_type > to_vector() const
Definition: linq.hpp:511
bool empty() const
Definition: linq.hpp:355
Definition: linq_take.hpp:66
T aggregate(T initialValue, Fn fn) const
Definition: linq.hpp:282
linq_driver< iter_cursor< typename util::container_traits< TContainer >::iterator > > from(TContainer &c)
Definition: linq.hpp:556
std::list< typename Collection::cursor::element_type > to_list() const
Definition: linq.hpp:516
linq_driver< linq_skip< Collection > > skip(std::size_t n) const
Definition: linq.hpp:468
linq_driver< dynamic_collection< typename Collection::cursor::reference_type > > late_bind() const
Definition: linq.hpp:545
element_type first_or_default() const
Definition: linq.hpp:378
Cursor::element_type linq_last_(Cursor c, onepass_cursor_tag)
Definition: linq_last.hpp:11
Definition: linq_cursor.hpp:64
auto first() -> operator_factory< first_tag >
For each item from this observable reduce it by sending only the first item.
Definition: rx-reduce.hpp:378
std::iterator_traits< iterator >::reference operator[](std::size_t ix) const
Definition: linq.hpp:535
std::iterator_traits< iterator >::difference_type count(Predicate p) const
Definition: linq.hpp:325
bool any() const
Definition: linq.hpp:287
auto sum() -> operator_factory< sum_tag >
For each item from this observable reduce it by adding to the previous items.
Definition: rx-reduce.hpp:454
Definition: linq_skip.hpp:12
element_type aggregate(Fn fn) const
Definition: linq.hpp:270
Definition: linq_cursor.hpp:293
auto cast() -> decltype(static_cast< linq_driver * >(0) ->select(detail::cast_to< U >()))
Definition: linq.hpp:307
element_type last_or_default(Predicate pred) const
Definition: linq.hpp:422
auto min() -> operator_factory< min_tag >
For each item from this observable reduce it by taking the min value of the previous items...
Definition: rx-reduce.hpp:475
linq_driver< TContainer > from_value(const TContainer &c)
Definition: linq.hpp:573
element_type sum(typename element_type seed) const
Definition: linq.hpp:480
Cursor::element_type linq_last_or_default_(Cursor c, onepass_cursor_tag)
Definition: linq_last.hpp:50
reference_type min() const
Definition: linq.hpp:443
bool all(Predicate p) const
Definition: linq.hpp:296
linq_driver & operator=(const linq_driver< TC2 > &other)
Definition: linq.hpp:532
std::enable_if< std::is_default_constructible< ITEM >::value, ITEM >::type sum() const
Definition: linq.hpp:475
bool any(Predicate p) const
Definition: linq.hpp:290
reference_type element_at(std::size_t ix) const
Definition: linq.hpp:335
linq_driver< linq_where< Collection, Predicate > > where(Predicate p) const
Definition: linq.hpp:262
Result sum(Selector sel, Result seed) const
Definition: linq.hpp:490
linq_driver(Collection c)
Definition: linq.hpp:228
cursor get_cursor()
Definition: linq.hpp:542
linq_driver< linq_select< Collection, Selector > > select(Selector sel) const
Definition: linq.hpp:244
Definition: linq_where.hpp:10
std::iterator_traits< iterator >::difference_type count() const
Definition: linq.hpp:320
reference_type first() const
Definition: linq.hpp:362
Definition: linq_groupby.hpp:58
Collection::cursor cursor
Definition: linq.hpp:541
iterator end() const
Definition: linq.hpp:529
reference_type first(Predicate pred) const
Definition: linq.hpp:369
element_type first_or_default(Predicate pred) const
Definition: linq.hpp:385
std::enable_if< std::is_default_constructible< Result >::value, Result >::type sum(Selector sel) const
Definition: linq.hpp:485
linq_driver & operator=(const linq_driver &other)
Definition: linq.hpp:530
element_type element_at_or_default(std::size_t ix) const
Definition: linq.hpp:345
auto accumulate(AN &&...an) -> operator_factory< reduce_tag, AN... >
For each item from this observable use Accumulator to combine items, when completed use ResultSelecto...
Definition: rx-reduce.hpp:361
std::conditional< std::is_convertible< typename Collection::cursor::cursor_category, forward_cursor_tag >::value, reference_type, element_type >::type last() const
Definition: linq.hpp:404
linq_driver< linq_select_many< Collection, Fn, Fn2 > > select_many(Fn fn, Fn2 fn2) const
Definition: linq.hpp:256
reference_type min(Compare less) const
Definition: linq.hpp:449