-
Notifications
You must be signed in to change notification settings - Fork 291
/
OrderedHashSet.cs
163 lines (142 loc) · 4.37 KB
/
OrderedHashSet.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Advanced.Algorithms.DataStructures.Foundation;
/// <summary>
/// A sorted HashSet implementation using balanced binary search tree. IEnumerable will enumerate in sorted order.
/// This may be better than regular HashSet implementation which can give o(K) in worst case (but O(1) amortized when
/// collisions K is avoided).
/// </summary>
/// <typeparam name="T">The value datatype.</typeparam>
public class OrderedHashSet<T> : IEnumerable<T> where T : IComparable
{
//use red-black tree as our balanced BST since it gives good performance for both deletion/insertion
private readonly RedBlackTree<T> binarySearchTree;
public OrderedHashSet()
{
binarySearchTree = new RedBlackTree<T>();
}
/// <summary>
/// Initialize the sorted hashset with given sorted key collection.
/// Time complexity: log(n).
/// </summary>
public OrderedHashSet(IEnumerable<T> sortedKeys)
{
binarySearchTree = new RedBlackTree<T>(sortedKeys);
}
public int Count => binarySearchTree.Count;
/// <summary>
/// Time complexity: O(log(n))
/// </summary>
public T this[int index] => ElementAt(index);
//Implementation for the GetEnumerator method.
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return binarySearchTree.GetEnumerator();
}
/// <summary>
/// Does this hash table contains the given value.
/// Time complexity: O(log(n)).
/// </summary>
/// <param name="value">The value to check.</param>
/// <returns>True if this hashset contains the given value.</returns>
public bool Contains(T value)
{
return binarySearchTree.HasItem(value);
}
/// <summary>
/// Add a new key.
/// Time complexity: O(log(n)).
/// Returns the position (index) of the key in sorted order of this OrderedHashSet.
/// </summary>
public int Add(T key)
{
return binarySearchTree.Insert(key);
}
/// <summary>
/// Time complexity: O(log(n))
/// </summary>
public T ElementAt(int index)
{
return binarySearchTree.ElementAt(index);
}
/// <summary>
/// Time complexity: O(log(n))
/// </summary>
public int IndexOf(T key)
{
return binarySearchTree.IndexOf(key);
}
/// <summary>
/// Remove the given key if present.
/// Time complexity: O(log(n)).
/// Returns the position (index) of the removed key if removed. Otherwise returns -1.
/// </summary>
public int Remove(T key)
{
return binarySearchTree.Delete(key);
}
/// <summary>
/// Remove the element at given index.
/// Time complexity: O(log(n)).
/// </summary>
public T RemoveAt(int index)
{
return binarySearchTree.RemoveAt(index);
}
/// <summary>
/// Return the next higher value after given value in this hashset.
/// Time complexity: O(log(n)).
/// </summary>
/// <returns>Null if the given value does'nt exist or next value does'nt exist.</returns>
public T NextHigher(T value)
{
return binarySearchTree.NextHigher(value);
}
/// <summary>
/// Return the next lower value before given value in this HashSet.
/// Time complexity: O(log(n)).
/// </summary>
/// <returns>Null if the given value does'nt exist or previous value does'nt exist.</returns>
public T NextLower(T value)
{
return binarySearchTree.NextLower(value);
}
/// <summary>
/// Time complexity: O(log(n)).
/// </summary>
public T Max()
{
return binarySearchTree.Max();
}
/// <summary>
/// Time complexity: O(log(n)).
/// </summary>
public T Min()
{
return binarySearchTree.Min();
}
/// <summary>
/// Clear the hashtable.
/// Time complexity: O(1).
/// </summary>
internal void Clear()
{
binarySearchTree.Clear();
}
/// <summary>
/// Descending enumerable.
/// </summary>
public IEnumerable<T> AsEnumerableDesc()
{
return GetEnumeratorDesc().AsEnumerable();
}
public IEnumerator<T> GetEnumeratorDesc()
{
return binarySearchTree.GetEnumeratorDesc();
}
}