LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
unordered_set.h
Go to the documentation of this file.
1/*
2 * Frozen
3 * Copyright 2016 QuarksLab
4 *
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23#ifndef FROZEN_LETITGO_UNORDERED_SET_H
24#define FROZEN_LETITGO_UNORDERED_SET_H
25
27#include "frozen/bits/elsa.h"
28#include "frozen/bits/pmh.h"
29#include "frozen/bits/version.h"
30#include "frozen/random.h"
31
32#include <utility>
33
34namespace frozen {
35
36namespace bits {
37
38struct Get {
39 template <class T> constexpr T const &operator()(T const &key) const {
40 return key;
41 }
42};
43
44} // namespace bits
45
46template <class Key, std::size_t N, typename Hash = elsa<Key>,
47 class KeyEqual = std::equal_to<Key>>
48class unordered_set : private KeyEqual {
49 static constexpr std::size_t storage_size =
50 bits::next_highest_power_of_two(N) * (N < 32 ? 2 : 1); // size adjustment to prevent high collision rate for small sets
51 using container_type = bits::carray<Key, N>;
52 using tables_type = bits::pmh_tables<storage_size, Hash>;
53
54 container_type keys_;
55 tables_type tables_;
56
57public:
58 /* typedefs */
59 using key_type = Key;
60 using value_type = Key;
63 using hasher = Hash;
64 using key_equal = KeyEqual;
71
72public:
73 /* constructors */
74 unordered_set(unordered_set const &) = default;
75 constexpr unordered_set(container_type keys, Hash const &hash,
76 KeyEqual const &equal)
77 : KeyEqual{equal}
78 , keys_{keys}
79 , tables_{bits::make_pmh_tables<storage_size>(
80 keys_, hash, equal, bits::Get{}, default_prg_t{})} {}
81 explicit constexpr unordered_set(container_type keys)
82 : unordered_set{keys, Hash{}, KeyEqual{}} {}
83
84 constexpr unordered_set(std::initializer_list<Key> keys)
85 : unordered_set{keys, Hash{}, KeyEqual{}} {}
86
87 constexpr unordered_set(std::initializer_list<Key> keys, Hash const & hash, KeyEqual const & equal)
88 : unordered_set{container_type{keys}, hash, equal} {
89 }
90
91 /* iterators */
92 constexpr const_iterator begin() const { return keys_.begin(); }
93 constexpr const_iterator end() const { return keys_.end(); }
94 constexpr const_iterator cbegin() const { return keys_.begin(); }
95 constexpr const_iterator cend() const { return keys_.end(); }
96
97 /* capacity */
98 constexpr bool empty() const { return !N; }
99 constexpr size_type size() const { return N; }
100 constexpr size_type max_size() const { return N; }
101
102 /* lookup */
103 template <class KeyType>
104 constexpr std::size_t count(KeyType const &key) const {
105 return find(key, hash_function(), key_eq()) != end();
106 }
107
108 template <class KeyType, class Hasher, class Equal>
109 constexpr const_iterator find(KeyType const &key, Hasher const &hash, Equal const &equal) const {
110 auto const pos = tables_.lookup(key, hash);
111 auto it = keys_.begin() + pos;
112 if (it != keys_.end() && equal(*it, key))
113 return it;
114 else
115 return keys_.end();
116 }
117 template <class KeyType>
118 constexpr const_iterator find(KeyType const &key) const {
119 auto const pos = tables_.lookup(key, hash_function());
120 auto it = keys_.begin() + pos;
121 if (it != keys_.end() && key_eq()(*it, key))
122 return it;
123 else
124 return keys_.end();
125 }
126
127 template <class KeyType>
128 constexpr bool contains(KeyType const &key) const {
129 return this->find(key) != keys_.end();
130 }
131
132 template <class KeyType>
133 constexpr std::pair<const_iterator, const_iterator> equal_range(KeyType const &key) const {
134 auto const it = find(key);
135 if (it != end())
136 return {it, it + 1};
137 else
138 return {keys_.end(), keys_.end()};
139 }
140
141 /* bucket interface */
142 constexpr std::size_t bucket_count() const { return storage_size; }
143 constexpr std::size_t max_bucket_count() const { return storage_size; }
144
145 /* observers*/
146 constexpr const hasher& hash_function() const { return tables_.hash_function(); }
147 constexpr const key_equal& key_eq() const { return static_cast<KeyEqual const&>(*this); }
148};
149
150template <typename T, std::size_t N>
151constexpr auto make_unordered_set(T const (&keys)[N]) {
152 return unordered_set<T, N>{keys};
153}
154
155template <typename T, std::size_t N, typename Hasher, typename Equal>
156constexpr auto make_unordered_set(T const (&keys)[N], Hasher const& hash, Equal const& equal) {
157 return unordered_set<T, N, Hasher, Equal>{keys, hash, equal};
158}
159
160template <typename T, std::size_t N>
161constexpr auto make_unordered_set(std::array<T, N> const &keys) {
162 return unordered_set<T, N>{keys};
163}
164
165template <typename T, std::size_t N, typename Hasher, typename Equal>
166constexpr auto make_unordered_set(std::array<T, N> const &keys, Hasher const& hash, Equal const& equal) {
167 return unordered_set<T, N, Hasher, Equal>{keys, hash, equal};
168}
169
170#ifdef FROZEN_LETITGO_HAS_DEDUCTION_GUIDES
171
172template <class T, class... Args>
173unordered_set(T, Args...) -> unordered_set<T, sizeof...(Args) + 1>;
174
175#endif
176
177} // namespace frozen
178
179#endif
Definition basic_types.h:90
const value_type & const_reference
Definition basic_types.h:109
std::ptrdiff_t difference_type
Definition basic_types.h:115
const_pointer const_iterator
Definition basic_types.h:113
const value_type * const_pointer
Definition basic_types.h:111
std::size_t size_type
Definition basic_types.h:114
Definition unordered_set.h:48
constexpr std::size_t bucket_count() const
Definition unordered_set.h:142
typename container_type::difference_type difference_type
Definition unordered_set.h:62
const_reference reference
Definition unordered_set.h:66
constexpr unordered_set(container_type keys, Hash const &hash, KeyEqual const &equal)
Definition unordered_set.h:75
constexpr bool contains(KeyType const &key) const
Definition unordered_set.h:128
constexpr unordered_set(std::initializer_list< Key > keys)
Definition unordered_set.h:84
Hash hasher
Definition unordered_set.h:63
Key key_type
Definition unordered_set.h:59
constexpr const_iterator cbegin() const
Definition unordered_set.h:94
typename container_type::const_reference const_reference
Definition unordered_set.h:65
const_pointer pointer
Definition unordered_set.h:68
typename container_type::const_iterator const_iterator
Definition unordered_set.h:69
constexpr unordered_set(std::initializer_list< Key > keys, Hash const &hash, KeyEqual const &equal)
Definition unordered_set.h:87
constexpr const_iterator end() const
Definition unordered_set.h:93
const_iterator iterator
Definition unordered_set.h:70
constexpr const_iterator begin() const
Definition unordered_set.h:92
constexpr size_type size() const
Definition unordered_set.h:99
unordered_set(unordered_set const &)=default
constexpr std::pair< const_iterator, const_iterator > equal_range(KeyType const &key) const
Definition unordered_set.h:133
constexpr unordered_set(container_type keys)
Definition unordered_set.h:81
constexpr size_type max_size() const
Definition unordered_set.h:100
constexpr const_iterator find(KeyType const &key) const
Definition unordered_set.h:118
constexpr const_iterator find(KeyType const &key, Hasher const &hash, Equal const &equal) const
Definition unordered_set.h:109
constexpr std::size_t count(KeyType const &key) const
Definition unordered_set.h:104
constexpr const key_equal & key_eq() const
Definition unordered_set.h:147
typename container_type::const_pointer const_pointer
Definition unordered_set.h:67
constexpr std::size_t max_bucket_count() const
Definition unordered_set.h:143
typename container_type::size_type size_type
Definition unordered_set.h:61
constexpr const hasher & hash_function() const
Definition unordered_set.h:146
Key value_type
Definition unordered_set.h:60
constexpr const_iterator cend() const
Definition unordered_set.h:95
KeyEqual key_equal
Definition unordered_set.h:64
constexpr bool empty() const
Definition unordered_set.h:98
Definition algorithms.h:33
auto constexpr next_highest_power_of_two(std::size_t v)
Definition algorithms.h:35
Definition algorithm.h:30
constexpr auto make_unordered_set(T const (&keys)[N])
Definition unordered_set.h:151
minstd_rand default_prg_t
Definition random.h:93
Definition unordered_set.h:38
constexpr T const & operator()(T const &key) const
Definition unordered_set.h:39
Definition pmh.h:151