This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
forked from MaxHalford/eaopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
227 lines (191 loc) · 4.75 KB
/
slice.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package eaopt
import "errors"
// A Slice is a genome with a list-like structure.
type Slice interface {
At(i int) interface{}
Set(i int, v interface{})
Len() int
Swap(i, j int)
Slice(a, b int) Slice
Split(k int) (Slice, Slice)
Append(Slice) Slice
Replace(Slice)
Copy() Slice
}
// Search for the first index of an element in a Slice.
func search(v interface{}, s Slice) (int, error) {
for i := 0; i < s.Len(); i++ {
if s.At(i) == v {
return i, nil
}
}
// Element not in slice
return 0, errors.New("Value not contained in slice")
}
// Make a lookup table from a slice, mapping values to indexes.
func newIndexLookup(s Slice) map[interface{}]int {
var lookup = make(map[interface{}]int)
for i := 0; i < s.Len(); i++ {
lookup[s.At(i)] = i
}
return lookup
}
// getCycles determines the cycles that exist between two slices. A cycle is a
// list of indexes indicating mirroring values between each slice.
func getCycles(s1, s2 Slice) (cycles [][]int) {
var (
s1Lookup = newIndexLookup(s1) // Matches values to indexes for quick lookup
visited = make(map[int]bool) // Indicates if an index is already in a cycle or not
)
for i := 0; i < s1.Len(); i++ {
if !visited[i] {
visited[i] = true
var (
cycle = []int{i}
j = s1Lookup[s2.At(i)]
)
// Continue building the cycle until it closes in on itself
for j != cycle[0] {
cycle = append(cycle, j)
visited[j] = true
j = s1Lookup[s2.At(j)]
}
cycles = append(cycles, cycle)
}
}
return
}
// getNeighbours converts a slice into an adjacency map mapping values to left
// and right neighbours. The values of the map are sets.
func getNeighbours(s Slice) map[interface{}]set {
var (
neighbours = make(map[interface{}]set)
n = s.Len()
)
neighbours[s.At(0)] = set{s.At(n - 1): true, s.At(1): true}
for i := 1; i < n-1; i++ {
neighbours[s.At(i)] = set{s.At(i - 1): true, s.At(i + 1): true}
}
neighbours[s.At(n-1)] = set{s.At(n - 2): true, s.At(0): true}
return neighbours
}
// IntSlice attaches the methods of Slice to []float64
type IntSlice []int
// At method from Slice
func (s IntSlice) At(i int) interface{} {
return s[i]
}
// Set method from Slice
func (s IntSlice) Set(i int, v interface{}) {
s[i] = v.(int)
}
// Len method from Slice
func (s IntSlice) Len() int {
return len(s)
}
// Swap method from Slice
func (s IntSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Slice method from Slice
func (s IntSlice) Slice(a, b int) Slice {
return s[a:b]
}
// Split method from Slice
func (s IntSlice) Split(k int) (Slice, Slice) {
return s[:k], s[k:]
}
// Append method from Slice
func (s IntSlice) Append(t Slice) Slice {
return append(s, t.(IntSlice)...)
}
// Replace method from Slice
func (s IntSlice) Replace(t Slice) {
copy(s, t.(IntSlice))
}
// Copy method from Slice
func (s IntSlice) Copy() Slice {
var t = make(IntSlice, len(s))
copy(t, s)
return t
}
// Float64Slice attaches the methods of Slice to []float64
type Float64Slice []float64
// At method from Slice
func (s Float64Slice) At(i int) interface{} {
return s[i]
}
// Set method from Slice
func (s Float64Slice) Set(i int, v interface{}) {
s[i] = v.(float64)
}
// Len method from Slice
func (s Float64Slice) Len() int {
return len(s)
}
// Swap method from Slice
func (s Float64Slice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Slice method from Slice
func (s Float64Slice) Slice(a, b int) Slice {
return s[a:b]
}
// Split method from Slice
func (s Float64Slice) Split(k int) (Slice, Slice) {
return s[:k], s[k:]
}
// Append method from Slice
func (s Float64Slice) Append(t Slice) Slice {
return append(s, t.(Float64Slice)...)
}
// Replace method from Slice
func (s Float64Slice) Replace(t Slice) {
copy(s, t.(Float64Slice))
}
// Copy method from Slice
func (s Float64Slice) Copy() Slice {
var t = make(Float64Slice, len(s))
copy(t, s)
return t
}
// StringSlice attaches the methods of Slice to []float64
type StringSlice []string
// At method from Slice
func (s StringSlice) At(i int) interface{} {
return s[i]
}
// Set method from Slice
func (s StringSlice) Set(i int, v interface{}) {
s[i] = v.(string)
}
// Len method from Slice
func (s StringSlice) Len() int {
return len(s)
}
// Swap method from Slice
func (s StringSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Slice method from Slice
func (s StringSlice) Slice(a, b int) Slice {
return s[a:b]
}
// Split method from Slice
func (s StringSlice) Split(k int) (Slice, Slice) {
return s[:k], s[k:]
}
// Append method from Slice
func (s StringSlice) Append(t Slice) Slice {
return append(s, t.(StringSlice)...)
}
// Replace method from Slice
func (s StringSlice) Replace(t Slice) {
copy(s, t.(StringSlice))
}
// Copy method from Slice
func (s StringSlice) Copy() Slice {
var t = make(StringSlice, len(s))
copy(t, s)
return t
}