Skip to content
gravittonPublic

About

Generic containers: a 2D array indexed by points and an immutable sorted set

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Gravitton container

Latest Stable Version Build Status Coverage Status Go Dev Reference Go Version Software License

Generic containers: a 2D array indexed by points, an immutable sorted set and a double-ended queue


Features

  • Arrays – a value per geom.Point of a size, in one slice, with sides of any integer type.
  • Sets – immutable sorted sets of any Compare element, with union, intersection and difference.
  • Deques – double-ended queues on a growing ring, pushed and popped at either end in constant time.

The rules behind them are in the package documentation.

Installation

go get github.com/gravitton/container
import "github.com/gravitton/container"

Usage

Arrays

An Array[T, S] holds one value for every index of a size in columns and rows. S is the integer type of its sides and indices, taken from the size it is made with. An array is a handle, as a slice is: a copy shares its values, and Clone gives storage of its own.

a := container.Arr[int](geom.Sz(4, 3))                  // an Array[int, int], every value zero
small := container.Arr[uint8](geom.Sz[int16](320, 200)) // an Array[uint8, int16], indexed by geom.Point[int16]

a.Set(geom.Pt(2, 1), 7)
*a.Get(geom.Pt(2, 1)) += 1 // a pointer into the array
a.Contains(geom.Pt(4, 0))  // false, the check before Get and Set, which panic outside
a.Size()                   // 4x3; also Width, Height and Len
snapshot := a.Clone()      // storage of its own

Iteration

The values are stored row by row, and every traversal follows that order: Keys walks the indices, Data is the storage itself.

An array of four columns and three rows, each cell labelled with its index and its offset, beside the same twelve values as one slice, row after row

for index, value := range a.All() { // also Keys and Values
	*value += index.X
}

values := a.Data() // the values row by row as a slice into the array, the fastest loop
for i := range values {
	values[i] = min(values[i], 9)
}

data, err := json.Marshal(a)   // {"w":4,"h":3,"d":[…]}, the values row by row
err = json.Unmarshal(data, &a) // and back; an error where the values do not fill the size

Sets

A Set[E] holds any element with a Compare method, once each and in that order. It never changes: every operation returns a set, so one is safe to copy and share.

a := container.SetOf(geom.Pt(1, 1), geom.Pt(2, 1), geom.Pt(1, 2), geom.Pt(2, 2))
from := container.SetFrom(points) // the same from a slice it takes as its own, without a copy

a.Contains(geom.Pt(2, 1)) // true, a binary search
a.Len()                   // 4; also IsEmpty
a.First()                 // (1,1), true; also Last
a.Index(geom.Pt(2, 1))    // 2, true, the index At reads it back from

Set operations

Each operation walks both sets once and allocates its result once, or not at all where it equals one of them.

Two overlapping squares of points, a and b, and the cells of their union, intersection, difference and symmetric difference

b := container.SetOf(geom.Pt(2, 2), geom.Pt(3, 2), geom.Pt(2, 3), geom.Pt(3, 3))

a.Union(b)               // (1,1) (1,2) (2,1) (2,2) (2,3) (3,2) (3,3)
a.Intersection(b)        // (2,2)
a.Difference(b)          // (1,1) (1,2) (2,1)
a.SymmetricDifference(b) // (1,1) (1,2) (2,1) (2,3) (3,2) (3,3)

a.Intersects(b) // true, without building the intersection
a.Encloses(b)   // false, whether a holds every element of b
a.Equal(b)      // false

A set is walked and stored in its order:

for p := range a.All() { // also Backward
	draw(p)
}

a.Elements()    // a fresh slice; also AppendElements
json.Marshal(a) // [{"x":1,"y":1},…], null for the empty set

Deques

A Deque[E] holds elements in order from its front to its back and changes in place, so unlike the other containers it is used through a pointer and never copied, which go vet reports. Its zero value is the empty deque, ready to use.

var body container.Deque[geom.Point[int]]
body.PushBack(geom.Pt(2, 1)) // also PushFront
body.PushBack(geom.Pt(1, 1))

body.PushFront(geom.Pt(3, 1)) // a step: the head in front,
body.PopBack()                // the tail dropped; (1,1), true, and false once empty; also PopFront
body.Front()                  // (3,1), true; also Back
body.At(1)                    // (2,1), counted from the front
body.Len()                    // 2; also IsEmpty

for i, cell := range body.All() { // from the front to the back
	draw(i, cell)
}

body.Clear() // keeps the ring, so the next pushes allocate nothing

Full reference: pkg.go.dev.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Generic containers: a 2D array indexed by points and an immutable sorted set

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Contributors

Languages