SgDotNet
Singapore Professional .NET User Group -For Cool Developers

C# Array Extend Ability ?

Latest post 12-03-2006 12:29 AM by tomas_raf. 17 replies.
  • 02-26-2005 12:21 PM In reply to

    Re: C# Array Extend Ability ?

    Hmm... that's a very interesting way to implement strong-types. Using the aliasing. Very innovative. I won't have thought of it before. Good stuff!! Yes
    Regards, triplez ------------------------------ http://triplez.mine.nu/blogs
  • 12-02-2006 11:12 PM In reply to

    Re: C# Array Extend Ability ?

    Directly You can not extent System.Array, but something You can do...

    Here I wrote my own class.

       public class DynamicArray<T>
        {
            private T[] data;
            private int length = 0;
            public int increaseSize = 5;

            public T this[int index]
            {
                get
                {
                    if (length >= 0 && index >= 0 && index < length)
                        return this.data[index];
                    else throw new IndexOutOfRangeException();
                }
                set
                {
                    if (length >= 0 && index >= 0 && index < length)
                        this.data[index] = value;
                    else throw new IndexOutOfRangeException();
                }
           
            }
            
            public static explicit operator T[](DynamicArray<T> dyna)
            {
                return dyna.data;
            }

            public int Length
            {
                get { return this.length; }
                set
                {
                    if (value > data.Length)
                    {
                        int incTimes = (int)Math.Ceiling((value - this.data.Length) / (decimal)this.increaseSize);
                        //T[] newData = new T[this.data.Length + this.increaseSize * times];
                        //this.data.CopyTo(newData, 0);
                        //this.data = newData;

                        // Resize does the same work as comented above code
                        Array.Resize<T>(ref this.data, this.data.Length + this.increaseSize * incTimes);
                    }
                    else if (value == 0)
                    {
                        Array.Resize<T>(ref this.data, value);
                    }
                    

                    this.length = value;
                }
            }

            public DynamicArray() : base()
            {
                this.data = new T[increaseSize];
            }

            public DynamicArray(int initialSize) : base()
            {
                this.data = new T[initialSize];
            }

            public DynamicArray(int initialSize, int incSize) : base()
            {
                this.increaseSize = incSize > 0 ? incSize : this.increaseSize;
                this.data = new T[initialSize > this.increaseSize ?
                                  initialSize : this.increaseSize];
            }

        }

     

     // If You want to update struct use convertion, just remember, that after convertion You'l get real array witch size difers from DynamicArray

    struct MyStruct

    {

          public int value; 

    }

    DynamicArray<MyStruct> dyna = new DynamicArray<MyStruct>();

    dyna.increaseSize = 10;

    dyna.Length = 11; // Extends data array to 20

    ((MyStruct[])dyna)[0].value = 0;

    ((MyStruct[])dyna)[1].value = 1;

    ....... 

    dyna.Length = 0; //empty array

     Sorry, I'v not tested yet well.., but I think should work :)

     

    Tomas Raf 

  • 12-03-2006 12:29 AM In reply to

    Re: C# Array Extend Ability ?

    Fixed some errors

    public class DynamicArray<T>
        {
            private T[] data;
            private int length = 0;
            public int increaseSize = 5;

            public T this[int index]
            {
                get
                {
                    if (index < length)
                        return this.data[index];
                    else throw new IndexOutOfRangeException();
                }
                set
                {
                    if (index < length)
                        this.data[index] = value;
                    else throw new IndexOutOfRangeException();
                }

            }

            public static explicit operator T[](DynamicArray<T> dyna)
            {
                return dyna.data;
            }

            public int Length
            {
                get { return this.length; }
                set
                {
                    if (value > data.Length)
                    {
                        int incTimes = (int)Math.Ceiling((value - this.data.Length) / (decimal)this.increaseSize);
                        //T[] newData = new T[this.data.Length + this.increaseSize * times];
                        //this.data.CopyTo(newData, 0);
                        //this.data = newData;

                        // Resize does the same work as comented above code
                        Array.Resize<T>(ref this.data, this.data.Length + this.increaseSize * incTimes);
                    }
                    else if (value == 0)
                    {
                        Array.Resize<T>(ref this.data, value);
                    }


                    this.length = value;
                }
            }

            public void CopyTo(ref Array dst, int index)
            {
                this.data.CopyTo(dst, index);
            }

            public DynamicArray() : base()
            {
                this.data = new T[increaseSize];
            }

            public DynamicArray(int initialLength) : base()
            {
                this.data = new T[initialLength];
                this.length = initialLength;
            }

            public DynamicArray(int initialLength, int incSize) : base()
            {
                this.increaseSize = incSize > 0 ? incSize : this.increaseSize;
                this.data = new T[this.increaseSize > initialLength ?
                                  this.increaseSize : initialLength];
                this.length = initialLength;
            }

Page 2 of 2 (18 items) < Previous 1 2 | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems