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 template <class input_iterator_t>
119 explicit hash_set(input_iterator_t first, input_iterator_t last) {
120 for (auto iterator = first; iterator != last; ++iterator)
121 add(*iterator);
122 }
124
126
130 auto capacity() const noexcept -> size_type {return items().bucket_count();}
131
135 auto comparer() const noexcept -> const iequality_comparer<key_type>& {
136 if (!data_->comparer) return equality_comparer <key_type>::default_equality_comparer();
137 return *data_->comparer;
138 }
139
145 auto count() const noexcept -> size_type override {return items().size();}
146
149 virtual auto items() const noexcept -> const base_type& {return data_->items;}
152 virtual auto items() noexcept -> base_type& {return data_->items;}
154
156
161 auto add(const key_type& item) noexcept -> bool override {
162 if (contains(item)) return false;
163 items().insert(item);
164 ++data_->version;
165 return true;
166 }
167
170 auto clear() noexcept -> void override {
171 items().clear();
172 ++data_->version;
173 }
174
178 auto contains(const value_type & item) const noexcept -> bool override {
179 return items().find(item) != items().end();
180 }
181
186 auto copy_to(xtd::array<type_t>& array) const -> void {
187 copy_to(0, array, 0, count());
188 }
189
196 auto copy_to(xtd::array<type_t>& array, size_type index) const -> void override {
197 copy_to(0, array, index, count());
198 }
199
207 auto copy_to(size_type index, xtd::array<type_t>& array, size_type array_index, size_type count) const -> void {
209 auto increment = size_type {};
210 for (const auto& item : self_) {
211 if (increment >= index + count) return;
212 if (increment++ >= index) array[array_index++] = item;
213 }
214 }
215
220 data_->items.reserve(capacity);
221 return self_.capacity();
222 }
223
227 auto except_with(const xtd::collections::generic::ienumerable<type_t>& other) noexcept -> void override {
228 if (&other == this) {
229 clear();
230 return;
231 }
232 for (const type_t& item : other)
233 remove(item);
234 }
235
238 enumerator<value_type> get_enumerator() const noexcept override {
239 struct hash_set_enumerator : public ienumerator < value_type > {
240 explicit hash_set_enumerator(const hash_set & items, size_type version) : items_(items), version_(version) {}
241
242 const value_type& current() const override {
244 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
245 value_ = value_type {*iterator_};
246 return value_;
247 }
248
249 bool move_next() override {
250 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
251 if (index_++ && iterator_ != items_.data_->items.cend()) ++iterator_;
252 else iterator_ = items_.items().cbegin();
253 return iterator_ != items_.data_->items.cend();
254 }
255
256 void reset() override {
257 index_ = 0;
258 version_ = items_.data_->version;
259 iterator_ = items_.items().cend();
260 }
261
262 private:
263 size_type index_ = 0;
264 const hash_set& items_;
265 typename hash_set::base_type::const_iterator iterator_ = items_.data_->items.cend();
266 mutable value_type value_;
267 size_type version_ = 0;
268 };
269 return {new_ptr < hash_set_enumerator > (self_, data_->version)};
270 }
271
276 auto to_keep = hash_set {other};
277 auto to_remove = hash_set {};
278 for (const type_t& item : self_)
279 if (!to_keep.contains(item))
280 to_remove.add(item);
281 except_with(to_remove);
282 }
283
287 auto is_proper_subset_of(const xtd::collections::generic::ienumerable<type_t>& other) const noexcept -> bool override {
288 auto set = hash_set {other};
289 if (count() == 0) return set.count() > 0;
290 if (count() >= set.count()) return false;
291 return sub_set(set);
292 }
293
298 auto set = hash_set {other};
299 if (set.count() == 0) return count() > 0;
300 if (set.count() >= count()) return false;
301 return super_set(set);
302 }
303
307 auto is_subset_of(const xtd::collections::generic::ienumerable<type_t>& other) const noexcept -> bool override {
308 return sub_set(hash_set {other});
309 }
310
314 auto is_superset_of(const xtd::collections::generic::ienumerable<type_t>& other) const noexcept -> bool override {
315 return super_set(hash_set {other});
316 }
317
321 auto overlaps(const xtd::collections::generic::ienumerable<type_t>& other) const noexcept -> bool override {
322 if (count() == 0) return false;
323 for (const auto& item : other)
324 if (contains(item)) return true;
325 return false;
326 }
327
331 auto remove(const type_t& item) noexcept -> bool override {
332 auto result = items().erase(item) == 1;
333 ++data_->version;
334 return result;
335 }
336
340 template<class predicate_t>
341 auto remove_where(predicate_t match) noexcept -> size_type {
342 auto to_remove = hash_set {};
343 for (const auto& item : self_)
344 if (match(item)) to_remove.add(item);
345 for (const auto& item : to_remove)
346 remove(item);
347 return to_remove.count();
348 }
349
353 auto set_equals(const xtd::collections::generic::ienumerable<type_t>& other) const noexcept -> bool override {
354 auto set = hash_set {other};
355 if (count() != set.count()) return false;
356 for (const auto& item : other)
357 if (!contains(item)) return false;
358 return true;
359 }
360
365 for (const auto& item : other)
366 if (contains(item)) remove(item);
367 else add(item);
368 }
369
372 auto to_string() const noexcept -> xtd::string override {return xtd::string::format("{{{}}}", xtd::string::join(", ", self_));}
373
377 auto union_with(const xtd::collections::generic::ienumerable<type_t>& other) noexcept -> void override {
378 for (const auto& item : other)
379 if (!contains(item)) add(item);
380 }
381
383
385
388
389 private:
390 auto is_read_only() const noexcept -> bool override {return false;}
391 auto is_synchronized() const noexcept -> bool override {return false;}
392 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
393 auto sub_set(const hash_set& set) const noexcept -> bool {
394 if (count() == 0) return true;
395 if (count() > set.count()) return false;
396 for (const auto& item : self_)
397 if (!set.contains(item)) return false;
398 return true;
399 }
400
401 auto super_set(const hash_set& set) const noexcept -> bool {
402 if (set.count() == 0) return true;
403 if (set.count() > count()) return false;
404 for (const auto& item : set)
405 if (!contains(item)) return false;
406 return true;
407 }
408
409 struct hash_set_data {
410 hash_set_data() = default;
411 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 {}} {}
412 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} {
413 for (const auto& item : items)
414 self_.items.insert(item);
415 }
416 hash_set_data(base_type&& items, size_type version) noexcept : version {version} {
417 for (auto&& item : items)
418 self_.items.insert(item);
419 }
420
421 const xtd::collections::generic::iequality_comparer<key_type>* comparer = null;
422 base_type items;
423 size_type version = 0;
424 xtd::object sync_root;
425 };
427 };
428
430 // Deduction guides for xtd::collections::generic::hash_set
431 // {
432 template<class type_t>
434
435 template<class type_t>
437
438 template <class type_t>
439 hash_set(std::initializer_list<type_t>) -> hash_set<type_t>;
440
441 template <class input_iterator_t >
442 hash_set(input_iterator_t, input_iterator_t) -> hash_set<typename input_iterator_t::value_type>;
443 // }
445 }
446 }
447}
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 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:178
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:275
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:364
auto capacity() const noexcept -> size_type
Gets the total numbers of elements the internal data structure can hold without resizing.
Definition hash_set.hpp:130
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:227
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:321
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:353
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:207
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:297
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:314
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:307
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:372
virtual auto items() const noexcept -> const base_type &
Returns the underlying base type items.
Definition hash_set.hpp:149
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:170
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:196
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:186
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:377
auto remove(const type_t &item) noexcept -> bool override
Removes a specified item from the hash_set <type_t>.
Definition hash_set.hpp:331
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:219
virtual auto items() noexcept -> base_type &
Returns the underlying base type items.
Definition hash_set.hpp:152
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:341
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:238
auto add(const key_type &item) noexcept -> bool override
Adds an item to the xtd::collections::generic::icollection <type_t>.
Definition hash_set.hpp:161
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:287
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:135
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:145
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:44
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.
@ 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