1
1
using System ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
+ using System . Linq ;
4
5
using Iesi . Collections . Generic ;
5
6
6
7
namespace NHibernate . Util
7
8
{
8
9
/// <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.
11
11
/// </summary>
12
- public class IdentitySet : Set < object >
12
+ public class IdentitySet : ISet < object >
13
13
{
14
14
private IDictionary map ;
15
15
private static readonly object DumpValue = new object ( ) ;
@@ -19,14 +19,34 @@ public IdentitySet()
19
19
map = IdentityMap . Instantiate ( 10 ) ;
20
20
}
21
21
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 )
23
41
{
24
42
object tempObject = map [ o ] ;
25
43
map [ o ] = DumpValue ;
26
44
return tempObject == null ;
27
45
}
28
46
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 )
30
50
{
31
51
bool changed = false ;
32
52
@@ -36,34 +56,17 @@ public override bool AddAll(ICollection<object> c)
36
56
return changed ;
37
57
}
38
58
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 )
50
60
{
51
61
foreach ( object o in c )
52
62
{
53
- if ( ! map . Contains ( o ) )
63
+ if ( ! map . Contains ( o ) )
54
64
return false ;
55
65
}
56
66
return true ;
57
67
}
58
68
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 )
67
70
{
68
71
bool changed = false ;
69
72
foreach ( object o in c )
@@ -74,45 +77,185 @@ public override bool RemoveAll(ICollection<object> c)
74
77
return changed ;
75
78
}
76
79
77
- public override bool RetainAll ( ICollection < object > c )
80
+ public bool RetainAll ( ICollection < object > c )
78
81
{
79
82
//doable if needed
80
83
throw new NotSupportedException ( ) ;
81
84
}
82
85
83
- public override void CopyTo ( object [ ] array , int index )
86
+ protected void NonGenericCopyTo ( Array array , int index )
84
87
{
85
88
map . CopyTo ( array , index ) ;
86
89
}
87
90
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 )
89
164
{
90
165
map . CopyTo ( array , index ) ;
91
166
}
92
167
93
- public override IEnumerator < object > GetEnumerator ( )
168
+ public IEnumerator < object > GetEnumerator ( )
94
169
{
95
170
return new EnumeratorAdapter < object > ( map . GetEnumerator ( ) ) ;
96
171
}
97
172
98
- public override bool IsEmpty
173
+ IEnumerator IEnumerable . GetEnumerator ( )
99
174
{
100
- get { return map . Count == 0 ; }
175
+ return GetEnumerator ( ) ;
101
176
}
102
177
103
- public override int Count
178
+ public int Count
104
179
{
105
180
get { return map . Count ; }
106
181
}
107
182
108
- public override bool IsSynchronized
183
+ public bool IsReadOnly
109
184
{
110
185
get { return false ; }
111
186
}
112
187
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 )
114
193
{
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);
116
214
}
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
+
117
260
}
118
261
}
0 commit comments