Skip to content

Commit 5eccf22

Browse files
committed
IdentitySet.cs: Directly implement the ISet<> interface, instead of inheriting from Set<object> which has no counterpart in the BCL.
1 parent 6c69d5a commit 5eccf22

File tree

1 file changed

+178
-35
lines changed

1 file changed

+178
-35
lines changed

src/NHibernate/Util/IdentitySet.cs

Lines changed: 178 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using Iesi.Collections.Generic;
56

67
namespace NHibernate.Util
78
{
89
/// <summary>
9-
/// Set implementation that use == instead of equals() as its comparison mechanism
10-
/// that base its implementation of IdentityMap
10+
/// Set implementation that use reference equals instead of Equals() as its comparison mechanism.
1111
/// </summary>
12-
public class IdentitySet : Set<object>
12+
public class IdentitySet : ISet<object>
1313
{
1414
private IDictionary map;
1515
private static readonly object DumpValue = new object();
@@ -19,14 +19,34 @@ public IdentitySet()
1919
map = IdentityMap.Instantiate(10);
2020
}
2121

22-
public override bool Add(object o)
22+
public IdentitySet(IEnumerable<object> members)
23+
{
24+
map = IdentityMap.Instantiate(10);
25+
foreach (var member in members)
26+
Add(member);
27+
}
28+
29+
30+
#region Implementation of ICollection<object>
31+
32+
void ICollection<object>.Add(object item)
33+
{
34+
Add(item);
35+
}
36+
37+
#endregion
38+
39+
40+
public bool Add(object o)
2341
{
2442
object tempObject = map[o];
2543
map[o] = DumpValue;
2644
return tempObject == null;
2745
}
2846

29-
public override bool AddAll(ICollection<object> c)
47+
48+
#if !NET_4_0 // Only in Iesi's ISet<>.
49+
public bool AddAll(ICollection<object> c)
3050
{
3151
bool changed = false;
3252

@@ -36,34 +56,17 @@ public override bool AddAll(ICollection<object> c)
3656
return changed;
3757
}
3858

39-
public override void Clear()
40-
{
41-
map.Clear();
42-
}
43-
44-
public override bool Contains(object o)
45-
{
46-
return map[o] == DumpValue;
47-
}
48-
49-
public override bool ContainsAll(ICollection<object> c)
59+
public bool ContainsAll(ICollection<object> c)
5060
{
5161
foreach (object o in c)
5262
{
53-
if(!map.Contains(o))
63+
if (!map.Contains(o))
5464
return false;
5565
}
5666
return true;
5767
}
5868

59-
public override bool Remove(object o)
60-
{
61-
object tempObject = map[o];
62-
map.Remove(o);
63-
return tempObject == DumpValue;
64-
}
65-
66-
public override bool RemoveAll(ICollection<object> c)
69+
public bool RemoveAll(ICollection<object> c)
6770
{
6871
bool changed = false;
6972
foreach (object o in c)
@@ -74,45 +77,185 @@ public override bool RemoveAll(ICollection<object> c)
7477
return changed;
7578
}
7679

77-
public override bool RetainAll(ICollection<object> c)
80+
public bool RetainAll(ICollection<object> c)
7881
{
7982
//doable if needed
8083
throw new NotSupportedException();
8184
}
8285

83-
public override void CopyTo(object[] array, int index)
86+
protected void NonGenericCopyTo(Array array, int index)
8487
{
8588
map.CopyTo(array, index);
8689
}
8790

88-
protected override void NonGenericCopyTo(Array array, int index)
91+
public bool IsEmpty
92+
{
93+
get { return map.Count == 0; }
94+
}
95+
96+
public bool IsSynchronized
97+
{
98+
get { return false; }
99+
}
100+
101+
public object SyncRoot
102+
{
103+
get { return this; }
104+
}
105+
106+
#region Implementation of ICloneable
107+
108+
public object Clone()
109+
{
110+
return new IdentitySet(this);
111+
}
112+
113+
#endregion
114+
115+
#region Implementation of ISet<object>
116+
117+
public ISet<object> Union(ISet<object> a)
118+
{
119+
return new IdentitySet(this.Concat(a));
120+
}
121+
122+
public ISet<object> Intersect(ISet<object> a)
123+
{
124+
// Be careful to use the Contains() implementation of the IdentitySet,
125+
// not the one from the other set.
126+
var elems = a.Where(e => Contains(a));
127+
return new IdentitySet(elems);
128+
}
129+
130+
public ISet<object> Minus(ISet<object> a)
131+
{
132+
var set = new IdentitySet(this);
133+
set.RemoveAll(a);
134+
return set;
135+
}
136+
137+
public ISet<object> ExclusiveOr(ISet<object> a)
138+
{
139+
return Union(a).Minus(Intersect(a));
140+
}
141+
142+
#endregion
143+
144+
#endif
145+
146+
public void Clear()
147+
{
148+
map.Clear();
149+
}
150+
151+
public bool Contains(object o)
152+
{
153+
return map[o] == DumpValue;
154+
}
155+
156+
public bool Remove(object o)
157+
{
158+
object tempObject = map[o];
159+
map.Remove(o);
160+
return tempObject == DumpValue;
161+
}
162+
163+
public void CopyTo(object[] array, int index)
89164
{
90165
map.CopyTo(array, index);
91166
}
92167

93-
public override IEnumerator<object> GetEnumerator()
168+
public IEnumerator<object> GetEnumerator()
94169
{
95170
return new EnumeratorAdapter<object>(map.GetEnumerator());
96171
}
97172

98-
public override bool IsEmpty
173+
IEnumerator IEnumerable.GetEnumerator()
99174
{
100-
get { return map.Count == 0; }
175+
return GetEnumerator();
101176
}
102177

103-
public override int Count
178+
public int Count
104179
{
105180
get { return map.Count; }
106181
}
107182

108-
public override bool IsSynchronized
183+
public bool IsReadOnly
109184
{
110185
get { return false; }
111186
}
112187

113-
public override object SyncRoot
188+
#if NET_4_0
189+
190+
#region Implementation of ISet<object>
191+
192+
public void UnionWith(IEnumerable<object> other)
114193
{
115-
get { return this; }
194+
throw new NotImplementedException();
195+
196+
//foreach (var o in other)
197+
// Add(o);
198+
}
199+
200+
public void IntersectWith(IEnumerable<object> other)
201+
{
202+
throw new NotImplementedException();
203+
204+
// Potential crude implementation.
205+
//var otherSet = new HashSet<object>(other, new IdentityEqualityComparer());
206+
//var ours = map.Keys.Cast<object>().ToList();
207+
208+
//foreach (var key in ours)
209+
// if (!otherSet.Contains(key))
210+
// map.Remove(key);
211+
212+
//foreach (var obj in otherSet)
213+
// Add(obj);
116214
}
215+
216+
public void ExceptWith(IEnumerable<object> other)
217+
{
218+
throw new NotImplementedException();
219+
}
220+
221+
public void SymmetricExceptWith(IEnumerable<object> other)
222+
{
223+
throw new NotImplementedException();
224+
}
225+
226+
public bool IsSubsetOf(IEnumerable<object> other)
227+
{
228+
throw new NotImplementedException();
229+
}
230+
231+
public bool IsSupersetOf(IEnumerable<object> other)
232+
{
233+
throw new NotImplementedException();
234+
}
235+
236+
public bool IsProperSupersetOf(IEnumerable<object> other)
237+
{
238+
throw new NotImplementedException();
239+
}
240+
241+
public bool IsProperSubsetOf(IEnumerable<object> other)
242+
{
243+
throw new NotImplementedException();
244+
}
245+
246+
public bool Overlaps(IEnumerable<object> other)
247+
{
248+
throw new NotImplementedException();
249+
}
250+
251+
public bool SetEquals(IEnumerable<object> other)
252+
{
253+
throw new NotImplementedException();
254+
}
255+
256+
#endregion
257+
258+
#endif
259+
117260
}
118261
}

0 commit comments

Comments
 (0)