-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2DArray.go
110 lines (87 loc) · 2.16 KB
/
2DArray.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
109
110
package arrays
import (
"errors"
)
type MDArray struct {
numRows int
numCols int
size int
capacity int
data [][]interface{}
}
func NewMDArray(numRows int, numCols int, initialData ...[]interface{}) *MDArray {
arr := &MDArray{
numRows: numRows,
numCols: numCols,
size: 0,
capacity: numRows,
data: make([][]interface{}, numRows),
}
// Initialize each row in the slice
for i := range arr.data {
arr.data[i] = make([]interface{}, numCols)
}
// Fill the array with data, if provided
if len(initialData) > 0 {
for row := 0; row < numRows && row < len(initialData); row++ {
for col := 0; col < numCols && col < len(initialData[row]); col++ {
arr.data[row][col] = initialData[row][col]
}
}
arr.size = len(initialData)
}
return arr
}
func (arr *MDArray) resize() {
newCapacity := arr.capacity * 2
newData := make([][]interface{}, len(arr.data), newCapacity)
copy(newData, arr.data)
arr.data = newData
arr.capacity = newCapacity
}
func (arr *MDArray) GetItem(rowIndex int) ([]interface{}, error) {
if rowIndex >= arr.size || rowIndex < 0 {
return nil, errors.New("index out of bounds")
}
return arr.data[rowIndex], nil
}
func (arr *MDArray) AddItem(item []interface{}, rowIndex ...int) error {
// Determine the row index
index := len(arr.data) // Default to adding at the end
if len(rowIndex) > 0 {
index = rowIndex[0]
}
// Validate index
if index < 0 || (index >= arr.size && arr.size > 0) {
return errors.New("index out of bounds")
}
// Ensure capacity
if arr.size == arr.capacity {
arr.resize()
}
// Shift elements and insert
if index == len(arr.data) {
// Appending to the end
arr.data = append(arr.data, item)
} else {
// Insert at specified index
arr.data = append(arr.data[:index+1], arr.data[index:]...)
arr.data[index] = item
}
arr.numRows++
arr.size++
return nil
}
func (arr *MDArray) RemoveItem(index int) error {
if index >= arr.size || index < 0 {
return errors.New("index out of bounds")
}
if index == len(arr.data)-1 {
arr.data = arr.data[:len(arr.data)-1]
} else {
arr.data = append(arr.data[:index], arr.data[index+1:]...)
}
arr.size--
arr.numRows--
return nil
}