Commit b8116108 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

move order_preserving_reinsertion_view

Summary: Move `order_preserving_reinsertion_view` to a new header `folly/container/View.h` and revise its customization facility in terms of `folly::tag_invoke`.

Reviewed By: iahs

Differential Revision: D25787117

fbshipit-source-id: b7ccd3b6a1413a2639ef1beb1f1ee23bbed045e3
parent 186ce88c
......@@ -810,23 +810,3 @@ template <>
struct is_unsigned<unsigned __int128> : ::std::true_type {};
FOLLY_NAMESPACE_STD_END
#endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS
namespace folly {
/**
* Extension point for containers to provide an order such that if entries are
* inserted into a new instance in that order, iteration order of the new
* instance matches the original's. This can be useful for containers that have
* defined but non-FIFO iteration order, such as F14Vector*.
*
* Should return an iterable view (a type that provides begin() and end()).
*
* Containers should provide overloads in their own namespace; resolution is
* expected to be done via ADL.
*/
template <typename Container>
const Container& order_preserving_reinsertion_view(const Container& container) {
return container;
}
} // namespace folly
......@@ -34,6 +34,7 @@
#include <folly/Range.h>
#include <folly/Traits.h>
#include <folly/container/View.h>
#include <folly/lang/Exception.h>
#include <folly/lang/SafeAssert.h>
......@@ -1176,13 +1177,12 @@ class F14VectorMap : public f14::detail::F14VectorMapImpl<
const_reverse_iterator riter(const_iterator it) const {
return this->table_.riter(it);
}
};
template <typename K, typename M, typename H, typename E, typename A>
Range<typename F14VectorMap<K, M, H, E, A>::const_reverse_iterator>
order_preserving_reinsertion_view(const F14VectorMap<K, M, H, E, A>& c) {
return {c.rbegin(), c.rend()};
}
friend Range<const_reverse_iterator> tag_invoke(
order_preserving_reinsertion_view_fn, F14VectorMap const& c) noexcept {
return {c.rbegin(), c.rend()};
}
};
template <
typename Key,
......
......@@ -31,6 +31,7 @@
#include <initializer_list>
#include <tuple>
#include <folly/container/View.h>
#include <folly/lang/SafeAssert.h>
#include <folly/container/F14Set-fwd.h>
......@@ -916,13 +917,12 @@ class F14VectorSet
const_reverse_iterator riter(const_iterator it) const {
return this->table_.riter(it);
}
};
template <typename K, typename H, typename E, typename A>
Range<typename F14VectorSet<K, H, E, A>::const_reverse_iterator>
order_preserving_reinsertion_view(const F14VectorSet<K, H, E, A>& c) {
return {c.rbegin(), c.rend()};
}
friend Range<const_reverse_iterator> tag_invoke(
order_preserving_reinsertion_view_fn, F14VectorSet const& c) noexcept {
return {c.rbegin(), c.rend()};
}
};
template <typename Key, typename Hasher, typename KeyEqual, typename Alloc>
class F14FastSet
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/Portability.h>
#include <folly/functional/Invoke.h>
#include <folly/lang/CustomizationPoint.h>
namespace folly {
// order_preserving_reinsertion_view_fn
// order_preserving_reinsertion_view
//
// Extension point for containers to provide an order such that if entries are
// inserted into a new instance in that order, iteration order of the new
// instance matches the original's. This can be useful for containers that have
// defined but non-FIFO iteration order, such as F14Vector*.
//
// Should return an iterable view (a type that provides begin() and end()).
//
// Containers should provide overloads via tag-invoke.
struct order_preserving_reinsertion_view_fn {
private:
using fn = order_preserving_reinsertion_view_fn;
public:
template <typename Container>
FOLLY_ERASE constexpr auto operator()(Container const& container) const
noexcept(is_nothrow_tag_invocable_v<fn, Container const&>)
-> tag_invoke_result_t<fn, Container const&> {
return tag_invoke(*this, container);
}
};
FOLLY_DEFINE_CPO(
order_preserving_reinsertion_view_fn, order_preserving_reinsertion_view)
// order_preserving_reinsertion_view_or_default_fn
// order_preserving_reinsertion_view_or_default
//
// If a tag-invoke extension of order_preserving_reinsertion_view is available
// over the given argument, forwards to that. Otherwise, returns the argument.
struct order_preserving_reinsertion_view_or_default_fn {
private:
using fn = order_preserving_reinsertion_view_fn;
public:
template <
typename Container,
std::enable_if_t<is_tag_invocable_v<fn, Container const&>, int> = 0>
FOLLY_ERASE constexpr auto operator()(Container const& container) const
noexcept(is_nothrow_tag_invocable_v<fn, Container const&>)
-> tag_invoke_result_t<fn, Container const&> {
return tag_invoke(fn{}, container);
}
template <
typename Container,
std::enable_if_t<!is_tag_invocable_v<fn, Container const&>, int> = 0>
FOLLY_ERASE constexpr Container const& operator()(
Container const& container) const noexcept {
return container;
}
};
FOLLY_DEFINE_CPO(
order_preserving_reinsertion_view_or_default_fn,
order_preserving_reinsertion_view_or_default)
} // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment