package maporder

Package maporder gives the order a map is walked in, the same on every machine and in every run. Go walks a map in an order that depends on a hidden random seed; the game's build step rewrites for k, v := range m in a map script to use Keys, so a script plays the same whatever the seed. A script never needs to import this package itself.

import "w4/maporder"

Functions

Compare

func Compare[T cmp.Ordered](a, b T) int

Compare orders two ordered values like a key comparator: -1, 0 or 1 (NaN sorts first).

CompareBool

func CompareBool(a, b bool) int

CompareBool orders false before true.

IterAll

func IterAll[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V]

IterAll is maps.All in ascending key order. The build step puts it in place of maps.All.

IterKeys

func IterKeys[K cmp.Ordered, V any](m map[K]V) iter.Seq[K]

IterKeys is maps.Keys in ascending key order. The build step puts it in place of maps.Keys.

IterValues

func IterValues[K cmp.Ordered, V any](m map[K]V) iter.Seq[V]

IterValues is maps.Values, the values in ascending key order. The build step puts it in place of maps.Values.

Keys

func Keys[K cmp.Ordered, V any](m map[K]V) []K

Keys returns the keys of m in ascending order (a nil or empty map gives none). The order of floats puts NaN first.

KeysFunc

func KeysFunc[K comparable, V any](m map[K]V, compare func(a, b K) int) []K

KeysFunc is Keys for a key type that is not cmp.Ordered (a struct of numbers and strings, say): the build step writes the comparison from the type's fields, field by field in the order they are declared.

Types

Map

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

Map is a map that always walks in ascending key order, for a script that wants the order without relying on the build step. The zero value is ready to use. Not safe for concurrent use (a script has one thread).

Map.Delete

func (o *Map[K, V]) Delete(k K)

Delete removes k (nothing happens if it is not there).

Map.Get

func (o *Map[K, V]) Get(k K) (V, bool)

Get returns the value for k and whether there is one.

Map.Keys

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

Keys returns the keys in ascending order. The slice is the caller's.

Map.Len

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

Len is the number of entries.

Map.Range

func (o *Map[K, V]) Range(fn func(k K, v V) bool)

Range calls fn for every entry in ascending key order until it returns false. Entries set or deleted by fn are seen as they are when their turn comes (a deleted one is skipped).

Map.Set

func (o *Map[K, V]) Set(k K, v V)

Set stores v under k.