Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Has Method for OrderedMap #54

Merged
merged 5 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,9 @@ func (m *OrderedMap) Copy() *OrderedMap {

return m2
}

// Has checks if a key exists in the map.
func (m *OrderedMap) Has(key interface{}) bool {
_, exists := m.kv[key]
return exists
}
175 changes: 175 additions & 0 deletions orderedmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,26 @@ func TestGetElement(t *testing.T) {
})
}

func TestOrderedMap_Has(t *testing.T) {
t.Run("ReturnsFalseIfKeyDoesNotExist", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
assert.False(t, m.Has("foo"))
})

t.Run("ReturnsTrueIfKeyExists", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
m.Set("foo", "bar")
assert.True(t, m.Has("foo"))
})

t.Run("KeyDoesNotExistAfterDelete", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
m.Set("foo", "bar")
m.Delete("foo")
assert.False(t, m.Has("foo"))
})
}

func benchmarkMap_Set(multiplier int) func(b *testing.B) {
return func(b *testing.B) {
m := make(map[int]bool)
Expand Down Expand Up @@ -524,6 +544,40 @@ func benchmarkOrderedMap_Iterate(multiplier int) func(b *testing.B) {
}
}

func benchmarkMap_Has(multiplier int) func(b *testing.B) {
elliotchance marked this conversation as resolved.
Show resolved Hide resolved
m := make(map[int]bool)
for i := 0; i < 1000*multiplier; i++ {
m[i] = true
}

return func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = m[i%1000*multiplier]
}
}
}

func BenchmarkMap_Has(b *testing.B) {
benchmarkMap_Has(1)(b)
}

func benchmarkOrderedMap_Has(multiplier int) func(b *testing.B) {
m := orderedmap.NewOrderedMap()
for i := 0; i < 1000*multiplier; i++ {
m.Set(i, true)
}

return func(b *testing.B) {
for i := 0; i < b.N; i++ {
m.Has(i % 1000 * multiplier)
}
}
}

func BenchmarkOrderedMap_Has(b *testing.B) {
benchmarkOrderedMap_Has(1)(b)
}

func BenchmarkOrderedMap_Iterate(b *testing.B) {
benchmarkOrderedMap_Iterate(1)(b)
}
Expand Down Expand Up @@ -698,6 +752,42 @@ func BenchmarkOrderedMapString_Iterate(b *testing.B) {
benchmarkOrderedMapString_Iterate(1)(b)
}

func benchmarkMapString_Has(multiplier int) func(b *testing.B) {
m := make(map[string]bool)
a := "12345678"
for i := 0; i < 1000*multiplier; i++ {
m[a+strconv.Itoa(i)] = true
}

return func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = m[a+strconv.Itoa(i%1000*multiplier)]
}
}
}

func BenchmarkMapString_Has(b *testing.B) {
benchmarkMapString_Has(1)(b)
}

func benchmarkOrderedMapString_Has(multiplier int) func(b *testing.B) {
m := orderedmap.NewOrderedMap()
a := "12345678"
for i := 0; i < 1000*multiplier; i++ {
m.Set(a+strconv.Itoa(i), true)
}

return func(b *testing.B) {
for i := 0; i < b.N; i++ {
m.Has(a + strconv.Itoa(i%1000*multiplier))
}
}
}

func BenchmarkOrderedMapString_Has(b *testing.B) {
benchmarkOrderedMapString_Has(1)(b)
}

func BenchmarkOrderedMap_Keys(b *testing.B) {
benchmarkOrderedMap_Keys(1)(b)
}
Expand Down Expand Up @@ -882,6 +972,44 @@ func benchmarkBigMapString_Set() func(b *testing.B) {
}
}

func benchmarkBigMap_Has() func(b *testing.B) {
m := make(map[int]bool)
for i := 0; i < 10000000; i++ {
m[i] = true
}

return func(b *testing.B) {
for j := 0; j < b.N; j++ {
for i := 0; i < 10000000; i++ {
_ = m[i]
}
}
}
}

func BenchmarkBigMap_Has(b *testing.B) {
benchmarkBigMap_Has()(b)
}

func benchmarkBigOrderedMap_Has() func(b *testing.B) {
m := orderedmap.NewOrderedMap()
for i := 0; i < 10000000; i++ {
m.Set(i, true)
}

return func(b *testing.B) {
for j := 0; j < b.N; j++ {
for i := 0; i < 10000000; i++ {
m.Has(i)
}
}
}
}

func BenchmarkBigOrderedMap_Has(b *testing.B) {
benchmarkBigOrderedMap_Has()(b)
}

func BenchmarkBigMapString_Set(b *testing.B) {
benchmarkBigMapString_Set()(b)
}
Expand Down Expand Up @@ -1001,6 +1129,45 @@ func BenchmarkBigOrderedMapString_Iterate(b *testing.B) {
benchmarkBigOrderedMapString_Iterate()(b)
}

func benchmarkBigMapString_Has() func(b *testing.B) {
m := make(map[string]bool)
a := "12345678"
for i := 0; i < 10000000; i++ {
m[a+strconv.Itoa(i)] = true
}

return func(b *testing.B) {
for j := 0; j < b.N; j++ {
for i := 0; i < 10000000; i++ {
_ = m[a+strconv.Itoa(i)]
}
}
}
}

func BenchmarkBigMapString_Has(b *testing.B) {
benchmarkBigMapString_Has()(b)
}

func benchmarkBigOrderedMapString_Has() func(b *testing.B) {
m := orderedmap.NewOrderedMap()
for i := 0; i < 10000000; i++ {
m.Set(i, true)
}

return func(b *testing.B) {
for j := 0; j < b.N; j++ {
for i := 0; i < 10000000; i++ {
m.Has(i)
}
}
}
}

func BenchmarkBigOrderedMapString_Has(b *testing.B) {
benchmarkBigOrderedMapString_Has()(b)
}

func BenchmarkAll(b *testing.B) {
b.Run("BenchmarkOrderedMap_Keys", BenchmarkOrderedMap_Keys)

Expand All @@ -1013,6 +1180,8 @@ func BenchmarkAll(b *testing.B) {
b.Run("BenchmarkMap_Delete", BenchmarkMap_Delete)
b.Run("BenchmarkOrderedMap_Iterate", BenchmarkOrderedMap_Iterate)
b.Run("BenchmarkMap_Iterate", BenchmarkMap_Iterate)
b.Run("BenchmarkOrderedMap_Has", BenchmarkOrderedMap_Has)
b.Run("BenchmarkMap_Has", BenchmarkMap_Has)

b.Run("BenchmarkBigMap_Set", BenchmarkBigMap_Set)
b.Run("BenchmarkBigOrderedMap_Set", BenchmarkBigOrderedMap_Set)
Expand All @@ -1021,6 +1190,8 @@ func BenchmarkAll(b *testing.B) {
b.Run("BenchmarkBigOrderedMap_GetElement", BenchmarkBigOrderedMap_GetElement)
b.Run("BenchmarkBigOrderedMap_Iterate", BenchmarkBigOrderedMap_Iterate)
b.Run("BenchmarkBigMap_Iterate", BenchmarkBigMap_Iterate)
b.Run("BenchmarkBigMap_Has", BenchmarkBigMap_Has)
b.Run("BenchmarkBigOrderedMap_Has", BenchmarkBigOrderedMap_Has)

b.Run("BenchmarkOrderedMapString_Set", BenchmarkOrderedMapString_Set)
b.Run("BenchmarkMapString_Set", BenchmarkMapString_Set)
Expand All @@ -1031,6 +1202,8 @@ func BenchmarkAll(b *testing.B) {
b.Run("BenchmarkMapString_Delete", BenchmarkMapString_Delete)
b.Run("BenchmarkOrderedMapString_Iterate", BenchmarkOrderedMapString_Iterate)
b.Run("BenchmarkMapString_Iterate", BenchmarkMapString_Iterate)
b.Run("BenchmarkMapString_Has", BenchmarkMapString_Has)
b.Run("BenchmarkOrderedMapString_Has", BenchmarkOrderedMapString_Has)

b.Run("BenchmarkBigMapString_Set", BenchmarkBigMapString_Set)
b.Run("BenchmarkBigOrderedMapString_Set", BenchmarkBigOrderedMapString_Set)
Expand All @@ -1039,4 +1212,6 @@ func BenchmarkAll(b *testing.B) {
b.Run("BenchmarkBigOrderedMapString_GetElement", BenchmarkBigOrderedMapString_GetElement)
b.Run("BenchmarkBigOrderedMapString_Iterate", BenchmarkBigOrderedMapString_Iterate)
b.Run("BenchmarkBigMapString_Iterate", BenchmarkBigMapString_Iterate)
b.Run("BenchmarkBigMapString_Has", BenchmarkBigMapString_Has)
b.Run("BenchmarkBigOrderedMapString_Has", BenchmarkBigOrderedMapString_Has)
}
6 changes: 6 additions & 0 deletions v2/orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,9 @@ func (m *OrderedMap[K, V]) Copy() *OrderedMap[K, V] {
}
return m2
}

// Has checks if a key exists in the map.
func (m *OrderedMap[K, V]) Has(key K) bool {
_, exists := m.kv[key]
return exists
}
Loading
Loading