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
|
package compressor
import (
"encoding/binary"
"fmt"
"sort"
)
type OriginalTable struct {
entries []int
rowCount int
colCount int
}
func NewOriginalTable(entries []int, colCount int) (*OriginalTable, error) {
if len(entries) == 0 {
return nil, fmt.Errorf("enries is empty")
}
if colCount <= 0 {
return nil, fmt.Errorf("colCount must be >=1")
}
if len(entries)%colCount != 0 {
return nil, fmt.Errorf("entries length or column count are incorrect; entries length: %v, column count: %v", len(entries), colCount)
}
return &OriginalTable{
entries: entries,
rowCount: len(entries) / colCount,
colCount: colCount,
}, nil
}
type Compressor interface {
Compress(orig *OriginalTable) error
Lookup(row, col int) (int, error)
OriginalTableSize() (int, int)
}
var (
_ Compressor = &UniqueEntriesTable{}
_ Compressor = &RowDisplacementTable{}
)
type UniqueEntriesTable struct {
UniqueEntries []int
RowNums []int
OriginalRowCount int
OriginalColCount int
}
func NewUniqueEntriesTable() *UniqueEntriesTable {
return &UniqueEntriesTable{}
}
func (tab *UniqueEntriesTable) Lookup(row, col int) (int, error) {
if row < 0 || row >= tab.OriginalRowCount || col < 0 || col >= tab.OriginalColCount {
return 0, fmt.Errorf("indexes are out of range: [%v, %v]", row, col)
}
return tab.UniqueEntries[tab.RowNums[row]*tab.OriginalColCount+col], nil
}
func (tab *UniqueEntriesTable) OriginalTableSize() (int, int) {
return tab.OriginalRowCount, tab.OriginalColCount
}
func (tab *UniqueEntriesTable) Compress(orig *OriginalTable) error {
var uniqueEntries []int
rowNums := make([]int, orig.rowCount)
hash2RowNum := map[string]int{}
nextRowNum := 0
for row := 0; row < orig.rowCount; row++ {
var rowHash string
{
buf := make([]byte, 0, orig.colCount*8)
for col := 0; col < orig.colCount; col++ {
b := make([]byte, 8)
binary.PutUvarint(b, uint64(orig.entries[row*orig.colCount+col]))
buf = append(buf, b...)
}
rowHash = string(buf)
}
rowNum, ok := hash2RowNum[rowHash]
if !ok {
rowNum = nextRowNum
nextRowNum++
hash2RowNum[rowHash] = rowNum
start := row * orig.colCount
entry := append([]int{}, orig.entries[start:start+orig.colCount]...)
uniqueEntries = append(uniqueEntries, entry...)
}
rowNums[row] = rowNum
}
tab.UniqueEntries = uniqueEntries
tab.RowNums = rowNums
tab.OriginalRowCount = orig.rowCount
tab.OriginalColCount = orig.colCount
return nil
}
const ForbiddenValue = -1
type RowDisplacementTable struct {
OriginalRowCount int
OriginalColCount int
EmptyValue int
Entries []int
Bounds []int
RowDisplacement []int
}
func NewRowDisplacementTable(emptyValue int) *RowDisplacementTable {
return &RowDisplacementTable{
EmptyValue: emptyValue,
}
}
func (tab *RowDisplacementTable) Lookup(row int, col int) (int, error) {
if row < 0 || row >= tab.OriginalRowCount || col < 0 || col >= tab.OriginalColCount {
return tab.EmptyValue, fmt.Errorf("indexes are out of range: [%v, %v]", row, col)
}
d := tab.RowDisplacement[row]
if tab.Bounds[d+col] != row {
return tab.EmptyValue, nil
}
return tab.Entries[d+col], nil
}
func (tab *RowDisplacementTable) OriginalTableSize() (int, int) {
return tab.OriginalRowCount, tab.OriginalColCount
}
type rowInfo struct {
rowNum int
nonEmptyCount int
nonEmptyCol []int
}
func (tab *RowDisplacementTable) Compress(orig *OriginalTable) error {
rowInfo := make([]rowInfo, orig.rowCount)
{
row := 0
col := 0
rowInfo[0].rowNum = 0
for _, v := range orig.entries {
if col == orig.colCount {
row++
col = 0
rowInfo[row].rowNum = row
}
if v != tab.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
})
}
origEntriesLen := len(orig.entries)
entries := make([]int, origEntriesLen)
bounds := make([]int, origEntriesLen)
resultBottom := orig.colCount
rowDisplacement := make([]int, orig.rowCount)
{
for i := 0; i < origEntriesLen; i++ {
entries[i] = tab.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] == tab.EmptyValue {
continue
}
nextRowDisplacement++
isOverlapped = true
break
}
if isOverlapped {
continue
}
rowDisplacement[rInfo.rowNum] = nextRowDisplacement
for _, col := range rInfo.nonEmptyCol {
entries[nextRowDisplacement+col] = orig.entries[(rInfo.rowNum*orig.colCount)+col]
bounds[nextRowDisplacement+col] = rInfo.rowNum
}
resultBottom = nextRowDisplacement + orig.colCount
nextRowDisplacement++
break
}
}
}
tab.OriginalRowCount = orig.rowCount
tab.OriginalColCount = orig.colCount
tab.Entries = entries[:resultBottom]
tab.Bounds = bounds[:resultBottom]
tab.RowDisplacement = rowDisplacement
return nil
}
|