summaryrefslogtreecommitdiff
path: root/Rx/v2/src/rxcpp/operators/rx-sequence_equal.hpp
blob: 3350e44fe58e4b016c6e89fb37b5cc3766cb1b34 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

#pragma once

/*! \file rx-sequence_equal.hpp

    \brief Determine whether two Observables emit the same sequence of items.

    \tparam OtherSource      the type of the other observable.
    \tparam BinaryPredicate  the type of the value comparing function (optional). The signature should be equivalent to the following: bool pred(const T1& a, const T2& b);
    \tparam Coordination  the type of the scheduler (optional).

    \param t     the other Observable that emits items to compare.
    \param pred  the function that implements comparison of two values (optional).
    \param cn    the scheduler (optional).

    \return  Observable that emits true only if both sequences terminate normally after emitting the same sequence of items in the same order; otherwise it will emit false.

    \sample
    \snippet sequence_equal.cpp sequence_equal sample
    \snippet output.txt sequence_equal sample
*/

#if !defined(RXCPP_OPERATORS_RX_SEQUENCE_EQUAL_HPP)
#define RXCPP_OPERATORS_RX_SEQUENCE_EQUAL_HPP

#include "../rx-includes.hpp"

namespace rxcpp {

namespace operators {

namespace detail {

template<class... AN>
struct sequence_equal_invalid_arguments {};

template<class... AN>
struct sequence_equal_invalid : public rxo::operator_base<sequence_equal_invalid_arguments<AN...>> {
    using type = observable<sequence_equal_invalid_arguments<AN...>, sequence_equal_invalid<AN...>>;
};
template<class... AN>
using sequence_equal_invalid_t = typename sequence_equal_invalid<AN...>::type;

template<class T, class Observable, class OtherObservable, class BinaryPredicate, class Coordination>
struct sequence_equal : public operator_base<bool>
{
    typedef rxu::decay_t<Observable> source_type;
    typedef rxu::decay_t<T> source_value_type;
    typedef rxu::decay_t<OtherObservable> other_source_type;
    typedef typename other_source_type::value_type other_source_value_type;
    typedef rxu::decay_t<BinaryPredicate> predicate_type;
    typedef rxu::decay_t<Coordination> coordination_type;
    typedef typename coordination_type::coordinator_type coordinator_type;

    struct values {
        values(source_type s, other_source_type t, predicate_type pred, coordination_type sf)
                : source(std::move(s))
                , other(std::move(t))
                , pred(std::move(pred))
                , coordination(std::move(sf))
        {
        }

        source_type source;
        other_source_type other;
        predicate_type pred;
        coordination_type coordination;
    };

    values initial;

    sequence_equal(source_type s, other_source_type t, predicate_type pred, coordination_type sf)
        : initial(std::move(s), std::move(t), std::move(pred), std::move(sf))
    {
    }

    template<class Subscriber>
    void on_subscribe(Subscriber s) const {

        typedef Subscriber output_type;

        struct state_type
            : public std::enable_shared_from_this<state_type>
            , public values
        {
            state_type(const values& vals, coordinator_type coor, const output_type& o)
                : values(vals)
                , coordinator(std::move(coor))
                , out(o)
                , source_completed(false)
                , other_completed(false)
            {
                out.add(other_lifetime);
                out.add(source_lifetime);
            }

            composite_subscription other_lifetime;
            composite_subscription source_lifetime;
            coordinator_type coordinator;
            output_type out;
            
            mutable std::list<source_value_type> source_values;
            mutable std::list<other_source_value_type> other_values;
            mutable bool source_completed;
            mutable bool other_completed;
        };

        auto coordinator = initial.coordination.create_coordinator();
        auto state = std::make_shared<state_type>(initial, std::move(coordinator), std::move(s));

        auto other = on_exception(
            [&](){ return state->coordinator.in(state->other); },
            state->out);
        if (other.empty()) {
            return;
        }

        auto source = on_exception(
            [&](){ return state->coordinator.in(state->source); },
            state->out);
        if (source.empty()) {
            return;
        }

        auto check_equal = [state]() {
            if(!state->source_values.empty() && !state->other_values.empty()) {
                auto x = std::move(state->source_values.front());
                state->source_values.pop_front();

                auto y = std::move(state->other_values.front());
                state->other_values.pop_front();

                if (!state->pred(x, y)) {
                    state->out.on_next(false);
                    state->out.on_completed();
                }
            } else {
                if((!state->source_values.empty() && state->other_completed) ||
                   (!state->other_values.empty() && state->source_completed)) {
                    state->out.on_next(false);
                    state->out.on_completed();
                }
            }
        };
        
        auto check_complete = [state]() {
            if(state->source_completed && state->other_completed) {
                state->out.on_next(state->source_values.empty() && state->other_values.empty());
                state->out.on_completed();
            }
        };

        auto sinkOther = make_subscriber<other_source_value_type>(
            state->out,
            state->other_lifetime,
            // on_next
            [state, check_equal](other_source_value_type t) {
                auto& values = state->other_values;
                values.push_back(t);
                check_equal();
            },
            // on_error
            [state](rxu::error_ptr e) {
                state->out.on_error(e);
            },
            // on_completed
            [state, check_complete]() {
                auto& completed = state->other_completed;
                completed = true;
                check_complete();
            }
        );

        auto selectedSinkOther = on_exception(
            [&](){ return state->coordinator.out(sinkOther); },
            state->out);
        if (selectedSinkOther.empty()) {
            return;
        }
        other->subscribe(std::move(selectedSinkOther.get()));

        source.get().subscribe(
            state->source_lifetime,
            // on_next
            [state, check_equal](source_value_type t) {
                auto& values = state->source_values;
                values.push_back(t);
                check_equal();
            },
            // on_error
            [state](rxu::error_ptr e) {
                state->out.on_error(e);
            },
            // on_completed
            [state, check_complete]() {
                auto& completed = state->source_completed;
                completed = true;
                check_complete();
            }
        );        
    }
};

}

/*! @copydoc rx-sequence_equal.hpp
*/
template<class... AN>
auto sequence_equal(AN&&... an)
    ->     operator_factory<sequence_equal_tag, AN...> {
    return operator_factory<sequence_equal_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
}

}

template<>
struct member_overload<sequence_equal_tag>
{
    template<class Observable, class OtherObservable,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            is_observable<OtherObservable>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class SequenceEqual = rxo::detail::sequence_equal<SourceValue, rxu::decay_t<Observable>, rxu::decay_t<OtherObservable>, rxu::equal_to<>, identity_one_worker>,
        class Value = rxu::value_type_t<SequenceEqual>,
        class Result = observable<Value, SequenceEqual>>
    static Result member(Observable&& o, OtherObservable&& t) {
        return Result(SequenceEqual(std::forward<Observable>(o), std::forward<OtherObservable>(t), rxu::equal_to<>(), identity_current_thread()));
    }

    template<class Observable, class OtherObservable, class BinaryPredicate,
        class IsCoordination = is_coordination<BinaryPredicate>,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            is_observable<OtherObservable>,
            rxu::negation<IsCoordination>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class SequenceEqual = rxo::detail::sequence_equal<SourceValue, rxu::decay_t<Observable>, rxu::decay_t<OtherObservable>, rxu::decay_t<BinaryPredicate>, identity_one_worker>,
        class Value = rxu::value_type_t<SequenceEqual>,
        class Result = observable<Value, SequenceEqual>>
    static Result member(Observable&& o, OtherObservable&& t, BinaryPredicate&& pred) {
        return Result(SequenceEqual(std::forward<Observable>(o), std::forward<OtherObservable>(t), std::forward<BinaryPredicate>(pred), identity_current_thread()));
    }

    template<class Observable, class OtherObservable, class Coordination,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            is_observable<OtherObservable>,
            is_coordination<Coordination>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class SequenceEqual = rxo::detail::sequence_equal<SourceValue, rxu::decay_t<Observable>, rxu::decay_t<OtherObservable>, rxu::equal_to<>, rxu::decay_t<Coordination>>,
        class Value = rxu::value_type_t<SequenceEqual>,
        class Result = observable<Value, SequenceEqual>>
    static Result member(Observable&& o, OtherObservable&& t, Coordination&& cn) {
        return Result(SequenceEqual(std::forward<Observable>(o), std::forward<OtherObservable>(t), rxu::equal_to<>(), std::forward<Coordination>(cn)));
    }

    template<class Observable, class OtherObservable, class BinaryPredicate, class Coordination,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>,
            is_observable<OtherObservable>,
            is_coordination<Coordination>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class SequenceEqual = rxo::detail::sequence_equal<SourceValue, rxu::decay_t<Observable>, rxu::decay_t<OtherObservable>, rxu::decay_t<BinaryPredicate>, rxu::decay_t<Coordination>>,
        class Value = rxu::value_type_t<SequenceEqual>,
        class Result = observable<Value, SequenceEqual>>
    static Result member(Observable&& o, OtherObservable&& t, BinaryPredicate&& pred, Coordination&& cn) {
        return Result(SequenceEqual(std::forward<Observable>(o), std::forward<OtherObservable>(t), std::forward<BinaryPredicate>(pred), std::forward<Coordination>(cn)));
    }

    template<class... AN>
    static operators::detail::sequence_equal_invalid_t<AN...> member(const AN&...) {
        std::terminate();
        return {};
        static_assert(sizeof...(AN) == 10000, "sequence_equal takes (OtherObservable, optional BinaryPredicate, optional Coordination)");
    }
};

}

#endif