Documentation ¶
Overview ¶
* Package syncx provides utility api for sync container or lock
Index ¶
- type Map
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) Copy() *Map[K, V]
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) Equals(mp *Map[K, V], eql base.EQL[V]) bool
- func (m *Map[K, V]) Exist(key K) bool
- func (m *Map[K, V]) ExistValue(value V) (k K, exist bool)
- func (m *Map[K, V]) ExistValueComparable(v base.Comparable[V]) (k K, exist bool)
- func (m *Map[K, V]) ExistValueWithComparator(value V, equal base.EQL[V]) (k K, exist bool)
- func (m *Map[K, V]) IsEmpty() (empty bool)
- func (m *Map[K, V]) Keys() []K
- func (m *Map[K, V]) Load(key K) (value V, ok bool)
- func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)
- func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *Map[K, V]) MaxKey(compare base.CMP[K]) (key K, v V)
- func (m *Map[K, V]) MaxValue(compare base.CMP[V]) (key K, v V)
- func (m *Map[K, V]) MinKey(compare base.CMP[K]) (key K, v V)
- func (m *Map[K, V]) MinValue(compare base.CMP[V]) (key K, v V)
- func (m *Map[K, V]) Range(f base.BiFunc[bool, K, V])
- func (m *Map[K, V]) Replace(key K, oldValue, newValue V, equal base.EQL[V]) bool
- func (m *Map[K, V]) ReplaceByCondition(key K, c base.BiFunc[V, K, V]) bool
- func (m *Map[K, V]) Size() int
- func (m *Map[K, V]) Store(key K, value V)
- func (m *Map[K, V]) StoreAll(other *Map[K, V])
- func (m *Map[K, V]) StoreAllOrigin(other map[K]V)
- func (m *Map[K, V]) ToMap() map[K]V
- func (m *Map[K, V]) Values() []V
- type Pool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is like a Go map[interface{}]interface{} but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time. By generics feature supports, all api will be more readable and safty.
The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.
The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.
The zero Map is empty and ready for use. A Map must not be copied after first use.
func NewMap ¶
func NewMap[K comparable, V any]() *Map[K, V]
NewMap create a new map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/concurrent/syncx" ) type MapExamplePojo struct { Name string } func (mp *MapExamplePojo) CompareTo(v *MapExamplePojo) int { return strings.Compare(mp.Name, v.Name) } func newMapExamplePojo(name string) *MapExamplePojo { return &MapExamplePojo{name} } func main() { mp := syncx.NewMap[string, *MapExamplePojo]() v := newMapExamplePojo("!") mp.Store("hello", v) v2, ok := mp.Load("hello") fmt.Println(v2.Name, ok) }
Output: ! true
func NewMapByInitial ¶
func NewMapByInitial[K comparable, V any](mmp map[K]V) *Map[K, V]
NewMapByInitial create a new map and store key and value from origin map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/concurrent/syncx" ) type MapExamplePojo struct { Name string } func (mp *MapExamplePojo) CompareTo(v *MapExamplePojo) int { return strings.Compare(mp.Name, v.Name) } func newMapExamplePojo(name string) *MapExamplePojo { return &MapExamplePojo{name} } func main() { mmp := map[string]*MapExamplePojo{ "key1": newMapExamplePojo("hello"), "key2": newMapExamplePojo("world"), } mp := syncx.NewMapByInitial(mmp) visitedCount := 0 mp.Range(func(s string, mep *MapExamplePojo) bool { // visit all elements here visitedCount++ return true }) fmt.Println(visitedCount) fmt.Println(mp.Size()) fmt.Println(mp.Exist("key1")) fmt.Println(mp.ExistValue(newMapExamplePojo("world"))) // LoadOrStore v, loaded := mp.LoadOrStore("key1", newMapExamplePojo("value1")) fmt.Println(v.Name, loaded) v, loaded = mp.LoadOrStore("key3", newMapExamplePojo("value3")) fmt.Println(v.Name, loaded) // LoadAndDelete v, loaded = mp.LoadAndDelete("key3") fmt.Println(v.Name, loaded) }
Output: 2 2 true key2 true hello true value3 false value3 true
func (*Map[K, V]) Equals ¶ added in v1.0.6
Equal test and return if all keys and values are some to
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) mp2 := mp.Copy() equal := mp.Equals(mp2, func(s1, s2 string) bool { return s1 == s2 }) fmt.Println(equal) }
Output: true
func (*Map[K, V]) ExistValue ¶
ExistValue return true if value exist
func (*Map[K, V]) ExistValueComparable ¶
func (m *Map[K, V]) ExistValueComparable(v base.Comparable[V]) (k K, exist bool)
ExistValue return true if value exist
func (*Map[K, V]) ExistValueWithComparator ¶
ExistValue return true if value exist
func (*Map[K, V]) Load ¶
Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) v, exist := mp.Load("key1") fmt.Println(v, exist) v, exist = mp.Load("key12") fmt.Println(v, exist) }
Output: hello true false
func (*Map[K, V]) LoadAndDelete ¶
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) v, loaded := mp.LoadAndDelete("key1") fmt.Println(v, loaded) v, loaded = mp.LoadAndDelete("key1") fmt.Println(v, loaded) }
Output: hello true false
func (*Map[K, V]) LoadOrStore ¶
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) v, loaded := mp.LoadOrStore("key1", "welcome") fmt.Println(v, loaded) v, loaded = mp.LoadOrStore("key3", "welcome") fmt.Println(v, loaded) }
Output: hello true welcome false
func (*Map[K, V]) MaxKey ¶
MaxKey to return max key in the map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) k, v := mp.MaxKey(func(s1, s2 string) int { return strings.Compare(s1, s2) }) fmt.Println(k, v) }
Output: key2 world
func (*Map[K, V]) MaxValue ¶
MaxValue to return max value in the map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) k, v := mp.MaxValue(func(s1, s2 string) int { return strings.Compare(s1, s2) }) fmt.Println(k, v) }
Output: key2 world
func (*Map[K, V]) MinKey ¶
MinKey to return min key in the map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) k, v := mp.MinKey(func(s1, s2 string) int { return strings.Compare(s1, s2) }) fmt.Println(k, v) }
Output: key1 hello
func (*Map[K, V]) MinValue ¶
MinValue to return min value in the map
Example ¶
package main import ( "fmt" "strings" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) k, v := mp.MinValue(func(s1, s2 string) int { return strings.Compare(s1, s2) }) fmt.Println(k, v) }
Output: key1 hello
func (*Map[K, V]) Range ¶
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m.
Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) count := 0 mp.Range(func(s1, s2 string) bool { count++ return true }) fmt.Println(count) }
Output: 2
func (*Map[K, V]) Replace ¶
Replace replaces the value for key if value compare condition
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) success := mp.Replace("key1", "hello", "welcome", func(s1, s2 string) bool { return s1 == s2 }) v, exist := mp.Load("key1") fmt.Println(v, exist) fmt.Println(success) }
Output: welcome true true
func (*Map[K, V]) ReplaceByCondition ¶
ReplaceByCondition replaces the value for key if value compare condition
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) success := mp.ReplaceByCondition("key1", func(key, oldvalue string) string { if oldvalue == "hello" { return "welcome" } return oldvalue }) v, exist := mp.Load("key1") fmt.Println(v, exist) fmt.Println(success) }
Output: welcome true true
func (*Map[K, V]) Store ¶
func (m *Map[K, V]) Store(key K, value V)
Store sets the value for a key.
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) mp.Store("key3", "!") v, exist := mp.Load("key3") fmt.Println(v, exist) }
Output: ! true
func (*Map[K, V]) StoreAll ¶
StoreAll sets all the key and values to map.
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMap[string, string]() mp1 := syncx.NewMapByInitial(mmp) mp.StoreAll(mp1) v, exist := mp.Load("key1") fmt.Println(v, exist) }
Output: hello true
func (*Map[K, V]) StoreAllOrigin ¶
func (m *Map[K, V]) StoreAllOrigin(other map[K]V)
StoreAll sets all the key and values to map.
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMap[string, string]() mp.StoreAllOrigin(mmp) v, exist := mp.Load("key1") fmt.Println(v, exist) }
Output: hello true
func (*Map[K, V]) ToMap ¶
func (m *Map[K, V]) ToMap() map[K]V
ToMap convert key and value to origin map struct
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) func main() { mmp := map[string]string{ "key1": ("hello"), "key2": ("world"), } mp := syncx.NewMapByInitial(mmp) mmp1 := mp.ToMap() fmt.Println(len(mmp1)) }
Output: 2
type Pool ¶
func NewPool ¶
NewPoolX create a new PoolX
Example ¶
package main import ( "fmt" "github.com/jhunters/goassist/concurrent/syncx" ) type ExamplePoolPojo struct { name string } func main() { name1 := "matt" name2 := "matthew" p := syncx.NewPool(func() *ExamplePoolPojo { return &ExamplePoolPojo{name1} }) p.Put(&ExamplePoolPojo{name2}) get1 := p.Get() fmt.Println(get1.name) fmt.Println(p.Get().name) p.Put(get1) fmt.Println(p.Get().name) }
Output: matthew matt matthew