[Mono-devel-list] [Patch] System.Collections.Generic List improvements

Ben Maurer bmaurer at ximian.com
Mon Jun 20 15:27:25 EDT 2005


On Mon, 2005-06-20 at 01:20 -0600, David Waite wrote:
> Attached are 'substantial' changes in the List implementation,
> including a 9-fold increase in tests (written while verifying behavior
> in the 2005 beta 2 implementation of List<T>)
> 
> 2005-06-20  David Waite  <mass at akuma.org>
> 
>     * List.cs : substantial changes and optimizations
>     (AddCollection, AddEnumerable): new internal specializations of AddRange
>     (AsReadOnly): returns specific IList<T> to match ms.net 2.0b2 api.
>     (Clear): reset size to zero on clear
>     (ConvertAll): catch null converter, use Add to prevent OutOfBounds
>     exception
>     (FindAll, FindIndex, FindLast, FindLastIndex, RemoveAll, TrueForAll):
>     check for null match
>     (FindLastIndex): correct index parameters based on ms.net 2005b2 behavior
>     (ForEach): catch null action
>     (CheckIndex): new internal function similar to CheckRange for functions
>     which only provide a starting index
>     (InsertCollection, InsertEnumerable): new internal specializations of
>     InsertRange
>     (ReadOnlyList): removed, ReadOnlyCollection in
>     System.Collections.ObjectModel is used instead now
>     * ListTest.cs: Substantial new tests


> +               static T[] EmptyArray = new T[0]; 

Can be `readonly'. Space between T and [].

This was much smarter than my version. Thanks!


> +                       if (c != null)
> +                       {

Coding style ;-). Also, since you have an else branch, make it if (c ==
null), its a bit easier to understand.


>                         if (size == data.Length)
> -                               Capacity = Math.Max (Capacity * 2, DefaultCapacity);
> -                       
> +                               Capacity = GrowSize();

I'd make GrowSize set Capacity. Since this path is less likely, it'd be
nice for it to be shorter.


> +                       if (idx + count > size)
> +                               throw new ArgumentException("index and count exceed length of list");

Integer overflow ;-). Read sp's blog.


> +                       foreach (T t in enumerable)
> +                       {

Style again


> +                       if (c != null)
> +                       { 
> +                               AddCollection(c);
> +                       }
> +                       else
> +                       {
> +                               AddEnumerable(collection);
> +                       }

Style ;-)


> +                       return new ReadOnlyCollection<T>(this);

Space between <T> and (


>                         get {
> -                               if ((uint) index >= (uint) size)
> -                                       throw new IndexOutOfRangeException ();
> +                               CheckIndex(index);
>                                 return data [index];
>                         }

We should ensure that this is inlined by doing it manually.

-- Ben




More information about the Mono-devel-list mailing list