-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from fgm/orderedmap
Implement slice-based orderedmap.
- Loading branch information
Showing
8 changed files
with
240 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/fgm/container/orderedmap" | ||
) | ||
|
||
type in struct { | ||
key string | ||
value int | ||
} | ||
|
||
func main() { | ||
const size = 8 | ||
input := make([]in, size) | ||
for i := 1; i <= size; i++ { | ||
input[i-1] = in{key: strconv.Itoa(i), value: i} | ||
} | ||
|
||
fmt.Println("Go map:") | ||
m := make(map[string]int, size) | ||
for _, e := range input { | ||
m[e.key] = e.value | ||
} | ||
delete(m, "5") | ||
m["1"] = 11 | ||
for k, v := range m { | ||
fmt.Println(k, v) | ||
} | ||
|
||
fmt.Println("OrderedMap:") | ||
var om = orderedmap.NewSlice[string, int](size) | ||
for _, e := range input { | ||
om.Store(e.key, e.value) | ||
} | ||
om.Delete("5") | ||
om.Store("1", 11) | ||
|
||
om.Range(func(k string, v int) bool { | ||
fmt.Println(k, v) | ||
return true | ||
}) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package orderedmap | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/fgm/container" | ||
) | ||
|
||
type Slice[K comparable, V any] struct { | ||
order []K | ||
store map[K]V | ||
} | ||
|
||
// mustIndexOf may only be used with a key known to be present in the map. | ||
func (s Slice[K, V]) mustIndexOf(k K) int { | ||
for i, ck := range s.order { | ||
if ck == k { | ||
return i | ||
} | ||
} | ||
// Should never happen: someone probably caused a race condition. | ||
panic(fmt.Errorf("structure inconsistency: key %v not found", k)) | ||
} | ||
|
||
func (s *Slice[K, V]) Delete(k K) { | ||
_, loaded := s.store[k] | ||
if !loaded { | ||
return | ||
} | ||
delete(s.store, k) | ||
index := s.mustIndexOf(k) | ||
s.order = append(s.order[:index], s.order[index+1:]...) | ||
} | ||
|
||
func (s Slice[K, V]) Load(key K) (V, bool) { | ||
v, loaded := s.store[key] | ||
return v, loaded | ||
} | ||
|
||
func (s Slice[K, V]) Range(f func(key K, value V) bool) { | ||
for _, k := range s.order { | ||
v, loaded := s.store[k] | ||
if !loaded { | ||
panic(fmt.Errorf("structure inconsistency: key %v not found", k)) | ||
} | ||
if !f(k, v) { | ||
break | ||
} | ||
} | ||
} | ||
|
||
func (s *Slice[K, V]) Store(k K, v V) { | ||
_, loaded := s.store[k] | ||
if !loaded { | ||
s.order = append(s.order, k) | ||
s.store[k] = v | ||
return | ||
} | ||
|
||
index := s.mustIndexOf(k) | ||
s.order = append(s.order[:index], s.order[index+1:]...) | ||
s.order = append(s.order, k) | ||
s.store[k] = v | ||
} | ||
|
||
func NewSlice[K comparable, V any](sizeHint int) container.OrderedMap[K, V] { | ||
s := &Slice[K, V]{ | ||
order: make([]K, 0, sizeHint), | ||
store: make(map[K]V, sizeHint), | ||
} | ||
|
||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package orderedmap | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
type in struct { | ||
key string | ||
value int | ||
} | ||
|
||
func TestSlice_Range(t *testing.T) { | ||
const size = 8 | ||
input := make([]in, size) | ||
for i := 1; i <= size; i++ { | ||
input[i-1] = in{key: strconv.Itoa(i), value: i} | ||
} | ||
expectedKeys := []string{"2", "3", "4", "6", "7", "8", "1"} | ||
expectedVals := []int{2, 3, 4, 6, 7, 8, 11} | ||
|
||
var om = NewSlice[string, int](size) | ||
for _, e := range input { | ||
om.Store(e.key, e.value) | ||
} | ||
om.Delete("5") | ||
om.Store("1", 11) | ||
|
||
var keys = make([]string, 0, size) | ||
var vals = make([]int, 0, size) | ||
om.Range(func(k string, v int) bool { | ||
keys = append(keys, k) | ||
vals = append(vals, v) | ||
return k != "1" // This is the last key because we added it after Delete. | ||
}) | ||
if !cmp.Equal(keys, expectedKeys) { | ||
t.Fatalf("Failed keys comparison:%s", cmp.Diff(keys, expectedKeys)) | ||
} | ||
if !cmp.Equal(vals, expectedVals) { | ||
t.Fatalf("Failed values comparison:%s", cmp.Diff(vals, expectedVals)) | ||
} | ||
} | ||
|
||
func TestSlice_Store_Load_Delete(t *testing.T) { | ||
const one = "one" | ||
var om = NewSlice[string, int](1) | ||
om.Store(one, 1) | ||
zero, loaded := om.Load("zero") | ||
if loaded { | ||
t.Fatalf("unexpected load success for missing key %s, value is %v", "zero", zero) | ||
} | ||
_, loaded = om.Load(one) | ||
if !loaded { | ||
t.Fatal("unexpected load failure for present key") | ||
} | ||
om.Delete(one) | ||
om.Delete(one) // Ensure multiple deletes do not cause an error | ||
actual, loaded := om.Load(one) | ||
if loaded { | ||
t.Fatalf("unexpected load success for missing key %s, value is %v", one, actual) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package orderedmap | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSlice_mustIndexOf(t *testing.T) { | ||
// Catch expected panic | ||
defer func() { _ = recover() }() | ||
|
||
var om = NewSlice[string, int](1).(*Slice[string, int]) | ||
om.store["one"] = 1 | ||
om.mustIndexOf("one") | ||
t.Fatalf("mustIndexOf did not panic on missing order key") | ||
} | ||
|
||
func TestSlice_Range_inconsistent(t *testing.T) { | ||
// Catch expected panic | ||
defer func() { _ = recover() }() | ||
|
||
var om = NewSlice[string, int](1).(*Slice[string, int]) | ||
om.Store("one", 1) | ||
delete(om.store, "one") | ||
om.Range(func(_ string, _ int) bool { return false }) | ||
t.Fatalf("Range did not panic on missing map entry") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters