diff options
| author | Paul Buetow <git@mx.buetow.org> | 2020-12-21 20:54:55 +0000 |
|---|---|---|
| committer | Paul Buetow <git@mx.buetow.org> | 2020-12-21 20:54:55 +0000 |
| commit | e80cca57e072cf902f3e91ebe05f1ab8fbd55af8 (patch) | |
| tree | 9a9abe1f49698a8c36594afcc439af5038d2727f /search | |
| parent | 7815c3d006b64d5a6a6664e7c2c96c9c27f29c45 (diff) | |
add gomap for search
Diffstat (limited to 'search')
| -rw-r--r-- | search/gomap.go | 36 | ||||
| -rw-r--r-- | search/search_test.go | 37 |
2 files changed, 60 insertions, 13 deletions
diff --git a/search/gomap.go b/search/gomap.go new file mode 100644 index 0000000..6749b0d --- /dev/null +++ b/search/gomap.go @@ -0,0 +1,36 @@ +package search + +type GoMap map[int]int + +func NewGoMap() GoMap { + return make(GoMap) +} + +func (m GoMap) Empty() bool { + return m.Size() == 0 +} + +func (m GoMap) Size() int { + return len(m) +} + +func (m GoMap) Put(key, val int) { + m[key] = val +} + +func (m GoMap) Get(key int) (int, error) { + val, ok := m[key] + if !ok { + return -1, NotFound + } + return val, nil +} + +func (m GoMap) Del(key int) (int, error) { + val, ok := m[key] + if !ok { + return -1, NotFound + } + delete(m, key) + return val, nil +} diff --git a/search/search_test.go b/search/search_test.go index 09ed906..ec977cc 100644 --- a/search/search_test.go +++ b/search/search_test.go @@ -10,9 +10,7 @@ import ( const factor int = 10 const minLength int = 1 -const maxLength int = 10 - -//const maxLength int = 10000 +const maxLength int = 10000 // Store results here to avoid compiler optimizations var benchResult int @@ -29,6 +27,18 @@ func TestBST(t *testing.T) { } } +func TestRedBlackBST(t *testing.T) { + for i := minLength; i <= maxLength; i *= factor { + test(NewRedBlackBST(), i, t) + } +} + +func TestGoMap(t *testing.T) { + for i := minLength; i <= maxLength; i *= factor { + test(NewGoMap(), i, t) + } +} + func test(s Put, l int, t *testing.T) { keys := ds.NewRandomArrayList(l, l) randoms := ds.NewRandomArrayList(l, -1) @@ -41,10 +51,10 @@ func test(s Put, l int, t *testing.T) { case del: defer delete(mapping, key) val, err = s.Del(key) - t.Log("Del", key, val, err) + //t.Log("Del", key, val, err) default: val, err = s.Get(key) - t.Log("Get", key, val, err) + //t.Log("Get", key, val, err) } if mVal, ok := mapping[key]; ok { @@ -72,14 +82,14 @@ func test(s Put, l int, t *testing.T) { testGet(key) } - t.Log("Put random key-values", l) + //t.Log("Put random key-values", l) var prevKey int for _, key := range sort.Shuffle(keys) { testPut(key, randoms[key]) testGet(prevKey) prevKey = key } - t.Log("Del random key-values", l) + //t.Log("Del random key-values", l) for _, key := range sort.Shuffle(keys) { testDel(key) testGet(prevKey) @@ -90,12 +100,6 @@ func test(s Put, l int, t *testing.T) { } } -func TestRedBlackBST(t *testing.T) { - for i := minLength; i <= maxLength; i *= factor { - test(NewRedBlackBST(), i, t) - } -} - func BenchmarkElementary(t *testing.B) { s := NewElementary() for i := minLength; i <= maxLength; i *= factor { @@ -117,6 +121,13 @@ func BenchmarkRedBlackBST(t *testing.B) { } } +func BenchmarkGoMap(t *testing.B) { + s := NewGoMap() + for i := minLength; i <= maxLength; i *= factor { + benchmark(s, i, t) + } +} + func benchmark(s Put, l int, b *testing.B) { list := ds.NewRandomArrayList(l, -1) |
