slice.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package gago
  2. import "errors"
  3. // A Slice is a genome with a list-like structure.
  4. type Slice interface {
  5. At(i int) interface{}
  6. Set(i int, v interface{})
  7. Len() int
  8. Swap(i, j int)
  9. Slice(a, b int) Slice
  10. Split(k int) (Slice, Slice)
  11. Append(Slice) Slice
  12. Replace(Slice)
  13. Copy() Slice
  14. }
  15. // Search for the first index of an element in a Slice.
  16. func search(v interface{}, s Slice) (int, error) {
  17. for i := 0; i < s.Len(); i++ {
  18. if s.At(i) == v {
  19. return i, nil
  20. }
  21. }
  22. // Element not in slice
  23. return 0, errors.New("Value not contained in slice")
  24. }
  25. // Make a lookup table from a slice, mapping values to indexes.
  26. func newIndexLookup(s Slice) map[interface{}]int {
  27. var lookup = make(map[interface{}]int)
  28. for i := 0; i < s.Len(); i++ {
  29. lookup[s.At(i)] = i
  30. }
  31. return lookup
  32. }
  33. // getCycles determines the cycles that exist between two slices. A cycle is a
  34. // list of indexes indicating mirroring values between each slice.
  35. func getCycles(s1, s2 Slice) (cycles [][]int) {
  36. var (
  37. s1Lookup = newIndexLookup(s1) // Matches values to indexes for quick lookup
  38. visited = make(map[int]bool) // Indicates if an index is already in a cycle or not
  39. )
  40. for i := 0; i < s1.Len(); i++ {
  41. if !visited[i] {
  42. visited[i] = true
  43. var (
  44. cycle = []int{i}
  45. j = s1Lookup[s2.At(i)]
  46. )
  47. // Continue building the cycle until it closes in on itself
  48. for j != cycle[0] {
  49. cycle = append(cycle, j)
  50. visited[j] = true
  51. j = s1Lookup[s2.At(j)]
  52. }
  53. cycles = append(cycles, cycle)
  54. }
  55. }
  56. return
  57. }
  58. // getNeighbours converts a slice into an adjacency map mapping values to left
  59. // and right neighbours. The values of the map are sets.
  60. func getNeighbours(s Slice) map[interface{}]set {
  61. var (
  62. neighbours = make(map[interface{}]set)
  63. n = s.Len()
  64. )
  65. neighbours[s.At(0)] = set{s.At(n - 1): true, s.At(1): true}
  66. for i := 1; i < n-1; i++ {
  67. neighbours[s.At(i)] = set{s.At(i - 1): true, s.At(i + 1): true}
  68. }
  69. neighbours[s.At(n-1)] = set{s.At(n - 2): true, s.At(0): true}
  70. return neighbours
  71. }
  72. // IntSlice attaches the methods of Slice to []float64
  73. type IntSlice []int
  74. // At method from Slice
  75. func (s IntSlice) At(i int) interface{} {
  76. return s[i]
  77. }
  78. // Set method from Slice
  79. func (s IntSlice) Set(i int, v interface{}) {
  80. s[i] = v.(int)
  81. }
  82. // Len method from Slice
  83. func (s IntSlice) Len() int {
  84. return len(s)
  85. }
  86. // Swap method from Slice
  87. func (s IntSlice) Swap(i, j int) {
  88. s[i], s[j] = s[j], s[i]
  89. }
  90. // Slice method from Slice
  91. func (s IntSlice) Slice(a, b int) Slice {
  92. return s[a:b]
  93. }
  94. // Split method from Slice
  95. func (s IntSlice) Split(k int) (Slice, Slice) {
  96. return s[:k], s[k:]
  97. }
  98. // Append method from Slice
  99. func (s IntSlice) Append(t Slice) Slice {
  100. return append(s, t.(IntSlice)...)
  101. }
  102. // Replace method from Slice
  103. func (s IntSlice) Replace(t Slice) {
  104. copy(s, t.(IntSlice))
  105. }
  106. // Copy method from Slice
  107. func (s IntSlice) Copy() Slice {
  108. var t = make(IntSlice, len(s))
  109. copy(t, s)
  110. return t
  111. }
  112. // Float64Slice attaches the methods of Slice to []float64
  113. type Float64Slice []float64
  114. // At method from Slice
  115. func (s Float64Slice) At(i int) interface{} {
  116. return s[i]
  117. }
  118. // Set method from Slice
  119. func (s Float64Slice) Set(i int, v interface{}) {
  120. s[i] = v.(float64)
  121. }
  122. // Len method from Slice
  123. func (s Float64Slice) Len() int {
  124. return len(s)
  125. }
  126. // Swap method from Slice
  127. func (s Float64Slice) Swap(i, j int) {
  128. s[i], s[j] = s[j], s[i]
  129. }
  130. // Slice method from Slice
  131. func (s Float64Slice) Slice(a, b int) Slice {
  132. return s[a:b]
  133. }
  134. // Split method from Slice
  135. func (s Float64Slice) Split(k int) (Slice, Slice) {
  136. return s[:k], s[k:]
  137. }
  138. // Append method from Slice
  139. func (s Float64Slice) Append(t Slice) Slice {
  140. return append(s, t.(Float64Slice)...)
  141. }
  142. // Replace method from Slice
  143. func (s Float64Slice) Replace(t Slice) {
  144. copy(s, t.(Float64Slice))
  145. }
  146. // Copy method from Slice
  147. func (s Float64Slice) Copy() Slice {
  148. var t = make(Float64Slice, len(s))
  149. copy(t, s)
  150. return t
  151. }
  152. // StringSlice attaches the methods of Slice to []float64
  153. type StringSlice []string
  154. // At method from Slice
  155. func (s StringSlice) At(i int) interface{} {
  156. return s[i]
  157. }
  158. // Set method from Slice
  159. func (s StringSlice) Set(i int, v interface{}) {
  160. s[i] = v.(string)
  161. }
  162. // Len method from Slice
  163. func (s StringSlice) Len() int {
  164. return len(s)
  165. }
  166. // Swap method from Slice
  167. func (s StringSlice) Swap(i, j int) {
  168. s[i], s[j] = s[j], s[i]
  169. }
  170. // Slice method from Slice
  171. func (s StringSlice) Slice(a, b int) Slice {
  172. return s[a:b]
  173. }
  174. // Split method from Slice
  175. func (s StringSlice) Split(k int) (Slice, Slice) {
  176. return s[:k], s[k:]
  177. }
  178. // Append method from Slice
  179. func (s StringSlice) Append(t Slice) Slice {
  180. return append(s, t.(StringSlice)...)
  181. }
  182. // Replace method from Slice
  183. func (s StringSlice) Replace(t Slice) {
  184. copy(s, t.(StringSlice))
  185. }
  186. // Copy method from Slice
  187. func (s StringSlice) Copy() Slice {
  188. var t = make(StringSlice, len(s))
  189. copy(t, s)
  190. return t
  191. }