sortedset

package module
v0.0.0-...-b4df678 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

README

Sorted Set

Go Reference Test License

Simple set implementation. Uses sorted slices and generics.

Documentation

Full documentation is available on go.dev.

Example
package main

import (
  "fmt"

  "github.com/bsm/sortedset"
)

func main() {
	// Create a new set
	set := sortedset.New[string]()

	// Seed with data
	set = set.Add("b")
	set = set.Add("a")
	set = set.Add("c", "a")
	fmt.Println(set.Slice()) // [a b c]

	// Check
	fmt.Println(set.Has("a")) // true
	fmt.Println(set.Has("d")) // false

	// Delete items
	set = set.Delete("a")
	set = set.Delete("d")
	fmt.Println(set.Slice()) // [b c]
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T constraints.Ordered] []T

Set represents a slice of unique items.

Example
package main

import (
	"fmt"

	"github.com/bsm/sortedset"
)

func main() {
	// Create a new set
	set := sortedset.New[string]()

	// Seed with data
	set = set.Add("b")
	set = set.Add("a")
	set = set.Add("c", "a")
	fmt.Println(set.Slice()) // [a b c]

	// Check
	fmt.Println(set.Has("a")) // true
	fmt.Println(set.Has("d")) // false

	// Delete items
	set = set.Delete("a")
	set = set.Delete("d")
	fmt.Println(set.Slice()) // [b c]

}
Output:

[a b c]
true
false
[b c]

func New

func New[T constraints.Ordered]() Set[T]

New inits a new Set of size.

func NewCap

func NewCap[T constraints.Ordered](cap int) Set[T]

NewCap inits a new Set with a cap.

func (Set[T]) Add

func (s Set[T]) Add(v ...T) Set[T]

Add adds all v to Set s, and returns the new set.

func (Set[T]) Clear

func (s Set[T]) Clear() Set[T]

Clear returns a truncated set.

func (Set[T]) Clone

func (s Set[T]) Clone() Set[T]

Clone returns a copy of the set.

func (Set[T]) Delete

func (s Set[T]) Delete(v ...T) Set[T]

Delete deletes all v from Set s and returns the new set.

func (Set[T]) Equal

func (s Set[T]) Equal(t Set[T]) bool

Equal reports whether the sets s and t have the exact same elements.

func (Set[T]) Has

func (s Set[T]) Has(v T) bool

Has reports whether v is an element of Set s.

func (Set[T]) Intersection

func (s Set[T]) Intersection(x, y Set[T]) Set[T]

Intersection adds the intersection x ∩ y to s and returns the result.

func (Set[T]) IntersectionWith

func (s Set[T]) IntersectionWith(t Set[T]) Set[T]

IntersectionWith sets s to the insersection of s ∪ t, and returns the result.

func (Set[T]) Intersects

func (s Set[T]) Intersects(t Set[T]) bool

Intersects reports whether s ∩ t ≠ ∅.

func (Set[T]) Len

func (s Set[T]) Len() int

Len returns the set length.

func (Set[T]) Slice

func (s Set[T]) Slice() []T

Slice returns the set as native slice.

func (Set[T]) Union

func (s Set[T]) Union(x, y Set[T]) Set[T]

Union adds the union x ∪ y to s.

func (Set[T]) UnionWith

func (s Set[T]) UnionWith(t Set[T]) Set[T]

UnionWith sets s to the union s ∪ t, and returns the result.

Jump to

Keyboard shortcuts

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