RxCpp
The Reactive Extensions for Native (RxCpp) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators in both C and C++.
linq_where.hpp
Go to the documentation of this file.
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2 
3 #if !defined(CPPLINQ_LINQ_WHERE_HPP)
4 #define CPPLINQ_LINQ_WHERE_HPP
5 #pragma once
6 
7 namespace cpplinq
8 {
9  template <class Collection, class Predicate>
10  class linq_where
11  {
12  typedef typename Collection::cursor
13  inner_cursor;
14  public:
15  struct cursor {
16  typedef typename util::min_iterator_category<
18  typename inner_cursor::cursor_category>::type
20  typedef typename inner_cursor::element_type
22  typedef typename inner_cursor::reference_type
24 
25  cursor(const inner_cursor& cur, const Predicate& p) : cur(cur), pred(p)
26  {
27  if (!cur.empty() && !pred(cur.get())) {
28  this->inc();
29  }
30  }
31 
32  void forget() { cur.forget(); }
33  bool empty() const { return cur.empty(); }
34  void inc() {
35  for (;;) {
36  cur.inc();
37  if (cur.empty() || pred(cur.get())) break;
38  }
39  }
40  reference_type get() const {
41  return cur.get();
42  }
43 
44  bool atbegin() const { return atbegin(cur); }
45  void dec() {
46  for (;;) {
47  cur.dec();
48  if (pred(cur.get())) break;
49  }
50  }
51  private:
52  inner_cursor cur;
53  Predicate pred;
54  };
55 
56  linq_where(const Collection& c, Predicate pred) : c(c), pred(pred) {}
57 
58  cursor get_cursor() const {
59  return cursor(c.get_cursor(), pred);
60  }
61 
62  private:
63  Collection c;
64  Predicate pred;
65  };
66 }
67 
68 #endif // !defined(CPPLINQ_LINQ_WHERE_HPP)
69 
util::min_iterator_category< bidirectional_cursor_tag, typename inner_cursor::cursor_category >::type cursor_category
Definition: linq_where.hpp:19
bool atbegin() const
Definition: linq_where.hpp:44
void dec()
Definition: linq_where.hpp:45
cursor(const inner_cursor &cur, const Predicate &p)
Definition: linq_where.hpp:25
cursor get_cursor() const
Definition: linq_where.hpp:58
linq_where(const Collection &c, Predicate pred)
Definition: linq_where.hpp:56
inner_cursor::element_type element_type
Definition: linq_where.hpp:21
Definition: linq.hpp:186
Definition: linq_cursor.hpp:65
Definition: linq_where.hpp:15
void forget()
Definition: linq_where.hpp:32
Definition: linq_where.hpp:10
void inc()
Definition: linq_where.hpp:34
inner_cursor::reference_type reference_type
Definition: linq_where.hpp:23
bool empty() const
Definition: linq_where.hpp:33