ordered

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package ordered implements an in-memory map whose keys are ordered.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

A Map is a mapping from keys to values, where the keys are ordered according to an arbitrary comparison function. The zero value of a Map is not meaningful since it has no comparison function. Use NewMap to create a Map. A nil *Map, like a nil Go map, can be read but not written and contains no entries.

func NewMap

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

NewMap returns a new Map[K, V] ordered according to cmp.

func (*Map[K, V]) Above

func (m *Map[K, V]) Above(lo K) MapSpan[K, V]

Above returns a MapSpan with lower bound lo, exclusive, and no upper bound.

func (*Map[K, V]) All

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

All returns an iterator over the map m from smallest to largest key. If m is modified during the iteration, All guarantees that when a key is yielded, it is the successor of the key that was previously yielded (or the minimum key in the map, if it is the first key).

For example, if the map contains keys 10 and 20, the iterator has yielded 10, and then 15 is inserted, then the next yielded key will be 15.

Another example: if the map contains keys 10, 20 and 30, the iterator has yielded 10, and then 20 is deleted, then the next yielded key will be 30.

Example
package main

import (
	"cmp"
	"fmt"

	"github.com/jba/omap/ordered"
)

func main() {
	m := ordered.NewMap[int, string](cmp.Compare)
	m.Set(1, "one")
	m.Set(2, "two")
	m.Set(3, "three")

	for k, v := range m.All() {
		fmt.Println(k, v)
	}

}
Output:
1 one
2 two
3 three

func (*Map[K, V]) At

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

At returns the value of m[key], or the zero value if key is not present.

func (*Map[K, V]) Backward

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

Backward returns an iterator over the map m from largest to smallest key. See Map.All for the guarantee provided if m is modified during the iteration.

func (*Map[K, V]) Below

func (m *Map[K, V]) Below(hi K) MapSpan[K, V]

Below returns a MapSpan with upper bound hi, exclusive, and no lower bound.

Example
package main

import (
	"cmp"
	"fmt"

	"github.com/jba/omap/ordered"
)

func main() {
	m := ordered.NewMap[int, string](cmp.Compare)
	m.Set(1, "one")
	m.Set(2, "two")
	m.Set(3, "three")

	for k, v := range m.Above(1).Below(3).All() {
		fmt.Println(k, v)
	}

}
Output:
2 two

func (*Map[K, V]) Clear

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

Clear removes all entries from the map.

func (*Map[K, V]) Clone

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

Clone returns a shallow copy of m.

func (*Map[K, V]) Contains

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

Contains reports whether the map contains an entry with the given key.

func (*Map[K, V]) ContainsAll

func (m *Map[K, V]) ContainsAll(keys iter.Seq[K]) bool

ContainsAll reports whether the map contains an entry for each key in the sequence.

func (*Map[K, V]) Delete

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

Delete removes the entry with the given key, if present. It reports whether the map changed, and returns the previous value, if any.

func (*Map[K, V]) DeleteAll

func (m *Map[K, V]) DeleteAll(keys iter.Seq[K]) bool

DeleteAll removes all the entries whose keys are in the sequence. It reports whether the map changed.

func (*Map[K, V]) DeleteFunc added in v0.7.0

func (m *Map[K, V]) DeleteFunc(f func(K, V) bool) bool

DeleteFunc removes all the entries for which f returns true. It reports whether the map changed.

func (*Map[K, V]) From

func (m *Map[K, V]) From(lo K) MapSpan[K, V]

From returns a MapSpan with lower bound lo, inclusive, and no upper bound.

Example
package main

import (
	"cmp"
	"fmt"

	"github.com/jba/omap/ordered"
)

func main() {
	m := ordered.NewMap[int, string](cmp.Compare)
	m.Set(1, "one")
	m.Set(2, "two")
	m.Set(3, "three")

	for k, v := range m.From(2).All() {
		fmt.Println(k, v)
	}

}
Output:
2 two
3 three

func (*Map[K, V]) Get

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

Get returns the value of m[key] and reports whether it exists.

func (*Map[K, V]) Index

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

Index returns the index of key in m, or -1 if key is not present. The index of a key is its position in the sequence of keys. For example, the smallest key has index 0, and the largest has index m.Len()-1. See Map.Nth for the inverse operation.

func (*Map[K, V]) Keys

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

Keys returns an iterator over the keys in m from smallest to largest. See Map.All for the guarantee provided if m is modified during the iteration.

func (*Map[K, V]) Len

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

Len returns the number of keys in m.

func (*Map[K, V]) Max

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

Max returns the maximum key in m, its value, and true. If m is empty, the third return value is false.

func (*Map[K, V]) Min

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

Min returns the minimum key in m, its value, and true. If m is empty, the third return value is false.

func (*Map[K, V]) Nth

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

Nth returns the key and value at index i. It panics if i < 0 or i >= m.Len(). See Map.Index for the inverse operation.

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(key K, val V) (old V, added bool)

Set sets m[key] = val. If the entry was present, Set returns the former value and false. Otherwise it returns the zero value and true.

func (*Map[K, V]) SetAll

func (m *Map[K, V]) SetAll(seq iter.Seq2[K, V]) bool

SetAll calls Set(k, v) for each key/value pair in the sequence. It reports whether the number of map entries increased.

func (*Map[K, V]) String

func (m *Map[K, V]) String() string

String returns a string representation of m in the form "{k1: v1, k2: v2}".

func (*Map[K, V]) To

func (m *Map[K, V]) To(hi K) MapSpan[K, V]

To returns a MapSpan with upper bound hi, inclusive, and no lower bound.

func (*Map[K, V]) Values

func (m *Map[K, V]) Values() iter.Seq[V]

Values returns an iterator over the values in m from smallest to largest key. See Map.All for the guarantee provided if m is modified during the iteration.

type MapSpan

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

A MapSpan is a subsequence of keys in a Map.

func (MapSpan[K, V]) Above

func (s MapSpan[K, V]) Above(lo K) MapSpan[K, V]

Above returns a MapSpan with lower bound lo, exclusive and the same upper bound as s, if lo is within s's current lower bound. Otherwise, it returns s.

func (MapSpan[K, V]) All

func (s MapSpan[K, V]) All() iter.Seq2[K, V]

All returns an iterator over s's underlying map from smallest to largest key in s. See Map.All for the guarantee provided if m is modified during the iteration.

func (MapSpan[K, V]) Backward

func (s MapSpan[K, V]) Backward() iter.Seq2[K, V]

Backward returns an iterator over s's underlying map from largest to smallest key in s. See Map.All for the guarantee provided if m is modified during the iteration.

func (MapSpan[K, V]) Below

func (s MapSpan[K, V]) Below(hi K) MapSpan[K, V]

Below returns a MapSpan with upper bound hi, exclusive and the same lower bound as s, if hi is within s's current upper bound. Otherwise, it returns s.

func (MapSpan[K, V]) Clear

func (s MapSpan[K, V]) Clear()

Clear deletes all the entries in s from s's underlying map.

func (MapSpan[K, V]) Clone

func (s MapSpan[K, V]) Clone() *Map[K, V]

Clone returns a new Map containing only the keys in s.

func (MapSpan[K, V]) From

func (s MapSpan[K, V]) From(lo K) MapSpan[K, V]

From returns a MapSpan with lower bound lo, inclusive and the same upper bound as s, if lo is within s's current lower bound. Otherwise, it returns s.

func (MapSpan[K, V]) Index

func (s MapSpan[K, V]) Index(key K) int

Index returns the index of key within s, or -1 if key is not present or not in bounds.

func (MapSpan[K, V]) Keys

func (s MapSpan[K, V]) Keys() iter.Seq[K]

Keys returns an iterator over the keys in s from smallest to largest. See Map.All for the guarantee provided if m is modified during the iteration.

func (MapSpan[K, V]) Len

func (s MapSpan[K, V]) Len() int

Len returns the number of keys in s.

func (MapSpan[K, V]) Max

func (s MapSpan[K, V]) Max() (K, V, bool)

Max returns the maximum key from r's underlying map that is in s, its value, and true. If m is empty, the third return value is false.

func (MapSpan[K, V]) Min

func (s MapSpan[K, V]) Min() (K, V, bool)

Min returns the minimum key from s's underlying map that is in s, its value, and true. If m is empty, the third return value is false.

func (MapSpan[K, V]) Nth

func (s MapSpan[K, V]) Nth(i int) (K, V)

Nth returns the key and value at index i within s. It panics if i < 0 or i >= s.Len().

func (MapSpan[K, V]) String

func (s MapSpan[K, V]) String() string

func (MapSpan[K, V]) To

func (s MapSpan[K, V]) To(hi K) MapSpan[K, V]

To returns a MapSpan with upper bound hi, inclusive and the same lower bound as s, if hi is within s's current upper bound. Otherwise, it returns s.

func (MapSpan[K, V]) Values

func (s MapSpan[K, V]) Values() iter.Seq[V]

Values returns an iterator over the values in s from smallest to largest key. See Map.All for the guarantee provided if m is modified during the iteration.

Jump to

Keyboard shortcuts

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