forked from sjwhitworth/golearn
/
fixed.go
100 lines (83 loc) · 2.45 KB
/
fixed.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
package base
import (
"bytes"
"fmt"
)
// FixedAttributeGroups contain a particular number of rows of
// a particular number of Attributes, all of a given type.
type FixedAttributeGroup struct {
parent DataGrid
attributes []Attribute
size int
alloc []byte
maxRow int
}
// String gets a human-readable summary
func (f *FixedAttributeGroup) String() string {
return "FixedAttributeGroup"
}
// RowSizeInBytes returns the size of each row in bytes
func (f *FixedAttributeGroup) RowSizeInBytes() int {
return len(f.attributes) * f.size
}
// Attributes returns a slice of Attributes in this FixedAttributeGroup
func (f *FixedAttributeGroup) Attributes() []Attribute {
ret := make([]Attribute, len(f.attributes))
// Add Attributes
for i, a := range f.attributes {
ret[i] = a
}
return ret
}
// AddAttribute adds an attribute to this FixedAttributeGroup
func (f *FixedAttributeGroup) AddAttribute(a Attribute) error {
f.attributes = append(f.attributes, a)
return nil
}
// addStorage appends the given storage reference to this FixedAttributeGroup
func (f *FixedAttributeGroup) setStorage(a []byte) {
f.alloc = a
}
// Storage returns a slice of FixedAttributeGroupStorageRefs which can
// be used to access the memory in this pond.
func (f *FixedAttributeGroup) Storage() []byte {
return f.alloc
}
func (f *FixedAttributeGroup) offset(col, row int) int {
return row*f.RowSizeInBytes() + col*f.size
}
func (f *FixedAttributeGroup) set(col int, row int, val []byte) {
// Double-check the length
if len(val) != f.size {
panic(fmt.Sprintf("Tried to call set() with %d bytes, should be %d", len(val), f.size))
}
// Find where in the pond the byte is
offset := f.offset(col, row)
// Copy the value in
copied := copy(f.alloc[offset:], val)
if copied != f.size {
panic(fmt.Sprintf("set() terminated by only copying %d bytes, should be %d", copied, f.size))
}
row++
if row > f.maxRow {
f.maxRow = row
}
}
func (f *FixedAttributeGroup) get(col int, row int) []byte {
offset := f.offset(col, row)
return f.alloc[offset : offset+f.size]
}
func (f *FixedAttributeGroup) appendToRowBuf(row int, buffer *bytes.Buffer) {
for i, a := range f.attributes {
postfix := " "
if i == len(f.attributes)-1 {
postfix = ""
}
buffer.WriteString(fmt.Sprintf("%s%s", a.GetStringFromSysVal(f.get(i, row)), postfix))
}
}
func (f *FixedAttributeGroup) resize(add int) {
newAlloc := make([]byte, len(f.alloc)+add)
copy(newAlloc, f.alloc)
f.alloc = newAlloc
}