-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunordered_set.h
More file actions
172 lines (169 loc) · 3.91 KB
/
Copy pathunordered_set.h
File metadata and controls
172 lines (169 loc) · 3.91 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// Created by arsen on 30.05.25.
//
#ifndef UNORDERED_SET_H
#define UNORDERED_SET_H
#include <functional>
#include <iterator>
#include <list>
#include <vector>
template <typename KeyT>
class UnorderedSet {
std::vector<std::list<KeyT>> buckets_;
size_t size_ = 0;
void Resize() {
if (buckets_.empty() || size_ >= buckets_.size()) {
size_t new_size = 0;
if (buckets_.empty()) {
new_size = 1;
} else {
new_size = buckets_.size() * 2;
}
Rehash(new_size);
}
}
public:
UnorderedSet() = default;
explicit UnorderedSet(size_t count) {
buckets_ = std::vector<std::list<KeyT>>(count);
size_ = 0;
}
template <typename ForwardIterator>
UnorderedSet(ForwardIterator start, ForwardIterator end) {
size_ = 0;
if (size_t length = std::distance(start, end); length > 0) {
buckets_.resize(length);
for (auto it = start; it != end; ++it) {
Insert(*it);
}
} else {
buckets_.resize(1);
}
}
UnorderedSet(const UnorderedSet &other) {
buckets_ = other.buckets_;
size_ = other.size_;
}
UnorderedSet(UnorderedSet &&other) noexcept {
buckets_ = std::move(other.buckets_);
size_ = other.size_;
other.size_ = 0;
}
~UnorderedSet() = default;
bool Empty() const {
return size_ == 0;
}
size_t Size() const {
return size_;
}
bool Find(const KeyT &key) const {
if (buckets_.empty()) {
return false;
}
for (auto &value : buckets_[Bucket(key)]) {
if (value == key) {
return true;
}
}
return false;
}
bool Insert(const KeyT &key) {
if (Find(key)) {
return false;
}
Resize();
buckets_[Bucket(key)].push_back(key);
++size_;
return true;
}
UnorderedSet &operator=(const UnorderedSet &other) {
if (this != &other) {
buckets_ = other.buckets_;
size_ = other.size_;
}
return *this;
}
UnorderedSet &operator=(UnorderedSet &&other) noexcept {
if (this != &other) {
buckets_ = std::move(other.buckets_);
size_ = other.size_;
other.size_ = 0;
other.buckets_.clear();
}
return *this;
}
bool Erase(const KeyT &key) {
if (buckets_.empty()) {
return false;
}
auto &bucket = buckets_[Bucket(key)];
for (auto it = bucket.begin(); it != bucket.end(); ++it) {
if (key == *it) {
bucket.erase(it);
--size_;
return true;
}
}
return false;
}
void Clear() {
for (auto &bucket : buckets_) {
bucket.clear();
}
size_ = 0;
}
void Rehash(size_t new_buckets_count) {
if (new_buckets_count == buckets_.size() || new_buckets_count < size_) {
return;
}
std::vector<std::list<KeyT>> new_buckets(new_buckets_count);
for (auto &bucket : buckets_) {
while (!bucket.empty()) {
auto it = bucket.begin();
KeyT key = std::move(*it);
bucket.erase(it);
std::hash<KeyT> hasher;
size_t new_index = hasher(key) % new_buckets_count;
new_buckets[new_index].push_back(std::move(key));
}
}
buckets_ = std::move(new_buckets);
}
void Reserve(size_t new_size) {
if (new_size > buckets_.size()) {
Rehash(new_size);
}
}
size_t BucketCount() const {
return buckets_.size();
}
size_t BucketSize(size_t index) const {
if (index >= buckets_.size()) {
return 0;
}
return buckets_[index].size();
}
size_t Bucket(const KeyT &key) const {
if (buckets_.empty()) {
return 0;
}
std::hash<KeyT> hash;
return hash(key) % buckets_.size();
}
double LoadFactor() const {
if (buckets_.empty()) {
return 0.0;
}
return static_cast<double>(size_) / static_cast<double>(buckets_.size());
}
bool Insert(KeyT &&key) {
if (Find(key)) {
return false;
}
Resize();
buckets_[Bucket(key)].emplace_back(std::move(key));
++size_;
return true;
}
};
#endif // UNORDERED_SET_H