typemap

package
v0.0.0-...-2f37693 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2013 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package typemap defines type M, a hash-table-based mapping from types (go/types.Type) to arbitrary values, and a hash function on types.

The concrete types that implement the Type interface are pointers. Since they are not canonicalized, == cannot be used to check for equivalence, and thus we cannot simply use a Go map.

Not thread-safe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hasher

type Hasher struct {
	// contains filtered or unexported fields
}

A Hasher maps each type to its hash value. For efficiency, a hasher uses memoization; thus its memory footprint grows monotonically over time. Hashers are not thread-safe. Hashers have reference semantics. Call MakeHasher to create a Hasher.

func MakeHasher

func MakeHasher() Hasher

MakeHasher returns a new Hasher instance.

func (Hasher) Hash

func (h Hasher) Hash(t types.Type) uint32

Hash computes a hash value for the given type t such that IsIdentical(t, t') => Hash(t) == Hash(t').

type M

type M struct {
	// contains filtered or unexported fields
}

typemap.M is a mapping from types.Type to interface{} values.

Just as with map[K]V, a nil *typemap.M is a valid empty map.

func (*M) At

func (m *M) At(key types.Type) interface{}

At returns the map entry for the given key. The result is nil if the entry is not present.

func (*M) Delete

func (m *M) Delete(key types.Type) bool

Delete removes the entry with the given key, if any. It returns true if the entry was found.

func (*M) Iterate

func (m *M) Iterate(f func(key types.Type, value interface{}))

Iterate calls function f on each entry in the map in unspecified order.

If f should mutate the map, Iterate provides the same guarantees as Go maps: if f deletes a map entry that Iterate has not yet reached, f will not be invoked for it, but if f inserts a map entry that Iterate has not yet reached, whether or not f will be invoked for it is unspecified.

func (*M) Keys

func (m *M) Keys() []types.Type

Keys returns a new slice containing the set of map keys. The order is unspecified.

func (*M) KeysString

func (m *M) KeysString() string

KeysString returns a string representation of the map's key set. Order is unspecified.

func (*M) Len

func (m *M) Len() int

Len returns the number of map entries.

func (*M) Set

func (m *M) Set(key types.Type, value interface{}) (prev interface{})

Set sets the map entry for key to val, and returns the previous entry, if any.

func (*M) SetHasher

func (m *M) SetHasher(hasher Hasher)

SetHasher sets the hasher used by typemap.M.

All Hashers are functionally equivalent but contain internal state used to cache the results of hashing previously seen types.

A single Hasher created by MakeHasher() may be shared among many typemap.M instances. This is recommended if the instances have many keys in common, as it will amortize the cost of hash computation.

A Hasher may grow without bound as new types are seen. Even when a type is deleted from the map, the Hasher never shrinks, since other types in the map may reference the deleted type indirectly.

Hashers are not thread-safe, and read-only operations such as M.Lookup require updates to the hasher, so a full Mutex lock (not a read-lock) is require around all typemap.M operations if a shared hasher is accessed from multiple threads.

If SetHasher is not called, the type-map will create a private hasher at the first call to Insert.

func (*M) String

func (m *M) String() string

String returns a string representation of the map's entries. Values are printed using fmt.Sprintf("%v", v). Order is unspecified.

Jump to

Keyboard shortcuts

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