[libcxx-commits] [libcxx] [libc++] Implement P0429R9 `std::flat_map` (PR #98643)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Sep 6 10:31:29 PDT 2024
================
@@ -0,0 +1,1367 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___FLAT_MAP_FLAT_MAP_H
+#define _LIBCPP___FLAT_MAP_FLAT_MAP_H
+
+#include <__algorithm/lexicographical_compare_three_way.h>
+#include <__algorithm/ranges_equal.h>
+#include <__algorithm/ranges_lower_bound.h>
+#include <__algorithm/ranges_partition_point.h>
+#include <__algorithm/ranges_stable_sort.h>
+#include <__algorithm/ranges_unique.h>
+#include <__algorithm/ranges_upper_bound.h>
+#include <__compare/synth_three_way.h>
+#include <__concepts/convertible_to.h>
+#include <__config>
+#include <__flat_map/sorted_unique.h>
+#include <__functional/invoke.h>
+#include <__functional/is_transparent.h>
+#include <__functional/operations.h>
+#include <__iterator/concepts.h>
+#include <__iterator/distance.h>
+#include <__iterator/iterator_traits.h>
+#include <__iterator/ranges_iterator_traits.h>
+#include <__iterator/reverse_iterator.h>
+#include <__memory/allocator_traits.h>
+#include <__memory/uses_allocator.h>
+#include <__memory/uses_allocator_construction.h>
+#include <__ranges/concepts.h>
+#include <__ranges/container_compatible_range.h>
+#include <__ranges/ref_view.h>
+#include <__ranges/subrange.h>
+#include <__ranges/zip_view.h>
+#include <__type_traits/conjunction.h>
+#include <__type_traits/invoke.h>
+#include <__type_traits/is_allocator.h>
+#include <__type_traits/is_nothrow_constructible.h>
+#include <__type_traits/maybe_const.h>
+#include <__utility/pair.h>
+#include <initializer_list>
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if _LIBCPP_STD_VER >= 23
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _Key,
+ class _Tp,
+ class _Compare = less<_Key>,
+ class _KeyContainer = vector<_Key>,
+ class _MappedContainer = vector<_Tp>>
+class flat_map {
+ template <bool _Const>
+ struct __iterator;
+
+ template <class, class, class, class, class>
+ friend class flat_map;
+
+public:
+ // types
+ using key_type = _Key;
+ using mapped_type = _Tp;
+ using value_type = pair<key_type, mapped_type>;
+ using key_compare = _Compare;
+ // TODO : the following is the spec, but not implementable for vector<bool>
+ // using reference = pair<const key_type&, mapped_type&>;
+ // using const_reference = pair<const key_type&, const mapped_type&>;
+ using reference = pair<ranges::range_reference_t<const _KeyContainer>, ranges::range_reference_t<_MappedContainer>>;
+ using const_reference =
+ pair<ranges::range_reference_t<const _KeyContainer>, ranges::range_reference_t<const _MappedContainer>>;
+ using size_type = size_t;
+ using difference_type = ptrdiff_t;
+ using iterator = __iterator<false>; // see [container.requirements]
+ using const_iterator = __iterator<true>; // see [container.requirements]
+ using reverse_iterator = std::reverse_iterator<iterator>;
+ using const_reverse_iterator = std::reverse_iterator<const_iterator>;
+ using key_container_type = _KeyContainer;
+ using mapped_container_type = _MappedContainer;
+
+ class value_compare {
+ private:
+ key_compare __comp_;
+ value_compare(key_compare __c) : __comp_(__c) {}
+ friend flat_map;
+
+ public:
+ _LIBCPP_HIDE_FROM_ABI bool operator()(const_reference __x, const_reference __y) const {
+ return __comp_(__x.first, __y.first);
+ }
+ };
+
+ struct containers {
+ key_container_type keys;
+ mapped_container_type values;
+ };
+
+private:
+ template <class _Allocator>
+ _LIBCPP_HIDE_FROM_ABI static constexpr bool __allocator_ctor_constraint =
+ _And<uses_allocator<key_container_type, _Allocator>, uses_allocator<mapped_container_type, _Allocator>>::value;
+
+ _LIBCPP_HIDE_FROM_ABI static constexpr bool __is_compare_transparent = __is_transparent_v<_Compare, _Compare>;
+
+ template <bool _Const>
+ struct __iterator {
+ private:
+ using __key_iterator = ranges::iterator_t<const key_container_type>;
+ using __mapped_iterator = ranges::iterator_t<__maybe_const<_Const, mapped_container_type>>;
+ using __reference = pair<iter_reference_t<__key_iterator>, iter_reference_t<__mapped_iterator>>;
+
+ struct __arrow_proxy {
+ __reference __ref_;
+ _LIBCPP_HIDE_FROM_ABI __reference* operator->() { return std::addressof(__ref_); }
+ };
+
+ __key_iterator __key_iter_;
+ __mapped_iterator __mapped_iter_;
+
+ friend flat_map;
+
+ public:
+ using iterator_concept = random_access_iterator_tag;
+ using iterator_category = input_iterator_tag;
+ using value_type = flat_map::value_type;
+ using difference_type = flat_map::difference_type;
+
+ _LIBCPP_HIDE_FROM_ABI __iterator() = default;
+
+ _LIBCPP_HIDE_FROM_ABI __iterator(__iterator<!_Const> __i)
+ requires _Const && convertible_to<ranges::iterator_t<key_container_type>, __key_iterator> &&
+ convertible_to<ranges::iterator_t<mapped_container_type>, __mapped_iterator>
+ : __key_iter_(std::move(__i.__key_iter_)), __mapped_iter_(std::move(__i.__mapped_iter_)) {}
+
+ _LIBCPP_HIDE_FROM_ABI __iterator(__key_iterator __key_iter, __mapped_iterator __mapped_iter)
+ : __key_iter_(std::move(__key_iter)), __mapped_iter_(std::move(__mapped_iter)) {}
+
+ _LIBCPP_HIDE_FROM_ABI __reference operator*() const { return __reference(*__key_iter_, *__mapped_iter_); }
+ _LIBCPP_HIDE_FROM_ABI __arrow_proxy operator->() const { return __arrow_proxy(**this); }
+
+ _LIBCPP_HIDE_FROM_ABI __iterator& operator++() {
+ ++__key_iter_;
+ ++__mapped_iter_;
+ return *this;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI __iterator operator++(int) {
+ __iterator __tmp(*this);
+ ++*this;
+ return __tmp;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI __iterator& operator--() {
+ --__key_iter_;
+ --__mapped_iter_;
+ return *this;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI __iterator operator--(int) {
+ __iterator __tmp(*this);
+ --*this;
+ return __tmp;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI __iterator& operator+=(difference_type __x) {
+ __key_iter_ += __x;
+ __mapped_iter_ += __x;
+ return *this;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI __iterator& operator-=(difference_type __x) {
+ __key_iter_ -= __x;
+ __mapped_iter_ -= __x;
+ return *this;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI __reference operator[](difference_type __n) const { return *(*this + __n); }
+
+ _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) {
+ return __x.__key_iter_ == __y.__key_iter_;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI friend bool operator<(const __iterator& __x, const __iterator& __y) {
+ return __x.__key_iter_ < __y.__key_iter_;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI friend bool operator>(const __iterator& __x, const __iterator& __y) { return __y < __x; }
+
+ _LIBCPP_HIDE_FROM_ABI friend bool operator<=(const __iterator& __x, const __iterator& __y) { return !(__y < __x); }
+
+ _LIBCPP_HIDE_FROM_ABI friend bool operator>=(const __iterator& __x, const __iterator& __y) { return !(__x < __y); }
+
+ _LIBCPP_HIDE_FROM_ABI friend auto operator<=>(const __iterator& __x, const __iterator& __y)
+ requires three_way_comparable<__key_iterator>
+ {
+ return __x.__key_iter_ <=> __y.__key_iter_;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI friend __iterator operator+(const __iterator& __i, difference_type __n) {
+ auto __tmp = __i;
+ __tmp += __n;
+ return __tmp;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI friend __iterator operator+(difference_type __n, const __iterator& __i) { return __i + __n; }
+
+ _LIBCPP_HIDE_FROM_ABI friend __iterator operator-(const __iterator& __i, difference_type __n) {
+ auto __tmp = __i;
+ __tmp -= __n;
+ return __tmp;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI friend difference_type operator-(const __iterator& __x, const __iterator& __y) {
+ return difference_type(__x.__key_iter_ - __y.__key_iter_);
+ }
+ };
+
+public:
+ // [flat.map.cons], construct/copy/destroy
+ _LIBCPP_HIDE_FROM_ABI flat_map() noexcept(
+ is_nothrow_default_constructible_v<_KeyContainer> && is_nothrow_default_constructible_v<_MappedContainer> &&
+ is_nothrow_default_constructible_v<_Compare>)
+ : __containers_(), __compare_() {}
+
+ template <class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI flat_map(const flat_map& __other, const _Allocator& __alloc)
+ : flat_map(__ctor_uses_allocator_tag{},
+ __alloc,
+ __other.__containers_.keys,
+ __other.__containers_.values,
+ __other.__compare_) {}
+
+ template <class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI flat_map(flat_map&& __other, const _Allocator& __alloc)
+ : flat_map(__ctor_uses_allocator_tag{},
+ __alloc,
+ std::move(__other.__containers_.keys),
+ std::move(__other.__containers_.values),
+ std::move(__other.__compare_)) {}
+
+ _LIBCPP_HIDE_FROM_ABI flat_map(
+ key_container_type __key_cont, mapped_container_type __mapped_cont, const key_compare& __comp = key_compare())
+ : __containers_{.keys = std::move(__key_cont), .values = std::move(__mapped_cont)}, __compare_(__comp) {
+ __sort_and_unique();
+ }
+
+ template <class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI
+ flat_map(const key_container_type& __key_cont, const mapped_container_type& __mapped_cont, const _Allocator& __alloc)
+ : flat_map(__ctor_uses_allocator_tag{}, __alloc, __key_cont, __mapped_cont) {
+ __sort_and_unique();
+ }
+
+ template <class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI
+ flat_map(const key_container_type& __key_cont,
+ const mapped_container_type& __mapped_cont,
+ const key_compare& __comp,
+ const _Allocator& __alloc)
+ : flat_map(__ctor_uses_allocator_tag{}, __alloc, __key_cont, __mapped_cont, __comp) {
+ __sort_and_unique();
+ }
+
+ _LIBCPP_HIDE_FROM_ABI
+ flat_map(sorted_unique_t,
+ key_container_type __key_cont,
+ mapped_container_type __mapped_cont,
+ const key_compare& __comp = key_compare())
+ : __containers_{.keys = std::move(__key_cont), .values = std::move(__mapped_cont)}, __compare_(__comp) {}
----------------
ldionne wrote:
This makes sense to me. We can use `_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT` (we have precedent for that e.g. https://github.com/llvm/llvm-project/blob/2e9b3316ee8b4339e2f023173db780ea09c44a88/libcxx/include/__debug_utils/strict_weak_ordering_check.h#L34).
This should also be the target of a test (for which we have precedent too, look at tests named `assert.FOO.pass.cpp`)
https://github.com/llvm/llvm-project/pull/98643
More information about the libcxx-commits
mailing list