-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfretboard_layout.go
78 lines (65 loc) · 1.74 KB
/
fretboard_layout.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
package musicgo
import (
"bytes"
"sync"
)
type StringIndex int
type FretboardLayout struct {
strings []FretboardString
}
type fretboardLayoutHashConsCacheType struct {
cache []*FretboardLayout
mutex sync.Mutex
}
func (flc *fretboardLayoutHashConsCacheType) HashCons(fl *FretboardLayout) *FretboardLayout {
flc.mutex.Lock()
for _, existingLayout := range flc.cache {
existingStrings := existingLayout.strings
if len(existingStrings) != len(fl.strings) {
continue
}
foundDifference := false
for i, existingString := range existingLayout.strings {
if fl.strings[i] != existingString {
foundDifference = true
break
}
}
if !foundDifference {
flc.mutex.Unlock()
return existingLayout
}
}
flc.cache = append(flc.cache, fl)
flc.mutex.Unlock()
return fl
}
var fretboardLayoutCache fretboardLayoutHashConsCacheType = fretboardLayoutHashConsCacheType{}
func NewFretboardLayout(strings ...FretboardString) *FretboardLayout {
return fretboardLayoutCache.HashCons(&FretboardLayout{strings: strings})
}
func (fl *FretboardLayout) String() string {
var buf bytes.Buffer
for i, strng := range fl.strings {
if i > 0 {
buf.WriteRune(' ')
}
buf.WriteString(strng.String())
}
return buf.String()
}
func (fl *FretboardLayout) Position(String StringIndex, Fret FretboardOffset) FretboardCoordinate {
if int(String) < 0 || int(String) >= len(fl.strings) {
panic("string index out of bounds")
}
if Fret < 0 {
panic("fretboard offset much be non-negative")
}
return FretboardCoordinate{fretboardString: String, fret: Fret, fretboardLayout: fl}
}
func (fl *FretboardLayout) NumString() int {
return len(fl.strings)
}
func (fl *FretboardLayout) FretboardString(index StringIndex) FretboardString {
return fl.strings[index]
}