-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package day97 | ||
|
||
import ( | ||
"sort" | ||
) | ||
|
||
// TimestampedValue represents the value set at a particular time. | ||
type TimestampedValue struct { | ||
Time int | ||
Value interface{} | ||
} | ||
|
||
// TimeMap is a map that is aware of changes over time. | ||
type TimeMap struct { | ||
data map[interface{}][]*TimestampedValue | ||
} | ||
|
||
// NewTimeMap returns a new timestamp aware map. | ||
func NewTimeMap() *TimeMap { | ||
return &TimeMap{make(map[interface{}][]*TimestampedValue)} | ||
} | ||
|
||
// Set a key-value pair with a particular timestamp. | ||
func (tm *TimeMap) Set(key, value interface{}, time int) { | ||
tm.data[key] = append(tm.data[key], &TimestampedValue{time, value}) | ||
sort.Slice(tm.data[key], func(i, j int) bool { | ||
return tm.data[key][i].Time < tm.data[key][j].Time | ||
}) | ||
} | ||
|
||
// Get returns the value for a key at a particular time. | ||
func (tm *TimeMap) Get(key interface{}, time int) interface{} { | ||
pos := sort.Search(len(tm.data[key]), func(i int) bool { | ||
return tm.data[key][i].Time > time | ||
}) | ||
if pos <= 0 { | ||
if len(tm.data[key]) == 0 || tm.data[key][0].Time > time { | ||
return nil | ||
} | ||
} | ||
return tm.data[key][pos-1].Value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package day97 | ||
|
||
import "testing" | ||
|
||
func TestTimeMap(t *testing.T) { | ||
t.Parallel() | ||
d := NewTimeMap() | ||
d.Set(1, 1, 0) // set key 1 to value 1 at time 0 | ||
d.Set(1, 2, 2) // set key 1 to value 2 at time 2 | ||
if result := d.Get(1, 1); result != 1 { | ||
t.Errorf("get key 1 at time 1 should be 1") | ||
} | ||
if result := d.Get(1, 3); result != 2 { | ||
t.Errorf("get key 1 at time 3 should be 2") | ||
} | ||
d.Set(1, 1, 5) // set key 1 to value 1 at time 5 | ||
if result := d.Get(1, 0); result != 1 { | ||
t.Errorf("get key 1 at time 0 should be 1") | ||
} | ||
if result := d.Get(1, 10); result != 1 { | ||
t.Errorf("get key 1 at time 10 should be 1") | ||
} | ||
d.Set(1, 1, 0) // set key 1 to value 1 at time 0 | ||
d.Set(1, 2, 0) // set key 1 to value 2 at time 0 | ||
if result := d.Get(1, 0); result != 2 { | ||
t.Errorf("get key 1 at time 0 should be 2") | ||
} | ||
if result := d.Get(2, 0); result != nil { | ||
t.Errorf("unused keys should return nil") | ||
} | ||
if result := d.Get(1, -1); result != nil { | ||
t.Errorf("No value should be set back in time.") | ||
} | ||
} | ||
|
||
func BenchmarkTimeMap(b *testing.B) { | ||
d := NewTimeMap() | ||
d.Set(1, 1, 0) // set key 1 to value 1 at time 0 | ||
d.Set(1, 2, 2) // set key 1 to value 2 at time 2 | ||
d.Set(1, 1, 5) // set key 1 to value 1 at time 5 | ||
for i := 0; i < b.N; i++ { | ||
d.Get(1, 1) | ||
d.Get(1, 3) | ||
d.Get(1, 0) | ||
d.Get(1, 10) | ||
d.Get(1, 0) | ||
d.Get(2, 0) | ||
d.Get(1, -1) | ||
} | ||
} |