refactor: implement Order and Pattern types: slices returning default values for out of bound indices

This commit is contained in:
vsariola
2021-05-12 11:31:38 +03:00
parent ce6e5d4942
commit a2723829da
9 changed files with 68 additions and 51 deletions

21
order.go Normal file
View File

@ -0,0 +1,21 @@
package sointu
// Order is the pattern order for a track, in practice just a slice of integers,
// but provides convenience functions that return -1 values for indices out of
// bounds of the array, and functions to increase the size of the slice only by
// necessary amount when a new item is added, filling the unused slots with -1s.
type Order []int
func (s Order) Get(index int) int {
if index < 0 || index >= len(s) {
return -1
}
return s[index]
}
func (s *Order) Set(index, value int) {
for len(*s) <= index {
*s = append(*s, -1)
}
(*s)[index] = value
}