1
+ // Dynamic Array
2
+ // description: A dynamic array is quite similar to a regular array, but its size is modifiable during program runtime.
3
+ // details: for more details check out those links below here:
4
+ // geeks for geeks article : https://www.geeksforgeeks.org/how-do-dynamic-arrays-work/
5
+ // We can mention that Dynamic Array is like Slice for more detail about `Go` Slices check those articles :
6
+ // https://blog.golang.org/slices-intro
7
+ // https://blog.golang.org/slices
8
+ // authors [Wesllhey Holanda](https://github.com/wesllhey), [Milad](https://github.com/miraddo)
9
+ // see dynamic_array.go, dynamic_array_test.go
10
+
1
11
package dynamicarray
2
12
13
+
14
+ // errors: used to handle CheckRangeFromIndex function with a reasonable error value
3
15
import (
4
16
"errors"
5
17
)
6
18
7
19
var defaultCapacity = 10
8
20
9
21
// DynamicArray structure
22
+ // size: length of array
23
+ // capacity: the maximum length of the segment
24
+ // elementData: an array of any type of data with interface
10
25
type DynamicArray struct {
11
26
size int
12
27
capacity int
13
28
elementData []interface {}
14
29
}
15
30
16
- // Put function
31
+ // Put function is change/update the value in array with the index and new value
17
32
func (da * DynamicArray ) Put (index int , element interface {}) error {
18
33
err := da .CheckRangeFromIndex (index )
19
34
@@ -26,7 +41,7 @@ func (da *DynamicArray) Put(index int, element interface{}) error {
26
41
return nil
27
42
}
28
43
29
- // Add function
44
+ // Add function is add new element to our array
30
45
func (da * DynamicArray ) Add (element interface {}) {
31
46
if da .size == da .capacity {
32
47
da .NewCapacity ()
@@ -36,7 +51,7 @@ func (da *DynamicArray) Add(element interface{}) {
36
51
da .size ++
37
52
}
38
53
39
- // Remove function
54
+ // Remove function is remove an element with the index
40
55
func (da * DynamicArray ) Remove (index int ) error {
41
56
err := da .CheckRangeFromIndex (index )
42
57
@@ -52,7 +67,7 @@ func (da *DynamicArray) Remove(index int) error {
52
67
return nil
53
68
}
54
69
55
- // Get function
70
+ // Get function is return one element with the index of array
56
71
func (da * DynamicArray ) Get (index int ) (interface {}, error ) {
57
72
err := da .CheckRangeFromIndex (index )
58
73
@@ -63,25 +78,25 @@ func (da *DynamicArray) Get(index int) (interface{}, error) {
63
78
return da .elementData [index ], nil
64
79
}
65
80
66
- // IsEmpty function
81
+ // IsEmpty function is check that the array has value or not
67
82
func (da * DynamicArray ) IsEmpty () bool {
68
83
return da .size == 0
69
84
}
70
85
71
- // GetData function
86
+ // GetData function return all value of array
72
87
func (da * DynamicArray ) GetData () []interface {} {
73
88
return da .elementData [:da .size ]
74
89
}
75
90
76
- // CheckRangeFromIndex function
91
+ // CheckRangeFromIndex function it will check the range from the index
77
92
func (da * DynamicArray ) CheckRangeFromIndex (index int ) error {
78
93
if index >= da .size || index < 0 {
79
94
return errors .New ("index out of range" )
80
95
}
81
96
return nil
82
97
}
83
98
84
- // NewCapacity function
99
+ // NewCapacity function increase the capacity
85
100
func (da * DynamicArray ) NewCapacity () {
86
101
if da .capacity == 0 {
87
102
da .capacity = defaultCapacity
@@ -95,28 +110,3 @@ func (da *DynamicArray) NewCapacity() {
95
110
96
111
da .elementData = newDataElement
97
112
}
98
-
99
- // func main() {
100
- // numbers := dynamicArray{}
101
- // fmt.Println(numbers.isEmpty())
102
-
103
- // numbers.add(10)
104
- // numbers.add(20)
105
- // numbers.add(30)
106
- // numbers.add(40)
107
- // numbers.add(50)
108
-
109
- // fmt.Println(numbers.isEmpty())
110
-
111
- // fmt.Println(numbers.getData())
112
-
113
- // numbers.remove(1)
114
-
115
- // fmt.Println(numbers.getData())
116
-
117
- // numberFound, _ := numbers.get(1)
118
- // fmt.Println(numberFound)
119
-
120
- // numbers.put(0, 100)
121
- // fmt.Println(numbers.getData())
122
- // }
0 commit comments