/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #if NET35 namespace System.Collections.Generic { [Serializable] public class SortedSet : ISet, ICollection { private readonly SortedList _list; public SortedSet() : this(Comparer.Default) { } public SortedSet(IComparer comparer) { _list = new SortedList(comparer); } public T Min { get { return (_list.Count) >= 1 ? _list.Keys[0] : default(T); } } public T Max { get { return (_list.Count) >= 1 ? _list.Keys[_list.Count - 1] : default(T); } } /// /// Removes all items from the . /// /// The is read-only. /// public void Clear() { _list.Clear(); } public void CopyTo(T[] array, int arrayIndex) { _list.Keys.CopyTo(array, arrayIndex); } public bool Remove(T item) { return _list.Remove(item); } public bool Contains(T value) { return _list.ContainsKey(value); } public bool Add(T item) { if (!_list.ContainsKey(item)) { _list.Add(item, 0); return true; } return false; } public void UnionWith(IEnumerable other) { foreach (var obj in other) Add(obj); } public IEnumerator GetEnumerator() { return _list.Keys.GetEnumerator(); } public IComparer Comparer { get { return _list.Comparer; } } public int Count { get { return _list.Count; } } #region Explicit Interface Implementations void ICollection.Add(T item) { Add(item); } void ICollection.CopyTo(Array array, int index) { CopyTo((T[]) array, index); } bool ICollection.IsReadOnly { get { return false; } } bool ICollection.IsSynchronized { get { return false; } } object ICollection.SyncRoot { get { throw new NotSupportedException(); } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } int ICollection.Count { get { return Count; } } #endregion #region ISet Implementation void ISet.ExceptWith(IEnumerable other) { foreach(var obj in other) { _list.Remove(obj); } } void ISet.IntersectWith(IEnumerable other) { throw new NotImplementedException(); } bool ISet.IsProperSubsetOf(IEnumerable other) { throw new NotImplementedException(); } bool ISet.IsProperSupersetOf(IEnumerable other) { throw new NotImplementedException(); } bool ISet.IsSubsetOf(IEnumerable other) { throw new NotImplementedException(); } bool ISet.IsSupersetOf(IEnumerable other) { throw new NotImplementedException(); } bool ISet.Overlaps(IEnumerable other) { throw new NotImplementedException(); } bool ISet.SetEquals(IEnumerable other) { throw new NotImplementedException(); } void ISet.SymmetricExceptWith(IEnumerable other) { throw new NotImplementedException(); } #endregion } } #endif