-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector.go
90 lines (80 loc) · 2.46 KB
/
vector.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
package data
//----------------------------------------------------------------------
// This file is part of Gospel.
// Copyright (C) 2011-2023 Bernd Fix >Y<
//
// Gospel is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// Gospel is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later
//----------------------------------------------------------------------
// Vector data structure
type Vector struct {
data [](interface{}) // list of elements
}
// NewVector instantiates a new (empty) Vector object.
func NewVector() *Vector {
return &Vector{
data: make([](interface{}), 0),
}
}
// Len returns the number of elements in the vector.
func (vec *Vector) Len() int {
return len(vec.data)
}
// Add element to the end of the vector.
func (vec *Vector) Add(v interface{}) {
vec.data = append(vec.data, v)
}
// Insert element at given position. Add 'nil' elements if index
// is beyond the end of the vector.
func (vec *Vector) Insert(i int, v interface{}) {
if i < 0 {
// create a prepending slice
pre := make([](interface{}), -i)
pre[0] = v
vec.data = append(pre, vec.data...)
} else if i >= len(vec.data) {
// create appending slice
idx := i - len(vec.data) + 1
app := make([](interface{}), idx)
app[idx-1] = v
vec.data = append(vec.data, app...)
} else {
pre := vec.data[:i]
app := vec.data[i:]
vec.data = append(append(pre, v), app...)
}
}
// Drop the last element from the vector.
func (vec *Vector) Drop() (v interface{}) {
pos := len(vec.data) - 1
v, vec.data = vec.data[pos], vec.data[:pos]
return
}
// Delete indexed element from the vector.
func (vec *Vector) Delete(i int) (v interface{}) {
if i < 0 || i > len(vec.data)-1 {
return nil
}
v = vec.data[i]
vec.data = append(vec.data[:i], vec.data[i+1:]...)
return
}
// At return the indexed element from vector.
func (vec *Vector) At(i int) (v interface{}) {
if i < 0 || i > len(vec.data)-1 {
return nil
}
return vec.data[i]
}