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.
- using System;
- using System.Text;
- namespace CoolStuff
- {
- public class DynamicArray<T>
- {
- private UInt32 ui32_size = 2;
- private T[] at_elements;
- private UInt32 ui32_pos = 0;
- public DynamicArray() {
- at_elements = new T[ui32_size];
- }
- // Gets the type of the stack
- public Type GetType(){ return at_elements.GetType(); }
- // Reverses the stack
- public void Reverse() {
- int pos = 0;
- while (ui32_pos - (pos * 2) > 1) {
- T temp = at_elements[pos];
- at_elements[pos] = at_elements[ui32_pos - (pos + 1)];
- at_elements[ui32_pos - (pos + 1)] = temp;
- pos++;
- }
- }
- // Adds an element to stack
- public void Add(T elem){
- at_elements[ui32_pos++] = elem;
- if (ui32_pos == ui32_size - 1)
- {
- T[] _temp = new T[ui32_size+10];
- Array.Copy(at_elements, _temp, at_elements.Length);
- at_elements = _temp;
- _temp = null;
- ui32_size += 10;
- }
- }
- // Add an array of elements to stack
- public void AddRange(T[] elements) { for (int x = 0; x < elements.Length; x++) { this.Add(elements[x]); } }
- // Return the current max size of the array
- public int Size { get { return (int)ui32_size; } }
- // Return the number of elements in the array
- public int Count { get { return (int)ui32_pos; } }
- // Removes the element situated at i index
- public void RemoveAt(uint i) {
- if (i < ui32_pos)
- {
- for (uint x = i; x < ui32_pos; x++)
- at_elements[x] = at_elements[x + 1];
- ui32_pos--;
- }
- else throw new Exception("Index out of bounds");
- }
- /* For <3.5 versions of .net Framework
- public string Join(){
- StringBuilder sb = new StringBuilder();
- if (at_elements.GetType().Name.ToLower() == "string[]")
- {
- for (int x = 0; x < Count; x++)
- sb.Append(at_elements[x]);
- return sb.ToString();
- }
- else
- throw new Exception("To join this stack must be a string type stack");
- }
- public string Join(string separator) {
- StringBuilder sb = new StringBuilder();
- if (at_elements.GetType().Name.ToLower() == "string[]")
- {
- for (int x = 0; x < Count; x++)
- sb.Append(at_elements[x]+separator);
- return sb.ToString().Remove(sb.Length-separator.Length,separator.Length);
- }
- else
- throw new Exception("To join this stack must be a string type stack");
- }
- */
- // Removes the last element in the stack
- public void RemoveLast()
- {
- ui32_pos--;
- if (ui32_pos < (ui32_size / 2))
- {
- T[] _temp = new T[ui32_size / 2];
- Array.Copy(at_elements, _temp, _temp.Length);
- at_elements = _temp;
- _temp = null;
- ui32_size /= 2;
- }
- }
- // Removes the last element in the stack and return its value
- public T Pop() {
- T _tmp = at_elements[ui32_pos--];
- if (ui32_pos < (ui32_size / 2))
- {
- T[] _temp = new T[ui32_size / 2];
- Array.Copy(at_elements, _temp, _temp.Length);
- at_elements = _temp;
- _temp = null;
- ui32_size /= 2;
- }
- return _tmp;
- }
- // The [] operator
- public T this[int i] {
- get {
- if (i > ui32_pos)
- throw new Exception("Out of bounds");
- else if (i < 0)
- throw new Exception("Index cannot be negative");
- else
- return at_elements[i];
- } set
- {
- if (i < ui32_pos)
- at_elements[i] = value;
- else
- throw new Exception("Out of bounds");
- }
- }
- }
- // Join extensions. Only for .net framework 3.5 or later. Need System.Data.DataSetExtensions referenece.
- public static class Extensions
- {
- public static string Join(this DynamicArray<string> da_str) {
- StringBuilder sb = new StringBuilder();
- for (int x = 0; x < da_str.Count; x++)
- sb.Append(da_str[x]);
- return sb.ToString();
- }
- public static string Join(this DynamicArray<string> da_str,string separator)
- {
- StringBuilder sb = new StringBuilder();
- for (int x = 0; x < da_str.Count; x++)
- sb.Append(da_str[x]+separator);
- return sb.ToString().Remove(sb.Length - separator.Length, separator.Length); ;
- }
- }
- }
Exemplu
- using System;
- using System.Collections.Generic;
- using System.Text;
- using CoolStuff;
- namespace TestDynamicArray
- {
- class Program
- {
- static DynamicArray<DynamicArray<string>> d2vec = new DynamicArray<DynamicArray<string>>();
- static DynamicArray<string> vec = new DynamicArray<string>();
- static void Main(string[] args)
- {
- DynamicArray<string> arr1 = new DynamicArray<string>();
- arr1.Add("Yahoo");
- arr1.Add("Google");
- d2vec.Add(arr1);
- DynamicArray<string> arr2 = new DynamicArray<string>();
- arr2.Add("Ask");
- arr2.Add("Bitcell");
- d2vec.Add(arr2);
- Console.WriteLine(d2vec[1][1]); // Will print Bitcell
- string[] elements = {"Welcome","to","BitCell","!" };
- vec.AddRange(elements);
- Console.ReadLine();
- string ret = vec.Join(" ");
- Console.WriteLine(ret); // Will Print Welcome to BitCell !
- Console.ReadLine();
- }
- }
- }
Welcome to BitCell. Click here to register !