4 using System.Collections.Generic;
5 using System.Collections.ObjectModel;
8 namespace SiliconStudio.Presentation.Collections
18 public class SortedObservableCollection<T> : ObservableCollection<T>, IObservableCollection<T>, IReadOnlyObservableCollection<T>
32 DefaultCompareFunc = comparer.Compare;
36 if (DefaultCompareFunc == null && typeof(T).GetInterfaces().Contains(typeof(
IComparable<T>)))
38 if (typeof(T).IsValueType)
40 DefaultCompareFunc = (item1, item2) => ((
IComparable<T>)item1).CompareTo(item2);
44 DefaultCompareFunc = (item1, item2) =>
47 if ((
object)item1 == null && (object)item2 == null)
57 if (DefaultCompareFunc == null && typeof(T).GetInterfaces().Contains(typeof(
IComparable)))
59 if (typeof(T).IsValueType)
61 DefaultCompareFunc = (item1, item2) => ((
IComparable)item1).CompareTo(item2);
65 DefaultCompareFunc = (item1, item2) =>
68 if ((
object)item1 == null && (object)item2 == null)
72 return (
object)item1 != null ? ((
IComparable)item1).CompareTo(item2) : -((
IComparable)item2).CompareTo(item1);
78 if (DefaultCompareFunc == null)
80 throw new ArgumentException(
"The type of this collection does not implement any IComparable interface and no IComparer has been provided");
91 return GetIndex(item,
false);
101 public int BinarySearch<TSearch>(TSearch key, Func<T, TSearch, int> compareFunc)
103 if (compareFunc == null)
104 throw new ArgumentNullException(
"compareFunc");
106 return GetIndex(key,
false, compareFunc);
112 return string.Format(
"{{SortedObservableCollection}} Count = {0}", Count);
115 protected int GetIndex(T item,
bool returnInsertIndex)
117 if (DefaultCompareFunc == null)
throw new InvalidOperationException(
"No comparison available or provided for items of this collection.");
118 return GetIndex(item, returnInsertIndex, DefaultCompareFunc);
121 protected int GetIndex<TSearch>(TSearch key,
bool returnInsertIndex, Func<T, TSearch, int> compareFunc)
124 int imax = Count - 1;
127 int imid = (imin + imax) / 2;
129 int comp = compareFunc(
this[imid], key);
138 return returnInsertIndex ? imin : -1;
144 int i = GetIndex(item,
true);
145 base.InsertItem(i, item);
149 protected override void MoveItem(
int oldIndex,
int newIndex)
151 throw new InvalidOperationException(
"Cannot call MoveItem on a SortedObservableCollection");
156 base.MoveItem(oldIndex, newIndex);
160 protected override void SetItem(
int index, T item)
162 throw new InvalidOperationException(
"Cannot call SetItem on a SortedObservableCollection");
override void InsertItem(int index, T item)
override void SetItem(int index, T item)
override void MoveItem(int oldIndex, int newIndex)
void ObservableCollectionMoveItem(int oldIndex, int newIndex)
SortedObservableCollection(IComparer< T > comparer=null)
Public constructor. A comparer can be provided to compare items. If null, the default comparer will b...
int GetIndex(T item, bool returnInsertIndex)
override string ToString()
readonly Func< T, T, int > DefaultCompareFunc
int BinarySearch(T item)
Seach the index of an item.