Skip to content

Commit ba6aba9

Browse files
committed
add Map.GetAndSet, rename Map.{EnsureGet => Use}
1 parent 7db509a commit ba6aba9

File tree

7 files changed

+103
-24
lines changed

7 files changed

+103
-24
lines changed

collections/BiMap_example_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ func ExampleBiMap_GetKey() {
7979
// false
8080
}
8181

82-
func ExampleBiMap_EnsureGet() {
83-
m := collections.NewMap([]collections.MapEntry[string, string]{
82+
func ExampleBiMap_Use() {
83+
m := collections.NewBiMap([]collections.MapEntry[string, string]{
8484
{"foo", "Hi"},
8585
})
8686

87-
val1 := m.EnsureGet("foo", func() string { return "Hello" })
88-
val2 := m.EnsureGet("bar", func() string { return "World" })
87+
val1 := m.Use("foo", func() string { return "Hello" })
88+
val2 := m.Use("bar", func() string { return "World" })
8989

9090
fmt.Println(val1)
9191
fmt.Println(val2)
@@ -94,6 +94,22 @@ func ExampleBiMap_EnsureGet() {
9494
// World
9595
}
9696

97+
func ExampleBiMap_GetAndSet() {
98+
m := collections.NewBiMap([]collections.MapEntry[string, string]{
99+
{"foo", "Hello"},
100+
{"bar", "World"},
101+
})
102+
103+
val1, _ := m.GetAndSet("foo", "Hi")
104+
val2, _ := m.Get("foo")
105+
106+
fmt.Println(val1)
107+
fmt.Println(val2)
108+
// Output:
109+
// Hello
110+
// Hi
111+
}
112+
97113
func ExampleBiMap_Has() {
98114
m := collections.NewBiMap([]collections.MapEntry[string, string]{
99115
{"foo", "Hello"},

collections/CiMap.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,9 @@ func (self *CiMap[K, V]) Has(key K) bool {
6565
return self.Map.Has(K(strings.ToLower(string(key))))
6666
}
6767

68-
// Retrieves a value by the given key. If the key doesn't exist yet, invokes the `setter` function
69-
// to set the value and return it.
70-
//
71-
// This function is atomic.
72-
func (self *CiMap[K, V]) EnsureGet(key K, setter func() V) V {
68+
// Retrieves a value by the given key. If the key doesn't exist yet, invokes the `init` function
69+
// for setting the value and return it.
70+
func (self *CiMap[K, V]) Use(key K, init func() V) V {
7371
self.mut.Lock()
7472
defer self.mut.Unlock()
7573

@@ -78,7 +76,7 @@ func (self *CiMap[K, V]) EnsureGet(key K, setter func() V) V {
7876
var value V
7977

8078
if idx == -1 {
81-
value = setter()
79+
value = init()
8280
self.set(key, value)
8381
} else {
8482
record := self.records[idx]
@@ -88,6 +86,24 @@ func (self *CiMap[K, V]) EnsureGet(key K, setter func() V) V {
8886
return value
8987
}
9088

89+
// Retrieves the previous value by the given key and set a new value.
90+
func (self *CiMap[K, V]) GetAndSet(key K, value V) (V, bool) {
91+
self.mut.Lock()
92+
defer self.mut.Unlock()
93+
94+
id := strings.ToLower(string(key))
95+
idx := self.findIndex(K(id))
96+
97+
if idx == -1 {
98+
self.set(key, value)
99+
return *new(V), false
100+
} else {
101+
record := self.records[idx]
102+
self.set(key, value)
103+
return record.Value, true
104+
}
105+
}
106+
91107
// Removes the key-value pair by the given key.
92108
func (self *CiMap[K, V]) Delete(key K) bool {
93109
self.mut.Lock()

collections/CiMap_example_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ func ExampleCiMap_Has() {
7979
// false
8080
}
8181

82-
func ExampleCiMap_EnsureGet() {
82+
func ExampleCiMap_Use() {
8383
m := collections.NewCiMap([]collections.MapEntry[string, string]{
8484
{"foo", "Hi"},
8585
})
8686

87-
val1 := m.EnsureGet("Foo", func() string { return "Hello" })
88-
val2 := m.EnsureGet("Bar", func() string { return "World" })
87+
val1 := m.Use("Foo", func() string { return "Hello" })
88+
val2 := m.Use("Bar", func() string { return "World" })
8989

9090
fmt.Println(val1)
9191
fmt.Println(val2)
@@ -94,6 +94,22 @@ func ExampleCiMap_EnsureGet() {
9494
// World
9595
}
9696

97+
func ExampleCiMap_GetAndSet() {
98+
m := collections.NewCiMap([]collections.MapEntry[string, string]{
99+
{"foo", "Hello"},
100+
{"bar", "World"},
101+
})
102+
103+
val1, _ := m.GetAndSet("Foo", "Hi")
104+
val2, _ := m.Get("foo")
105+
106+
fmt.Println(val1)
107+
fmt.Println(val2)
108+
// Output:
109+
// Hello
110+
// Hi
111+
}
112+
97113
func ExampleCiMap_Delete() {
98114
m := collections.NewCiMap([]collections.MapEntry[string, string]{
99115
{"Foo", "Hello"},

collections/Map.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,17 @@ func (self *Map[K, V]) Has(key K) bool {
9797
return idx != -1
9898
}
9999

100-
// Retrieves a value by the given key. If the key doesn't exist yet, invokes the `setter` function
101-
// to set the value and return it.
102-
//
103-
// This function is atomic.
104-
func (self *Map[K, V]) EnsureGet(key K, setter func() V) V {
100+
// Retrieves a value by the given key. If the key doesn't exist yet, invokes the `init` function
101+
// for setting the value and return it.
102+
func (self *Map[K, V]) Use(key K, init func() V) V {
105103
self.mut.Lock()
106104
defer self.mut.Unlock()
107105

108106
idx := self.findIndex(key)
109107
var value V
110108

111109
if idx == -1 {
112-
value = setter()
110+
value = init()
113111
self.set(key, value)
114112
} else {
115113
record := self.records[idx]
@@ -119,6 +117,23 @@ func (self *Map[K, V]) EnsureGet(key K, setter func() V) V {
119117
return value
120118
}
121119

120+
// Retrieves the previous value by the given key and set a new value.
121+
func (self *Map[K, V]) GetAndSet(key K, value V) (V, bool) {
122+
self.mut.Lock()
123+
defer self.mut.Unlock()
124+
125+
idx := self.findIndex(key)
126+
127+
if idx == -1 {
128+
self.set(key, value)
129+
return *new(V), false
130+
} else {
131+
record := self.records[idx]
132+
self.set(key, value)
133+
return record.Value, true
134+
}
135+
}
136+
122137
func (self *Map[K, V]) deleteAt(idx int) bool {
123138
if idx == -1 {
124139
return false

collections/Map_example_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ func ExampleMap_Has() {
7979
// false
8080
}
8181

82-
func ExampleMap_EnsureGet() {
82+
func ExampleMap_Use() {
8383
m := collections.NewMap([]collections.MapEntry[string, string]{
8484
{"foo", "Hi"},
8585
})
8686

87-
val1 := m.EnsureGet("foo", func() string { return "Hello" })
88-
val2 := m.EnsureGet("bar", func() string { return "World" })
87+
val1 := m.Use("foo", func() string { return "Hello" })
88+
val2 := m.Use("bar", func() string { return "World" })
8989

9090
fmt.Println(val1)
9191
fmt.Println(val2)
@@ -94,6 +94,22 @@ func ExampleMap_EnsureGet() {
9494
// World
9595
}
9696

97+
func ExampleMap_GetAndSet() {
98+
m := collections.NewMap([]collections.MapEntry[string, string]{
99+
{"foo", "Hello"},
100+
{"bar", "World"},
101+
})
102+
103+
val1, _ := m.GetAndSet("foo", "Hi")
104+
val2, _ := m.Get("foo")
105+
106+
fmt.Println(val1)
107+
fmt.Println(val2)
108+
// Output:
109+
// Hello
110+
// Hi
111+
}
112+
97113
func ExampleMap_Delete() {
98114
m := collections.NewMap([]collections.MapEntry[string, string]{
99115
{"foo", "Hello"},

collections/Set.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewSet[T comparable](base []T) *Set[T] {
2626

2727
// Adds an item to the set. If the item already exists, the set remains untouched.
2828
func (self *Set[T]) Add(item T) *Set[T] {
29-
self.m.EnsureGet(item, func() int { return self.m.size })
29+
self.m.Use(item, func() int { return self.m.size })
3030
return self
3131
}
3232

throttle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func Throttle[A any, R any, Fn func(arg A) (R, error)](
8787
if forKey == "" {
8888
cache = &throttleCache[R]{key: "", mut: &sync.Mutex{}}
8989
} else {
90-
cache = (throttleCaches.EnsureGet(forKey, func() any {
90+
cache = (throttleCaches.Use(forKey, func() any {
9191
return any(&throttleCache[R]{key: forKey, mut: &sync.Mutex{}})
9292
})).(*throttleCache[R])
9393
}

0 commit comments

Comments
 (0)