maps

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFullMap occurs when a map is full. Readers must return this
	// error as is and not wrap it as callers are expected to check for
	// this error with the == operator.
	//
	// Format:
	// 	"map is full"
	ErrFullMap error = errors.New("map is full")
)

Functions

func ContainsKey

func ContainsKey[K comparable, V any](m Map[K, V], target K) bool

ContainsKey checks if the map contains the given key.

Parameters:

  • m: The map to check.
  • target: The key to check for.

Returns:

  • bool: True if the map contains the key, false otherwise.

Types

type Map

type Map[K comparable, V any] interface {
	// Insert adds a key-value pair to the map. If the key already
	// exists, the value is updated and the result (false, nil) is
	// returned.
	//
	// Parameters:
	//   - key: The key of the pair.
	//   - value: The value of the pair.
	//
	// Returns:
	//   - bool: A boolean indicating if the key already exists in the
	//     map.
	//   - error: An error if the key cannot be added to the map.
	//
	// Errors:
	//   - common.ErrNilReceiver: If the receiver is nil.
	//   - any other error: Implementation-dependent.
	Insert(key K, value V) (bool, error)

	// ByKey returns the value associated with the given key.
	//
	// Parameters:
	//   - key: The key of the pair.
	//
	// Returns:
	//   - V: The value associated with the key.
	//   - bool: A boolean indicating if the key exists in the map.
	ByKey(key K) (V, bool)

	// Key returns a sequence of keys in the map. The order of the
	// returned keys is undefined.
	//
	// Returns:
	//   - iter.Seq[K]: A sequence of keys in the map. Never returns nil.
	Key() iter.Seq[K]

	// Value returns a sequence of values in the map. The order of the
	// returned values is undefined.
	//
	// Returns:
	//   - iter.Seq[V]: A sequence of values in the map. Never returns
	// 	nil.
	Value() iter.Seq[V]

	// Entry returns a sequence of key-value pairs in the map. The order
	// of the returned pairs is undefined.
	//
	// Returns:
	//   - iter.Seq2[K, V]: A sequence of key-value pairs in the map.
	//     Never returns nil.
	Entry() iter.Seq2[K, V]

	// Reset resets the map for reuse.
	//
	// Returns:
	//   - error: An error if the map could not be reset.
	//
	// Errors:
	//   - common.ErrNilReceiver: If the receiver is nil.
	//   - any other error: Implementation-dependent.
	Reset() error

	// Delete removes the key-value pair with the given key from the
	// map.
	//
	// Parameters:
	//   - key: The key of the pair to remove.
	//
	// Returns:
	//   - bool: A boolean indicating if the key was found in the map.
	//   - error: An error if the key cannot be removed from the map.
	//
	// Errors:
	//   - common.ErrNilReceiver: If the receiver is nil.
	//   - any other error: Implementation-dependent.
	Delete(key K) (bool, error)
}

Map is an interface that represents a map.

type OrderMap

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

OrderMap is a map that keeps the keys in order of insertion.

An empty OrderMap can be created with either the `var o OrderMap[K, V]` syntax or the `o := new(OrderMap[K, V])` constructor.

func (OrderMap[K, V]) ByKey

func (s OrderMap[K, V]) ByKey(key K) (V, bool)

ByKey implements Map.

func (*OrderMap[K, V]) Delete

func (s *OrderMap[K, V]) Delete(key K) (bool, error)

Delete implements Map.

func (OrderMap[K, V]) Entry

func (s OrderMap[K, V]) Entry() iter.Seq2[K, V]

Entry implements Map.

func (OrderMap[K, V]) IndexOf

func (s OrderMap[K, V]) IndexOf(key K) (uint32, bool)

IndexOf returns the index of the provided key.

Parameters:

  • key: The key to search for.

Returns:

  • uint32: The index of the key.
  • bool: True if the key was found, false otherwise.

func (*OrderMap[K, V]) Insert

func (s *OrderMap[K, V]) Insert(key K, value V) (bool, error)

Insert implements Map.

func (OrderMap[K, V]) Key

func (s OrderMap[K, V]) Key() iter.Seq[K]

Key implements Map.

func (*OrderMap[K, V]) Reset

func (s *OrderMap[K, V]) Reset() error

Reset implements Map.

func (OrderMap[K, V]) Size

func (s OrderMap[K, V]) Size() uint32

Size returns the number of elements in the map.

Returns:

  • uint32: The number of elements in the map.

func (OrderMap[K, V]) Value

func (s OrderMap[K, V]) Value() iter.Seq[V]

Value implements Map.

type SortMap

type SortMap[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

SortMap is a map that keeps the keys in order using the < operator.

An empty SortMap can be created with either the `var o SortMap[K, V]` syntax or the `o := new(SortMap[K, V])` constructor.

func (SortMap[K, V]) ByKey

func (s SortMap[K, V]) ByKey(key K) (V, bool)

ByKey implements Map.

func (*SortMap[K, V]) Delete

func (s *SortMap[K, V]) Delete(key K) (bool, error)

Delete implements Map.

func (SortMap[K, V]) Entry

func (s SortMap[K, V]) Entry() iter.Seq2[K, V]

Entry implements Map.

func (SortMap[K, V]) IndexOf

func (s SortMap[K, V]) IndexOf(key K) (uint32, bool)

IndexOf returns the index of the provided key.

Parameters:

  • key: The key to search for.

Returns:

  • uint32: The index of the key.
  • bool: True if the key was found, false otherwise.

func (*SortMap[K, V]) Insert

func (s *SortMap[K, V]) Insert(key K, value V) (bool, error)

Insert implements Map.

func (SortMap[K, V]) Key

func (s SortMap[K, V]) Key() iter.Seq[K]

Key implements Map.

func (*SortMap[K, V]) Reset

func (s *SortMap[K, V]) Reset() error

Reset implements Map.

func (SortMap[K, V]) Size

func (s SortMap[K, V]) Size() uint32

Size returns the number of elements in the map.

Returns:

  • uint32: The number of elements in the map.

func (SortMap[K, V]) Value

func (s SortMap[K, V]) Value() iter.Seq[V]

Value implements Map.

Jump to

Keyboard shortcuts

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