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_take.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_TAKE_HPP)
4 #define CPPLINQ_LINQ_TAKE_HPP
5 #pragma once
6 
7 #include <cstddef>
8 
9 namespace cpplinq
10 {
11  template <class InnerCursor>
13  {
14  typedef typename InnerCursor::element_type element_type;
15  typedef typename InnerCursor::reference_type reference_type;
16  typedef typename InnerCursor::cursor_category cursor_category;
17 
18  linq_take_cursor(const InnerCursor& cur, std::size_t rem) : cur(cur), rem(rem) {}
19 
20  void forget() { cur.forget(); }
21  bool empty() const { return cur.empty() || rem == 0; }
22  void inc() { cur.inc(); --rem; }
23  reference_type get() const { return cur.get(); }
24 
25  bool atbegin() const { return cur.atbegin(); }
26  void dec() { cur.dec(); --rem; }
27 
28  void skip(std::size_t n) { cur.skip(n); rem -= n; }
29  std::size_t position() const { return cur.position(); }
30  std::size_t size() const { return cur.size(); }
31 
32  private:
33  InnerCursor cur;
34  std::size_t rem;
35  };
36 
37  namespace detail {
38  template <class Collection>
40  take_get_cursor_(
41  const Collection& c,
42  std::size_t n,
44  )
45  {
46  return linq_take_cursor<typename Collection::cursor>(c.get_cursor(), n);
47  }
48 
49  template <class Collection>
50  typename Collection::cursor
51  take_get_cursor_(
52  const Collection& c,
53  std::size_t n,
55  )
56  {
57  auto cur = c.get_cursor();
58  if (cur.size() > n) {
59  cur.truncate(n);
60  }
61  return cur;
62  }
63  }
64 
65  template <class Collection>
66  struct linq_take
67  {
68  typedef typename std::conditional<
71  typename Collection::cursor::cursor_category>::value,
72  typename Collection::cursor,
75 
76  linq_take(const Collection& c, std::size_t n) : c(c), n(n) {}
77 
78  cursor get_cursor() const {
79  return detail::take_get_cursor_(c, n, typename Collection::cursor::cursor_category());
80  }
81 
82  Collection c;
83  std::size_t n;
84  };
85 
86  template <class Collection>
87  auto get_cursor(
89  )
90  -> decltype(get_cursor_(take, typename Collection::cursor::cursor_category()))
91  {
92  return get_cursor_(take, typename Collection::cursor::cursor_category());
93  }
94 
95 
96 }
97 #endif // !defined(CPPLINQ_LINQ_TAKE_HPP)
98 
std::conditional< util::less_or_equal_cursor_category< random_access_cursor_tag, typename Collection::cursor::cursor_category >::value, typename Collection::cursor, linq_take_cursor< typename Collection::cursor > >::type cursor
Definition: linq_take.hpp:74
cursor get_cursor() const
Definition: linq_take.hpp:78
std::size_t position() const
Definition: linq_take.hpp:29
Definition: linq_cursor.hpp:63
InnerCursor::cursor_category cursor_category
Definition: linq_take.hpp:16
InnerCursor::reference_type reference_type
Definition: linq_take.hpp:15
Definition: linq_cursor.hpp:66
Definition: linq_take.hpp:66
Definition: linq.hpp:186
Definition: linq_take.hpp:12
Collection c
Definition: linq_take.hpp:82
void inc()
Definition: linq_take.hpp:22
void skip(std::size_t n)
Definition: linq_take.hpp:28
linq_take(const Collection &c, std::size_t n)
Definition: linq_take.hpp:76
bool atbegin() const
Definition: linq_take.hpp:25
std::size_t n
Definition: linq_take.hpp:83
Definition: linq_cursor.hpp:107
auto take(AN &&...an) -> operator_factory< take_tag, AN... >
For the first count items from this observable emit them from the new observable that is returned...
Definition: rx-take.hpp:133
auto get_cursor(const linq_take< Collection > &take) -> decltype(get_cursor_(take, typename Collection::cursor::cursor_category()))
Definition: linq_take.hpp:87
std::size_t size() const
Definition: linq_take.hpp:30
void forget()
Definition: linq_take.hpp:20
InnerCursor::element_type element_type
Definition: linq_take.hpp:14
linq_take_cursor(const InnerCursor &cur, std::size_t rem)
Definition: linq_take.hpp:18
bool empty() const
Definition: linq_take.hpp:21
void dec()
Definition: linq_take.hpp:26