// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #pragma once /*! \file rx-distinct.hpp \brief For each item from this observable, filter out repeated values and emit only items that have not already been emitted. \return Observable that emits those items from the source observable that are distinct. \note istinct keeps an unordered_set of past values. Due to an issue in multiple implementations of std::hash, rxcpp maintains a whitelist of hashable types. new types can be added by specializing rxcpp::filtered_hash \sample \snippet distinct.cpp distinct sample \snippet output.txt distinct sample */ #if !defined(RXCPP_OPERATORS_RX_DISTINCT_HPP) #define RXCPP_OPERATORS_RX_DISTINCT_HPP #include "../rx-includes.hpp" namespace rxcpp { namespace operators { namespace detail { template struct distinct_invalid_arguments {}; template struct distinct_invalid : public rxo::operator_base> { using type = observable, distinct_invalid>; }; template using distinct_invalid_t = typename distinct_invalid::type; template struct distinct { typedef rxu::decay_t source_value_type; template struct distinct_observer { typedef distinct_observer this_type; typedef source_value_type value_type; typedef rxu::decay_t dest_type; typedef observer observer_type; dest_type dest; mutable std::unordered_set> remembered; distinct_observer(dest_type d) : dest(d) { } void on_next(source_value_type v) const { if (remembered.empty() || remembered.count(v) == 0) { remembered.insert(v); dest.on_next(v); } } void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { dest.on_completed(); } static subscriber> make(dest_type d) { return make_subscriber(d, this_type(d)); } }; template auto operator()(Subscriber dest) const -> decltype(distinct_observer::make(std::move(dest))) { return distinct_observer::make(std::move(dest)); } }; } /*! @copydoc rx-distinct.hpp */ template auto distinct(AN&&... an) -> operator_factory { return operator_factory(std::make_tuple(std::forward(an)...)); } } template<> struct member_overload { template, class Enabled = rxu::enable_if_all_true_type_t< is_observable, is_hashable>, class Distinct = rxo::detail::distinct> static auto member(Observable&& o) -> decltype(o.template lift(Distinct())) { return o.template lift(Distinct()); } template static operators::detail::distinct_invalid_t member(AN...) { std::terminate(); return {}; static_assert(sizeof...(AN) == 10000, "distinct takes no arguments"); } }; } #endif