-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcomplexslice.go
76 lines (63 loc) · 2.04 KB
/
complexslice.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
package villa
//import "fmt"
// ComplexSlice is wrapper to a slice of complex128.
//
// See examples of IntSlice for reference.
type ComplexSlice []complex128
// Add appends the specified element to the end of this slice.
func (s *ComplexSlice) Add(e ...complex128) *ComplexSlice {
*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 *ComplexSlice) Insert(index int, e ...complex128) {
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 ComplexSlice) 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 *ComplexSlice) Remove(index int) complex128 {
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 *ComplexSlice) RemoveRange(from, to int) {
*s = append((*s)[0:from], (*s)[to:]...)
}
// Fill sets the elements between from, inclusive, and to, exclusive, to a speicified value.
func (s ComplexSlice) Fill(from, to int, vl complex128) {
for i := from; i < to; i++ {
s[i] = vl
} // for i
}
// Pop removes and returns the last element in the slice
func (s *ComplexSlice) Pop() complex128 {
return s.Remove(len(*s) - 1)
}
// Clear removes all of the elements from this slice.
func (s *ComplexSlice) Clear() {
*s = (*s)[:0]
}
// Equals returns true if a given slice has the same contents (with maximum error of epsilon) with the slice
func (s ComplexSlice) Equals(t []complex128, epsilon float64) bool {
if len(s) != len(t) {
return false
} // if
for i := range s {
e := s[i] - t[i]
if imag(e) > epsilon || imag(e) < -epsilon || real(e) > epsilon || real(e) < -epsilon {
return false
} // if
} // for i
return true
}