-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.go
67 lines (57 loc) · 1.6 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
package std
// A List covers an []interface{} slice with various helper methods.
type List struct {
entries []interface{}
}
// Len returns the amount of elements in the list
func (l *List) Len() int {
return len(l.entries)
}
// Add appends another unboxed element
func (l *List) Add(v *Box) {
l.entries = append(l.entries, v.Unbox())
}
// AddAll appends all other elements
func (l *List) AddAll(v List) {
l.entries = append(l.entries, v.entries...)
}
// Remove deletes the element at the given index. If out of bounds, false is returned instead of panic.
func (l *List) Remove(idx int) bool {
if idx < 0 || idx >= len(l.entries) {
return false
}
copy(l.entries[idx:], l.entries[idx+1:])
l.entries[len(l.entries)-1] = nil //be GC friendly
l.entries = l.entries[:len(l.entries)-1]
return true
}
// Get returns the element at the given position or nil, if otherwise an out of bounds would be thrown
func (l *List) Get(idx int) *Box {
if idx < 0 || idx >= len(l.entries) {
return nil
}
return Wrap(l.entries[idx])
}
// Set replaces the unboxed value. If index is out of bounds, false is returned
func (l *List) Set(idx int, value *Box) bool {
if idx < 0 || idx >= len(l.entries) {
return false
}
l.entries[idx] = value.Unbox()
return true
}
// Clear removes all entries
func (l *List) Clear() {
// let the GC do its job
l.entries = nil
}
// Box returns this list as a box
func (l *List) Box() *Box {
return Wrap(l)
}
// Slice returns the underlying slice in a slice container as a defensive copy
func (l *List) Slice() *Slice {
cpy := make([]interface{}, l.Len())
copy(cpy, l.entries)
return &Slice{cpy}
}