forked from hyper0x/goc2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq.go
138 lines (117 loc) · 2.59 KB
/
seq.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
131
132
133
134
135
136
137
138
package basic
import (
"reflect"
"sort"
)
type Sortable interface {
sort.Interface
Sort()
}
type GenericSeq interface {
Sortable
Append(e interface{}) bool
Set(index int, e interface{}) bool
Delete(index int) (interface{}, bool)
ElemValue(index int) interface{}
ElemType() reflect.Type
Value() interface{}
}
type StringSeq struct {
value []string
}
func (self *StringSeq) Len() int {
return len(self.value)
}
func (self *StringSeq) Less(i, j int) bool {
return self.value[i] < self.value[j]
}
func (self *StringSeq) Swap(i, j int) {
self.value[i], self.value[j] = self.value[j], self.value[i]
}
func (self *StringSeq) Sort() {
sort.Sort(self)
}
func (self *StringSeq) Append(e interface{}) bool {
s, ok := e.(string)
if !ok {
return false
}
self.value = append(self.value, s)
return true
}
func (self *StringSeq) Set(index int, e interface{}) bool {
if index >= self.Len() {
return false
}
s, ok := e.(string)
if !ok {
return false
}
self.value[index] = s
return true
}
func (self *StringSeq) Delete(index int) (interface{}, bool) {
length := self.Len()
if index >= length {
return nil, false
}
s := self.value[index]
if index < (length - 1) {
copy(self.value[index:], self.value[index+1:])
}
invalidIndex := length - 1
self.value[invalidIndex] = ""
self.value = self.value[:invalidIndex]
return s, true
}
func (self StringSeq) ElemValue(index int) interface{} {
if index >= self.Len() {
return nil
}
return self.value[index]
}
func (self *StringSeq) ElemType() reflect.Type {
return reflect.TypeOf(self.value).Elem()
}
func (self StringSeq) Value() interface{} {
return self.value
}
type Sequence struct {
GenericSeq
sorted bool
elemType reflect.Type
}
func (self *Sequence) Sort() {
self.GenericSeq.Sort()
self.sorted = true
}
func (self *Sequence) Append(e interface{}) bool {
result := self.GenericSeq.Append(e)
if result && self.sorted {
self.sorted = false
}
return result
}
func (self *Sequence) Set(index int, e interface{}) bool {
result := self.GenericSeq.Set(index, e)
if result && self.sorted {
self.sorted = false
}
return result
}
func (self *Sequence) ElemType() reflect.Type {
if self.elemType == nil && self.GenericSeq != nil {
self.elemType = self.GenericSeq.ElemType()
}
return self.elemType
}
func (self *Sequence) Init() (ok bool) {
if self.GenericSeq != nil {
self.elemType = self.GenericSeq.ElemType()
ok = true
}
return ok
}
func (self *Sequence) Sorted() bool {
return self.sorted
}