1
2
3
4
5
6 using System;
7 using System.Concepts;
8 using System.IO;
9
10 namespace System.Collections
11 {
12 public class HashSet<T, H = Hasher<T>, C = EqualTo<T>>
13 where T is Semiregular and HashFunction<H, T> and C is Relation and C.Domain is T
14 {
15 public typedef T ValueType;
16 public typedef T KeyType;
17 public typedef H HashFun;
18 public typedef C Compare;
19 public typedef TableType.ConstIterator ConstIterator;
20 public typedef TableType.Iterator Iterator;
21 private typedef HashSet<ValueType, HashFun, Compare> Self;
22 private typedef Hashtable<KeyType, ValueType, Identity<ValueType>, HashFun, Compare> TableType;
23
24 public inline nothrow Iterator Begin()
25 {
26 return table.Begin();
27 }
28 public inline nothrow ConstIterator Begin() const
29 {
30 return table.CBegin();
31 }
32 public inline nothrow ConstIterator CBegin() const
33 {
34 return table.CBegin();
35 }
36 public inline nothrow Iterator End()
37 {
38 return table.End();
39 }
40 public inline nothrow ConstIterator End() const
41 {
42 return table.CEnd();
43 }
44 public inline nothrow ConstIterator CEnd() const
45 {
46 return table.CEnd();
47 }
48 public inline nothrow long Count() const
49 {
50 return table.Count();
51 }
52 public inline nothrow bool IsEmpty() const
53 {
54 return table.IsEmpty();
55 }
56 public inline nothrow void Clear()
57 {
58 table.Clear();
59 }
60 public inline nothrow Iterator Find(const KeyType& key)
61 {
62 return table.Find(key);
63 }
64 public inline nothrow ConstIterator Find(const KeyType& key) const
65 {
66 return table.CFind(key);
67 }
68 public inline nothrow ConstIterator CFind(const KeyType& key) const
69 {
70 return table.CFind(key);
71 }
72 public inline Pair<Iterator, bool> Insert(const ValueType& value)
73 {
74 return table.Insert(value);
75 }
76 public inline nothrow void Remove(const KeyType& key)
77 {
78 table.Remove(key);
79 }
80 public inline nothrow void Remove(Iterator pos)
81 {
82 table.Remove(pos);
83 }
84 private TableType table;
85 }
86
87 public nothrow bool operator==<T, H, C>(const HashSet<T, H, C>& left, const HashSet<T, H, C>& right) where T is Semiregular and HashFunction<H, T> and C is Relation and C.Domain is T
88 {
89 if (left.Count() != right.Count()) return false;
90 for (const T& value : left)
91 {
92 if (right.CFind(value) == right.CEnd()) return false;
93 }
94 return true;
95 }
96
97 [system_default="true"]
98 public TextWriter& operator<<<T, H, C>(TextWriter& writer, const HashSet<T, H, C>& hashSet)
99 {
100 writer << "{";
101 bool first = true;
102 for (const T& element : hashSet)
103 {
104 if (first)
105 {
106 first = false;
107 }
108 else
109 {
110 writer << ", ";
111 }
112 writer << element;
113 }
114 writer << "}";
115 return writer;
116 }
117
118 }