-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
bitmaskmap.go
86 lines (76 loc) · 1.71 KB
/
bitmaskmap.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
package main
import (
"encoding/gob"
"fmt"
"io"
"log"
"os"
"reflect"
)
// BitmaskMap - struct to hold common masks
type BitmaskMap struct {
Nodes *Bitmask
Ways *Bitmask
Relations *Bitmask
WayRefs *Bitmask
RelNodes *Bitmask
RelWays *Bitmask
RelRelation *Bitmask
}
// NewBitmaskMap - constructor
func NewBitmaskMap() *BitmaskMap {
return &BitmaskMap{
Nodes: NewBitMask(),
Ways: NewBitMask(),
Relations: NewBitMask(),
WayRefs: NewBitMask(),
RelNodes: NewBitMask(),
RelWays: NewBitMask(),
RelRelation: NewBitMask(),
}
}
// WriteTo - write to destination
func (m *BitmaskMap) WriteTo(sink io.Writer) (int64, error) {
encoder := gob.NewEncoder(sink)
err := encoder.Encode(m)
return 0, err
}
// ReadFrom - read from destination
func (m *BitmaskMap) ReadFrom(tap io.Reader) (int64, error) {
decoder := gob.NewDecoder(tap)
err := decoder.Decode(m)
return 0, err
}
// WriteToFile - write to disk
func (m *BitmaskMap) WriteToFile(path string) {
file, err := os.Create(path)
if err != nil {
panic(err)
}
m.WriteTo(file)
log.Println("wrote bitmask:", path)
}
// ReadFromFile - read from disk
func (m *BitmaskMap) ReadFromFile(path string) {
// bitmask file doesn't exist
if _, err := os.Stat(path); err != nil {
fmt.Println("bitmask file not found:", path)
os.Exit(1)
}
file, err := os.Open(path)
if err != nil {
panic(err)
}
m.ReadFrom(file)
log.Println("read bitmask:", path)
}
// Print -- print debug stats
func (m BitmaskMap) Print() {
k := reflect.TypeOf(m)
v := reflect.ValueOf(m)
for i := 0; i < k.NumField(); i++ {
key := k.Field(i).Name
val := v.Field(i).Interface()
fmt.Printf("%s: %v\n", key, (val.(*Bitmask)).Len())
}
}