-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring_table.go
More file actions
89 lines (74 loc) · 1.73 KB
/
string_table.go
File metadata and controls
89 lines (74 loc) · 1.73 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
// Copyright 2021 Grabtaxi Holdings Pte Ltd (GRAB), All rights reserved.
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
package gosm
type stringTable struct {
indexMap map[string]int
table []string
all []string
}
func newStringTable() *stringTable {
return &stringTable{
indexMap: map[string]int{},
table: []string{""},
}
}
// --- DenseNodes ---
func (t *stringTable) add(str string) {
if _, ok := t.indexMap[str]; !ok {
t.table = append(t.table, str)
t.indexMap[str] = len(t.table) - 1
}
t.all = append(t.all, str)
}
func (t *stringTable) index(str string) int {
if v, ok := t.indexMap[str]; ok {
return v
}
return -1
}
func (t *stringTable) endOne() {
t.all = append(t.all, "##")
}
// toKeysVals is used in DenseNodes.
func (t *stringTable) toKeysVals() []int32 {
var result []int32
for _, s := range t.all {
if s == "##" {
result = append(result, 0)
continue
}
result = append(result, int32(t.index(s)))
}
return result
}
// --- Node / Way / Relation ---
func (t *stringTable) addTags(tags map[string]string) ([]uint32, []uint32) {
keyIDs := make([]uint32, len(tags))
valIDs := make([]uint32, len(tags))
cnt := 0
for k, v := range tags {
kIdx := t.append(k)
keyIDs[cnt] = kIdx
vIdx := t.append(v)
valIDs[cnt] = vIdx
cnt++
}
return keyIDs, valIDs
}
func (t *stringTable) append(str string) uint32 {
idx, ok := t.indexMap[str]
if !ok {
t.table = append(t.table, str)
idx = len(t.table) - 1
}
return uint32(idx)
}
// used in relations.
func (t *stringTable) addRoles(roles []string) []int32 {
result := make([]int32, len(roles))
for i, role := range roles {
id := t.append(role)
result[i] = int32(id)
}
return result
}