-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontainer.go
126 lines (99 loc) · 2.27 KB
/
container.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
package errors
import (
"sync"
)
var container = &classContainer{}
// classContainer is the container for the subclass boundaries and definitions.
type classContainer struct {
sync.Mutex
major Major
minors []Minor
indexes [][]Index
}
func (c *classContainer) newMajor() (Major, error) {
c.Lock()
defer c.Unlock()
if c.major+1 == 0 {
return 0, New(ClInvalidMajor, "reached maximum number of 'Major' classes")
}
c.major++
c.resizeMinors(c.major)
c.resizeIndexesMajor(c.major)
return c.major, nil
}
func (c *classContainer) newMinor(mjr Major) (Minor, error) {
c.Lock()
defer c.Unlock()
if !mjr.Valid() {
return 0, New(ClInvalidMajor, "provided invalid Major")
}
c.resizeMinors(mjr)
if c.minors[mjr] == maxMinorValue {
return 0, Newf(ClInvalidMinor, "created maximum number of minors for major: '%d'", mjr)
}
c.minors[mjr]++
c.resizeIndexesMinors(mjr, c.minors[mjr])
return c.minors[mjr], nil
}
func (c *classContainer) newIndex(mjr Major, mnr Minor) (Index, error) {
c.Lock()
defer c.Unlock()
if !mjr.Valid() {
return 0, New(ClInvalidMajor, "provided invalid Major")
}
if !mnr.Valid() {
return 0, New(ClInvalidMinor, "provided invalid Minor")
}
c.resizeIndexesMajor(mjr)
c.resizeIndexesMinors(mjr, mnr)
if c.indexes[mjr][mnr] == maxIndexValue {
return 0, Newf(ClInvalidIndex, "reached maximum index subclass number for mjr: '%d', mnr: '%d'", mjr, mnr)
}
c.indexes[mjr][mnr]++
return c.indexes[mjr][mnr], nil
}
func (c *classContainer) resizeMinors(mjr Major) {
if int(mjr) < len(c.minors)-2 {
return
}
size := len(c.minors) - 2
if size <= 0 {
size = 4
}
for size <= int(mjr) {
size *= 2
}
temp := make([]Minor, size)
copy(temp, c.minors)
c.minors = temp
}
func (c *classContainer) resizeIndexesMajor(mjr Major) {
if int(mjr) <= len(c.indexes)-2 {
return
}
size := len(c.indexes) - 2
if size <= 0 {
size = 4
}
for size <= int(mjr) {
size *= 2
}
temp := make([][]Index, size)
copy(temp, c.indexes)
c.indexes = temp
}
func (c *classContainer) resizeIndexesMinors(mjr Major, mnr Minor) {
if int(mnr) <= len(c.indexes[mjr])-2 {
return
}
size := len(c.indexes[mjr]) - 2
if size <= 0 {
size = 4
}
for size <= int(mnr) {
size *= 2
}
temp := make([]Index, size)
copy(temp, c.indexes[mjr])
c.indexes[mjr] = temp
}