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

feat: add SetIf and DeleteIf methods. #6

Merged
merged 1 commit into from
Nov 7, 2023
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
24 changes: 24 additions & 0 deletions concurrent_swiss_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ func (m *CsMap[K, V]) Delete(key K) bool {
return shard.items.DeleteWithHash(key, hashShardPair.hash)
}

func (m *CsMap[K, V]) DeleteIf(key K, condition func(value V) bool) bool {
hashShardPair := m.getShard(key)
shard := hashShardPair.shard
shard.Lock()
defer shard.Unlock()
value, ok := shard.items.GetWithHash(key, hashShardPair.hash)
if ok && condition(value) {
return shard.items.DeleteWithHash(key, hashShardPair.hash)
}
return false
}

func (m *CsMap[K, V]) Load(key K) (V, bool) {
hashShardPair := m.getShard(key)
shard := hashShardPair.shard
Expand Down Expand Up @@ -127,6 +139,18 @@ func (m *CsMap[K, V]) SetIfAbsent(key K, value V) {
}
}

func (m *CsMap[K, V]) SetIf(key K, conditionFn func(previousVale V, previousFound bool) (value V, set bool)) {
hashShardPair := m.getShard(key)
shard := hashShardPair.shard
shard.Lock()
defer shard.Unlock()
value, found := shard.items.GetWithHash(key, hashShardPair.hash)
value, ok := conditionFn(value, found)
if ok {
shard.items.PutWithHash(key, value, hashShardPair.hash)
}
}

func (m *CsMap[K, V]) SetIfPresent(key K, value V) {
hashShardPair := m.getShard(key)
shard := hashShardPair.shard
Expand Down
60 changes: 60 additions & 0 deletions concurrent_swiss_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,66 @@ func TestSetIfPresent(t *testing.T) {
}
}

func TestSetIf(t *testing.T) {
myMap := csmap.Create[int, string]()
valueA := "value a"
myMap.SetIf(1, func(previousVale string, previousFound bool) (value string, set bool) {
// operate like a SetIfAbsent...
if !previousFound {
return valueA, true
}
return "", false
})
value, _ := myMap.Load(1)
if value != valueA {
t.Fatal("value should value a")
}

myMap.SetIf(1, func(previousVale string, previousFound bool) (value string, set bool) {
// operate like a SetIfAbsent...
if !previousFound {
return "bad", true
}
return "", false
})
value, _ = myMap.Load(1)
if value != valueA {
t.Fatal("value should value a")
}
}

func TestDeleteIf(t *testing.T) {
myMap := csmap.Create[int, string]()
myMap.Store(1, "value b")
ok1 := myMap.DeleteIf(20, func(value string) bool {
t.Fatal("condition function should not have been called")
return false
})
if ok1 {
t.Fatal("ok1 should be false")
}

ok2 := myMap.DeleteIf(1, func(value string) bool {
if value != "value b" {
t.Fatal("condition function arg should be tests")
}
return false // don't delete
})
if ok2 {
t.Fatal("ok1 should be false")
}

ok3 := myMap.DeleteIf(1, func(value string) bool {
if value != "value b" {
t.Fatal("condition function arg should be tests")
}
return true // delete the entry
})
if !ok3 {
t.Fatal("ok2 should be true")
}
}

func TestCount(t *testing.T) {
myMap := csmap.Create[int, string]()
myMap.SetIfAbsent(1, "test")
Expand Down