go-hashmap, v1.1
Posted on

The library that implements a hash map with synchronized and concurrent wrappers.
Implementation of a synchronized hash map.
Change Log
- implementation of a synchronized hash map:
- use the implementation described above as an inner map;
- use a mutex lock to access the inner map;
- support operations:
- getting of an item by a key;
- setting of an item by a key;
- deleting of an item by a key:
- support a success flag.
Features
- implementation of a hash map:
- use the open addressing strategy for collision resolution;
- use the key interface for supporting custom types;
- support operations:
- getting of an item by a key;
- setting of an item by a key;
- deleting of an item by a key:
- support a success flag;
- implementation of a synchronized hash map:
- use the implementation described above as an inner map;
- use a mutex lock to access the inner map;
- support operations:
- getting of an item by a key;
- setting of an item by a key;
- deleting of an item by a key:
- support a success flag.
Example
hashmap.SynchronizedHashMap:
package main
import (
"fmt"
"hash/fnv"
"io"
hashmap "github.com/thewizardplusplus/go-hashmap"
)
type StringKey string
func (key StringKey) Hash() int {
hash := fnv.New32()
io.WriteString(hash, string(key))
return int(hash.Sum32())
}
func (key StringKey) Equals(other interface{}) bool {
return key == other.(StringKey)
}
func main() {
timeZones := hashmap.NewSynchronizedHashMap()
timeZones.Set(StringKey("EST"), -5*60*60)
timeZones.Set(StringKey("CST"), -6*60*60)
timeZones.Set(StringKey("MST"), -7*60*60)
estOffset, ok := timeZones.Get(StringKey("EST"))
fmt.Println(estOffset, ok)
// Output:
// -18000 true
}
Benchmarks
BenchmarkSynchronizedBuiltinMap/Get/1000/1-4 10000000 1459 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Get/1000/10-4 1000000 10359 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Get/1000/100-4 200000 67092 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Get/1000/1000-4 50000 381497 ns/op 18 B/op 1 allocs/op
BenchmarkSyncMap/Get/1000/1-4 10000000 1409 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Get/1000/10-4 2000000 9741 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Get/1000/100-4 200000 66314 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Get/1000/1000-4 50000 386608 ns/op 18 B/op 1 allocs/op
BenchmarkSynchronizedHashMap/Get/1000/1-4 10000000 1841 ns/op 39 B/op 4 allocs/op
BenchmarkSynchronizedHashMap/Get/1000/10-4 1000000 14205 ns/op 255 B/op 40 allocs/op
BenchmarkSynchronizedHashMap/Get/1000/100-4 200000 81408 ns/op 2415 B/op 400 allocs/op
BenchmarkSynchronizedHashMap/Get/1000/1000-4 30000 477555 ns/op 24063 B/op 3999 allocs/op
BenchmarkSynchronizedBuiltinMap/Set/1000/1-4 200000 121075 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Set/1000/10-4 5000 3924351 ns/op 42 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Set/1000/100-4 300 40956121 ns/op 356 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Set/1000/1000-4 30 432632968 ns/op 8193 B/op 62 allocs/op
BenchmarkSyncMap/Set/1000/1-4 50000 255414 ns/op 32002 B/op 2999 allocs/op
BenchmarkSyncMap/Set/1000/10-4 5000 3604140 ns/op 319904 B/op 29981 allocs/op
BenchmarkSyncMap/Set/1000/100-4 500 37029045 ns/op 3199333 B/op 299812 allocs/op
BenchmarkSyncMap/Set/1000/1000-4 20 547466007 ns/op 32080577 B/op 2999260 allocs/op
BenchmarkSynchronizedHashMap/Set/1000/1-4 50000 348997 ns/op 32002 B/op 4998 allocs/op
BenchmarkSynchronizedHashMap/Set/1000/10-4 3000 6011785 ns/op 319918 B/op 49973 allocs/op
BenchmarkSynchronizedHashMap/Set/1000/100-4 200 60215359 ns/op 3199397 B/op 499740 allocs/op
BenchmarkSynchronizedHashMap/Set/1000/1000-4 10 1055409411 ns/op 32054326 B/op 4998449 allocs/op
BenchmarkSynchronizedBuiltinMap/Delete/1000/1-4 10000000 1401 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Delete/1000/10-4 2000000 9781 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Delete/1000/100-4 200000 67847 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Delete/1000/1000-4 50000 393990 ns/op 18 B/op 1 allocs/op
BenchmarkSyncMap/Delete/1000/1-4 10000000 1329 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Delete/1000/10-4 2000000 8929 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Delete/1000/100-4 200000 61734 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Delete/1000/1000-4 50000 374220 ns/op 18 B/op 1 allocs/op
BenchmarkSynchronizedHashMap/Delete/1000/1-4 10000000 1845 ns/op 39 B/op 4 allocs/op
BenchmarkSynchronizedHashMap/Delete/1000/10-4 1000000 13879 ns/op 256 B/op 40 allocs/op
BenchmarkSynchronizedHashMap/Delete/1000/100-4 100000 128004 ns/op 2420 B/op 400 allocs/op
BenchmarkSynchronizedHashMap/Delete/1000/1000-4 10000 1034500 ns/op 24281 B/op 4002 allocs/op
Repository
Link: https://github.com/thewizardplusplus/go-hashmap/tree/v1.1.
Content: code.
License: MIT.