Skip to content

proposal: built-in map: add delete hook func And set hook func #60311

Closed as not planned
@someview

Description

@someview

#47649

// Package maps defines various functions useful with maps of any type.
package maps

// Keys returns the keys of the map m.
// The keys will be an indeterminate order.
func Keys[M constraints.Map[K, V], K comparable, V any](m M) []K

// Values returns the values of the map m.
// The values will be in an indeterminate order.
func Values[M constraints.Map[K, V], K comparable, V any](m M) []V

// Equal reports whether two maps contain the same key/value pairs.
// Values are compared using ==.
func Equal[M1, M2 constraints.Map[K, V], K, V comparable](m1 M1, m2 M2) bool

// EqualFunc is like Equal, but compares values using cmp.
// Keys are still compared with ==.
func EqualFunc[M1 constraints.Map[K, V1], M2 constraints.Map[K, V2], K comparable, V1, V2 any](m1 m1, m2 M2, cmp func(V1, V2) bool) bool

// Clear removes all entries from m, leaving it empty.
func Clear[M constraints.Map[K, V], K comparable, V any](m M)

// Clone returns a copy of m.  This is a shallow clone:
// the new keys and values are set using ordinary assignment.
func Clone[M constraints.Map[K, V], K comparable, V any](m M) M

// Copy copies all key/value pairs in src adding them to dst.
// When a key in src is already present in dst,
// the value in dst will be overwritten by the value associated
// with the key in src.
func Copy[M constraints.Map[K, V], K comparable, V any](dst, src M)

// PreDelete deletes any key/value pairs from m for which del returns true.
func PreDelete[M constraints.Map[K, V], K comparable, V any](k K, del func(K, V) bool)

//PostDelete deletes a key/value pairs, if oldvalue exists,  post will be called
func PostDelete[M constraints.Map[K, V], K comparable, V any](k K, post func((oldValue V))

// PostSet sets a key/value pair, if oldvalue exists,  post will be called
func PostSet[M constraints.Map[K, V], K comparable, V any](k K,post func((oldValue V))

some cases:

  • when delete a key,want to know if kvpairs exists exec some op
  • when set a key, want to know if kvpairs exists and exec some op

eg:
when cache evited some item, delete key from map want to do some op: only kvpairs exists,
set a kvpairs, we want to get the old kvpairs to eslimate cost,and using the new cost - the old cost

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions