set

package
v0.0.0-...-8a12d84 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: BSD-2-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package set provides primitives for inserting distinct values into ordered sets.

Example
package main

import (
	"fmt"
	"sort"

	"dasa.cc/x/set"
)

func main() {
	a := []string{
		"a", "b", "c",
		"b", "c", "d",
		"c", "d", "e",
	}

	// filter without allocating
	b := set.Slice[string](a[:0])
	for _, x := range a {
		b.Insert(x)
	}

	fmt.Println(b)
	fmt.Println("sorted", sort.StringsAreSorted(b))

}
Output:

[a b c d e]
sorted true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[T constraints.Ordered](a *[]T)

Filter without allocating.

Types

type Chain

type Chain[T constraints.Ordered] []Slice[T]

Chain is always strictly ordered by its indices, given as [0 .. N-1].

func (*Chain[T]) Upsert

func (a *Chain[T]) Upsert(x T, i int, ok bool) (int, bool)

Upsert inserts slice{x} at i if ok, and returns 0 and true. Otherwise, slice at i attempts insert of distinct x, and returns x index and true if inserted.

type Simple

type Simple[T constraints.Ordered] []T

Simple is always strictly ordered by its indices, given as [0 .. N-1].

func (*Simple[T]) Upsert

func (a *Simple[T]) Upsert(x T, i int, ok bool)

Upsert inserts x at i if ok; otherwise, updates i to x.

type Slice

type Slice[T constraints.Ordered] []T

Slice must be sorted in ascending order.

func (Slice[T]) Has

func (a Slice[T]) Has(x T) bool

func (*Slice[T]) Insert

func (a *Slice[T]) Insert(x T) (i int, ok bool)

Insert x in place if not exists; returns x index and true if inserted. The slice must be sorted in ascending order.

Jump to

Keyboard shortcuts

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