4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Collections.ObjectModel;
7 using System.Diagnostics;
8 using System.Runtime.InteropServices;
9 using SiliconStudio.Core.Serialization.Serializers;
11 namespace SiliconStudio.Core.Collections
18 [DebuggerDisplay(
"Count = {Count}")]
22 private const int _defaultCapacity = 4;
23 private static readonly T[] _emptyArray;
28 public T[] Items {
get;
private set; }
35 _emptyArray =
new T[0];
48 int count = is2.Count;
56 Items =
new T[_defaultCapacity];
57 using (IEnumerator<T> enumerator = collection.GetEnumerator())
59 while (enumerator.MoveNext())
61 Add(enumerator.Current);
69 Items =
new T[capacity];
74 get {
return Items.Length; }
77 if (value != Items.Length)
81 var destinationArray =
new T[value];
84 Array.Copy(Items, 0, destinationArray, 0, _size);
86 Items = destinationArray;
96 #region IList<T> Members
98 public void Add(T item)
100 if (_size == Items.Length)
102 EnsureCapacity(_size + 1);
104 Items[_size++] = item;
109 EnsureCapacity(_size + index);
122 for (
int j = 0; j < _size; j++)
124 if (Items[j] == null)
132 for (
int i = 0; i < _size; i++)
134 if (comparer.Equals(Items[i], item))
142 public void CopyTo(T[] array,
int arrayIndex)
144 Array.Copy(Items, 0, array, arrayIndex, _size);
149 return Array.IndexOf(Items, item, 0, _size);
154 if (_size == Items.Length)
156 EnsureCapacity(_size + 1);
160 Array.Copy(Items, index, Items, index + 1, _size - index);
168 int index = IndexOf(item);
182 Array.Copy(Items, index + 1, Items, index, _size - index);
184 Items[_size] =
default(T);
189 return new Enumerator(
this);
192 IEnumerator IEnumerable.GetEnumerator()
194 return new Enumerator(
this);
199 get {
return _size; }
202 public T
this[
int index]
204 get {
return Items[index]; }
207 Items[index] = value;
213 get {
return false; }
224 if (!fastClear && _size > 0)
226 Array.Clear(Items, 0, _size);
233 InsertRange(_size, collection);
238 return new ReadOnlyCollection<T>(
this);
243 return BinarySearch(0, Count, item, null);
248 return BinarySearch(0, Count, item, comparer);
253 return Array.BinarySearch(Items, index,
count, item, comparer);
261 public void CopyTo(
int index, T[] array,
int arrayIndex,
int count)
263 Array.Copy(Items, index, array, arrayIndex,
count);
268 if (Items.Length < min)
270 int num = (Items.Length == 0) ? _defaultCapacity : (Items.Length*2);
281 return (FindIndex(match) != -1);
284 public T
Find(Predicate<T> match)
286 for (
int i = 0; i < _size; i++)
299 for (
int i = 0; i < _size; i++)
311 return FindIndex(0, _size, match);
314 public int FindIndex(
int startIndex, Predicate<T> match)
316 return FindIndex(startIndex, _size - startIndex, match);
321 int num = startIndex +
count;
322 for (
int i = startIndex; i < num; i++)
334 for (
int i = _size - 1; i >= 0; i--)
346 return FindLastIndex(_size - 1, _size, match);
351 return FindLastIndex(startIndex, startIndex + 1, match);
356 int num = startIndex -
count;
357 for (
int i = startIndex; i > num; i--)
369 for (
int i = 0; i < _size; i++)
377 return new Enumerator(
this);
383 Array.Copy(Items, index, list.Items, 0,
count);
390 return Array.IndexOf(Items, item, index, _size - index);
395 return Array.IndexOf(Items, item, index,
count);
403 int count = is2.Count;
406 EnsureCapacity(_size + count);
409 Array.Copy(Items, index, Items, index +
count, _size - index);
413 Array.Copy(Items, 0, Items, index, index);
414 Array.Copy(Items, (index +
count), Items, (index*2), (_size - index));
418 is2.CopyTo(Items, index);
425 using (IEnumerator<T> enumerator = collection.GetEnumerator())
427 while (enumerator.MoveNext())
429 Insert(index++, enumerator.Current);
435 private static bool IsCompatibleObject(
object value)
437 return ((value is T) || ((value == null) && (
default(T) == null)));
446 return LastIndexOf(item, _size - 1, _size);
451 return LastIndexOf(item, index, index + 1);
460 return Array.LastIndexOf(Items, item, index,
count);
466 while ((index < _size) && !match(Items[index]))
474 int num2 = index + 1;
477 while ((num2 < _size) && match(Items[num2]))
483 Items[index++] = Items[num2++];
486 Array.Clear(Items, index, _size - index);
487 int num3 = _size - index;
499 Array.Copy(Items, index +
count, Items, index, _size - index);
501 Array.Clear(Items, _size,
count);
512 Array.Reverse(Items, index,
count);
517 Sort(0, Count, null);
522 Sort(0, Count, comparer);
536 Array.Sort(Items, index,
count, comparer);
541 var destinationArray =
new T[_size];
542 Array.Copy(Items, 0, destinationArray, 0, _size);
543 return destinationArray;
548 var num = (int) (Items.Length*0.9);
557 for (
int i = 0; i < _size; i++)
559 if (!match(Items[i]))
571 #region Nested type: Enumerator
573 [StructLayout(LayoutKind.Sequential)]
574 public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
584 current =
default(T);
594 if (index < list._size)
596 current = list.Items[index];
600 return MoveNextRare();
603 private bool MoveNextRare()
605 index = list._size + 1;
606 current =
default(T);
612 get {
return current; }
615 object IEnumerator.Current
617 get {
return Current; }
620 void IEnumerator.Reset()
623 current =
default(T);
int FindLastIndex(int startIndex, Predicate< T > match)
int LastIndexOf(T item, int index, int count)
void Sort(int index, int count, IComparer< T > comparer)
void AddRange(IEnumerable< T > collection)
DataSerializerGenericMode
Defines what generic parameters to pass to the serializer.
FastList< T > GetRange(int index, int count)
void CopyTo(T[] array, int arrayIndex)
bool Exists(Predicate< T > match)
void EnsureCapacity(int min)
void ForEach(Action< T > action)
Similar to List{T}, with direct access to underlying array.
int BinarySearch(T item, IComparer< T > comparer)
FastList(IEnumerable< T > collection)
bool TrueForAll(Predicate< T > match)
void Clear(bool fastClear)
Clears this list with a fast-clear option.
int RemoveAll(Predicate< T > match)
void Sort(IComparer< T > comparer)
void Reverse(int index, int count)
int IndexOf(T item, int index)
int IndexOf(T item, int index, int count)
ReadOnlyCollection< T > AsReadOnly()
int FindIndex(Predicate< T > match)
int LastIndexOf(T item, int index)
void CopyTo(int index, T[] array, int arrayIndex, int count)
int FindLastIndex(Predicate< T > match)
void Insert(int index, T item)
void InsertRange(int index, IEnumerable< T > collection)
int FindLastIndex(int startIndex, int count, Predicate< T > match)
int FindIndex(int startIndex, int count, Predicate< T > match)
FastList< T > FindAll(Predicate< T > match)
T Find(Predicate< T > match)
Enumerator GetEnumerator()
T FindLast(Predicate< T > match)
int BinarySearch(int index, int count, T item, IComparer< T > comparer)
void RemoveRange(int index, int count)
void IncreaseCapacity(int index)
int FindIndex(int startIndex, Predicate< T > match)