maps

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComparableKeyX

func ComparableKeyX[T comparable](key T) T

ComparableKeyX returns it-self

Types

type HMap

type HMap[T comparable, K any, V any] struct {
	// contains filtered or unexported fields
}

HMap is implemented by native go map. due to go map key unsupported generic type, so we need to use two go map to implement it, keysmap store the key, valuesmap store the value, and use a comparable key to related above two maps. In this way, the genericization of key is indirectly achieved, but it requires more memory to maintain one more go map at runtime.

func NewHMap

func NewHMap[T comparable, K any, V any](capacity int, keyFn KeyX[T, K]) *HMap[T, K, V]

NewHMap returns a new hmap whose key can be not-comparable, it according to keyX fn to return the true map key if you just want to use a comparable key map, SMap is better.

func (*HMap[T, K, V]) Clear

func (hmap *HMap[T, K, V]) Clear()

func (*HMap[T, K, V]) Clone

func (hmap *HMap[T, K, V]) Clone() Map[K, V]

func (*HMap[T, K, V]) Copy

func (hmap *HMap[T, K, V]) Copy(src Map[K, V])

func (*HMap[T, K, V]) Get

func (hmap *HMap[T, K, V]) Get(k K) (_ V, _ bool)

func (*HMap[T, K, V]) Keys

func (hmap *HMap[T, K, V]) Keys() []K

func (*HMap[T, K, V]) Remove

func (hmap *HMap[T, K, V]) Remove(k K)

func (*HMap[T, K, V]) Set

func (hmap *HMap[T, K, V]) Set(k K, v V)

func (*HMap[T, K, V]) Size

func (hmap *HMap[T, K, V]) Size() int

func (*HMap[T, K, V]) String

func (hmap *HMap[T, K, V]) String() string

func (*HMap[T, K, V]) Values

func (hmap *HMap[T, K, V]) Values() []V

type KeyX

type KeyX[T comparable, K any] func(key K) T

KeyX returns the unique flag of the given key whose type must be comparable

type Map

type Map[K any, V any] interface {
	// Get returns the value corresponding to the given key
	Get(k K) (V, bool)
	// Set sets replace the value corresponding to the given key
	Set(k K, v V)
	// Remove removes the key-value pair from the map
	Remove(k K)
	// Clone returns a clone of the map
	Clone() Map[K, V]
	// Copy copies the src map into the destination map
	Copy(src Map[K, V])
	// Keys returns the all keys of the map
	Keys() []K

	containers.Container[V]
}

Map is the base interface of all maps implementations

type SMap

type SMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SMap is simple hmap, implemented by the native go map, just a simple encapsulation

func NewSimpleMap

func NewSimpleMap[K comparable, V any](capacity int) *SMap[K, V]

func (*SMap[K, V]) Clear

func (hmap *SMap[K, V]) Clear()

func (*SMap[K, V]) Clone

func (hmap *SMap[K, V]) Clone() Map[K, V]

func (*SMap[K, V]) Copy

func (hmap *SMap[K, V]) Copy(src Map[K, V])

func (*SMap[K, V]) Get

func (hmap *SMap[K, V]) Get(k K) (V, bool)

func (*SMap[K, V]) Keys

func (hmap *SMap[K, V]) Keys() []K

func (*SMap[K, V]) Remove

func (hmap *SMap[K, V]) Remove(k K)

func (*SMap[K, V]) Set

func (hmap *SMap[K, V]) Set(k K, v V)

func (*SMap[K, V]) Size

func (hmap *SMap[K, V]) Size() int

func (*SMap[K, V]) String

func (hmap *SMap[K, V]) String() string

func (*SMap[K, V]) Values

func (hmap *SMap[K, V]) Values() []V

Jump to

Keyboard shortcuts

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