aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/flat_hash/lib/set.h
blob: 5266293c6c39a3f8551aa2be4cc185c4375c7de9 (plain) (blame)
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
#pragma once

#include "table.h"
#include "concepts/iterator.h"

#include <util/generic/algorithm.h>

namespace NFlatHash {

namespace NPrivate {

struct TSimpleKeyGetter {
    template <class T>
    static constexpr auto& Apply(T& t) noexcept { return t; };

    template <class T>
    static constexpr const auto& Apply(const T& t) noexcept { return t; };
};

}  // namespace NPrivate

template <class Key,
          class Hash,
          class KeyEqual,
          class Container,
          class Probing,
          class SizeFitter,
          class Expander>
class TSet : private TTable<Hash,
                            KeyEqual,
                            Container,
                            NPrivate::TSimpleKeyGetter,
                            Probing,
                            SizeFitter,
                            Expander,
                            std::add_const>
{
private:
    using TBase = TTable<Hash,
                         KeyEqual,
                         Container,
                         NPrivate::TSimpleKeyGetter,
                         Probing,
                         SizeFitter,
                         Expander,
                         std::add_const>;

    static_assert(std::is_same_v<Key, typename Container::value_type>);

public:
    using key_type = Key;
    using typename TBase::value_type;
    using typename TBase::size_type;
    using typename TBase::difference_type;
    using typename TBase::hasher;
    using typename TBase::key_equal;
    using typename TBase::reference;
    using typename TBase::const_reference;
    using typename TBase::iterator;
    using typename TBase::const_iterator;
    using typename TBase::allocator_type;
    using typename TBase::pointer;
    using typename TBase::const_pointer;

private:
    static constexpr size_type INIT_SIZE = 8;

public:
    TSet() : TBase(INIT_SIZE) {}

    template <class... Rest>
    TSet(size_type initSize, Rest&&... rest) : TBase(initSize, std::forward<Rest>(rest)...) {}

    template <class I, class... Rest>
    TSet(I first, I last,
         std::enable_if_t<NConcepts::IteratorV<I>, size_type> initSize = INIT_SIZE,
         Rest&&... rest)
        : TBase(initSize, std::forward<Rest>(rest)...)
    {
        insert(first, last);
    }

    template <class... Rest>
    TSet(std::initializer_list<value_type> il, size_type initSize = INIT_SIZE, Rest&&... rest)
        : TBase(initSize, std::forward<Rest>(rest)...)
    {
        insert(il.begin(), il.end());
    }

    TSet(std::initializer_list<value_type> il, size_type initSize = INIT_SIZE)
        : TBase(initSize)
    {
        insert(il.begin(), il.end());
    }

    TSet(const TSet&) = default;
    TSet(TSet&&) = default;

    TSet& operator=(const TSet&) = default;
    TSet& operator=(TSet&&) = default;

    // Iterators
    using TBase::begin;
    using TBase::cbegin;
    using TBase::end;
    using TBase::cend;

    // Capacity
    using TBase::empty;
    using TBase::size;

    // Modifiers
    using TBase::clear;
    using TBase::insert;
    using TBase::emplace;
    using TBase::emplace_hint;
    using TBase::erase;
    using TBase::swap;

    // Lookup
    using TBase::count;
    using TBase::find;
    using TBase::contains;

    // Bucket interface
    using TBase::bucket_count;
    using TBase::bucket_size;

    // Hash policy
    using TBase::load_factor;
    using TBase::rehash;
    using TBase::reserve;

    // Observers
    using TBase::hash_function;
    using TBase::key_eq;

    friend bool operator==(const TSet& lhs, const TSet& rhs) {
        return lhs.size() == rhs.size() && AllOf(lhs, [&rhs](const auto& v) {
            return rhs.contains(v);
        });
    }

    friend bool operator!=(const TSet& lhs, const TSet& rhs) { return !(lhs == rhs); }
};

}  // namespace NFlatHash