-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_enum.go
450 lines (359 loc) · 10.1 KB
/
signal_enum.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package acmelib
import (
"fmt"
"slices"
"strings"
)
// SignalEnum is the representation of an enum that can be assigned
// to a signal.
type SignalEnum struct {
*entity
*withRefs[*EnumSignal]
parErrID EntityID
values *set[EntityID, *SignalEnumValue]
valueNames *set[string, EntityID]
valueIndexes *set[int, EntityID]
maxIndex int
minSize int
}
func newSignalEnumFromEntity(ent *entity) *SignalEnum {
return &SignalEnum{
entity: ent,
withRefs: newWithRefs[*EnumSignal](),
parErrID: "",
values: newSet[EntityID, *SignalEnumValue](),
valueNames: newSet[string, EntityID](),
valueIndexes: newSet[int, EntityID](),
maxIndex: 0,
minSize: 1,
}
}
// NewSignalEnum creates a new [SignalEnum] with the given name.
func NewSignalEnum(name string) *SignalEnum {
return newSignalEnumFromEntity(newEntity(name, EntityKindSignalEnum))
}
// Clone creates a new [SignalEnum] with the same values as the current one.
func (se *SignalEnum) Clone() (*SignalEnum, error) {
cloned := newSignalEnumFromEntity(se.entity.clone())
for _, tmpVal := range se.values.getValues() {
if err := cloned.AddValue(tmpVal.Clone()); err != nil {
return nil, err
}
}
return cloned, nil
}
func (se *SignalEnum) errorf(err error) error {
enumErr := &EntityError{
Kind: EntityKindSignalEnum,
EntityID: se.entityID,
Name: se.name,
Err: err,
}
if se.refs.size() > 0 {
if se.parErrID != "" {
parSig, err := se.refs.getValue(se.parErrID)
if err != nil {
panic(err)
}
se.parErrID = ""
return parSig.errorf(enumErr)
}
return se.refs.getValues()[0].errorf(enumErr)
}
return enumErr
}
func (se *SignalEnum) verifyValueName(name string) error {
err := se.valueNames.verifyKeyUnique(name)
if err != nil {
return &NameError{
Name: name,
Err: err,
}
}
return nil
}
func (se *SignalEnum) verifyValueIndex(index int) error {
if err := se.valueIndexes.verifyKeyUnique(index); err != nil {
return err
}
if index > se.maxIndex {
prevSize := se.GetSize()
newSize := calcSizeFromValue(index)
for _, tmpSig := range se.refs.entries() {
if tmpSig.hasParentMsg() {
if err := tmpSig.parentMsg.verifySignalSizeAmount(tmpSig.entityID, newSize-prevSize); err != nil {
se.parErrID = tmpSig.entityID
return &ValueIndexError{
Index: index,
Err: err,
}
}
}
if tmpSig.hasParentMuxSig() {
if err := tmpSig.parentMuxSig.verifySignalSizeAmount(tmpSig.entityID, newSize-prevSize); err != nil {
se.parErrID = tmpSig.entityID
return &ValueIndexError{
Index: index,
Err: err,
}
}
}
}
}
return nil
}
func (se *SignalEnum) modifyValueIndex(value *SignalEnumValue, newIndex int) {
gtMaxIndex := false
if maxSize > se.maxIndex {
gtMaxIndex = true
}
updateMaxIdx := false
if value.index == se.maxIndex && newIndex < se.maxIndex {
updateMaxIdx = true
}
if gtMaxIndex || updateMaxIdx {
amount := calcSizeFromValue(newIndex) - se.GetSize()
for _, tmpSig := range se.refs.entries() {
if err := tmpSig.modifySize(amount); err != nil {
panic(err)
}
}
if gtMaxIndex {
se.maxIndex = newIndex
} else {
se.setMaxIndex()
}
}
oldIndex := value.index
se.valueIndexes.modifyKey(oldIndex, newIndex, value.entityID)
}
func (se *SignalEnum) setMaxIndex() {
currMax := 0
for _, tmpVal := range se.values.entries() {
tmpIdx := tmpVal.Index()
if tmpIdx > currMax {
currMax = tmpIdx
}
}
se.maxIndex = currMax
}
// UpdateName updates the name of the [SignalEnum] to the given new one.
func (se *SignalEnum) UpdateName(newName string) {
se.name = newName
}
func (se *SignalEnum) stringify(b *strings.Builder, tabs int) {
se.entity.stringify(b, tabs)
tabStr := getTabString(tabs)
b.WriteString(fmt.Sprintf("%smax_index: %d\n", tabStr, se.maxIndex))
if se.values.size() == 0 {
return
}
b.WriteString(fmt.Sprintf("%svalues:\n", tabStr))
for _, enumVal := range se.Values() {
enumVal.stringify(b, tabs+1)
b.WriteRune('\n')
}
refCount := se.ReferenceCount()
if refCount > 0 {
b.WriteString(fmt.Sprintf("%sreference_count: %d\n", tabStr, refCount))
}
}
func (se *SignalEnum) String() string {
builder := new(strings.Builder)
se.stringify(builder, 0)
return builder.String()
}
// AddValue adds the given [SignalEnumValue] to the [SignalEnum].
// It may return an error if the value name is already in use within
// the signal enum, or if it has an invalid index.
func (se *SignalEnum) AddValue(value *SignalEnumValue) error {
if value == nil {
return &ArgumentError{
Name: "value",
Err: ErrIsNil,
}
}
addValErr := &AddEntityError{
EntityID: value.entityID,
Name: value.name,
}
if err := se.verifyValueIndex(value.index); err != nil {
addValErr.Err = err
return se.errorf(addValErr)
}
if err := se.verifyValueName(value.name); err != nil {
addValErr.Err = err
return se.errorf(addValErr)
}
index := value.index
if index > se.maxIndex {
se.maxIndex = index
}
se.values.add(value.entityID, value)
se.valueNames.add(value.name, value.entityID)
se.valueIndexes.add(value.index, value.entityID)
value.setParentEnum(se)
return nil
}
// RemoveValue removes the [SignalEnumValue] with the given entity id from the [SignalEnum].
// It may return an error if the value with the given entity id is not found.
func (se *SignalEnum) RemoveValue(valueEntityID EntityID) error {
val, err := se.values.getValue(valueEntityID)
if err != nil {
return se.errorf(&RemoveEntityError{
EntityID: valueEntityID,
Err: err,
})
}
valIdx := val.index
wasMaxIndex := false
if valIdx == se.maxIndex {
wasMaxIndex = true
}
val.setParentEnum(nil)
se.values.remove(valueEntityID)
se.valueNames.remove(val.name)
se.valueIndexes.remove(val.index)
if wasMaxIndex {
se.setMaxIndex()
}
return nil
}
// RemoveAllValues removes all enum values from the [SignalEnum].
func (se *SignalEnum) RemoveAllValues() {
for _, tmpVal := range se.values.entries() {
tmpVal.setParentEnum(nil)
}
se.values.clear()
se.valueNames.clear()
se.valueIndexes.clear()
}
// Values returns a slice of all the enum values of the [SignalEnum].
func (se *SignalEnum) Values() []*SignalEnumValue {
valueSlice := se.values.getValues()
slices.SortFunc(valueSlice, func(a *SignalEnumValue, b *SignalEnumValue) int { return a.index - b.index })
return valueSlice
}
// GetValue returns the [SignalEnumValue] with the given entity id.
//
// It returns a [GetEntityError] if the value with the given entity id is not found.
func (se *SignalEnum) GetValue(valueEntityID EntityID) (*SignalEnumValue, error) {
val, err := se.values.getValue(valueEntityID)
if err != nil {
return nil, se.errorf(&GetEntityError{
EntityID: valueEntityID,
Err: err,
})
}
return val, nil
}
// GetSize returns the size of the [SignalEnum] in bits.
func (se *SignalEnum) GetSize() int {
maxIdxSize := calcSizeFromValue(se.maxIndex)
if se.minSize > maxIdxSize {
return se.minSize
}
return maxIdxSize
}
// MaxIndex returns the highest index of the enum values of the [SignalEnum].
func (se *SignalEnum) MaxIndex() int {
return se.maxIndex
}
// SetMinSize sets the minimum size in bit of the [SignalEnum].
// By defaul it is set to 1.
func (se *SignalEnum) SetMinSize(minSize int) {
se.minSize = minSize
}
// MinSize return the minimum size of the [SignalEnum] in bits.
func (se *SignalEnum) MinSize() int {
return se.minSize
}
// SignalEnumValue holds the key (name) and the value (index) of a signal enum
// entry.
type SignalEnumValue struct {
*entity
parentEnum *SignalEnum
index int
}
func newSignalEnumValueFromEntity(ent *entity, index int) *SignalEnumValue {
return &SignalEnumValue{
entity: ent,
parentEnum: nil,
index: index,
}
}
// NewSignalEnumValue creates a new [SignalEnumValue] with the given name and index.
func NewSignalEnumValue(name string, index int) *SignalEnumValue {
return newSignalEnumValueFromEntity(newEntity(name, EntityKindSignalEnumValue), index)
}
// Clone creates a new [SignalEnumValue] with the values as the current one.
func (sev *SignalEnumValue) Clone() *SignalEnumValue {
return newSignalEnumValueFromEntity(sev.entity.clone(), sev.index)
}
func (sev *SignalEnumValue) hasParentEnum() bool {
return sev.parentEnum != nil
}
func (sev *SignalEnumValue) setParentEnum(enum *SignalEnum) {
sev.parentEnum = enum
}
func (sev *SignalEnumValue) errorf(err error) error {
enumValErr := &EntityError{
Kind: EntityKindSignalEnumValue,
EntityID: sev.entityID,
Name: sev.name,
Err: err,
}
if sev.hasParentEnum() {
return sev.parentEnum.errorf(enumValErr)
}
return enumValErr
}
func (sev *SignalEnumValue) stringify(b *strings.Builder, tabs int) {
sev.entity.stringify(b, tabs)
tabStr := getTabString(tabs)
b.WriteString(fmt.Sprintf("%sindex: %d\n", tabStr, sev.index))
}
func (sev *SignalEnumValue) String() string {
builder := new(strings.Builder)
sev.stringify(builder, 0)
return builder.String()
}
// UpdateName updates the name of the [SignalEnumValue] to the given new one.
// It may return an error if the new name is already in use within the parent enum.
func (sev *SignalEnumValue) UpdateName(newName string) error {
if sev.name == newName {
return nil
}
if sev.hasParentEnum() {
if err := sev.parentEnum.verifyValueName(newName); err != nil {
return sev.errorf(&UpdateNameError{Err: err})
}
sev.parentEnum.valueNames.modifyKey(sev.name, newName, sev.entityID)
}
sev.name = newName
return nil
}
// ParentEnum returns the parent [SignalEnum] of the [SignalEnumValue],
// or nil if not set.
func (sev *SignalEnumValue) ParentEnum() *SignalEnum {
return sev.parentEnum
}
// UpdateIndex updates the index of the [SignalEnumValue] to the given new one.
// It may return an error if the new index is invalid.
func (sev *SignalEnumValue) UpdateIndex(newIndex int) error {
if sev.index == newIndex {
return nil
}
if sev.hasParentEnum() {
if err := sev.parentEnum.verifyValueIndex(newIndex); err != nil {
return sev.errorf(&UpdateIndexError{Err: err})
}
sev.parentEnum.modifyValueIndex(sev, newIndex)
}
sev.index = newIndex
return nil
}
// Index returns the index of the [SignalEnumValue].
func (sev *SignalEnumValue) Index() int {
return sev.index
}