stringset

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2021 License: MIT Imports: 6 Imported by: 0

README

stringset

.github/workflows/stringset.yaml

String based set data structure.

go get -u github.com/absurdlab/pkg/stringset

Features

  • Ordered and non-Ordered implementations
  • Utility functions
  • JSON marshalling ready
  • YAML marshalling ready
  • SQL marshalling ready

Usage

// create a new ordered set with values
stringset.NewOrderedWith("foo", "bar")

// create a new ordered set from space delimited values
stringset.NewOrderedBySpace("foo bar")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddAll

func AddAll(set Interface, elements ...string)

AddAll adds all elements into the set. Existing elements are skipped.

func ContainsAll

func ContainsAll(set Interface, elements ...string) bool

ContainsAll returns true if the set contains all given elements

func Equals

func Equals(s1 Interface, s2 Interface) bool

Equals returns true s1 and s2 are equal: same size and same elements.

func IsEmpty

func IsEmpty(set Interface) bool

IsEmpty returns true if this set's size is 0.

func RemoveAll

func RemoveAll(set Interface, elements ...string)

RemoveAll removes all elements from the set.

func Subset

func Subset(super Interface, sub Interface) bool

Subset returns true if sub is a subset of the super.

Types

type Criteria

type Criteria func(value string) bool

Criteria is a function that help selects an element in the set.

type Interface

type Interface interface {
	json.Marshaler
	json.Unmarshaler
	yaml.Marshaler
	yaml.Unmarshaler
	yaml.IsZeroer
	sql.Scanner
	driver.Valuer

	// Size returns the size of the set. If set is nil, returns 0.
	Size() int
	// Contains returns true if the queried element is in the set.
	Contains(element string) bool
	// Add adds the given element to the set, if it does not already exist. Adding to a nil set has no effect.
	Add(element string)
	// Remove removes the given element from the set, if it exists.
	Remove(element string)
	// All returns true if all elements in the set matches the given criteria. Empty set always returns false.
	All(criteria Criteria) bool
	// Any returns true if at least one element in the set matches given criteria. Empty set always returns false.
	Any(criteria Criteria) bool
	// None returns true if none elements in the set matches given criteria. Empty set always returns false.
	None(criteria Criteria) bool
	// Array returns a copied slice of the elements in this set in ordered fashion. Subsequent operations on the returned
	// slice will not affect the set in any way. If set is nil or empty, an empty slice is returned.
	Array() []string
	// One returns one element from this set. It is usually invoked when Size is 1, but can be implemented for
	// any Size greater than 0. If set is empty, implementations should panic.
	One() string
}

Interface is the main abstraction for a string set data structure.

func Coalesce

func Coalesce(sets ...Interface) Interface

Coalesce returns the first non-empty set. If all sets are empty, a new Ordered set is returned.

type Ordered

type Ordered struct {
	// contains filtered or unexported fields
}

Ordered is a ordered set data structure for strings.

func NewOrdered

func NewOrdered() *Ordered

NewOrdered returns a new order string set.

func NewOrderedBy

func NewOrderedBy(value string, delimiter rune) *Ordered

NewOrderedBy returns a new order string set with delimited elements from the value string.

func NewOrderedBySpace

func NewOrderedBySpace(value string) *Ordered

NewOrderedBySpace is NewOrderedBy with space delimiter.

func NewOrderedWith

func NewOrderedWith(elements ...string) *Ordered

NewOrderedWith returns a new order string set with given elements.

func (*Ordered) Add

func (s *Ordered) Add(element string)

func (*Ordered) All

func (s *Ordered) All(criteria Criteria) bool

func (*Ordered) Any

func (s *Ordered) Any(criteria Criteria) bool

func (*Ordered) Array

func (s *Ordered) Array() []string

func (*Ordered) Contains

func (s *Ordered) Contains(element string) bool

func (*Ordered) IsZero

func (s *Ordered) IsZero() bool

func (*Ordered) MarshalJSON

func (s *Ordered) MarshalJSON() ([]byte, error)

func (*Ordered) MarshalYAML

func (s *Ordered) MarshalYAML() (interface{}, error)

func (*Ordered) None

func (s *Ordered) None(criteria Criteria) bool

func (*Ordered) One

func (s *Ordered) One() string

func (*Ordered) Remove

func (s *Ordered) Remove(element string)

func (*Ordered) Scan

func (s *Ordered) Scan(src interface{}) error

func (*Ordered) Size

func (s *Ordered) Size() int

func (*Ordered) SpaceJoin

func (s *Ordered) SpaceJoin() string

func (*Ordered) UnmarshalJSON

func (s *Ordered) UnmarshalJSON(bytes []byte) error

func (*Ordered) UnmarshalYAML

func (s *Ordered) UnmarshalYAML(unmarshal func(interface{}) error) error

func (*Ordered) Value

func (s *Ordered) Value() (driver.Value, error)

type UnOrdered

type UnOrdered struct {
	// contains filtered or unexported fields
}

func NewUnOrdered

func NewUnOrdered() *UnOrdered

NewUnOrdered returns a new unordered string set.

func NewUnOrderedWith

func NewUnOrderedWith(elements ...string) *UnOrdered

NewUnOrderedWith returns a new unordered string set with given elements.

func (*UnOrdered) Add

func (s *UnOrdered) Add(element string)

func (*UnOrdered) All

func (s *UnOrdered) All(criteria Criteria) bool

func (*UnOrdered) Any

func (s *UnOrdered) Any(criteria Criteria) bool

func (*UnOrdered) Array

func (s *UnOrdered) Array() []string

func (*UnOrdered) Contains

func (s *UnOrdered) Contains(element string) bool

func (*UnOrdered) IsZero

func (s *UnOrdered) IsZero() bool

func (*UnOrdered) MarshalJSON

func (s *UnOrdered) MarshalJSON() ([]byte, error)

func (*UnOrdered) MarshalYAML

func (s *UnOrdered) MarshalYAML() (interface{}, error)

func (*UnOrdered) None

func (s *UnOrdered) None(criteria Criteria) bool

func (*UnOrdered) One

func (s *UnOrdered) One() string

func (*UnOrdered) Remove

func (s *UnOrdered) Remove(element string)

func (*UnOrdered) Scan

func (s *UnOrdered) Scan(src interface{}) error

func (*UnOrdered) Size

func (s *UnOrdered) Size() int

func (*UnOrdered) UnmarshalJSON

func (s *UnOrdered) UnmarshalJSON(bytes []byte) error

func (*UnOrdered) UnmarshalYAML

func (s *UnOrdered) UnmarshalYAML(unmarshal func(interface{}) error) error

func (*UnOrdered) Value

func (s *UnOrdered) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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