5 // Jb Evain <jbevain@novell.com>
7 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Collections.Generic;
33 using System.Runtime.Serialization;
34 using System.Runtime.InteropServices;
35 using System.Security;
36 using System.Security.Permissions;
37 using System.Diagnostics;
39 // SortedSet is basically implemented as a reduction of SortedDictionary<K, V>
43 namespace System.Collections.Generic {
46 [DebuggerDisplay ("Count={Count}")]
47 [DebuggerTypeProxy (typeof (CollectionDebuggerView))]
48 public class SortedSet<T> : ISet<T>, ICollection, ISerializable, IDeserializationCallback
50 class Node : RBTree.Node {
59 public override void SwapValue (RBTree.Node other)
68 class NodeHelper : RBTree.INodeHelper<T> {
70 static NodeHelper Default = new NodeHelper (Comparer<T>.Default);
72 public IComparer<T> comparer;
74 public int Compare (T item, RBTree.Node node)
76 return comparer.Compare (item, ((Node) node).item);
79 public RBTree.Node CreateNode (T item)
81 return new Node (item);
84 NodeHelper (IComparer<T> comparer)
86 this.comparer = comparer;
89 public static NodeHelper GetHelper (IComparer<T> comparer)
91 if (comparer == null || comparer == Comparer<T>.Default)
94 return new NodeHelper (comparer);
100 SerializationInfo si;
103 : this (Comparer<T>.Default)
107 public SortedSet (IEnumerable<T> collection)
108 : this (collection, Comparer<T>.Default)
112 public SortedSet (IEnumerable<T> collection, IComparer<T> comparer)
115 if (collection == null)
116 throw new ArgumentNullException ("collection");
118 foreach (var item in collection)
122 public SortedSet (IComparer<T> comparer)
124 this.helper = NodeHelper.GetHelper (comparer);
125 this.tree = new RBTree (this.helper);
128 protected SortedSet (SerializationInfo info, StreamingContext context)
133 public IComparer<T> Comparer {
134 get { return helper.comparer; }
138 get { return tree.Count; }
146 return ((Node) tree [tree.Count - 1]).item;
155 return ((Node) tree [0]).item;
159 public bool Add (T item)
161 return TryAdd (item);
164 internal virtual bool TryAdd (T item)
166 var node = new Node (item);
167 return tree.Intern (item, node) == node;
170 public virtual void Clear ()
175 public virtual bool Contains (T item)
177 return tree.Lookup (item) != null;
180 public void CopyTo (T [] array)
182 CopyTo (array, 0, Count);
185 public void CopyTo (T [] array, int index)
187 CopyTo (array, index, Count);
190 public void CopyTo (T [] array, int index, int count)
193 throw new ArgumentNullException ("array");
195 throw new ArgumentOutOfRangeException ("index");
196 if (index > array.Length)
197 throw new ArgumentException ("index larger than largest valid index of array");
198 if (array.Length - index < count)
199 throw new ArgumentException ("destination array cannot hold the requested elements");
201 foreach (Node node in tree) {
205 array [index++] = node.item;
209 public bool Remove (T item)
211 return TryRemove (item);
214 internal virtual bool TryRemove (T item)
216 return tree.Remove (item) != null;
219 public int RemoveWhere (Predicate<T> match)
221 var array = ToArray ();
224 foreach (var item in array) {
235 public IEnumerable<T> Reverse ()
237 for (int i = tree.Count - 1; i >= 0; i--)
238 yield return ((Node) tree [i]).item;
243 var array = new T [this.Count];
248 public Enumerator GetEnumerator ()
250 return new Enumerator (this);
253 IEnumerator<T> IEnumerable<T>.GetEnumerator ()
255 return new Enumerator (this);
258 IEnumerator IEnumerable.GetEnumerator ()
260 return new Enumerator (this);
263 public static IEqualityComparer<SortedSet<T>> CreateSetComparer ()
265 return CreateSetComparer (EqualityComparer<T>.Default);
269 public static IEqualityComparer<SortedSet<T>> CreateSetComparer (IEqualityComparer<T> memberEqualityComparer)
271 throw new NotImplementedException ();
275 protected virtual void GetObjectData (SerializationInfo info, StreamingContext context)
277 throw new NotImplementedException ();
280 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
282 GetObjectData (info, context);
286 protected virtual void OnDeserialization (object sender)
291 throw new NotImplementedException ();
294 void IDeserializationCallback.OnDeserialization (object sender)
296 OnDeserialization (sender);
300 public void ExceptWith (IEnumerable<T> other)
302 throw new NotImplementedException ();
306 public virtual SortedSet<T> GetViewBetween (T lowerValue, T upperValue)
308 throw new NotImplementedException ();
312 public virtual void IntersectWith (IEnumerable<T> other)
314 throw new NotImplementedException ();
318 public bool IsProperSubsetOf (IEnumerable<T> other)
320 throw new NotImplementedException ();
324 public bool IsProperSupersetOf (IEnumerable<T> other)
326 throw new NotImplementedException ();
330 public bool IsSubsetOf (IEnumerable<T> other)
332 throw new NotImplementedException ();
336 public bool IsSupersetOf (IEnumerable<T> other)
338 throw new NotImplementedException ();
342 public bool Overlaps (IEnumerable<T> other)
344 throw new NotImplementedException ();
348 public bool SetEquals (IEnumerable<T> other)
350 throw new NotImplementedException ();
354 public void SymmetricExceptWith (IEnumerable<T> other)
356 throw new NotImplementedException ();
360 public void UnionWith (IEnumerable<T> other)
362 throw new NotImplementedException ();
365 void ICollection<T>.Add (T item)
370 void ICollection<T>.CopyTo (T [] array, int index)
372 CopyTo (array, index, Count);
375 bool ICollection<T>.IsReadOnly {
376 get { return false; }
379 void ICollection.CopyTo (Array array, int index)
384 throw new ArgumentNullException ("array");
385 if (index < 0 || array.Length <= index)
386 throw new ArgumentOutOfRangeException ("index");
387 if (array.Length - index < Count)
388 throw new ArgumentException ();
390 foreach (Node node in tree)
391 array.SetValue (node.item, index++);
394 bool ICollection.IsSynchronized {
395 get { return false; }
398 // TODO:Is this correct? If this is wrong,please fix.
399 object ICollection.SyncRoot {
404 public struct Enumerator : IEnumerator<T>, IDisposable {
406 RBTree.NodeEnumerator host;
410 internal Enumerator (SortedSet<T> set)
412 host = set.tree.GetEnumerator ();
413 current = default (T);
417 get { return current; }
420 object IEnumerator.Current {
422 host.check_current ();
423 return ((Node) host.Current).item;
427 public bool MoveNext ()
429 if (!host.MoveNext ())
432 current = ((Node) host.Current).item;
436 public void Dispose ()
441 void IEnumerator.Reset ()