soa

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: MIT Imports: 4 Imported by: 0

README

soa - Structure of Arrays for Go

Go Reference

soa is an SoA code generator and generic SoA slice library.

Code Generator

Installation

To install the code generator, add it as a tool dependency:

go get -tool github.com/ichiban/soa/cmd/soagen
Basic Usage

The basic usage is to include the line //go:generate go tool soagen in your file with the structs you want to generate SoA slices.

Let's say you have point.go containing Point, you can generate point_soa.go containing PointSlice by executing go generate ./....

point.go:

package main

//go:generate go tool soagen

type Point struct {
	X int
	Y int
}

point_soa.go:

// Code generated by soagen; DO NOT EDIT.
package main

type PointSlice struct {
	X []int
	Y []int
}

// And some methods.

Now, PointSlice works as an alternative to []Point.

package main

import "fmt"

func main() {
	// s := make([]Point, 3, 10)
	var s PointSlice
	s = s.Grow(10)
	s = s.Slice(0, 3, s.Cap())

	// s[i] = ...
	s.Set(0, Point{X: 1, Y: 1})
	s.Set(1, Point{X: 2, Y: 2})
	s.Set(2, Point{X: 3, Y: 3})

	for i := 0; i < s.Len(); i++ {
		// p := s[i]
		p := s.Get(i)

		fmt.Printf("s[%d] = (%d, %d)\n", i, p.X, p.Y)
	}
}

Output:

s[0] = (1, 1)
s[1] = (2, 2)
s[2] = (3, 3)
Advanced Usage
Specify the SoA slice name pattern
You can specify the pattern for the SoA slices with `-name`. It'll replace `{{.}}` with the original struct name.

point.go:

package main

//go:generate go tool soagen -name "{{.}}Collection"

type Point struct {
	X int
	Y int
}

point_soa.go

// Code generated by soagen; DO NOT EDIT.
package main

type PointCollection struct {
	X []int
	Y []int
}

// And some methods.
Specify the output filename
You can specify the output filenames with `-out`.

point.go:

package main

//go:generate go tool soagen -out point_gen.go

type Point struct {
	X int
	Y int
}

point_gen.go:

// Code generated by soagen; DO NOT EDIT.
package main

type PointSlice struct {
	X []int
	Y []int
}

// And some methods.
Specify which structs to be processed
If you list struct names, it'll process listed structs only.

foo_bar_baz.go:

package main

//go:generate go tool soagen Foo Baz

type Foo struct {
	X int
	Y int
}

type Bar struct {
	X int
	Y int
}

type Baz struct {
	X int
	Y int
}

foo_bar_baz_soa.go:

// Code generated by soagen; DO NOT EDIT.
package main

type FooSlice struct {
	X []int
	Y []int
}

// And some methods.

type BazSlice struct {
	X []int
	Y []int
}

// And some methods.

Library

You can manipulate SoA slices with the library github.com/ichiban/soa.

It implements slices-compatible functions.

With the library, the example in Basic Usage can be written with much ease.

package main

import (
	"fmt"

	"github.com/ichiban/soa"
)

func main() {
	// s := make([]Point, 0, 10)
	s := soa.Make[PointSlice](0, 10)

	// s = append(s, ...)
	s = soa.Append(s, Point{X: 1, Y: 1}, Point{X: 2, Y: 2}, Point{X: 3, Y: 3})

	// for i, p := range s {
	for i, p := range soa.All(s) {
		fmt.Printf("s[%d] = (%d, %d)\n", i, p.X, p.Y)
	}
}

License

Distributed under the MIT license. See LICENSE for more information.

Contributing

  1. Fork it (https://github.com/ichiban/soa/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[S Slice[S, E], E any](slice S) iter.Seq2[int, E]

All returns an iterator over the Slice.

func Append

func Append[S Slice[S, E], E any](slice S, elems ...E) S

Append appends elements to a Slice.

func AppendSeq

func AppendSeq[S Slice[S, E], E any](s S, seq iter.Seq[E]) S

AppendSeq appends the values from seq to the slice and returns the extended slice.

func Backward

func Backward[S Slice[S, E], E any](s S) iter.Seq2[int, E]

Backward returns an iterator over index-value pairs in the slice, traversing it backward with descending indices.

func BinarySearchFunc

func BinarySearchFunc[S Slice[S, E], E, T any](x S, target T, cmp func(E, T) int) (int, bool)

BinarySearchFunc searches for target in a sorted Slice.

func Chunk

func Chunk[S Slice[S, E], E any](s S, n int) iter.Seq[S]

Chunk returns an iterator over chunks.

func Clear

func Clear[S Slice[S, E], E any](slice S)

Clear clears the slice.

func Clip

func Clip[S Slice[S, E], E any](s S) S

Clip removes unused capacity from the slice.

func Clone

func Clone[S Slice[S, E], E any](s S) S

Clone returns a copy of the slice.

func Collect

func Collect[S Slice[S, E], E any](seq iter.Seq[E]) S

Collect collects values from the iterator and returns as a new slice.

func Compact

func Compact[S Slice[S, E], E comparable](s S) S

Compact removes duplicates from the slice.

func CompactFunc

func CompactFunc[S Slice[S, E], E any](s S, eq func(E, E) bool) S

CompactFunc removes duplicates from the slice comparing the elements with the given function.

func CompareFunc

func CompareFunc[S1 Slice[S1, E1], S2 Slice[S2, E2], E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) int

CompareFunc compares 2 slices.

func Concat

func Concat[S Slice[S, E], E any](slices ...S) S

Concat concatenates the slices.

func Contains

func Contains[S Slice[S, E], E comparable](s S, v E) bool

Contains checks if the slice contains the element.

func ContainsFunc

func ContainsFunc[S Slice[S, E], E any](s S, f func(E) bool) bool

ContainsFunc checks if the slice contains an element that satisfies the predicate.

func Copy

func Copy[S Slice[S, E], E any](dst, src S) int

Copy copies elements from one slice to another.

func Delete

func Delete[S Slice[S, E], E any](s S, i, j int) S

Delete deletes elements from the slice.

func DeleteFunc

func DeleteFunc[S Slice[S, E], E any](s S, del func(E) bool) S

DeleteFunc deletes elements that satisfy the predicate.

func Equal

func Equal[S Slice[S, E], E comparable](s1, s2 S) bool

Equal checks equality of two slices.

func EqualFunc

func EqualFunc[S1 Slice[S1, E1], S2 Slice[S2, E2], E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool

EqualFunc checks equality of two slices by a predicate.

func Grow

func Grow[S Slice[S, E], E any](s S, n int) S

Grow grows the capacity of the slice.

func Index

func Index[S Slice[S, E], E comparable](s S, v E) int

Index returns the index of the first occurrence of the element if it exists. Otherwise, it returns -1.

func IndexFunc

func IndexFunc[S Slice[S, E], E any](s S, f func(E) bool) int

IndexFunc returns the index of the first occurrence of the element that satisfies the predicate. If not exists, it returns -1.

func Insert

func Insert[S Slice[S, E], E any](s S, i int, v ...E) S

Insert inserts elements to the slice.

func IsSortedFunc

func IsSortedFunc[S Slice[S, E], E any](x S, cmp func(a, b E) int) bool

IsSortedFunc checks if the elements are sorted.

func Make

func Make[S Slice[S, E], E any](len, cap int) S

Make creates a new Slice.

func MaxFunc

func MaxFunc[S Slice[S, E], E any](x S, cmp func(a, b E) int) E

MaxFunc returns the maximal element in the slice.

func MinFunc

func MinFunc[S Slice[S, E], E any](x S, cmp func(a, b E) int) E

MinFunc returns the minimum element in the slice.

func Repeat

func Repeat[S Slice[S, E], E any](x S, count int) S

Repeat returns a slice that repeats the elements the given times.

func Replace

func Replace[S Slice[S, E], E any](s S, i, j int, v ...E) S

Replace replaces values.

func Reverse

func Reverse[S Slice[S, E], E any](s S)

Reverse reverses the slice.

func SortFunc

func SortFunc[S Slice[S, E], E any](x S, cmp func(a, b E) int)

SortFunc sorts the slice.

func SortStableFunc

func SortStableFunc[S Slice[S, E], E any](x S, cmp func(a, b E) int)

SortStableFunc stable sorts the slice.

func SortedFunc

func SortedFunc[S Slice[S, E], E any](seq iter.Seq[E], cmp func(E, E) int) S

SortedFunc collects values from seq into a new sorted slice.

func SortedStableFunc

func SortedStableFunc[S Slice[S, E], E any](seq iter.Seq[E], cmp func(E, E) int) S

SortedStableFunc collects values from seq into a new stable-sorted slice.

func Values

func Values[S Slice[S, E], E any](s S) iter.Seq[E]

Values returns an iterator over the Slice.

Types

type Slice

type Slice[S Slicer[S, E], E any] interface {
	Slicer[S, E]

	// Get gets the value of the index. i.e. s[n]
	Get(int) E
	// Set sets the value of the index. i.e. s[n] = v
	Set(int, E)
	// Len returns the length of the slice. i.e. len(s)
	Len() int
	// Cap returns the capacity of the slice. i.e. cap(s)
	Cap() int
}

Slice is a structure-of-arrays slice derived from a struct E.

type Slicer

type Slicer[S, E any] interface {
	// Slice i.e. s[low:high:max]
	Slice(low, high, max int) S
	Grow(n int) S
}

Slicer is a type constraint for a Slice which methods return another Slice.

Directories

Path Synopsis
cmd
soagen command
examples
user command
Code generated by soagen; DO NOT EDIT.
Code generated by soagen; DO NOT EDIT.
internal
gen

Jump to

Keyboard shortcuts

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