2019-05-08 17:21:18 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with gm file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gogf/gf/g/internal/rwmutex"
|
|
|
|
"github.com/gogf/gf/g/util/gconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IntStrMap struct {
|
|
|
|
mu *rwmutex.RWMutex
|
|
|
|
data map[int]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIntStrMap returns an empty IntStrMap object.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <unsafe> used to specify whether using map in un-concurrent-safety,
|
2019-05-08 17:21:18 +08:00
|
|
|
// which is false in default, means concurrent-safe.
|
|
|
|
func NewIntStrMap(unsafe ...bool) *IntStrMap {
|
|
|
|
return &IntStrMap{
|
|
|
|
mu : rwmutex.New(unsafe...),
|
|
|
|
data : make(map[int]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 20:47:25 +08:00
|
|
|
// NewIntStrMapFrom returns a hash map from given map <data>.
|
|
|
|
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
2019-05-08 17:21:18 +08:00
|
|
|
// there might be some concurrent-safe issues when changing the map outside.
|
|
|
|
func NewIntStrMapFrom(data map[int]string, unsafe ...bool) *IntStrMap {
|
|
|
|
return &IntStrMap{
|
|
|
|
mu : rwmutex.New(unsafe...),
|
|
|
|
data : data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterator iterates the hash map with custom callback function <f>.
|
|
|
|
// If <f> returns true, then it continues iterating; or false to stop.
|
|
|
|
func (m *IntStrMap) Iterator(f func(k int, v string) bool) {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
for k, v := range m.data {
|
|
|
|
if !f(k, v) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone returns a new hash map with copy of current map data.
|
|
|
|
func (m *IntStrMap) Clone() *IntStrMap {
|
|
|
|
return NewIntStrMapFrom(m.Map(), !m.mu.IsSafe())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map returns a copy of the data of the hash map.
|
|
|
|
func (m *IntStrMap) Map() map[int]string {
|
|
|
|
m.mu.RLock()
|
2019-05-12 21:11:26 +08:00
|
|
|
data := make(map[int]string, len(m.data))
|
2019-05-08 17:21:18 +08:00
|
|
|
for k, v := range m.data {
|
|
|
|
data[k] = v
|
|
|
|
}
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets key-value to the hash map.
|
|
|
|
func (m *IntStrMap) Set(key int, val string) {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.data[key] = val
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets batch sets key-values to the hash map.
|
|
|
|
func (m *IntStrMap) Sets(data map[int]string) {
|
|
|
|
m.mu.Lock()
|
|
|
|
for k, v := range data {
|
|
|
|
m.data[k] = v
|
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-05-10 13:38:06 +08:00
|
|
|
// Search searches the map with given <key>.
|
|
|
|
// Second return parameter <found> is true if key was found, otherwise false.
|
|
|
|
func (m *IntStrMap) Search(key int) (value string, found bool) {
|
|
|
|
m.mu.RLock()
|
|
|
|
value, found = m.data[key]
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-08 17:21:18 +08:00
|
|
|
// Get returns the value by given <key>.
|
|
|
|
func (m *IntStrMap) Get(key int) string {
|
|
|
|
m.mu.RLock()
|
|
|
|
val, _ := m.data[key]
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
|
|
|
// if not exists, set value to the map with given <key>,
|
|
|
|
// or else just return the existing value.
|
|
|
|
//
|
|
|
|
// It returns value with given <key>.
|
|
|
|
func (m *IntStrMap) doSetWithLockCheck(key int, value string) string {
|
|
|
|
m.mu.Lock()
|
|
|
|
if v, ok := m.data[key]; ok {
|
|
|
|
m.mu.Unlock()
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
m.data[key] = value
|
|
|
|
m.mu.Unlock()
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrSet returns the value by key,
|
|
|
|
// or set value with given <value> if not exist and returns this value.
|
|
|
|
func (m *IntStrMap) GetOrSet(key int, value string) string {
|
2019-05-10 13:38:06 +08:00
|
|
|
if v, ok := m.Search(key); !ok {
|
2019-05-08 17:21:18 +08:00
|
|
|
return m.doSetWithLockCheck(key, value)
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrSetFunc returns the value by key,
|
|
|
|
// or sets value with return value of callback function <f> if not exist and returns this value.
|
|
|
|
func (m *IntStrMap) GetOrSetFunc(key int, f func() string) string {
|
2019-05-10 13:38:06 +08:00
|
|
|
if v, ok := m.Search(key); !ok {
|
2019-05-08 17:21:18 +08:00
|
|
|
return m.doSetWithLockCheck(key, f())
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrSetFuncLock returns the value by key,
|
|
|
|
// or sets value with return value of callback function <f> if not exist and returns this value.
|
|
|
|
//
|
|
|
|
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
|
|
|
// with mutex.Lock of the hash map.
|
|
|
|
func (m *IntStrMap) GetOrSetFuncLock(key int, f func() string) string {
|
2019-05-10 13:38:06 +08:00
|
|
|
if v, ok := m.Search(key); !ok {
|
2019-05-08 17:21:18 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2019-05-10 13:38:06 +08:00
|
|
|
if v, ok = m.data[key]; ok {
|
2019-05-08 17:21:18 +08:00
|
|
|
return v
|
|
|
|
}
|
2019-05-10 13:38:06 +08:00
|
|
|
v = f()
|
|
|
|
m.data[key] = v
|
|
|
|
return v
|
2019-05-08 17:21:18 +08:00
|
|
|
} else {
|
2019-05-10 13:38:06 +08:00
|
|
|
return v
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIfNotExist sets <value> to the map if the <key> does not exist, then return true.
|
|
|
|
// It returns false if <key> exists, and <value> would be ignored.
|
|
|
|
func (m *IntStrMap) SetIfNotExist(key int, value string) bool {
|
|
|
|
if !m.Contains(key) {
|
|
|
|
m.doSetWithLockCheck(key, value)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIfNotExistFunc sets value with return value of callback function <f>, then return true.
|
|
|
|
// It returns false if <key> exists, and <value> would be ignored.
|
|
|
|
func (m *IntStrMap) SetIfNotExistFunc(key int, f func() string) bool {
|
|
|
|
if !m.Contains(key) {
|
|
|
|
m.doSetWithLockCheck(key, f())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIfNotExistFuncLock sets value with return value of callback function <f>, then return true.
|
|
|
|
// It returns false if <key> exists, and <value> would be ignored.
|
|
|
|
//
|
|
|
|
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
|
|
|
// it executes function <f> with mutex.Lock of the hash map.
|
|
|
|
func (m *IntStrMap) SetIfNotExistFuncLock(key int, f func() string) bool {
|
|
|
|
if !m.Contains(key) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if _, ok := m.data[key]; !ok {
|
|
|
|
m.data[key] = f()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes batch deletes values of the map by keys.
|
|
|
|
func (m *IntStrMap) Removes(keys []int) {
|
|
|
|
m.mu.Lock()
|
|
|
|
for _, key := range keys {
|
|
|
|
delete(m.data, key)
|
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes value from map by given <key>, and return this deleted value.
|
|
|
|
func (m *IntStrMap) Remove(key int) string {
|
|
|
|
m.mu.Lock()
|
|
|
|
val, exists := m.data[key]
|
|
|
|
if exists {
|
|
|
|
delete(m.data, key)
|
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keys returns all keys of the map as a slice.
|
|
|
|
func (m *IntStrMap) Keys() []int {
|
|
|
|
m.mu.RLock()
|
2019-05-12 21:11:26 +08:00
|
|
|
keys := make([]int, len(m.data))
|
|
|
|
index := 0
|
2019-05-08 17:21:18 +08:00
|
|
|
for key := range m.data {
|
2019-05-12 21:11:26 +08:00
|
|
|
keys[index] = key
|
|
|
|
index++
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values returns all values of the map as a slice.
|
|
|
|
func (m *IntStrMap) Values() []string {
|
|
|
|
m.mu.RLock()
|
2019-05-12 21:11:26 +08:00
|
|
|
values := make([]string, len(m.data))
|
|
|
|
index := 0
|
2019-05-08 17:21:18 +08:00
|
|
|
for _, value := range m.data {
|
2019-05-12 21:11:26 +08:00
|
|
|
values[index] = value
|
|
|
|
index++
|
2019-05-08 17:21:18 +08:00
|
|
|
}
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains checks whether a key exists.
|
|
|
|
// It returns true if the <key> exists, or else false.
|
|
|
|
func (m *IntStrMap) Contains(key int) bool {
|
|
|
|
m.mu.RLock()
|
|
|
|
_, exists := m.data[key]
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of the map.
|
|
|
|
func (m *IntStrMap) Size() int {
|
|
|
|
m.mu.RLock()
|
|
|
|
length := len(m.data)
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return length
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty checks whether the map is empty.
|
|
|
|
// It returns true if map is empty, or else false.
|
|
|
|
func (m *IntStrMap) IsEmpty() bool {
|
|
|
|
m.mu.RLock()
|
|
|
|
empty := len(m.data) == 0
|
|
|
|
m.mu.RUnlock()
|
|
|
|
return empty
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear deletes all data of the map, it will remake a new underlying data map.
|
|
|
|
func (m *IntStrMap) Clear() {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.data = make(map[int]string)
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LockFunc locks writing with given callback function <f> within RWMutex.Lock.
|
|
|
|
func (m *IntStrMap) LockFunc(f func(m map[int]string)) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
f(m.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
|
|
|
|
func (m *IntStrMap) RLockFunc(f func(m map[int]string)) {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
f(m.data)
|
|
|
|
}
|
|
|
|
|
2019-05-10 13:38:06 +08:00
|
|
|
// Flip exchanges key-value of the map to value-key.
|
2019-05-08 17:21:18 +08:00
|
|
|
func (m *IntStrMap) Flip() {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
n := make(map[int]string, len(m.data))
|
|
|
|
for k, v := range m.data {
|
|
|
|
n[gconv.Int(v)] = gconv.String(k)
|
|
|
|
}
|
|
|
|
m.data = n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges two hash maps.
|
|
|
|
// The <other> map will be merged into the map <m>.
|
|
|
|
func (m *IntStrMap) Merge(other *IntStrMap) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if other != m {
|
|
|
|
other.mu.RLock()
|
|
|
|
defer other.mu.RUnlock()
|
|
|
|
}
|
|
|
|
for k, v := range other.data {
|
|
|
|
m.data[k] = v
|
|
|
|
}
|
|
|
|
}
|