| 18 | } |
| 19 | |
| 20 | func (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 | |
| 45 | func (m *TimeMap) Get(key string, timestamp int) string { |
| 46 | entries, ok := m.m[key] |