stringset

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 1 Imported by: 0

README

stringset

Implementation of a Set of strings in Go. Supports all the standard set operations.

Package stringset provides the capabilities of a standard set with strings, including the standard set operations (add, delete, check membership, intersection, union) and a specialized ability to have a "negative" set for intersections.

Negative sets are useful for inverting the sense of a set when combining them using Intersection. Negative sets do not change the behavior of any other set operations, including Union and Difference. Negative operations were created for managing a set of tags, where it is useful to be able to search for items that contain some tags but do not contain others.

The API is designed to be chainable so that common operations become one-liners, and it is designed to be easy to use with slices of strings.

This package does not return errors. Set operations should be fast and chainable; adding items more than once, or attempting to remove things that don't exist are not errors.

Full documentation on GoDoc

Documentation

Overview

Package stringset provides the capabilities of a standard Set with strings, including the standard set operations and a specialized ability to have a "negative" set for intersections.

Negative sets are useful for inverting the sense of a set when combining them using Intersection. Negative sets do not change the behavior of any other set operations, including Union and Difference. Negative operations were created for managing a set of tags, where it is useful to be able to search for items that contain some tags but do not contain others.

The API is designed to be chainable so that common operations become one-liners, and it is designed to be easy to use with slices of strings.

This package does not return errors. Set operations should be fast and chainable; adding items more than once, or attempting to remove things that don't exist are not errors.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StringSet

type StringSet struct {
	IsNegative bool
	// contains filtered or unexported fields
}

A StringSet is implemented using a map[string]nothing -- strings are copied when added to the set.

func New

func New() *StringSet

New constructs an empty StringSet.

func (*StringSet) Add

func (ss *StringSet) Add(sa ...string) *StringSet

Add puts one or more strings into the set. If a string is already present the set is unchanged.

Example
package main

import (
	"fmt"
	"sort"

	"github.com/AchievementNetwork/stringset"
)

func main() {
	greetings := stringset.New().Add("hello")
	greetings.Add("aloha", "bonjour", "g'day")
	// the Strings() function will return results in random order
	output := greetings.Strings()
	sort.Strings(output)
	fmt.Println(output)
}
Output:
[aloha bonjour g'day hello]

func (*StringSet) Clone

func (ss *StringSet) Clone() *StringSet

Clone duplicates a StringSet.

func (*StringSet) Contains

func (ss *StringSet) Contains(s string) bool

Contains returns whether a given string is in the set.

func (*StringSet) Delete

func (ss *StringSet) Delete(sa ...string) *StringSet

Delete removes one or more items from a set if they exist in the set.

Example
package main

import (
	"fmt"
	"sort"

	"github.com/AchievementNetwork/stringset"
)

func main() {
	nums := stringset.New().Add("1", "2", "3", "4", "5", "6", "7")
	nums.Delete("2", "4", "6")
	output := nums.Strings()
	sort.Strings(output)
	fmt.Println(output)
}
Output:
[1 3 5 7]

func (*StringSet) Difference

func (ss *StringSet) Difference(ss2 *StringSet) *StringSet

Difference is an assymetric set difference. It subtracts the rhs from the lhs and returns a new set. IsNegative has no effect.

func (*StringSet) Equals

func (ss *StringSet) Equals(ss2 *StringSet) bool

Equals checks whether two string sets have the same members. IsNegative has no effect.

func (*StringSet) Filter

func (ss *StringSet) Filter(f func(s string) bool) *StringSet

Filter returns a new StringSet that contains only the members of the original set where f(member) evaluates to true. Negate has no effect.

func (*StringSet) Intersection

func (ss *StringSet) Intersection(ss2 *StringSet) *StringSet

Intersection returns the symmetric difference of the two sets, with the possibility that either or both sets may be negative. It's the equivalent of a boolean AND with either or both of the targets being inverted.

This returns a new set -- both operands are left unchanged.

abc & cde == c

abc & !cde == ab

!abc & cde == de

!abc & !cde == !abcde

Example
package main

import (
	"fmt"
	"sort"

	"github.com/AchievementNetwork/stringset"
)

func main() {
	ss1 := stringset.New().Add("a", "b", "c", "d")
	ss2 := stringset.New().Add("c", "d", "e", "f")

	inter := ss1.Intersection(ss2)
	output := inter.Strings()
	sort.Strings(output)
	fmt.Println(output)
}
Output:
[c d]

func (*StringSet) Join

func (ss *StringSet) Join(sep string) string

Join returns all the items in the set as a single string, in an arbitrarily-ordered list, separated by a separator.

func (*StringSet) Length

func (ss *StringSet) Length() int

Length returns the number of items currently in the set.

func (*StringSet) Negate

func (ss *StringSet) Negate() *StringSet

Negate flips the IsNegative bit.

Example
package main

import (
	"fmt"
	"sort"

	"github.com/AchievementNetwork/stringset"
)

func main() {
	ss1 := stringset.New().Add("a", "b", "c")
	ss2 := stringset.New().Add("c", "d", "e").Negate()

	// set 2 is negative so its elements will be deleted from set 1
	inter1 := ss1.Intersection(ss2)
	output1 := inter1.Strings()
	sort.Strings(output1)
	fmt.Println(output1)

	ss1.Negate()
	ss2.Negate()

	// set 1 is negative so its elements will be deleted from set 2
	inter2 := ss1.Intersection(ss2)
	output2 := inter2.Strings()
	sort.Strings(output2)
	fmt.Println(output2)

}
Output:
[a b]
[d e]

func (*StringSet) Strings

func (ss *StringSet) Strings() []string

Strings returns all the items in the set as a slice of strings.

func (*StringSet) Union

func (ss *StringSet) Union(ss2 *StringSet) *StringSet

Union generates the union (OR) of the two sets. Returns a new set and leaves the operands unchanged. IsNegative has no effect.

func (*StringSet) WrappedJoin

func (ss *StringSet) WrappedJoin(prefix string, sep string, suffix string) string

WrappedJoin does the same as Join but allows a prefix and a suffix.

Jump to

Keyboard shortcuts

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