improve performance for Map/Keys/Values functions for gmap; add unit test cases for gtree.RedBlackTree

This commit is contained in:
John
2019-05-12 21:11:26 +08:00
parent cf1077bec4
commit 17b29cd19f
8 changed files with 169 additions and 35 deletions

View File

@ -55,8 +55,8 @@ func (m *AnyAnyMap) Clone(unsafe ...bool) *AnyAnyMap {
// Map returns a copy of the data of the hash map.
func (m *AnyAnyMap) Map() map[interface{}]interface{} {
data := make(map[interface{}]interface{})
m.mu.RLock()
data := make(map[interface{}]interface{}, len(m.data))
for k, v := range m.data {
data[k] = v
}
@ -234,9 +234,11 @@ func (m *AnyAnyMap) Removes(keys []interface{}) {
// Keys returns all keys of the map as a slice.
func (m *AnyAnyMap) Keys() []interface{} {
m.mu.RLock()
keys := make([]interface{}, 0)
keys := make([]interface{}, len(m.data))
index := 0
for key := range m.data {
keys = append(keys, key)
keys[index] = key
index++
}
m.mu.RUnlock()
return keys
@ -245,9 +247,11 @@ func (m *AnyAnyMap) Keys() []interface{} {
// Values returns all values of the map as a slice.
func (m *AnyAnyMap) Values() []interface{} {
m.mu.RLock()
values := make([]interface{}, 0)
values := make([]interface{}, len(m.data))
index := 0
for _, value := range m.data {
values = append(values, value)
values[index] = value
index++
}
m.mu.RUnlock()
return values

View File

@ -57,8 +57,8 @@ func (m *IntAnyMap) Clone() *IntAnyMap {
// Map returns a copy of the data of the hash map.
func (m *IntAnyMap) Map() map[int]interface{} {
data := make(map[int]interface{})
m.mu.RLock()
data := make(map[int]interface{}, len(m.data))
for k, v := range m.data {
data[k] = v
}
@ -238,9 +238,11 @@ func (m *IntAnyMap) Remove(key int) interface{} {
// Keys returns all keys of the map as a slice.
func (m *IntAnyMap) Keys() []int {
m.mu.RLock()
keys := make([]int, 0)
keys := make([]int, len(m.data))
index := 0
for key := range m.data {
keys = append(keys, key)
keys[index] = key
index++
}
m.mu.RUnlock()
return keys
@ -249,9 +251,11 @@ func (m *IntAnyMap) Keys() []int {
// Values returns all values of the map as a slice.
func (m *IntAnyMap) Values() []interface{} {
m.mu.RLock()
values := make([]interface{}, 0)
values := make([]interface{}, len(m.data))
index := 0
for _, value := range m.data {
values = append(values, value)
values[index] = value
index++
}
m.mu.RUnlock()
return values

View File

@ -54,8 +54,8 @@ func (m *IntIntMap) Clone() *IntIntMap {
// Map returns a copy of the data of the hash map.
func (m *IntIntMap) Map() map[int]int {
data := make(map[int]int)
m.mu.RLock()
data := make(map[int]int, len(m.data))
for k, v := range m.data {
data[k] = v
}
@ -212,9 +212,11 @@ func (m *IntIntMap) Remove(key int) int {
// Keys returns all keys of the map as a slice.
func (m *IntIntMap) Keys() []int {
m.mu.RLock()
keys := make([]int, 0)
keys := make([]int, len(m.data))
index := 0
for key := range m.data {
keys = append(keys, key)
keys[index] = key
index++
}
m.mu.RUnlock()
return keys
@ -223,9 +225,11 @@ func (m *IntIntMap) Keys() []int {
// Values returns all values of the map as a slice.
func (m *IntIntMap) Values() []int {
m.mu.RLock()
values := make([]int, 0)
values := make([]int, len(m.data))
index := 0
for _, value := range m.data {
values = append(values, value)
values[index] = value
index++
}
m.mu.RUnlock()
return values

View File

@ -55,8 +55,8 @@ func (m *IntStrMap) Clone() *IntStrMap {
// Map returns a copy of the data of the hash map.
func (m *IntStrMap) Map() map[int]string {
data := make(map[int]string)
m.mu.RLock()
data := make(map[int]string, len(m.data))
for k, v := range m.data {
data[k] = v
}
@ -213,9 +213,11 @@ func (m *IntStrMap) Remove(key int) string {
// Keys returns all keys of the map as a slice.
func (m *IntStrMap) Keys() []int {
m.mu.RLock()
keys := make([]int, 0)
keys := make([]int, len(m.data))
index := 0
for key := range m.data {
keys = append(keys, key)
keys[index] = key
index++
}
m.mu.RUnlock()
return keys
@ -224,9 +226,11 @@ func (m *IntStrMap) Keys() []int {
// Values returns all values of the map as a slice.
func (m *IntStrMap) Values() []string {
m.mu.RLock()
values := make([]string, 0)
values := make([]string, len(m.data))
index := 0
for _, value := range m.data {
values = append(values, value)
values[index] = value
index++
}
m.mu.RUnlock()
return values

View File

@ -57,8 +57,8 @@ func (m *StrAnyMap) Clone() *StrAnyMap {
// Map returns a copy of the data of the hash map.
func (m *StrAnyMap) Map() map[string]interface{} {
data := make(map[string]interface{})
m.mu.RLock()
data := make(map[string]interface{}, len(m.data))
for k, v := range m.data {
data[k] = v
}
@ -238,9 +238,11 @@ func (m *StrAnyMap) Remove(key string) interface{} {
// Keys returns all keys of the map as a slice.
func (m *StrAnyMap) Keys() []string {
m.mu.RLock()
keys := make([]string, 0)
keys := make([]string, len(m.data))
index := 0
for key := range m.data {
keys = append(keys, key)
keys[index] = key
index++
}
m.mu.RUnlock()
return keys
@ -249,9 +251,11 @@ func (m *StrAnyMap) Keys() []string {
// Values returns all values of the map as a slice.
func (m *StrAnyMap) Values() []interface{} {
m.mu.RLock()
values := make([]interface{}, 0)
values := make([]interface{}, len(m.data))
index := 0
for _, value := range m.data {
values = append(values, value)
values[index] = value
index++
}
m.mu.RUnlock()
return values

View File

@ -56,8 +56,8 @@ func (m *StrIntMap) Clone() *StrIntMap {
// Map returns a copy of the data of the hash map.
func (m *StrIntMap) Map() map[string]int {
data := make(map[string]int)
m.mu.RLock()
data := make(map[string]int, len(m.data))
for k, v := range m.data {
data[k] = v
}
@ -216,9 +216,11 @@ func (m *StrIntMap) Remove(key string) int {
// Keys returns all keys of the map as a slice.
func (m *StrIntMap) Keys() []string {
m.mu.RLock()
keys := make([]string, 0)
keys := make([]string, len(m.data))
index := 0
for key := range m.data {
keys = append(keys, key)
keys[index] = key
index++
}
m.mu.RUnlock()
return keys
@ -227,9 +229,11 @@ func (m *StrIntMap) Keys() []string {
// Values returns all values of the map as a slice.
func (m *StrIntMap) Values() []int {
m.mu.RLock()
values := make([]int, 0)
values := make([]int, len(m.data))
index := 0
for _, value := range m.data {
values = append(values, value)
values[index] = value
index++
}
m.mu.RUnlock()
return values

View File

@ -55,8 +55,8 @@ func (m *StrStrMap) Clone() *StrStrMap {
// Map returns a copy of the data of the hash map.
func (m *StrStrMap) Map() map[string]string {
data := make(map[string]string)
m.mu.RLock()
data := make(map[string]string, len(m.data))
for k, v := range m.data {
data[k] = v
}
@ -215,9 +215,11 @@ func (m *StrStrMap) Remove(key string) string {
// Keys returns all keys of the map as a slice.
func (m *StrStrMap) Keys() []string {
m.mu.RLock()
keys := make([]string, 0)
keys := make([]string, len(m.data))
index := 0
for key := range m.data {
keys = append(keys, key)
keys[index] = key
index++
}
m.mu.RUnlock()
return keys
@ -226,9 +228,11 @@ func (m *StrStrMap) Keys() []string {
// Values returns all values of the map as a slice.
func (m *StrStrMap) Values() []string {
m.mu.RLock()
values := make([]string, 0)
values := make([]string, len(m.data))
index := 0
for _, value := range m.data {
values = append(values, value)
values[index] = value
index++
}
m.mu.RUnlock()
return values

View File

@ -0,0 +1,106 @@
// Copyright 2017-2019 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 gtree_test
import (
"github.com/gogf/gf/g/container/gtree"
"github.com/gogf/gf/g/test/gtest"
"github.com/gogf/gf/g/util/gutil"
"testing"
)
func getValue() interface{} {
return 3
}
func Test_RedBlackTree_Basic(t *testing.T) {
gtest.Case(t, func() {
m := gtree.NewRedBlackTree(gutil.ComparatorString)
m.Set("key1", "val1")
gtest.Assert(m.Keys(), []interface{}{"key1"})
gtest.Assert(m.Get("key1"), "val1")
gtest.Assert(m.Size(), 1)
gtest.Assert(m.IsEmpty(), false)
gtest.Assert(m.GetOrSet("key2", "val2"), "val2")
gtest.Assert(m.SetIfNotExist("key2", "val2"), false)
gtest.Assert(m.SetIfNotExist("key3", "val3"), true)
gtest.Assert(m.Remove("key2"), "val2")
gtest.Assert(m.Contains("key2"), false)
gtest.AssertIN("key3", m.Keys())
gtest.AssertIN("key1", m.Keys())
gtest.AssertIN("val3", m.Values())
gtest.AssertIN("val1", m.Values())
m.Flip()
gtest.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
m.Clear()
gtest.Assert(m.Size(), 0)
gtest.Assert(m.IsEmpty(), true)
m2 := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
gtest.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
})
}
func Test_RedBlackTree_Set_Fun(t *testing.T) {
m := gtree.NewRedBlackTree(gutil.ComparatorString)
m.GetOrSetFunc("fun", getValue)
m.GetOrSetFuncLock("funlock", getValue)
gtest.Assert(m.Get("funlock"), 3)
gtest.Assert(m.Get("fun"), 3)
m.GetOrSetFunc("fun", getValue)
gtest.Assert(m.SetIfNotExistFunc("fun", getValue), false)
gtest.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
}
func Test_RedBlackTree_Batch(t *testing.T) {
m := gtree.NewRedBlackTree(gutil.ComparatorString)
m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
gtest.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
m.Removes([]interface{}{"key1", 1})
gtest.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
}
func Test_RedBlackTree_Iterator(t *testing.T){
expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
m := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, expect)
m.Iterator(func(k interface{}, v interface{}) bool {
gtest.Assert(expect[k], v)
return true
})
// 断言返回值对遍历控制
i := 0
j := 0
m.Iterator(func(k interface{}, v interface{}) bool {
i++
return true
})
m.Iterator(func(k interface{}, v interface{}) bool {
j++
return false
})
gtest.Assert(i, 2)
gtest.Assert(j, 1)
}
func Test_RedBlackTree_Clone(t *testing.T) {
//clone 方法是深克隆
m := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
m_clone := m.Clone()
m.Remove(1)
//修改原 map,clone 后的 map 不影响
gtest.AssertIN(1, m_clone.Keys())
m_clone.Remove("key1")
//修改clone map,原 map 不影响
gtest.AssertIN("key1", m.Keys())
}