forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu64.go
137 lines (112 loc) · 2.62 KB
/
u64.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
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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package interval
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
)
type U64 struct {
first uint64
count uint64
}
type U64List []U64
func CreateU64(first, count uint64) U64 {
return U64{first, count}
}
func CreateU64Inc(first, last uint64) U64 {
if last < first {
panic(fmt.Errorf("Attempting to set last before first! [0x%.16x-0x%.16x]", first, last))
}
return U64{first, 1 + last - first}
}
func (i U64) Expand(value uint64) U64 {
if i.first > value {
i.first = value
}
if i.Last() < value {
i.count += value - i.Last()
}
return i
}
func (i U64) Contains(value uint64) bool {
return i.first <= value && value <= i.Last()
}
func (i U64) Range() (start, end uint64) {
return i.first, i.first + i.count
}
func (i U64) First() uint64 {
return i.first
}
func (i U64) Last() uint64 {
return i.first + i.count - 1
}
func (i U64) Count() uint64 {
return i.count
}
func (i U64) String() string {
return fmt.Sprintf("[0x%.16x-0x%.16x]", i.first, i.Last())
}
func (i U64) Span() (start, end uint64) {
return i.first, i.first + i.count
}
// encoding.BinaryMarshaler compliance
func (i U64) MarshalBinary() ([]byte, error) {
buf := &bytes.Buffer{}
err := binary.Write(buf, binary.LittleEndian, i.first)
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, i.count)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// encoding.BinaryUnmarshaler compliance
func (i *U64) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
err := binary.Read(buf, binary.LittleEndian, &i.first)
if err != nil {
return err
}
err = binary.Read(buf, binary.LittleEndian, &i.count)
return err
}
func (l U64List) Len() int {
return len(l)
}
func (l U64List) Cap() int {
return cap(l)
}
func (l *U64List) SetLen(len int) {
*l = (*l)[:len]
}
func (l *U64List) GrowTo(length, capacity int) {
old := *l
*l = make(U64List, length, capacity)
copy(*l, old)
}
func (l U64List) Copy(to, from, count int) {
copy(l[to:to+count], l[from:from+count])
}
func (l U64List) GetInterval(index int) (start, end uint64) {
return l[index].Span()
}
func (l U64List) SetInterval(index int, start, end uint64) {
l[index].first = start
l[index].count = end - start
}
func (l U64List) Overlaps(i IntData) U64List {
first, count := Intersect(l, i)
return l[first : first+count]
}
func (l U64List) String() string {
s := make([]string, len(l))
for i, v := range l {
s[i] = fmt.Sprintf("%d%s", i, v)
}
return "{" + strings.Join(s, ",") + "}"
}