Lists VS. Dynamic Arrays VS. Re-defining Arrays 
Author Message
 Lists VS. Dynamic Arrays VS. Re-defining Arrays

I need to implement a Contaning class for an unknown number of objects of a
second class...

This is more of a design question, which is the best (more efficient, more
scalable, less resources) way of handling this?

I have come to 3 possible solutions:

1) Define an array of my inner objects and re-define the array everytime a
new one needs to be added:

    int[] TempArray = new int[MyArray.Length + 1];

    //Copy all elements of base array
    MyArray.CopyTo(TempArray,0);

    //Get new value on new position (Lenght of base + 1)
    TempArray[TempArray.Length - 1] = iNewElement;

    //Set array back to base array
    MyArray = TempArray;

2) Use the the ArrayList class from the CLR:

    MyArrayList.Add(iNewElement);
    return MyArrayList.ToArray(typeof(int));

3) Implement a double / single list:
    public class Node
    {
        public Node next;
        public string value;
    public Node(string val):this(val,null,){}
    public Node(string val,Node Llink,Node Rlink)
    {   next=Llink;
        value=val;}
    }

Is there any other solution to this?
Which one do you guys recommend?

Thanks for any help!

Adolfo W.



Fri, 19 Dec 2003 23:51:05 GMT  
 Lists VS. Dynamic Arrays VS. Re-defining Arrays
I like the ArrayList solution. It would be by far the
simpler to implement. If you know ahead of time about how
many objects will be going in , for example, more then 20
but less then 50, you can set the Capacity to 50, and save
on perf. You can still add more then 50, but when you do
it will actually do your first idea behind the scenes. The
default Capacity is 16 I think.

Quote:
>-----Original Message-----
>I need to implement a Contaning class for an unknown

number of objects of a
Quote:
>second class...

>This is more of a design question, which is the best

(more efficient, more
Quote:
>scalable, less resources) way of handling this?

>I have come to 3 possible solutions:

>1) Define an array of my inner objects and re-define the
array everytime a
>new one needs to be added:

>    int[] TempArray = new int[MyArray.Length + 1];

>    //Copy all elements of base array
>    MyArray.CopyTo(TempArray,0);

>    //Get new value on new position (Lenght of base + 1)
>    TempArray[TempArray.Length - 1] = iNewElement;

>    //Set array back to base array
>    MyArray = TempArray;

>2) Use the the ArrayList class from the CLR:

>    MyArrayList.Add(iNewElement);
>    return MyArrayList.ToArray(typeof(int));

>3) Implement a double / single list:
>    public class Node
>    {
>        public Node next;
>        public string value;
>    public Node(string val):this(val,null,){}
>    public Node(string val,Node Llink,Node Rlink)
>    {   next=Llink;
>        value=val;}
>    }

>Is there any other solution to this?
>Which one do you guys recommend?

>Thanks for any help!

>Adolfo W.

>.



Sat, 20 Dec 2003 00:01:52 GMT  
 Lists VS. Dynamic Arrays VS. Re-defining Arrays
Without knowing anything more I would assume the ArrayList class does the
trick.

Correct me if I am wrong but I would assume the ArrayList class holds an
array potentially larger than the actual size and then only resize it
whenever the actual size of the list exceeds the size of the array and then
it grows by some amount so that you can add N more elements before it needs
to move the data over to new array.

If it doesn't, then you should make such a class :-)

If the container is to hold references (including 'object') then make sure
to set all unused slots in the array to null so that the GC can pick up
those elements.

Also, what to do about elements removed from the list? If you do a lot of
removals inside the list, then maybe a linked list is a better choice. You
consume more space since you need locations for the next and maybe also a
prev pointer for each element but removing and inserting elements in the
middle of the list is easier.

If you always insert or remove at the ends of the list an array is a better
choice.

For example allocate an array of 50 elements for a list of 30 elements so
that you have 10 elements in front and 10 elements at rear end available.
Keep an integer to indicate the start and end of the list.

class MyList {
    private int head;
    private int tail;
    object [] elems;

   .....

Quote:
}

if you have a list of 30 elements, head is set to 10, tail is set to 40.
Allowing room for inserting elements at front by decreasing head

    elems[--head] = new_elem;

or appending elements at tail by

elems[tail++] = new_elem_at_rear;

Number of elements in list is tail - head;

Remove elements in front of list by elems[head++] = null; and at tail by
elems[--tail] = null;

If head is about to get negative or tail is about to get outside the upper
bounds of the array elems you need to move the list over to a new array,
make the new array large enough to hold a convenient space available in
front and/or rear as needed. Then make a shallow copy of the elems array
over to the new array and put the new array in elems, code like this will do
the trick:

object [] old = elems;
elems = new object[new_size];
Array.Copy(old, head, elems, new_head, tail - head);
tail += new_head - head;
head = new_head;

Good luck.

Me


Quote:
> I need to implement a Contaning class for an unknown number of objects of
a
> second class...

> This is more of a design question, which is the best (more efficient, more
> scalable, less resources) way of handling this?

> I have come to 3 possible solutions:

> 1) Define an array of my inner objects and re-define the array everytime a
> new one needs to be added:

>     int[] TempArray = new int[MyArray.Length + 1];

>     //Copy all elements of base array
>     MyArray.CopyTo(TempArray,0);

>     //Get new value on new position (Lenght of base + 1)
>     TempArray[TempArray.Length - 1] = iNewElement;

>     //Set array back to base array
>     MyArray = TempArray;

> 2) Use the the ArrayList class from the CLR:

>     MyArrayList.Add(iNewElement);
>     return MyArrayList.ToArray(typeof(int));

> 3) Implement a double / single list:
>     public class Node
>     {
>         public Node next;
>         public string value;
>     public Node(string val):this(val,null,){}
>     public Node(string val,Node Llink,Node Rlink)
>     {   next=Llink;
>         value=val;}
>     }

> Is there any other solution to this?
> Which one do you guys recommend?

> Thanks for any help!

> Adolfo W.



Sat, 20 Dec 2003 00:57:04 GMT  
 Lists VS. Dynamic Arrays VS. Re-defining Arrays
Do an arraylist, and then profile your code. If it turns out that the perf
of that area is critical, you could explore option #1, but I don't think
you'll see much size benefit (and it will definitely be slower on adds, as
ArrayList grows by doubling its size).

Note that you can force an arraylist to be allocated to a specific size if
you want.


Quote:
> I need to implement a Contaning class for an unknown number of objects of
a
> second class...

> This is more of a design question, which is the best (more efficient, more
> scalable, less resources) way of handling this?

> I have come to 3 possible solutions:

> 1) Define an array of my inner objects and re-define the array everytime a
> new one needs to be added:

>     int[] TempArray = new int[MyArray.Length + 1];

>     //Copy all elements of base array
>     MyArray.CopyTo(TempArray,0);

>     //Get new value on new position (Lenght of base + 1)
>     TempArray[TempArray.Length - 1] = iNewElement;

>     //Set array back to base array
>     MyArray = TempArray;

> 2) Use the the ArrayList class from the CLR:

>     MyArrayList.Add(iNewElement);
>     return MyArrayList.ToArray(typeof(int));

> 3) Implement a double / single list:
>     public class Node
>     {
>         public Node next;
>         public string value;
>     public Node(string val):this(val,null,){}
>     public Node(string val,Node Llink,Node Rlink)
>     {   next=Llink;
>         value=val;}
>     }

> Is there any other solution to this?
> Which one do you guys recommend?

> Thanks for any help!

> Adolfo W.



Sat, 20 Dec 2003 00:01:30 GMT  
 Lists VS. Dynamic Arrays VS. Re-defining Arrays
Thank you all for you recommendations.... but.....

ArrayList accepts elements of type object
If I want a type-safe ArrayList:
1) Should I inherit from ArrayList and build my own?
2) Should I implement a container class for the ArrayList that hides the
functionality and presents a type-safe interface?

Seems to me that any of this 2 options are even more work than using any of
the two other solutions....

Is this the right approach?

TIA
Adolfo W.


Quote:
> I need to implement a Contaning class for an unknown number of objects of
a
> second class...

> This is more of a design question, which is the best (more efficient, more
> scalable, less resources) way of handling this?

> I have come to 3 possible solutions:

> 1) Define an array of my inner objects and re-define the array everytime a
> new one needs to be added:

>     int[] TempArray = new int[MyArray.Length + 1];

>     //Copy all elements of base array
>     MyArray.CopyTo(TempArray,0);

>     //Get new value on new position (Lenght of base + 1)
>     TempArray[TempArray.Length - 1] = iNewElement;

>     //Set array back to base array
>     MyArray = TempArray;

> 2) Use the the ArrayList class from the CLR:

>     MyArrayList.Add(iNewElement);
>     return MyArrayList.ToArray(typeof(int));

> 3) Implement a double / single list:
>     public class Node
>     {
>         public Node next;
>         public string value;
>     public Node(string val):this(val,null,){}
>     public Node(string val,Node Llink,Node Rlink)
>     {   next=Llink;
>         value=val;}
>     }

> Is there any other solution to this?
> Which one do you guys recommend?

> Thanks for any help!

> Adolfo W.



Sat, 20 Dec 2003 16:10:57 GMT  
 Lists VS. Dynamic Arrays VS. Re-defining Arrays
Do you want to typesafety at compile time or runtime? depending on that, you
can build one or the other.
AFAIK (which is very less :)
I think , option 1 is the way to go if you want the typesafety at compile
time. If you dont care about that, i.e. you just want it to be runtime
safety, you can use play around with attributes and build it the way you
need.
Following is basically a simple implementation of using Attributes to
validate the data input to Add() call.
Note that it is not complete in any manner and this might not be what you
are interested in. I tried to make that attributes to apply to a member
variable but with no success.. So, you basically have a separate class for
that.
Anyway, See if this helps..:)

using System;
using System.Collections;
using System.Reflection;

[AttributeUsage(AttributeTargets.All,Inherited=true,AllowMultiple=false)]
class TypeForAttribute: System.Attribute
{
 private string _type;

 public TypeForAttribute(string type)
 {
  _type = type;
  Console.WriteLine("_type == " + type);
 }

 public Type GetTypeStored()
 {
  return(Type.GetType(_type));
 }

Quote:
}

class TypedArrayException:Exception
{
 public TypedArrayException(string t):base(t)
 {

 }

Quote:
}

class TypedArrayList:ArrayList
{
 public TypedArrayList() :base()
 {
  _type = GetAttributeTypeFor(this.GetType());
 }

 public TypedArrayList(int capacity):base(capacity)
 {
  _type = GetAttributeTypeFor(this.GetType());
 }

 public override int Add(object o)
 {
  if (_type == null)
   return(base.Add(o));

  if (_type.Equals(o.GetType()))
  {
   return base.Add(o);
  }
  else
   throw new TypedArrayException("Not a valid type");
 }

 private Type GetAttributeTypeFor(Type t)
 {
  TypeForAttribute tx = (TypeForAttribute)Attribute.GetCustomAttribute(t,
       typeof(TypeForAttribute));
  if (null == tx)
  {
   return(null);

   }
  else
  {
   return(tx.GetTypeStored());
  }
 }

 private Type _type;

Quote:
}

[TypeForAttribute("System.String")]
class StringArrayList : TypedArrayList
{
 public StringArrayList():base()
 {

 }

 public StringArrayList(int capacity):base(capacity)
 {

 }

Quote:
}

class TestType
{

 StringArrayList _typeArr;

 private TypedArrayList _noTypeArr;

 public TestType()
 {
  _typeArr = new StringArrayList();
  _noTypeArr = new TypedArrayList();
  _typeArr.Add("Added one");
  _typeArr.Add("Added type two");
  _noTypeArr.Add("one");
  _noTypeArr.Add(1);
 }

 public void AddBadData()
 {
  _typeArr.Add(1);
 }
 public void AddGoodData()
 {
  _typeArr.Add("String");
 }

 public void Print()
 {
  Console.WriteLine("Printing _typeArr");
  foreach (object o in  _typeArr)
  {
   string s = (string) o;
   Console.WriteLine(s);
  }
  Console.WriteLine("Printing _noTypeArr");
  foreach (object x in  _noTypeArr)
  {
   string s = x.ToString();
   Console.WriteLine(s);
  }
 }

 public static void Main(String[] args)
 {
  TestType t = new TestType();
  Console.WriteLine("---- After construction----");
  t.Print();
  t.AddGoodData();
  Console.WriteLine("after Adding good data ");
  t.Print();
  Console.WriteLine("about to add bad data ");
  t.AddBadData();
  Console.WriteLine("after bad data ");
  t.Print();
 }

Quote:
}

--
Girish Bharadwaj


Quote:
> Thank you all for you recommendations.... but.....

> ArrayList accepts elements of type object
> If I want a type-safe ArrayList:
> 1) Should I inherit from ArrayList and build my own?
> 2) Should I implement a container class for the ArrayList that hides the
> functionality and presents a type-safe interface?

> Seems to me that any of this 2 options are even more work than using any
of
> the two other solutions....

> Is this the right approach?

> TIA
> Adolfo W.



> > I need to implement a Contaning class for an unknown number of objects
of
> a
> > second class...

> > This is more of a design question, which is the best (more efficient,
more
> > scalable, less resources) way of handling this?

> > I have come to 3 possible solutions:

> > 1) Define an array of my inner objects and re-define the array everytime
a
> > new one needs to be added:

> >     int[] TempArray = new int[MyArray.Length + 1];

> >     //Copy all elements of base array
> >     MyArray.CopyTo(TempArray,0);

> >     //Get new value on new position (Lenght of base + 1)
> >     TempArray[TempArray.Length - 1] = iNewElement;

> >     //Set array back to base array
> >     MyArray = TempArray;

> > 2) Use the the ArrayList class from the CLR:

> >     MyArrayList.Add(iNewElement);
> >     return MyArrayList.ToArray(typeof(int));

> > 3) Implement a double / single list:
> >     public class Node
> >     {
> >         public Node next;
> >         public string value;
> >     public Node(string val):this(val,null,){}
> >     public Node(string val,Node Llink,Node Rlink)
> >     {   next=Llink;
> >         value=val;}
> >     }

> > Is there any other solution to this?
> > Which one do you guys recommend?

> > Thanks for any help!

> > Adolfo W.



Sat, 20 Dec 2003 23:24:28 GMT  
 Lists VS. Dynamic Arrays VS. Re-defining Arrays
The best way to achieve this is to create your own class and derive from the
base class 'CollectionBase' which is found in System.Collections.

This allows you quickly create Type safe collections.

There are a lot of system collection classes that also use this as their
base class. Also, there's ReadOnlyCollectionBase which, suprisingly enough,
is a read only implementation.


Quote:
> Thank you all for you recommendations.... but.....

> ArrayList accepts elements of type object
> If I want a type-safe ArrayList:
> 1) Should I inherit from ArrayList and build my own?
> 2) Should I implement a container class for the ArrayList that hides the
> functionality and presents a type-safe interface?

> Seems to me that any of this 2 options are even more work than using any
of
> the two other solutions....

> Is this the right approach?

> TIA
> Adolfo W.



Mon, 22 Dec 2003 19:04:27 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Array objects vs. square bracket [] arrays

2. record of arrays vs array of records?

3. array[1] vs *(array +

4. fn(array) vs. fn(&array)

5. MFC arrays vs. C++ arrays

6. array of structures vs. linked list

7. Linked list vs. Array of structs

8. Arrays vs Linked Lists

9. OLE DB Consumer Template Accessors (Dynamic Vs. Manual)??

10. char[] vs char * vs #defines for constant strings

11. ATL Vs. VB Vs. C# COM Component Performance

12. #define VS. const

 

 
Powered by phpBB® Forum Software