strset

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2019 License: MIT Imports: 2 Imported by: 1

README

String Set

Build Status GoDoc Go Report Card License

Simplest-possible string set implementation - uses sorted slices.

Documentation

Full documentation is available on GoDoc

Example
package main

import (
  "fmt"

  "github.com/bsm/strset"
)

func main() {
	// Create a new set
	set := strset.New(3)
	set.Add("b")	// true
	set.Add("a")	// true
	set.Add("c")	// true
	set.Add("a")	// false

	fmt.Println(set.Slice())	// [a b c]

	set.Has("a")	// true
	set.Has("d")	// false

	set.Remove("a")			// true
	set.Remove("d")			// false
	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 struct {
	// contains filtered or unexported fields
}

Set represents a set of unique strings.

Example
package main

import (
	"fmt"

	"github.com/bsm/strset"
)

func main() {
	// Create a new set
	set := strset.New(3)
	set.Add("b") // true
	set.Add("a") // true
	set.Add("c") // true
	set.Add("a") // false

	fmt.Println(set.Slice()) // [a b c]

	set.Has("a") // true
	set.Has("d") // false

	set.Remove("a")          // true
	set.Remove("d")          // false
	fmt.Println(set.Slice()) // [b c]

}
Output:

[a b c]
[b c]

func New

func New(size int) *Set

New creates a set with a given cap.

func Use

func Use(vv ...string) *Set

Use turns a slice into a set, re-using the underlying slice. WARNING: this function is destructive and will mutate the passed slice.

func (*Set) Add

func (s *Set) Add(v string) bool

Add adds x to the set s, and reports whether the set grew.

func (*Set) Clear

func (s *Set) Clear()

Clear removes all elements from the set s.

func (*Set) Copy

func (s *Set) Copy(x *Set)

Copy sets s to the value of x.

func (*Set) Equals

func (s *Set) Equals(t *Set) bool

Equals reports whether the sets s and t have the same elements.

func (*Set) Has

func (s *Set) Has(v string) bool

Has reports whether x is an element of the set s.

func (*Set) Intersection

func (s *Set) Intersection(x, y *Set)

Intersection sets s to the intersection x ∩ y.

func (*Set) IntersectionWith

func (s *Set) IntersectionWith(x *Set)

IntersectionWith sets s to the intersection s ∩ x.

func (*Set) Intersects

func (s *Set) Intersects(x *Set) bool

Intersects reports whether s ∩ x ≠ ∅.

func (*Set) Len

func (s *Set) Len() int

Len returns the set length.

func (*Set) MarshalJSON

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

MarshalJSON encodes the set as JSON

func (*Set) Remove

func (s *Set) Remove(v string) bool

Remove removes x from the set s, and reports whether the set shrank.

func (*Set) Slice

func (s *Set) Slice() []string

Slice returns the string slice

func (*Set) Union

func (s *Set) Union(x, y *Set)

Union sets s to the union x ∪ y.

func (*Set) UnionWith

func (s *Set) UnionWith(x *Set) bool

UnionWith sets s to the union s ∪ x, and reports whether s grew.

func (*Set) UnmarshalJSON

func (s *Set) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes JSON into a set

Jump to

Keyboard shortcuts

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