xtd 0.2.0
Loading...
Searching...
No Matches
hash_set.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "helpers/equator.hpp"
7#include "helpers/hasher.hpp"
8#include "iset.hpp"
9#include <unordered_set>
10
12namespace xtd {
14 namespace collections {
16 namespace generic {
36 template<class type_t, class hasher_t = xtd::collections::generic::helpers::hasher<type_t>, class equator_t = xtd::collections::generic::helpers::equator<type_t>, class allocator_t = xtd::collections::generic::helpers::allocator<type_t >>
37 class hash_set : public xtd::object, public xtd::collections::generic::iset<type_t> {
38 public:
40
49 using base_type = std::unordered_set<key_type, hasher_t, equator_t, allocator_t>;
51
53
72 hash_set() noexcept = default;
77 hash_set(const xtd::collections::generic::iequality_comparer<key_type>& comparer) noexcept : data_(xtd::new_ptr<hash_set_data>(comparer)) {}
86 hash_set(const ienumerable<value_type>& collection) noexcept {
87 for (const auto& item : collection)
88 add(item);
89 }
90
97 for (const auto& item : collection)
98 add(item);
99 }
100
106 hash_set(hash_set&& other) noexcept = default;
107 hash_set(const hash_set& other) noexcept : data_(xtd::new_ptr<hash_set_data>(other.data_->comparer, other.data_->items, other.data_->version)) {}
108 hash_set(std::unordered_set<key_type>&& other) noexcept : data_(xtd::new_ptr<hash_set_data>(std::move(other))) {}
109 hash_set(const std::unordered_set<key_type>& other) noexcept {
110 for (auto iterator = other.begin(); iterator != other.end(); ++iterator)
111 add(*iterator);
112 }
113 hash_set(std::initializer_list <value_type> init) {
114 ensure_capacity(init.size());
115 for (const auto& value : init)
116 add(value);
117 }
118 hash_set(std::initializer_list <value_type> init, const xtd::collections::generic::iequality_comparer<key_type>& comparer) noexcept : data_(xtd::new_ptr<hash_set_data>(comparer)) {
119 ensure_capacity(init.size());
120 for (const auto& value : init)
121 add(value);
122 }
123 template <class input_iterator_t>
124 explicit hash_set(input_iterator_t first, input_iterator_t last) {
125 for (auto iterator = first; iterator != last; ++iterator)
126 add(*iterator);
127 }
129
131
135 auto capacity() const noexcept -> size_type {return items().bucket_count();}
136
140 auto comparer() const noexcept -> const iequality_comparer<key_type>& {
141 if (!data_->comparer) return equality_comparer <key_type>::default_equality_comparer();
142 return *data_->comparer;
143 }
144
150 auto count() const noexcept -> size_type override {return items().size();}
151
154 virtual auto items() const noexcept -> const base_type& {return data_->items;}
157 virtual auto items() noexcept -> base_type& {return data_->items;}
159
161
166 auto add(const key_type& item) noexcept -> bool override {
167 if (contains(item)) return false;
168 items().insert(item);
169 ++data_->version;
170 return true;
171 }
172
175 auto clear() noexcept -> void override {
176 items().clear();
177 ++data_->version;
178 }
179
183 auto contains(const value_type & item) const noexcept -> bool override {
184 return items().find(item) != items().end();
185 }
186
190 auto copy_to(xtd::array<type_t>& array) const -> void {
191 copy_to(0, array, 0, count());
192 }
193
199 auto copy_to(xtd::array<type_t>& array, size_type index) const -> void override {
200 copy_to(0, array, index, count());
201 }
202
209 auto copy_to(size_type index, xtd::array<type_t>& array, size_type array_index, size_type count) const -> void {
211 auto increment = size_type {};
212 for (const auto& item : self_) {
213 if (increment >= index + count) return;
214 if (increment++ >= index) array[array_index++] = item;
215 }
216 }
217
222 data_->items.reserve(capacity);
223 return self_.capacity();
224 }
225
229 auto except_with(const xtd::collections::generic::ienumerable<type_t>& other) noexcept -> void override {
230 if (&other == this) {
231 clear();
232 return;
233 }
234 for (const type_t& item : other)
235 remove(item);
236 }
237
240 enumerator<value_type> get_enumerator() const noexcept override {
241 struct hash_set_enumerator : public ienumerator < value_type > {
242 explicit hash_set_enumerator(const hash_set & items, size_type version) : items_(items), version_(version) {}
243
244 const value_type& current() const override {
246 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
247 value_ = value_type {*iterator_};
248 return value_;
249 }
250
251 bool move_next() override {
252 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
253 if (index_++ && iterator_ != items_.data_->items.cend()) ++iterator_;
254 else iterator_ = items_.items().cbegin();
255 return iterator_ != items_.data_->items.cend();
256 }
257
258 void reset() override {
259 index_ = 0;
260 version_ = items_.data_->version;
261 iterator_ = items_.items().cend();
262 }
263
264 private:
265 size_type index_ = 0;
266 const hash_set& items_;
267 typename hash_set::base_type::const_iterator iterator_ = items_.data_->items.cend();
268 mutable value_type value_;
269 size_type version_ = 0;
270 };
271 return {new_ptr < hash_set_enumerator > (self_, data_->version)};
272 }
273
278 auto to_keep = hash_set {other};
279 auto to_remove = hash_set {};
280 for (const type_t& item : self_)
281 if (!to_keep.contains(item))
282 to_remove.add(item);
283 except_with(to_remove);
284 }
285
289 auto is_proper_subset_of(const xtd::collections::generic::ienumerable<type_t>& other) const noexcept -> bool override {
290 auto set = hash_set {other};
291 if (count() == 0) return set.count() > 0;
292 if (count() >= set.count()) return false;
293 return sub_set(set);
294 }
295
300 auto set = hash_set {other};
301 if (set.count() == 0) return count() > 0;
302 if (set.count() >= count()) return false;
303 return super_set(set);
304 }
305
309 auto is_subset_of(const xtd::collections::generic::ienumerable<type_t>& other) const noexcept -> bool override {
310 return sub_set(hash_set {other});
311 }
312
316 auto is_superset_of(const xtd::collections::generic::ienumerable<type_t>& other) const noexcept -> bool override {
317 return super_set(hash_set {other});
318 }
319
323 auto overlaps(const xtd::collections::generic::ienumerable<type_t>& other) const noexcept -> bool override {
324 if (count() == 0) return false;
325 for (const auto& item : other)
326 if (contains(item)) return true;
327 return false;
328 }
329
333 auto remove(const type_t& item) noexcept -> bool override {
334 auto result = items().erase(item) == 1;
335 ++data_->version;
336 return result;
337 }
338
342 template<class predicate_t>
343 auto remove_where(predicate_t match) noexcept -> size_type {
344 auto to_remove = hash_set {};
345 for (const auto& item : self_)
346 if (match(item)) to_remove.add(item);
347 for (const auto& item : to_remove)
348 remove(item);
349 return to_remove.count();
350 }
351
355 auto set_equals(const xtd::collections::generic::ienumerable<type_t>& other) const noexcept -> bool override {
356 auto set = hash_set {other};
357 if (count() != set.count()) return false;
358 for (const auto& item : other)
359 if (!contains(item)) return false;
360 return true;
361 }
362
367 for (const auto& item : other)
368 if (contains(item)) remove(item);
369 else add(item);
370 }
371
374 auto to_string() const noexcept -> xtd::string override {return xtd::string::format("{{{}}}", xtd::string::join(", ", self_));}
375
379 auto union_with(const xtd::collections::generic::ienumerable<type_t>& other) noexcept -> void override {
380 for (const auto& item : other)
381 if (!contains(item)) add(item);
382 }
383
385
387
390
391 private:
392 auto is_read_only() const noexcept -> bool override {return false;}
393 auto is_synchronized() const noexcept -> bool override {return false;}
394 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
395 auto sub_set(const hash_set& set) const noexcept -> bool {
396 if (count() == 0) return true;
397 if (count() > set.count()) return false;
398 for (const auto& item : self_)
399 if (!set.contains(item)) return false;
400 return true;
401 }
402
403 auto super_set(const hash_set& set) const noexcept -> bool {
404 if (set.count() == 0) return true;
405 if (set.count() > count()) return false;
406 for (const auto& item : set)
407 if (!contains(item)) return false;
408 return true;
409 }
410
411 struct hash_set_data {
412 hash_set_data() = default;
413 hash_set_data(const xtd::collections::generic::iequality_comparer<key_type>& comparer) : comparer {&comparer}, items {size_type {}, hasher_t {comparer}, equator_t {comparer}, allocator_t {}} {}
414 hash_set_data(const xtd::collections::generic::iequality_comparer<key_type>& comparer, const base_type& items, size_type version) noexcept : comparer {&comparer}, items {size_type {}, hasher_t {comparer}, equator_t {comparer}, allocator_t {}}, version {version} {
415 for (const auto& item : items)
416 self_.items.insert(item);
417 }
418 hash_set_data(base_type&& items, size_type version) noexcept : version {version} {
419 for (auto&& item : items)
420 self_.items.insert(item);
421 }
422
423 const xtd::collections::generic::iequality_comparer<key_type>* comparer = null;
424 base_type items;
425 size_type version = 0;
426 xtd::object sync_root;
427 };
429 };
430
432 // Deduction guides for xtd::collections::generic::hash_set
433 // {
434 template<class type_t>
436
437 template<class type_t>
439
440 template <class type_t>
441 hash_set(std::initializer_list<type_t>) -> hash_set<type_t>;
442
443 template <class input_iterator_t >
444 hash_set(input_iterator_t, input_iterator_t) -> hash_set<std::iter_value_t<input_iterator_t>>;
445 // }
447 }
448 }
449}
Contains xtd::collections::generic::helpers::allocator alias.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
virtual size_type length() const noexcept
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:124
static basic_string join(const basic_string &separator, const collection_t &values) noexcept
Definition basic_string.hpp:1702
static auto default_equality_comparer() -> const equality_comparer &
Gets the default equality comparer for the type specified by the generic argument.
Definition equality_comparer.hpp:42
Represents a set of values.
Definition hash_set.hpp:37
auto contains(const value_type &item) const noexcept -> bool override
Determines whether an element is in the xtd::collections::generic::dictionary <key_t,...
Definition hash_set.hpp:183
typename xtd::collections::generic::iset< type_t >::value_type value_type
Represents the hash set value type.
Definition hash_set.hpp:45
auto intersect_with(const xtd::collections::generic::ienumerable< type_t > &other) noexcept -> void override
Modifies the current set so that it contains only elements that are also in a specified collection.
Definition hash_set.hpp:277
auto symetric_excep_with(const xtd::collections::generic::ienumerable< type_t > &other) noexcept -> void override
Modifies the current set so that it contains only elements that are present either in the current set...
Definition hash_set.hpp:366
auto capacity() const noexcept -> size_type
Gets the total numbers of elements the internal data structure can hold without resizing.
Definition hash_set.hpp:135
auto except_with(const xtd::collections::generic::ienumerable< type_t > &other) noexcept -> void override
Removes all elements in the specified collection from the current set.
Definition hash_set.hpp:229
auto overlaps(const xtd::collections::generic::ienumerable< type_t > &other) const noexcept -> bool override
Determines whether the current set overlaps with the specified collection.
Definition hash_set.hpp:323
auto set_equals(const xtd::collections::generic::ienumerable< type_t > &other) const noexcept -> bool override
Determines whether the current set and the specified collection contain the same elements.
Definition hash_set.hpp:355
auto copy_to(size_type index, xtd::array< type_t > &array, size_type array_index, size_type count) const -> void
Copies a specified number of elements from hash_set <type_t> to a compatible one-dimensional array,...
Definition hash_set.hpp:209
auto is_proper_superset_of(const xtd::collections::generic::ienumerable< type_t > &other) const noexcept -> bool override
Determines whether the current set is a proper (strict) superset of a specified collection.
Definition hash_set.hpp:299
auto is_superset_of(const xtd::collections::generic::ienumerable< type_t > &other) const noexcept -> bool override
Determines whether a set is a superset of a specified collection.
Definition hash_set.hpp:316
hash_set(const ienumerable< value_type > &collection) noexcept
Initializes a new instance of the xtd::collections::generic::hash_set <type_t> class that uses the de...
Definition hash_set.hpp:86
auto is_subset_of(const xtd::collections::generic::ienumerable< type_t > &other) const noexcept -> bool override
Determines whether a set is a subset of a specified collection.
Definition hash_set.hpp:309
xtd::size size_type
Represents the hash set size type.
Definition hash_set.hpp:47
auto to_string() const noexcept -> xtd::string override
Gets a string that represents the current object.
Definition hash_set.hpp:374
virtual auto items() const noexcept -> const base_type &
Returns the underlying base type items.
Definition hash_set.hpp:154
hash_set() noexcept=default
Initializes a new instance of the xtd::collections::generic::hash_set <type_t> class that is empty an...
auto clear() noexcept -> void override
Removes all keys and values from the xtd::collections::generic::dictionary <key_t,...
Definition hash_set.hpp:175
auto copy_to(xtd::array< type_t > &array, size_type index) const -> void override
Copies the complete hash_set <type_t> to a compatible one-dimensional array, starting at the specifie...
Definition hash_set.hpp:199
auto copy_to(xtd::array< type_t > &array) const -> void
Copies the complete hash_set <type_t> to a compatible one-dimensional array, starting at the beginnin...
Definition hash_set.hpp:190
auto union_with(const xtd::collections::generic::ienumerable< type_t > &other) noexcept -> void override
Modifies the current set so that it contains all elements that are present in the current set,...
Definition hash_set.hpp:379
auto remove(const type_t &item) noexcept -> bool override
Removes a specified item from the hash_set <type_t>.
Definition hash_set.hpp:333
auto ensure_capacity(xtd::size capacity) noexcept -> xtd::size
Ensures that the dictionary can hold up to a specified number of entries without any further expansio...
Definition hash_set.hpp:221
virtual auto items() noexcept -> base_type &
Returns the underlying base type items.
Definition hash_set.hpp:157
std::unordered_set< key_type, hasher_t, equator_t, allocator_t > base_type
Represents the hash set base type.
Definition hash_set.hpp:49
hash_set(size_type capacity) noexcept
Initializes a new instance of the xtd::collections::generic::hash_set <type_t> class that is empty,...
Definition hash_set.hpp:103
auto remove_where(predicate_t match) noexcept -> size_type
Removes all elements that match the conditions defined by the specified predicate from a hash_set <ty...
Definition hash_set.hpp:343
enumerator< value_type > get_enumerator() const noexcept override
Returns an enumerator that iterates through the xtd::collections::generic::hash_set <type_t>.
Definition hash_set.hpp:240
auto add(const key_type &item) noexcept -> bool override
Adds an item to the xtd::collections::generic::icollection <type_t>.
Definition hash_set.hpp:166
auto is_proper_subset_of(const xtd::collections::generic::ienumerable< type_t > &other) const noexcept -> bool override
Determines whether the current set is a proper (strict) superset of a specified collection.
Definition hash_set.hpp:289
auto comparer() const noexcept -> const iequality_comparer< key_type > &
Gets the xtd::collections::generic::iequality_comparer <type_t> that is used to determine equality of...
Definition hash_set.hpp:140
auto count() const noexcept -> size_type override
Gets the number of key/value pairs contained in the xtd::collections::generic::dictionary <key_t,...
Definition hash_set.hpp:150
hash_set(const ienumerable< value_type > &collection, const xtd::collections::generic::iequality_comparer< key_type > &comparer) noexcept
Initializes a new instance of the xtd::collections::generic::hash_set <type_t> class that uses the sp...
Definition hash_set.hpp:96
typename xtd::collections::generic::iset< type_t >::key_type key_type
Represents the hash set key type.
Definition hash_set.hpp:43
friend class ienumerable
Exposes an enumerator, which supports a simple iteration over a non-generic collection.
Definition ienumerable_abstract.hpp:38
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
typename xtd::collections::generic::extensions::enumerable_iterators< type_t, xtd::collections::generic::ienumerable< type_t > >::iterator iterator
Represents the iterator of xtd::collections::generic::ienumerable value type.
Definition ienumerable.hpp:48
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
Defines methods to support the comparison of objects for equality.
Definition iequality_comparer.hpp:34
Provides the base interface for the abstraction of sets.
Definition iset.hpp:33
type_t value_type
Represents the set value type.
Definition iset.hpp:41
typename icollection< type_t >::iterator iterator
Represents the iterator of xtd::collections::generic::ienumerable value type.
Definition iset.hpp:43
type_t key_type
Represents the set key type.
Definition iset.hpp:39
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Represents the version number of an assembly, operating system, or the xtd. This class cannot be inhe...
Definition version.hpp:115
Contains xtd::collections::generic::helpers::equator struct.
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
@ argument
The argument is not valid.
Definition exception_case.hpp:31
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:65
#define self_
The self_ expression is a reference value expression whose value is the reference of the implicit obj...
Definition self.hpp:20
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
null_ptr null
Represents a null pointer value.
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ other
The operating system is other.
Definition platform_id.hpp:60
@ add
The Add key.
Definition console_key.hpp:170
Contains xtd::collections::generic::helpers::hasher struct.
Contains xtd::collections::generic::iset <type_t> interface.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
read_only_span< type_t, count > first() const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:282
read_only_span< type_t, count > last() const
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:307
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38