-
Notifications
You must be signed in to change notification settings - Fork 11
/
dynamicarray.go
108 lines (84 loc) · 2.62 KB
/
dynamicarray.go
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
// Package dynamicarray
// A dynamic array is quite similar to a regular array, but its Size is modifiable during program runtime,
// very similar to how a slice in Go works. The implementation is for educational purposes and explains
// how one might go about implementing their own version of slices.
//
// For more details check out those links below here:
// GeeksForGeeks article : https://www.geeksforgeeks.org/how-do-dynamic-arrays-work/
// Go blog: https://blog.golang.org/slices-intro
// Go blog: https://blog.golang.org/slices
// authors [Wesllhey Holanda](https://github.com/wesllhey), [Milad](https://github.com/miraddo)
// see dynamicarray.go, dynamicarray_test.go
package dynamicarray
import (
"errors"
)
var defaultCapacity = 10
// DynamicArray structure
type DynamicArray struct {
Size int
Capacity int
ElementData []interface{}
}
// Put function is change/update the value in array with the index and new value
func (da *DynamicArray) Put(index int, element interface{}) error {
err := da.CheckRangeFromIndex(index)
if err != nil {
return err
}
da.ElementData[index] = element
return nil
}
// Add function is add new element to our array
func (da *DynamicArray) Add(element interface{}) {
if da.Size == da.Capacity {
da.NewCapacity()
}
da.ElementData[da.Size] = element
da.Size++
}
// Remove function is remove an element with the index
func (da *DynamicArray) Remove(index int) error {
err := da.CheckRangeFromIndex(index)
if err != nil {
return err
}
copy(da.ElementData[index:], da.ElementData[index+1:da.Size])
da.ElementData[da.Size-1] = nil
da.Size--
return nil
}
// Get function is return one element with the index of array
func (da *DynamicArray) Get(index int) (interface{}, error) {
err := da.CheckRangeFromIndex(index)
if err != nil {
return nil, err
}
return da.ElementData[index], nil
}
// IsEmpty function is check that the array has value or not
func (da *DynamicArray) IsEmpty() bool {
return da.Size == 0
}
// GetData function return all value of array
func (da *DynamicArray) GetData() []interface{} {
return da.ElementData[:da.Size]
}
// CheckRangeFromIndex function it will check the range from the index
func (da *DynamicArray) CheckRangeFromIndex(index int) error {
if index >= da.Size || index < 0 {
return errors.New("index out of range")
}
return nil
}
// NewCapacity function increase the Capacity
func (da *DynamicArray) NewCapacity() {
if da.Capacity == 0 {
da.Capacity = defaultCapacity
} else {
da.Capacity = da.Capacity << 1
}
newDataElement := make([]interface{}, da.Capacity)
copy(newDataElement, da.ElementData)
da.ElementData = newDataElement
}