/
list.go
130 lines (111 loc) · 2.64 KB
/
list.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package list
import (
"bytes"
"fmt"
)
// List very useful general purpose list container
type List struct {
elements []interface{}
}
// New returns an initialized list.
func New() *List {
return new(List)
}
// Count The number of items in the list
func (p List) Count() int {
return len(p.elements)
}
// Empty If an empty list
func (p List) Empty() bool {
return p.Count() == 0
}
// Add Add an item to the list
func (p *List) Add(v interface{}) int {
p.elements = append(p.elements, v)
return len(p.elements) - 1
}
// Get Gets an item from the list by its list positiont
func (p List) Get(i int) (interface{}, bool) {
if !p.inRange(i) {
return nil, false
}
return p.elements[i], true
}
// First Gets the first item in the list
func (p *List) First() (interface{}, bool) {
return p.Get(0)
}
// Clear Removes all list items, setting the Count to 0
func (p *List) Clear() {
p.elements = make([]interface{}, 0)
}
// Delete Removes an item from the list by its list position
func (p *List) Delete(i int) {
if !p.inRange(i) {
return
}
p.elements = append(p.elements[:i], p.elements[i+1:]...)
}
// Last Gets the last item in the list
func (p *List) Last() (interface{}, bool) {
return p.Get(p.Count() - 1)
}
// IndexOf Gives the list position of a specified object in the list
func (p *List) IndexOf(v interface{}) int {
var i int
for i < p.Count() {
if p.elements[i] == v {
return i
}
i++
}
return -1
}
// Insert Inserts a new item into the list at a given index position
func (p *List) Insert(i int, v interface{}) {
p.elements = append(p.elements, 0)
copy(p.elements[i+1:], p.elements[i:])
p.elements[i] = v
}
// Swap Moves an item to a new list position
func (p *List) Swap(a, b int) {
if p.inRange(a) && p.inRange(b) {
p.elements[a], p.elements[b] = p.elements[b], p.elements[a]
}
}
// Put Set a new item into the list at a given index position
func (p *List) Put(i int, v interface{}) bool {
if !p.inRange(i) {
return false
}
p.elements[i] = v
return true
}
func (p *List) inRange(index int) bool {
return index >= 0 && index < p.Count()
}
// Remove Removes an item from the list by its object
func (p *List) Remove(v interface{}) int {
i := p.IndexOf(v)
if i >= 0 {
p.Delete(i)
}
return i
}
// Grow Used to set the size (number object pointers) of the list
func (p *List) Grow(n int) {
newSlice := make([]interface{}, len(p.elements), n)
copy(newSlice, p.elements)
p.elements = newSlice
}
func (p *List) String() string {
var b bytes.Buffer
if p.Empty() {
return ""
}
b.WriteString(fmt.Sprintf("%v", p.elements[0]))
for i := 1; i < p.Count(); i++ {
b.WriteString(fmt.Sprintf(",%v", p.elements[i]))
}
return b.String()
}