-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparss.go
More file actions
179 lines (155 loc) · 3.91 KB
/
Copy pathsparss.go
File metadata and controls
179 lines (155 loc) · 3.91 KB
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
package sparss
import (
"fmt"
"sort"
)
const (
DefaultEmptyValue = int(0)
ForbiddenValue = maxInt
maxUInt = ^uint(0)
maxInt = int(maxUInt >> 1)
)
type Table struct {
entries []int
numOfEntries int
numOfRows int
numOfCols int
rowLen int
emptyValue int
}
func NewTable(entries []int, rowLen int, options ...TableOption) (*Table, error) {
if len(entries) <= 0 {
return nil, fmt.Errorf("len(entries) must be >= 1")
}
if rowLen <= 0 {
return nil, fmt.Errorf("rowLen must be >=1")
}
if len(entries)%rowLen != 0 {
return nil, fmt.Errorf("len(entries) %% rowLen must be 0")
}
numOfRows := len(entries) / rowLen
numOfCols := len(entries) / numOfRows
t := &Table{
entries: entries,
numOfEntries: len(entries),
numOfRows: numOfRows,
numOfCols: numOfCols,
rowLen: rowLen,
emptyValue: DefaultEmptyValue,
}
for _, option := range options {
err := option(t)
if err != nil {
return nil, fmt.Errorf("failed to apply TableOption functions to a Table object; error: %w", err)
}
}
return t, nil
}
type TableOption func(t *Table) error
func EmptyValue(e int) TableOption {
return func(t *Table) error {
t.emptyValue = e
return nil
}
}
type RDResult struct {
OrigNumOfRows int
OrigNumOfCols int
EmptyEntry int
Entries []int
Bounds []int
RowDisplacement []int
}
func (r *RDResult) Lookup(row int, col int) (int, error) {
if row < 0 || row >= r.OrigNumOfRows || col < 0 || col >= r.OrigNumOfCols {
err := fmt.Errorf("out of range; table size: %vx%v, accessed: (%v, %v)", r.OrigNumOfRows, r.OrigNumOfCols, row, col)
return r.EmptyEntry, err
}
if r.Bounds[r.RowDisplacement[row]+col] != row {
return r.EmptyEntry, nil
}
return r.Entries[r.RowDisplacement[row]+col], nil
}
type RDCompressor struct {
}
func NewRDCompressor() (*RDCompressor, error) {
return &RDCompressor{}, nil
}
type rowInfo struct {
rowNum int
nonEmptyCount int
nonEmptyCol []int
}
func (c *RDCompressor) Compress(origTable *Table) (*RDResult, error) {
if origTable.numOfEntries <= 0 {
return nil, fmt.Errorf("table is empty")
}
rowInfo := make([]rowInfo, origTable.numOfRows)
{
row := 0
col := 0
rowInfo[0].rowNum = 0
for _, v := range origTable.entries {
if col == origTable.rowLen {
row++
col = 0
rowInfo[row].rowNum = row
}
if v != origTable.emptyValue {
rowInfo[row].nonEmptyCount++
rowInfo[row].nonEmptyCol = append(rowInfo[row].nonEmptyCol, col)
}
col++
}
sort.SliceStable(rowInfo, func(i int, j int) bool {
return rowInfo[i].nonEmptyCount > rowInfo[j].nonEmptyCount
})
}
entries := make([]int, origTable.numOfEntries)
bounds := make([]int, origTable.numOfEntries)
resultBottom := origTable.rowLen
rowDisplacement := make([]int, origTable.numOfRows)
{
for i := 0; i < origTable.numOfEntries; i++ {
entries[i] = origTable.emptyValue
bounds[i] = ForbiddenValue
}
nextRowDisplacement := 0
for _, rInfo := range rowInfo {
if rInfo.nonEmptyCount <= 0 {
continue
}
for {
isOverlapped := false
for _, col := range rInfo.nonEmptyCol {
if entries[nextRowDisplacement+col] == origTable.emptyValue {
continue
}
nextRowDisplacement++
isOverlapped = true
break
}
if isOverlapped {
continue
}
rowDisplacement[rInfo.rowNum] = nextRowDisplacement
for _, col := range rInfo.nonEmptyCol {
entries[nextRowDisplacement+col] = origTable.entries[(rInfo.rowNum*origTable.rowLen)+col]
bounds[nextRowDisplacement+col] = rInfo.rowNum
}
resultBottom = nextRowDisplacement + origTable.rowLen
nextRowDisplacement++
break
}
}
}
result := &RDResult{
OrigNumOfRows: origTable.numOfRows,
OrigNumOfCols: origTable.numOfCols,
EmptyEntry: origTable.emptyValue,
Entries: entries[0:resultBottom],
Bounds: bounds[0:resultBottom],
RowDisplacement: rowDisplacement,
}
return result, nil
}