[C#] Dynamic Array

[C#] Dynamic Array

Postby Payne » 30 Apr 2010, 17:29

Clasa DynamicArray care face acelasi lucru ca si List<Type>. Acu o sa imi ziceti ca reinventez roata ... da, e adevarat, dar e pur educational.
Despre ce e vorba. E vorba despre un array care se autoredimensioneaza singur si in care poti baga atatea elemente cat iti permite memoria. Adica la folosire nu trebuie sa ii zici cate elemente o sa aiba array-ul deoarece el stie cand nu mai are loc si o sa se mareasca daca e nevoie asa ca nu trebuie sa te preocupi de ca nu ai loc in array.

Important:
Vedeti ca in cod am niste comentarii de genul "< .net framework 3.5" sau ".net framework 3.5 or later". Depinzand de sub ce versiune compilati clasa. Adica daca o compilati pentru versiuni mai mici de .net framework 3.5 comentati ultima clasa(Extensions) si stergeti comentariile celor doua functii Join din clasa DynamicArray.

  1.  
  2. using System;
  3. using System.Text;
  4.  
  5. namespace CoolStuff
  6. {
  7.     public class DynamicArray<T>
  8.     {
  9.         private UInt32 ui32_size = 2;
  10.         private T[] at_elements;
  11.         private UInt32 ui32_pos = 0;
  12.         public DynamicArray() {
  13.             at_elements = new T[ui32_size];
  14.         }
  15.         // Gets the type of the stack
  16.         public Type GetType(){ return at_elements.GetType(); }
  17.         // Reverses the stack
  18.         public void Reverse() {
  19.             int pos = 0;
  20.             while (ui32_pos - (pos * 2) > 1) {
  21.                 T temp = at_elements[pos];
  22.                 at_elements[pos] = at_elements[ui32_pos - (pos + 1)];
  23.                 at_elements[ui32_pos - (pos + 1)] = temp;
  24.                 pos++;
  25.             }
  26.         }
  27.         // Adds an element to stack
  28.         public void Add(T elem){
  29.             at_elements[ui32_pos++] = elem;
  30.             if (ui32_pos == ui32_size - 1)
  31.             {
  32.                 T[] _temp = new T[ui32_size+10];
  33.                 Array.Copy(at_elements, _temp, at_elements.Length);
  34.                 at_elements = _temp;
  35.                 _temp = null;
  36.                 ui32_size += 10;
  37.             }
  38.         }
  39.         // Add an array of elements to stack
  40.         public void AddRange(T[] elements) { for (int x = 0; x < elements.Length; x++) { this.Add(elements[x]); } }
  41.         // Return the current max size of the array
  42.         public int Size { get { return (int)ui32_size; } }
  43.         // Return the number of elements in the array
  44.         public int Count { get { return (int)ui32_pos; } }
  45.  
  46.         // Removes the element situated at i index
  47.         public void RemoveAt(uint i) {
  48.             if (i < ui32_pos)
  49.             {
  50.                 for (uint x = i; x < ui32_pos; x++)
  51.                     at_elements[x] = at_elements[x + 1];
  52.                 ui32_pos--;
  53.             }
  54.             else throw new Exception("Index out of bounds");
  55.         }
  56.         /* For <3.5 versions of .net Framework
  57.         public string Join(){
  58.             StringBuilder sb = new StringBuilder();
  59.             if (at_elements.GetType().Name.ToLower() == "string[]")
  60.             {
  61.                 for (int x = 0; x < Count; x++)
  62.                     sb.Append(at_elements[x]);
  63.                 return sb.ToString();
  64.             }
  65.             else
  66.                 throw new Exception("To join this stack must be a string type stack");
  67.         }
  68.         public string Join(string separator) {
  69.             StringBuilder sb = new StringBuilder();
  70.             if (at_elements.GetType().Name.ToLower() == "string[]")
  71.             {
  72.                 for (int x = 0; x < Count; x++)
  73.                     sb.Append(at_elements[x]+separator);
  74.                 return sb.ToString().Remove(sb.Length-separator.Length,separator.Length);
  75.             }
  76.             else
  77.                 throw new Exception("To join this stack must be a string type stack");
  78.         }
  79.         */
  80.         // Removes the last element in the stack
  81.         public void RemoveLast()
  82.         {
  83.             ui32_pos--;
  84.             if (ui32_pos < (ui32_size / 2))
  85.             {
  86.                 T[] _temp = new T[ui32_size / 2];
  87.                 Array.Copy(at_elements, _temp, _temp.Length);
  88.                 at_elements = _temp;
  89.                 _temp = null;
  90.                 ui32_size /= 2;
  91.             }
  92.         }
  93.         // Removes the last element in the stack and return its value
  94.         public T Pop() {
  95.             T _tmp = at_elements[ui32_pos--];
  96.             if (ui32_pos < (ui32_size / 2))
  97.             {
  98.                 T[] _temp = new T[ui32_size / 2];
  99.                 Array.Copy(at_elements, _temp, _temp.Length);
  100.                 at_elements = _temp;
  101.                 _temp = null;
  102.                 ui32_size /= 2;
  103.             }
  104.             return _tmp;
  105.         }
  106.         // The [] operator
  107.         public T this[int i] {
  108.             get {
  109.                 if (i > ui32_pos)
  110.                     throw new Exception("Out of bounds");
  111.                 else if (i < 0)
  112.                     throw new Exception("Index cannot be negative");
  113.                 else
  114.                     return at_elements[i];
  115.             } set
  116.             {
  117.                 if (i < ui32_pos)
  118.                     at_elements[i] = value;
  119.                 else
  120.                     throw new Exception("Out of bounds");
  121.             }
  122.         }
  123.     }
  124.  
  125.     // Join extensions. Only for .net framework 3.5 or later. Need System.Data.DataSetExtensions referenece.
  126.     public static class Extensions
  127.     {
  128.         public static string Join(this DynamicArray<string> da_str) {
  129.             StringBuilder sb = new StringBuilder();
  130.             for (int x = 0; x < da_str.Count; x++)
  131.                 sb.Append(da_str[x]);
  132.             return sb.ToString();
  133.         }
  134.         public static string Join(this DynamicArray<string> da_str,string separator)
  135.         {
  136.             StringBuilder sb = new StringBuilder();
  137.             for (int x = 0; x < da_str.Count; x++)
  138.                 sb.Append(da_str[x]+separator);
  139.             return sb.ToString().Remove(sb.Length - separator.Length, separator.Length); ;
  140.         }
  141.     }
  142. }
  143.  


Exemplu

  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using CoolStuff;
  6. namespace TestDynamicArray
  7. {
  8.     class Program
  9.     {
  10.         static DynamicArray<DynamicArray<string>> d2vec = new DynamicArray<DynamicArray<string>>();
  11.         static DynamicArray<string> vec = new DynamicArray<string>();
  12.         static void Main(string[] args)
  13.         {
  14.             DynamicArray<string> arr1 = new DynamicArray<string>();
  15.             arr1.Add("Yahoo");
  16.             arr1.Add("Google");
  17.             d2vec.Add(arr1);
  18.             DynamicArray<string> arr2 = new DynamicArray<string>();
  19.             arr2.Add("Ask");
  20.             arr2.Add("Bitcell");
  21.             d2vec.Add(arr2);
  22.             Console.WriteLine(d2vec[1][1]); // Will print Bitcell
  23.             string[] elements = {"Welcome","to","BitCell","!" };
  24.             vec.AddRange(elements);
  25.             Console.ReadLine();
  26.             string ret =  vec.Join(" ");
  27.             Console.WriteLine(ret); // Will Print Welcome to BitCell !
  28.             Console.ReadLine();
  29.         }
  30.     }
  31. }
  32.  
  33.  
2p / 1 votes
Suit up!
User avatar
Payne
Byte
 
Joined: 04 Jan 2010
Location: 0x7C00
Status: 17

Return to Snippets

Who is online

Users browsing this forum: No registered users and 0 guests