ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
distinct.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2023 - present.
4// Distributed under the Boost Software License, Version 1.0.
5// (See accompanying file LICENSE_1_0.txt or copy at
6// https://www.boost.org/LICENSE_1_0.txt)
7//
8// Project home: https://github.com/victimsnino/ReactivePlusPlus
9//
10
11#pragma once
12
13#include <rpp/operators/fwd.hpp>
14
15#include <rpp/defs.hpp>
16#include <rpp/operators/details/strategy.hpp>
17#include <rpp/utils/constraints.hpp>
18
19#include <unordered_set>
20
21namespace rpp::operators::details
22{
23 template<rpp::constraint::decayed_type Type, rpp::constraint::observer TObserver>
25 {
26 static constexpr auto preferred_disposables_mode = rpp::details::observers::disposables_mode::None;
27
28 RPP_NO_UNIQUE_ADDRESS TObserver observer;
29 mutable std::unordered_set<Type> past_values{};
30
31 template<typename T>
32 void on_next(T&& v) const
33 {
34 const auto [it, inserted] = past_values.insert(std::forward<T>(v));
35 if (inserted)
36 {
37 observer.on_next(*it);
38 }
39 }
40
41 void on_error(const std::exception_ptr& err) const { observer.on_error(err); }
42
43 void on_completed() const { observer.on_completed(); }
44
45 void set_upstream(const disposable_wrapper& d) { observer.set_upstream(d); }
46
47 bool is_disposed() const { return observer.is_disposed(); }
48 };
49
50 struct distinct_t : lift_operator<distinct_t>
51 {
52 using lift_operator<distinct_t>::lift_operator;
53
54 template<rpp::constraint::decayed_type T>
56 {
57 static_assert(rpp::constraint::hashable<T>, "T is not hashable");
58
59 using result_type = T;
60
61 template<rpp::constraint::observer_of_type<result_type> TObserver>
62 using observer_strategy = distinct_observer_strategy<T, TObserver>;
63 };
64
65 template<rpp::details::observables::constraint::disposables_strategy Prev>
66 using updated_optimal_disposables_strategy = Prev;
67 };
68} // namespace rpp::operators::details
69
70namespace rpp::operators
71{
82 *
83 * @ingroup filtering_operators
84 * @see https://reactivex.io/documentation/operators/distinct.html
85 */
86 inline auto distinct()
87 {
88 return details::distinct_t{};
89 }
90} // namespace rpp::operators
Definition constraints.hpp:56
disposable_wrapper_impl< interface_disposable > disposable_wrapper
Wrapper to keep "simple" disposable. Specialization of rpp::disposable_wrapper_impl.
Definition fwd.hpp:34
auto distinct()
For each item from this observable, filter out repeated values and emit only items that have not alre...
Definition distinct.hpp:82
Definition distinct.hpp:51