-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDynamicArray.cs
122 lines (97 loc) · 3.5 KB
/
DynamicArray.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace DataStructSandBox.DataStructures
{
public interface IDynamicArray<T>{
void Add(T elem);
int Length();
int GetFirstIndexOf(T elem);
int GetLastIndexOf(T elem);
T GetElemAt(int idx);
bool IsEmpty();
string ToString();
}
public class DynamicArray<T> : IDynamicArray<T>, IEnumerable<T>
{
/**
*
* In C# all arrays are dynamic but I'll implement an dynamic array using System.Array class anyway
* I know it doesn't make any sense to use a dynamic array as static array to implement a dynamic array
* but I am just doing it for practice (:
*
* So the idea is,
* 1- use an internal array of fixed size.
* 2- add elements to it
* 3- if array is about to overflow, create new array of double size than before and copy all elements to
* the new array, add next element to newly created array.
*
*/
// since we are using Templates, default will be null for ref. type and 0 for numeric
private T[] _arr;
private int ItemsInArray = 0; // actual filled slots
public DynamicArray()
{
this._arr = new T[15]; // initial capacity of the array.
}
public void Add(T elem)
{
if (this.ItemsInArray + 1 >= this._arr.Length)
{
T[] temp = new T[this._arr.Length * 2];
Array.ConstrainedCopy(this._arr, 0, temp, 0, this._arr.Length);
this._arr = temp;
}
this._arr[this.ItemsInArray] = elem;
this.ItemsInArray++;
}
public T GetElemAt(int idx)
{
if(idx <= this._arr.Length)
return this._arr[idx];
throw new IndexOutOfRangeException();
}
public IEnumerator<T> GetEnumerator() => this._arr.Take(this._arr.Length).GetEnumerator();
public int GetFirstIndexOf(T elem)
{
throw new NotImplementedException();
}
public int GetLastIndexOf(T elem)
{
throw new NotImplementedException();
}
public bool IsEmpty() => this._arr.Length == 0;
public int Length() => this._arr.Length;
IEnumerator IEnumerable.GetEnumerator() => this._arr.GetEnumerator();
public override string ToString()
{
if (this.ItemsInArray == 0)
return "[]";
StringBuilder sb = new StringBuilder();
sb.Append("[ ");
for (int i = 0; i < this.ItemsInArray-1; i++)
sb.AppendFormat("{0}, ",this._arr[i]);
sb.AppendFormat("{0} ]", this._arr[this.ItemsInArray-1]);
return sb.ToString();
}
}
//// driver
////public class Program
////{
//// public static int Main()
//// {
//// DynamicArray<int> intArr = new DynamicArray<int>();
//// intArr.Add(1);
//// intArr.Add(5);
//// intArr.Add(6);
//// intArr.Add(8);
//// intArr.Add(7);
//// Console.WriteLine(intArr.ToString());
//// // output: [ 1, 5, 6, 8, 7 ]
//// return 0;
//// }
////}
}