1 // =================================
  2 // Copyright (c) 2024 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.Concepts;
  8 using System.IO;
  9 
 10 namespace System.Collections
 11 {
 12     public class HashSet<TH = Hasher<T>C = EqualTo<T>>
 13         where T is Semiregular and HashFunction<HT> 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<ValueTypeHashFunCompare> Self;
 22         private typedef Hashtable<KeyTypeValueTypeIdentity<ValueType>HashFunCompare> TableType;
 23 
 24         public inline Iterator Begin()
 25         {
 26             return table.Begin();
 27         }
 28         public inline ConstIterator Begin() const
 29         {
 30             return table.CBegin();
 31         }
 32         public inline ConstIterator CBegin() const
 33         {
 34             return table.CBegin();
 35         }
 36         public inline Iterator End()
 37         {
 38             return table.End();
 39         }
 40         public inline ConstIterator End() const
 41         {
 42             return table.CEnd();
 43         }
 44         public inline ConstIterator CEnd() const
 45         {
 46             return table.CEnd();
 47         }
 48         public inline long Count() const
 49         {
 50             return table.Count();
 51         }
 52         public inline bool IsEmpty() const
 53         {
 54             return table.IsEmpty();
 55         }
 56         public inline void Clear()
 57         {
 58             table.Clear();
 59         }
 60         public inline Iterator Find(const KeyType& key)
 61         {
 62             return table.Find(key);
 63         }
 64         public inline ConstIterator Find(const KeyType& key) const
 65         {
 66             return table.CFind(key);
 67         }
 68         public inline ConstIterator CFind(const KeyType& key) const
 69         {
 70             return table.CFind(key);
 71         }
 72         public inline Pair<Iteratorbool> Insert(const ValueType& value)
 73         {
 74             return table.Insert(value);
 75         }
 76         public inline void Remove(const KeyType& key)
 77         {
 78             table.Remove(key);
 79         }
 80         public inline void Remove(Iterator pos)
 81         {
 82             table.Remove(pos);
 83         }
 84         private TableType table;
 85     }
 86 
 87     public bool operator==<THC>(const HashSet<THC>& leftconst HashSet<THC>& right) where T is Semiregular and HashFunction<HT> 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]
 98     public TextWriter& operator<<<THC>(TextWriter& writerconst HashSet<THC>& hashSet)
 99     {
100         if (writer.Error()) return writer;
101         writer << "{";
102         bool first = true;
103         for (const T& element : hashSet)
104         {
105             if (first)
106             {
107                 first = false;
108             }
109             else
110             {
111                 writer << ", ";
112             }
113             writer << element;
114         }
115         writer << "}";
116         return writer;
117     }
118