gf/container/gmap/gmap_list_map.go

563 lines
14 KiB
Go
Raw Normal View History

2019-05-12 20:43:52 +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/internal/json"
"github.com/gogf/gf/internal/empty"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/util/gconv"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/container/glist"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/internal/rwmutex"
2019-05-12 20:43:52 +08:00
)
2019-05-13 22:26:39 +08:00
type ListMap struct {
mu rwmutex.RWMutex
2019-06-19 09:06:52 +08:00
data map[interface{}]*glist.Element
list *glist.List
2019-05-12 20:43:52 +08:00
}
2019-05-13 22:26:39 +08:00
type gListMapNode struct {
2019-06-19 09:06:52 +08:00
key interface{}
value interface{}
2019-05-12 20:43:52 +08:00
}
2019-05-13 22:26:39 +08:00
// NewListMap returns an empty link map.
// ListMap is backed by a hash table to store values and doubly-linked list to store ordering.
2019-11-30 18:33:51 +08:00
// The parameter <safe> is used to specify whether using map in concurrent-safety,
// which is false in default.
func NewListMap(safe ...bool) *ListMap {
2019-05-13 22:26:39 +08:00
return &ListMap{
mu: rwmutex.Create(safe...),
2019-06-19 09:06:52 +08:00
data: make(map[interface{}]*glist.Element),
list: glist.New(),
2019-05-12 20:43:52 +08:00
}
}
2019-05-13 22:26:39 +08:00
// NewListMapFrom returns a link map from given map <data>.
2019-05-12 20:43:52 +08:00
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
// there might be some concurrent-safe issues when changing the map outside.
func NewListMapFrom(data map[interface{}]interface{}, safe ...bool) *ListMap {
m := NewListMap(safe...)
2019-06-19 09:06:52 +08:00
m.Sets(data)
return m
2019-05-12 20:43:52 +08:00
}
// Iterator is alias of IteratorAsc.
2019-06-19 09:06:52 +08:00
func (m *ListMap) Iterator(f func(key, value interface{}) bool) {
2019-05-12 20:43:52 +08:00
m.IteratorAsc(f)
}
// IteratorAsc iterates the map readonly in ascending order with given callback function <f>.
2019-05-12 20:43:52 +08:00
// If <f> returns true, then it continues iterating; or false to stop.
2019-06-19 09:06:52 +08:00
func (m *ListMap) IteratorAsc(f func(key interface{}, value interface{}) bool) {
m.mu.RLock()
defer m.mu.RUnlock()
if m.list != nil {
node := (*gListMapNode)(nil)
m.list.IteratorAsc(func(e *glist.Element) bool {
node = e.Value.(*gListMapNode)
return f(node.key, node.value)
})
}
2019-05-12 20:43:52 +08:00
}
// IteratorDesc iterates the map readonly in descending order with given callback function <f>.
2019-05-12 20:43:52 +08:00
// If <f> returns true, then it continues iterating; or false to stop.
2019-06-19 09:06:52 +08:00
func (m *ListMap) IteratorDesc(f func(key interface{}, value interface{}) bool) {
2019-05-12 20:43:52 +08:00
m.mu.RLock()
defer m.mu.RUnlock()
if m.list != nil {
node := (*gListMapNode)(nil)
m.list.IteratorDesc(func(e *glist.Element) bool {
node = e.Value.(*gListMapNode)
return f(node.key, node.value)
})
}
2019-05-12 20:43:52 +08:00
}
// Clone returns a new link map with copy of current map data.
func (m *ListMap) Clone(safe ...bool) *ListMap {
return NewListMapFrom(m.Map(), safe...)
2019-05-12 20:43:52 +08:00
}
// Clear deletes all data of the map, it will remake a new underlying data map.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Clear() {
2019-05-12 20:43:52 +08:00
m.mu.Lock()
m.data = make(map[interface{}]*glist.Element)
m.list = glist.New()
2019-05-12 20:43:52 +08:00
m.mu.Unlock()
}
2019-11-04 21:26:16 +08:00
// Replace the data of the map with given <data>.
func (m *ListMap) Replace(data map[interface{}]interface{}) {
m.mu.Lock()
m.data = make(map[interface{}]*glist.Element)
m.list = glist.New()
for key, value := range data {
if e, ok := m.data[key]; !ok {
m.data[key] = m.list.PushBack(&gListMapNode{key, value})
} else {
e.Value = &gListMapNode{key, value}
}
}
m.mu.Unlock()
}
2019-11-30 18:33:51 +08:00
// Map returns a copy of the underlying data of the map.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Map() map[interface{}]interface{} {
2019-06-19 09:06:52 +08:00
m.mu.RLock()
var node *gListMapNode
var data map[interface{}]interface{}
if m.list != nil {
data = make(map[interface{}]interface{}, len(m.data))
m.list.IteratorAsc(func(e *glist.Element) bool {
node = e.Value.(*gListMapNode)
data[node.key] = node.value
return true
})
}
2019-06-19 09:06:52 +08:00
m.mu.RUnlock()
return data
2019-05-12 20:43:52 +08:00
}
2019-11-30 18:33:51 +08:00
// MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.
func (m *ListMap) MapStrAny() map[string]interface{} {
m.mu.RLock()
var node *gListMapNode
var data map[string]interface{}
if m.list != nil {
data = make(map[string]interface{}, len(m.data))
m.list.IteratorAsc(func(e *glist.Element) bool {
node = e.Value.(*gListMapNode)
data[gconv.String(node.key)] = node.value
return true
})
}
m.mu.RUnlock()
return data
}
// FilterEmpty deletes all key-value pair of which the value is empty.
func (m *ListMap) FilterEmpty() {
m.mu.Lock()
if m.list != nil {
keys := make([]interface{}, 0)
node := (*gListMapNode)(nil)
m.list.IteratorAsc(func(e *glist.Element) bool {
node = e.Value.(*gListMapNode)
if empty.IsEmpty(node.value) {
keys = append(keys, node.key)
}
return true
})
if len(keys) > 0 {
for _, key := range keys {
if e, ok := m.data[key]; ok {
delete(m.data, key)
m.list.Remove(e)
}
}
}
}
m.mu.Unlock()
}
2019-05-12 20:43:52 +08:00
// Set sets key-value to the map.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Set(key interface{}, value interface{}) {
2019-06-19 09:06:52 +08:00
m.mu.Lock()
if m.data == nil {
m.data = make(map[interface{}]*glist.Element)
m.list = glist.New()
}
2019-06-19 09:06:52 +08:00
if e, ok := m.data[key]; !ok {
m.data[key] = m.list.PushBack(&gListMapNode{key, value})
} else {
e.Value = &gListMapNode{key, value}
}
m.mu.Unlock()
2019-05-12 20:43:52 +08:00
}
// Sets batch sets key-values to the map.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Sets(data map[interface{}]interface{}) {
2019-06-19 09:06:52 +08:00
m.mu.Lock()
if m.data == nil {
m.data = make(map[interface{}]*glist.Element)
m.list = glist.New()
}
2019-06-19 09:06:52 +08:00
for key, value := range data {
if e, ok := m.data[key]; !ok {
m.data[key] = m.list.PushBack(&gListMapNode{key, value})
} else {
e.Value = &gListMapNode{key, value}
}
}
m.mu.Unlock()
2019-05-12 20:43:52 +08:00
}
// Search searches the map with given <key>.
// Second return parameter <found> is true if key was found, otherwise false.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Search(key interface{}) (value interface{}, found bool) {
2019-05-12 20:43:52 +08:00
m.mu.RLock()
if m.data != nil {
if e, ok := m.data[key]; ok {
value = e.Value.(*gListMapNode).value
found = ok
}
2019-05-12 20:43:52 +08:00
}
m.mu.RUnlock()
return
}
// Get returns the value by given <key>.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Get(key interface{}) (value interface{}) {
2019-06-19 09:06:52 +08:00
m.mu.RLock()
if m.data != nil {
if e, ok := m.data[key]; ok {
value = e.Value.(*gListMapNode).value
}
2019-06-19 09:06:52 +08:00
}
m.mu.RUnlock()
return
2019-05-12 20:43:52 +08:00
}
// Pop retrieves and deletes an item from the map.
func (m *ListMap) Pop() (key, value interface{}) {
m.mu.Lock()
defer m.mu.Unlock()
for k, e := range m.data {
value = e.Value.(*gListMapNode).value
delete(m.data, k)
m.list.Remove(e)
return k, value
}
return
}
// Pops retrieves and deletes <size> items from the map.
// It returns all items if size == -1.
func (m *ListMap) Pops(size int) map[interface{}]interface{} {
m.mu.Lock()
defer m.mu.Unlock()
if size > len(m.data) || size == -1 {
size = len(m.data)
}
if size == 0 {
return nil
}
index := 0
newMap := make(map[interface{}]interface{}, size)
for k, e := range m.data {
value := e.Value.(*gListMapNode).value
delete(m.data, k)
m.list.Remove(e)
newMap[k] = value
index++
if index == size {
break
}
}
return newMap
}
2019-05-12 20:43:52 +08:00
// 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.
//
// When setting value, if <value> is type of <func() interface {}>,
// it will be executed with mutex.Lock of the map,
// and its return value will be set to the map with <key>.
//
// It returns value with given <key>.
2019-05-13 22:26:39 +08:00
func (m *ListMap) doSetWithLockCheck(key interface{}, value interface{}) interface{} {
2019-06-19 09:06:52 +08:00
m.mu.Lock()
defer m.mu.Unlock()
if m.data == nil {
m.data = make(map[interface{}]*glist.Element)
m.list = glist.New()
}
2019-06-19 09:06:52 +08:00
if e, ok := m.data[key]; ok {
return e.Value.(*gListMapNode).value
}
if f, ok := value.(func() interface{}); ok {
value = f()
}
2019-10-31 23:37:33 +08:00
if value != nil {
m.data[key] = m.list.PushBack(&gListMapNode{key, value})
}
2019-06-19 09:06:52 +08:00
return value
2019-05-12 20:43:52 +08:00
}
// GetOrSet returns the value by key,
// or sets value with given <value> if it does not exist and then returns this value.
2019-05-13 22:26:39 +08:00
func (m *ListMap) GetOrSet(key interface{}, value interface{}) interface{} {
2019-05-12 20:43:52 +08:00
if v, ok := m.Search(key); !ok {
2019-06-19 09:06:52 +08:00
return m.doSetWithLockCheck(key, value)
} else {
return v
}
2019-05-12 20:43:52 +08:00
}
// GetOrSetFunc returns the value by key,
// or sets value with returned value of callback function <f> if it does not exist
// and then returns this value.
2019-05-13 22:26:39 +08:00
func (m *ListMap) GetOrSetFunc(key interface{}, f func() interface{}) interface{} {
2019-05-12 20:43:52 +08:00
if v, ok := m.Search(key); !ok {
2019-06-19 09:06:52 +08:00
return m.doSetWithLockCheck(key, f())
} else {
return v
}
2019-05-12 20:43:52 +08:00
}
// GetOrSetFuncLock returns the value by key,
// or sets value with returned value of callback function <f> if it does not exist
// and then returns this value.
2019-05-12 20:43:52 +08:00
//
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
// with mutex.Lock of the map.
2019-05-13 22:26:39 +08:00
func (m *ListMap) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} {
2019-05-12 20:43:52 +08:00
if v, ok := m.Search(key); !ok {
2019-06-19 09:06:52 +08:00
return m.doSetWithLockCheck(key, f)
} else {
return v
}
2019-05-12 20:43:52 +08:00
}
// GetVar returns a Var with the value by given <key>.
// The returned Var is un-concurrent safe.
func (m *ListMap) GetVar(key interface{}) *gvar.Var {
return gvar.New(m.Get(key))
2019-05-12 20:43:52 +08:00
}
// GetVarOrSet returns a Var with result from GetVarOrSet.
// The returned Var is un-concurrent safe.
func (m *ListMap) GetVarOrSet(key interface{}, value interface{}) *gvar.Var {
return gvar.New(m.GetOrSet(key, value))
2019-05-12 20:43:52 +08:00
}
// GetVarOrSetFunc returns a Var with result from GetOrSetFunc.
// The returned Var is un-concurrent safe.
func (m *ListMap) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var {
return gvar.New(m.GetOrSetFunc(key, f))
2019-05-12 20:43:52 +08:00
}
// GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock.
// The returned Var is un-concurrent safe.
func (m *ListMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var {
return gvar.New(m.GetOrSetFuncLock(key, f))
2019-05-12 20:43:52 +08:00
}
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
2019-05-12 20:43:52 +08:00
// It returns false if <key> exists, and <value> would be ignored.
2019-05-13 22:26:39 +08:00
func (m *ListMap) SetIfNotExist(key interface{}, value interface{}) bool {
2019-06-19 09:06:52 +08:00
if !m.Contains(key) {
m.doSetWithLockCheck(key, value)
return true
}
return false
2019-05-12 20:43:52 +08:00
}
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
2019-05-12 20:43:52 +08:00
// It returns false if <key> exists, and <value> would be ignored.
2019-05-13 22:26:39 +08:00
func (m *ListMap) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
2019-05-12 20:43:52 +08:00
if !m.Contains(key) {
m.doSetWithLockCheck(key, f())
return true
}
return false
}
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
2019-05-12 20:43:52 +08:00
// 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 map.
2019-05-13 22:26:39 +08:00
func (m *ListMap) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool {
2019-05-12 20:43:52 +08:00
if !m.Contains(key) {
m.doSetWithLockCheck(key, f)
return true
}
return false
}
// Remove deletes value from map by given <key>, and return this deleted value.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Remove(key interface{}) (value interface{}) {
2019-06-19 09:06:52 +08:00
m.mu.Lock()
if m.data != nil {
if e, ok := m.data[key]; ok {
value = e.Value.(*gListMapNode).value
delete(m.data, key)
m.list.Remove(e)
}
2019-06-19 09:06:52 +08:00
}
m.mu.Unlock()
return
2019-05-12 20:43:52 +08:00
}
// Removes batch deletes values of the map by keys.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Removes(keys []interface{}) {
2019-05-12 20:43:52 +08:00
m.mu.Lock()
if m.data != nil {
for _, key := range keys {
if e, ok := m.data[key]; ok {
delete(m.data, key)
m.list.Remove(e)
}
2019-05-12 20:43:52 +08:00
}
}
m.mu.Unlock()
}
// Keys returns all keys of the map as a slice in ascending order.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Keys() []interface{} {
2019-06-19 09:06:52 +08:00
m.mu.RLock()
var (
keys = make([]interface{}, m.list.Len())
index = 0
)
if m.list != nil {
m.list.IteratorAsc(func(e *glist.Element) bool {
keys[index] = e.Value.(*gListMapNode).key
index++
return true
})
}
2019-06-19 09:06:52 +08:00
m.mu.RUnlock()
return keys
2019-05-12 20:43:52 +08:00
}
// Values returns all values of the map as a slice.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Values() []interface{} {
2019-06-19 09:06:52 +08:00
m.mu.RLock()
var (
values = make([]interface{}, m.list.Len())
index = 0
)
if m.list != nil {
m.list.IteratorAsc(func(e *glist.Element) bool {
values[index] = e.Value.(*gListMapNode).value
index++
return true
})
}
2019-06-19 09:06:52 +08:00
m.mu.RUnlock()
return values
2019-05-12 20:43:52 +08:00
}
// Contains checks whether a key exists.
// It returns true if the <key> exists, or else false.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Contains(key interface{}) (ok bool) {
2019-06-19 09:06:52 +08:00
m.mu.RLock()
if m.data != nil {
_, ok = m.data[key]
}
2019-06-19 09:06:52 +08:00
m.mu.RUnlock()
return
2019-05-12 20:43:52 +08:00
}
// Size returns the size of the map.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Size() (size int) {
2019-06-19 09:06:52 +08:00
m.mu.RLock()
size = len(m.data)
m.mu.RUnlock()
return
2019-05-12 20:43:52 +08:00
}
// IsEmpty checks whether the map is empty.
// It returns true if map is empty, or else false.
2019-05-13 22:26:39 +08:00
func (m *ListMap) IsEmpty() bool {
2019-06-19 09:06:52 +08:00
return m.Size() == 0
2019-05-12 20:43:52 +08:00
}
// Flip exchanges key-value of the map to value-key.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Flip() {
2019-06-19 09:06:52 +08:00
data := m.Map()
m.Clear()
for key, value := range data {
m.Set(value, key)
}
2019-05-12 20:43:52 +08:00
}
// Merge merges two link maps.
// The <other> map will be merged into the map <m>.
2019-05-13 22:26:39 +08:00
func (m *ListMap) Merge(other *ListMap) {
2019-06-19 09:06:52 +08:00
m.mu.Lock()
defer m.mu.Unlock()
if m.data == nil {
m.data = make(map[interface{}]*glist.Element)
m.list = glist.New()
}
2019-06-19 09:06:52 +08:00
if other != m {
other.mu.RLock()
defer other.mu.RUnlock()
}
2019-05-13 22:26:39 +08:00
node := (*gListMapNode)(nil)
2019-06-19 09:06:52 +08:00
other.list.IteratorAsc(func(e *glist.Element) bool {
node = e.Value.(*gListMapNode)
if e, ok := m.data[node.key]; !ok {
m.data[node.key] = m.list.PushBack(&gListMapNode{node.key, node.value})
} else {
e.Value = &gListMapNode{node.key, node.value}
}
return true
})
}
2020-09-02 19:53:58 +08:00
// String returns the map as a string.
func (m *ListMap) String() string {
b, _ := m.MarshalJSON()
return gconv.UnsafeBytesToStr(b)
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (m *ListMap) MarshalJSON() ([]byte, error) {
return json.Marshal(gconv.Map(m.Map()))
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (m *ListMap) UnmarshalJSON(b []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.data == nil {
m.data = make(map[interface{}]*glist.Element)
m.list = glist.New()
}
var data map[string]interface{}
if err := json.Unmarshal(b, &data); err != nil {
return err
}
for key, value := range data {
if e, ok := m.data[key]; !ok {
m.data[key] = m.list.PushBack(&gListMapNode{key, value})
} else {
e.Value = &gListMapNode{key, value}
}
}
return nil
}
// UnmarshalValue is an interface implement which sets any type of value for map.
func (m *ListMap) UnmarshalValue(value interface{}) (err error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.data == nil {
m.data = make(map[interface{}]*glist.Element)
m.list = glist.New()
}
for k, v := range gconv.Map(value) {
if e, ok := m.data[k]; !ok {
m.data[k] = m.list.PushBack(&gListMapNode{k, v})
} else {
e.Value = &gListMapNode{k, v}
}
}
return
}