MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / Set

Method Set

time_based_key_value_store_981/solution.go:20–43  ·  view source on GitHub ↗
(key string, value string, timestamp int)

Source from the content-addressed store, hash-verified

18}
19
20func (m *TimeMap) Set(key string, value string, timestamp int) {
21 entries, ok := m.m[key]
22 if !ok {
23 entries = make([]*entry, 0)
24 }
25
26 // perform a binary search for the sorted insertion point (timestamp asc)
27 index := sort.Search(len(entries), func(i int) bool {
28 return entries[i].timestamp > timestamp
29 })
30
31 // insert the new entry at the sorted insertion point
32 newEntry := &entry{value: value, timestamp: timestamp}
33 if index == len(entries) {
34 entries = append(entries, newEntry)
35 } else {
36 // perform a copy-shift insert
37 entries = append(entries, nil) // make space
38 copy(entries[index+1:], entries[index:])
39 entries[index] = newEntry
40 }
41
42 m.m[key] = entries
43}
44
45func (m *TimeMap) Get(key string, timestamp int) string {
46 entries, ok := m.m[key]

Callers 1

TestKVFunction · 0.80

Calls 1

SearchMethod · 0.80

Tested by 1

TestKVFunction · 0.64