-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (54 loc) · 971 Bytes
/
main.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
package main
func main() {
}
type TimeMap struct {
mp map[string][]val
}
type val struct {
value string
time int
}
func newVal(value string, time int) val {
return val{
value: value,
time: time,
}
}
func Constructor() TimeMap {
return TimeMap{
mp: make(map[string][]val),
}
}
func (this *TimeMap) Set(key string, value string, timestamp int) {
val := newVal(value, timestamp)
this.mp[key] = append(this.mp[key], val)
}
func (this *TimeMap) Get(key string, timestamp int) string {
values, ok := this.mp[key]
if !ok {
return ""
}
ind := bSearch(values, timestamp)
if ind == -1 {
return ""
}
return values[ind].value
}
func bSearch(a []val, x int) int {
r := -1 // not found
start := 0
end := len(a) - 1
for start <= end {
mid := (start + end) / 2
if a[mid].time == x {
r = mid // found
break
} else if a[mid].time < x {
start = mid + 1
r = mid
} else if a[mid].time > x {
end = mid - 1
}
}
return r
}