sorted

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2025 License: MIT Imports: 5 Imported by: 0

README

Sorted map and set implementations

Go

Iterators enabled sorted map and sorted set implementations, zero dependencies.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

A sorted map implementation. Keeps keys and values in separate arrays, to improve memory locality when searching through the key space.

func NewMap

func NewMap[K cmp.Ordered, V any]() *Map[K, V]

Creates a new, empty sorted map

func (*Map[K, V]) Contains

func (m *Map[K, V]) Contains(key K) bool

Checks if the key is present in the map

func (*Map[K, V]) Del

func (m *Map[K, V]) Del(key K) bool

Deletes the given key, returns true if the key was found, false otherwise

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (V, bool)

Returns the value in the map, if found and an indicator whether found or not. If not found the value will be the 0-value

func (*Map[K, V]) Items

func (m *Map[K, V]) Items() iter.Seq2[K, *V]

Iterates through the key-value set, in key order

func (*Map[K, V]) Keys

func (m *Map[K, V]) Keys() []K

Returns list of all keys

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Returns the number of element in the map

func (*Map[K, V]) MarshalJSON

func (m *Map[K, V]) MarshalJSON() ([]byte, error)

Custom JSON marshalling, to make its use transparent with Go's json package.

func (*Map[K, V]) Put

func (m *Map[K, V]) Put(key K, value V) bool

Sets a key-value pair at a specific key slot, returns true if the key was not already in the key-set.

func (*Map[K, V]) Vals

func (m *Map[K, V]) Vals() []V

Returns list of all values

type Set

type Set[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

A sorted set implementation. Keeps keys in a sorted vector to improve memory locality.

func NewSet

func NewSet[T cmp.Ordered](items ...T) *Set[T]

Creates a new empty sorted set, or populated with the given arguments

func (*Set[T]) Contains

func (s *Set[T]) Contains(val T) bool

func (*Set[T]) Del

func (s *Set[T]) Del(val T) bool

func (*Set[T]) Items

func (s *Set[T]) Items() iter.Seq[T]

func (*Set[T]) Len

func (s *Set[T]) Len() int

func (*Set[T]) MarshalJSON

func (s *Set[T]) MarshalJSON() ([]byte, error)

Custom JSON marshalling, to make its use transparent with Go's json package.

func (*Set[T]) Put

func (s *Set[T]) Put(val T) bool

func (*Set[T]) UnmarshalJSON

func (s *Set[T]) UnmarshalJSON(b []byte) error

Custom JSON marshalling, to make its use transparent with Go's json package.

func (*Set[T]) Values

func (s *Set[T]) Values() []T

Jump to

Keyboard shortcuts

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