mapx

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2023 License: Apache-2.0 Imports: 4 Imported by: 6 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys 返回 map 里面的所有的 key。 需要注意:这些 key 的顺序是随机。

func KeysValues

func KeysValues[K comparable, V any](m map[K]V) ([]K, []V)

KeysValues 返回 map 里面的所有的 key,value。 需要注意:这些 (key,value) 的顺序是随机,相对顺序是一致的。

func Values

func Values[K comparable, V any](m map[K]V) []V

Values 返回 map 里面的所有的 value。 需要注意:这些 value 的顺序是随机。

Types

type HashMap

type HashMap[T Hashable, ValType any] struct {
	// contains filtered or unexported fields
}

func NewHashMap

func NewHashMap[T Hashable, ValType any](size int) *HashMap[T, ValType]
Example
package main

import (
	"fmt"

	"github.com/ecodeclub/ekit/mapx"
)

func main() {
	m := mapx.NewHashMap[MockKey, int](10)
	_ = m.Put(MockKey{}, 123)
	val, _ := m.Get(MockKey{})
	fmt.Println(val)
}

type MockKey struct {
	values []int
}

func (m MockKey) Code() uint64 {
	res := 3
	for _, v := range m.values {
		res += v * 7
	}
	return uint64(res)
}

func (m MockKey) Equals(key any) bool {
	k, ok := key.(MockKey)
	if !ok {
		return false
	}
	if len(k.values) != len(m.values) {
		return false
	}
	if k.values == nil && m.values != nil {
		return false
	}
	if k.values != nil && m.values == nil {
		return false
	}
	for i, v := range m.values {
		if v != k.values[i] {
			return false
		}
	}
	return true
}
Output:
123

func (*HashMap[T, ValType]) Delete

func (m *HashMap[T, ValType]) Delete(key T) (ValType, bool)

Delete 第一个返回值为删除key的值,第二个是hashmap是否真的有这个key

func (*HashMap[T, ValType]) Get

func (m *HashMap[T, ValType]) Get(key T) (ValType, bool)

func (*HashMap[T, ValType]) Keys

func (m *HashMap[T, ValType]) Keys() []Hashable

Keys 返回 Hashmap 里面的所有的 key。 注意:key 的顺序是随机的。

func (*HashMap[T, ValType]) Put

func (m *HashMap[T, ValType]) Put(key T, val ValType) error

func (*HashMap[T, ValType]) Values

func (m *HashMap[T, ValType]) Values() []ValType

Values 返回 Hashmap 里面的所有的 value。 注意:value 的顺序是随机的。

type Hashable

type Hashable interface {
	// Code 返回该元素的哈希值
	// 注意:哈希值应该尽可能的均匀以避免冲突
	Code() uint64
	// Equals 比较两个元素是否相等。如果返回 true,那么我们会认为两个键是一样的。
	Equals(key any) bool
}

type TreeMap

type TreeMap[K any, V any] struct {
	*tree.RBTree[K, V]
}

TreeMap 是基于红黑树实现的Map

func NewTreeMap

func NewTreeMap[K any, V any](compare ekit.Comparator[K]) (*TreeMap[K, V], error)

NewTreeMap TreeMap构造方法,创建一个的TreeMap 需注意比较器compare不能为nil

Example
package main

import (
	"fmt"

	"github.com/ecodeclub/ekit"
	"github.com/ecodeclub/ekit/mapx"
)

func main() {
	m, _ := mapx.NewTreeMap[int, int](ekit.ComparatorRealNumber[int])
	_ = m.Put(1, 11)
	val, _ := m.Get(1)
	fmt.Println(val)
}
Output:
11

func NewTreeMapWithMap

func NewTreeMapWithMap[K comparable, V any](compare ekit.Comparator[K], m map[K]V) (*TreeMap[K, V], error)

NewTreeMapWithMap TreeMap构造方法 支持通过传入的map构造生成TreeMap

func (*TreeMap[K, V]) Get

func (treeMap *TreeMap[K, V]) Get(key K) (V, bool)

Get 在TreeMap找到指定Key的节点,返回Val TreeMap未找到指定节点将会返回false

func (*TreeMap[K, V]) Put

func (treeMap *TreeMap[K, V]) Put(key K, value V) error

Put 在TreeMap插入指定值 需注意如果TreeMap已存在该Key那么原值会被替换

func (*TreeMap[T, V]) Remove

func (treeMap *TreeMap[T, V]) Remove(k T)

Remove TreeMap中删除指定key的节点

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL