-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathintslice.go
95 lines (79 loc) · 2.23 KB
/
intslice.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
package villa
//import "fmt"
/*
IntSlice is a wrapper to a slice of int.
You can either create an IntSlice instance directly, or converting the type when necessary.
Usage 1:
var s IntSlice
s.Add(10, 20, 30)
s.Insert(1, 40, 50)
s.Swap(1, len(s) - 1)
s.RemoveRange(1, 3)
s.Fill(0, len(s), 55)
s.Clear()
Usage 2:
var s []int
s = append(s, 10, 20, 30)
(*IntSlice)(&s).Insert(1, 40, 50)
IntSlice(s).Swap(1, len(s) - 1)
(*IntSlice)(&s).RemoveRange(1, 3)
IntSlice(s).Fill(0, len(s), 55)
s = s[:0]
*/
type IntSlice []int
// Add appends the specified element to the end of this slice.
func (s *IntSlice) Add(e ...int) *IntSlice {
*s = append(*s, e...)
return s
}
// Insert inserts the specified element at the specified position.
// NOTE: the insertion algorithm is much better than the slice-trick in go community wiki
func (s *IntSlice) Insert(index int, e ...int) {
if cap(*s) >= len(*s)+len(e) {
*s = (*s)[:len(*s)+len(e)]
} else {
*s = append(*s, e...)
} // else
copy((*s)[index+len(e):], (*s)[index:])
copy((*s)[index:], e[:])
}
// The Swap method in sort.Interface.
func (s IntSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Remove removes the element at the specified position in this slice.
func (s *IntSlice) Remove(index int) int {
e := (*s)[index]
*s = append((*s)[0:index], (*s)[index+1:]...)
return e
}
// RemoveRange removes all of the elements whose index is between from, inclusive, and to, exclusive, from this slice.
func (s *IntSlice) RemoveRange(from, to int) {
*s = append((*s)[0:from], (*s)[to:]...)
}
// Pop removes and returns the last element in the slice
func (s *IntSlice) Pop() int {
return s.Remove(len(*s) - 1)
}
// Fill sets the elements between from, inclusive, and to, exclusive, to a speicified value.
func (s IntSlice) Fill(from, to int, vl int) {
for i := from; i < to; i++ {
s[i] = vl
} // for i
}
// Clear removes all of the elements from this slice.
func (s *IntSlice) Clear() {
*s = (*s)[:0]
}
// Equals returns true if a given slice has the same contents with the slice
func (s IntSlice) Equals(t []int) bool {
if len(s) != len(t) {
return false
} // if
for i := range s {
if s[i] != t[i] {
return false
} // if
} // for i
return true
}