-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstringvector.go
59 lines (51 loc) · 1.04 KB
/
stringvector.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
package ik
type StringVector []string
func (sv *StringVector) Append(v string) {
oldLength := len(*sv)
sv.ensureCapacity(oldLength + 1)
(*sv)[oldLength] = v
}
func (sv *StringVector) Push(v string) {
sv.Append(v)
}
func (sv *StringVector) Pop() string {
retval := (*sv)[len(*sv)-1]
*sv = (*sv)[0 : len(*sv)-1]
return retval
}
func (sv *StringVector) Shift() string {
retval := (*sv)[0]
*sv = (*sv)[1:len(*sv)]
return retval
}
func (sv *StringVector) Last() string {
return (*sv)[len(*sv)-1]
}
func (sv *StringVector) First() string {
return (*sv)[0]
}
func (sv *StringVector) ensureCapacity(l int) {
if l < 256 {
if l > cap(*sv) {
newSlice := make([]string, l)
copy(newSlice, *sv)
*sv = newSlice
}
} else {
newCapacity := cap(*sv)
if newCapacity < 256 {
newCapacity = 128
}
for l > newCapacity {
newCapacity = 2 * newCapacity
if newCapacity < cap(*sv) {
/* unlikely */
panic("out of memory")
}
}
newSlice := make([]string, newCapacity)
copy(newSlice, *sv)
*sv = newSlice
}
*sv = (*sv)[0:l]
}