https://github.com/i-redbyte/ring-map
The simplest ring map
https://github.com/i-redbyte/ring-map
Last synced: about 1 year ago
JSON representation
The simplest ring map
- Host: GitHub
- URL: https://github.com/i-redbyte/ring-map
- Owner: i-redbyte
- Created: 2020-03-19T22:50:51.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-04-26T21:55:15.000Z (about 4 years ago)
- Last Synced: 2025-01-31T07:15:15.721Z (over 1 year ago)
- Language: Go
- Size: 8.79 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
**An example of a solution to a problem:**
```go
package main
import (
"fmt"
)
// Vault structure stores limited number of key-value elements.
// It stores most recently addressed elements, so if it already stores maximum elements allowed,
// when we add next one, it should delete least recently used one (either by Get or Put).
// type Vault struct {
// ...
// }
func main() {
v := NewVault(3) // Create vault to store 3 key-value elements (string: string)
v.Put("key1", "value1") // Add "key1": "value1"
fmt.Println(v.Len()) // 1
v.Put("key2", "value2") // Add "key2": "value2"
fmt.Println(v.Len()) // 2
v.Put("key3", "value3") // Add "key3": "value3"
fmt.Println(v.Len()) // 3
fmt.Println(v.Get("key1")) // value1
v.Put("key4", "value4") // Add "key4": "value4" and remove "key2" as least recently used one
fmt.Println(v.Len()) // 3
fmt.Println(v.Keys()) // [key1 key4 key3] // keys order doesn't matter here
}
```