...
1 package matrix
2
3 import "sync"
4
5 type amatrix struct {
6 sync.Mutex
7 rows []*matrixrow
8 col_names []string
9 }
10 type matrixrow struct {
11 matrix *amatrix
12 name string
13 cells []*matrixcell
14 }
15 type matrixcell struct {
16 content interface{}
17 }
18
19 func (m *amatrix) GetColumnNames() []string {
20 return m.col_names
21 }
22 func (m *amatrix) SetCellByName(rowname, colname string, content interface{}) {
23 row := m.GetRowByName(rowname)
24 row.SetCellByHeader(colname, content)
25 }
26
27 func (m *amatrix) GetRowByName(name string) *matrixrow {
28 m.Lock()
29 defer m.Unlock()
30 for _, r := range m.rows {
31 if r.name == name {
32 return r
33 }
34 }
35 row := &matrixrow{matrix: m, name: name}
36 m.rows = append(m.rows, row)
37 return row
38 }
39
40 func (r *matrixrow) SetCellByHeader(header string, status interface{}) {
41 r.matrix.Lock()
42 defer r.matrix.Unlock()
43 col := -1
44 for i, head := range r.matrix.col_names {
45 if head == header {
46 col = i
47 break
48 }
49 }
50 if col == -1 {
51 r.matrix.col_names = append(r.matrix.col_names, header)
52 col = len(r.matrix.col_names) - 1
53 }
54 r.SetCell(col, status)
55 }
56 func (m *amatrix) Rows() []*matrixrow {
57 return m.rows
58 }
59 func (r *matrixrow) Name() string {
60 return r.name
61 }
62 func (r *matrixrow) Cells() []*matrixcell {
63 return r.cells
64 }
65 func (r *matrixrow) SetCell(col int, c interface{}) {
66 for len(r.cells) <= col {
67 r.cells = append(r.cells, &matrixcell{})
68 }
69 r.cells[col].content = c
70 }
71 func (c *matrixcell) Content() interface{} {
72 return c.content
73 }
74
View as plain text