summaryrefslogtreecommitdiff
path: root/mcs/class/corlib/System.Collections.Generic/List.cs
diff options
context:
space:
mode:
Diffstat (limited to 'mcs/class/corlib/System.Collections.Generic/List.cs')
-rw-r--r--mcs/class/corlib/System.Collections.Generic/List.cs87
1 files changed, 53 insertions, 34 deletions
diff --git a/mcs/class/corlib/System.Collections.Generic/List.cs b/mcs/class/corlib/System.Collections.Generic/List.cs
index 17056a1b21..840984f04f 100644
--- a/mcs/class/corlib/System.Collections.Generic/List.cs
+++ b/mcs/class/corlib/System.Collections.Generic/List.cs
@@ -69,7 +69,7 @@ namespace System.Collections.Generic {
AddEnumerable (collection);
} else {
_size = c.Count;
- _items = new T [Math.Max (_size, DefaultCapacity)];
+ _items = new T [_size];
c.CopyTo (_items, 0);
}
}
@@ -116,6 +116,18 @@ namespace System.Collections.Generic {
throw new ArgumentException ("index and count exceed length of list");
}
+ void CheckRangeOutOfRange (int idx, int count)
+ {
+ if (idx < 0)
+ throw new ArgumentOutOfRangeException ("index");
+
+ if (count < 0)
+ throw new ArgumentOutOfRangeException ("count");
+
+ if ((uint) idx + (uint) count > (uint) _size)
+ throw new ArgumentOutOfRangeException ("index and count exceed length of list");
+ }
+
void AddCollection (ICollection <T> collection)
{
int collectionCount = collection.Count;
@@ -212,14 +224,27 @@ namespace System.Collections.Generic {
public bool Exists (Predicate <T> match)
{
CheckMatch(match);
- return GetIndex(0, _size, match) != -1;
+
+ for (int i = 0; i < _size; i++) {
+ var item = _items [i];
+ if (match (item))
+ return true;
+ }
+
+ return false;
}
public T Find (Predicate <T> match)
{
CheckMatch(match);
- int i = GetIndex(0, _size, match);
- return (i != -1) ? _items [i] : default (T);
+
+ for (int i = 0; i < _size; i++) {
+ var item = _items [i];
+ if (match (item))
+ return item;
+ }
+
+ return default (T);
}
static void CheckMatch (Predicate <T> match)
@@ -296,68 +321,56 @@ namespace System.Collections.Generic {
public int FindIndex (Predicate <T> match)
{
CheckMatch (match);
- return GetIndex (0, _size, match);
+ return Array.GetIndex (_items, 0, _size, match);
}
public int FindIndex (int startIndex, Predicate <T> match)
{
CheckMatch (match);
- CheckIndex (startIndex);
- return GetIndex (startIndex, _size - startIndex, match);
+ CheckStartIndex (startIndex);
+ return Array.GetIndex (_items, startIndex, _size - startIndex, match);
}
public int FindIndex (int startIndex, int count, Predicate <T> match)
{
CheckMatch (match);
- CheckRange (startIndex, count);
- return GetIndex (startIndex, count, match);
- }
- int GetIndex (int startIndex, int count, Predicate <T> match)
- {
- int end = startIndex + count;
- for (int i = startIndex; i < end; i ++)
- if (match (_items [i]))
- return i;
-
- return -1;
+ CheckRangeOutOfRange (startIndex, count);
+ return Array.GetIndex (_items, startIndex, count, match);
}
public T FindLast (Predicate <T> match)
{
CheckMatch (match);
- int i = GetLastIndex (0, _size, match);
+ int i = Array.GetLastIndex (_items, 0, _size, match);
return i == -1 ? default (T) : this [i];
}
public int FindLastIndex (Predicate <T> match)
{
CheckMatch (match);
- return GetLastIndex (0, _size, match);
+ return Array.GetLastIndex (_items, 0, _size, match);
}
public int FindLastIndex (int startIndex, Predicate <T> match)
{
CheckMatch (match);
- CheckIndex (startIndex);
- return GetLastIndex (0, startIndex + 1, match);
+ CheckStartIndex (startIndex);
+ return Array.GetLastIndex (_items, 0, startIndex + 1, match);
}
public int FindLastIndex (int startIndex, int count, Predicate <T> match)
{
CheckMatch (match);
- int start = startIndex - count + 1;
- CheckRange (start, count);
- return GetLastIndex (start, count, match);
- }
+ CheckStartIndex (startIndex);
+
+ if (count < 0)
+ throw new ArgumentOutOfRangeException ("count");
- int GetLastIndex (int startIndex, int count, Predicate <T> match)
- {
- // unlike FindLastIndex, takes regular params for search range
- for (int i = startIndex + count; i != startIndex;)
- if (match (_items [--i]))
- return i;
- return -1;
+ if (startIndex - count + 1 < 0)
+ throw new ArgumentOutOfRangeException ("count must refer to a location within the collection");
+
+ return Array.GetLastIndex (_items, startIndex - count + 1, count, match);
}
-
+
public void ForEach (Action <T> action)
{
if (action == null)
@@ -423,6 +436,12 @@ namespace System.Collections.Generic {
if (index < 0 || (uint) index > (uint) _size)
throw new ArgumentOutOfRangeException ("index");
}
+
+ void CheckStartIndex (int index)
+ {
+ if (index < 0 || (uint) index > (uint) _size)
+ throw new ArgumentOutOfRangeException ("startIndex");
+ }
public void Insert (int index, T item)
{