summaryrefslogtreecommitdiff
path: root/Ix/CPP/src/cpplinq/linq_where.hpp
blob: 9b3d9369197d759bfa519fcc5e320a1aba432d4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

#if !defined(CPPLINQ_LINQ_WHERE_HPP)
#define CPPLINQ_LINQ_WHERE_HPP
#pragma once

namespace cpplinq 
{
    template <class Collection, class Predicate>
    class linq_where
    {
        typedef typename Collection::cursor 
            inner_cursor;
    public:
        struct cursor {
            typedef typename util::min_iterator_category<
                    bidirectional_cursor_tag,
                    typename inner_cursor::cursor_category>::type
                cursor_category;
            typedef typename inner_cursor::element_type
                element_type;
            typedef typename inner_cursor::reference_type
                reference_type;

            cursor(const inner_cursor& cur, const Predicate& p) : cur(cur), pred(p)
            {
                if (!cur.empty() && !pred(cur.get())) {
                    this->inc();
                }
            }

            void forget() { cur.forget(); }
            bool empty() const { return cur.empty(); }
            void inc() { 
                for (;;) {
                    cur.inc();
                    if (cur.empty() || pred(cur.get())) break;
                }
            }
            reference_type get() const { 
                return cur.get();
            }

            bool atbegin() const { return atbegin(cur); }
            void dec() {
                for (;;) {
                    cur.dec();
                    if (pred(cur.get())) break;
                }
            }
        private:
            inner_cursor cur;
            Predicate pred;
        };

        linq_where(const Collection& c, Predicate pred) : c(c), pred(pred) {}

        cursor get_cursor() const { 
            return cursor(c.get_cursor(), pred); 
        }

    private:
        Collection c;
        Predicate   pred;
    };
}

#endif // !defined(CPPLINQ_LINQ_WHERE_HPP)