Documentation ¶
Overview ¶
Package stringset allows the creation of sets for strings that are concurrecy safe.
Index ¶
- type StringSet
- func (s *StringSet) Add(str string)
- func (s *StringSet) AddSlice(slice []string) *StringSet
- func (s *StringSet) Contains(other *StringSet) bool
- func (s *StringSet) Delete(str string)
- func (s *StringSet) Difference(other *StringSet) (diff *StringSet)
- func (s *StringSet) Exists(str string) bool
- func (s *StringSet) Intersect(other *StringSet) (intersection *StringSet)
- func (s *StringSet) Len() int
- func (s *StringSet) Pop() (str string, ok bool)
- func (s *StringSet) Strings() []string
- func (s *StringSet) Unify(other *StringSet)
- func (s *StringSet) Union(other *StringSet) (union *StringSet)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type StringSet ¶
type StringSet struct {
// contains filtered or unexported fields
}
StringSet is a set of unique strings. The lock sync.RWMutex allows to solve concurrency issues
func NewStringSet ¶
NewStringSet creates a new set for strings.
func (*StringSet) Add ¶
Add adds a string to the set. If string is already in the set, it has no effect.
Example ¶
package main import ( "fmt" "sort" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo") testSet.Add("pluto") testSet.Add("nonna papera") slice := testSet.Strings() sort.Strings(slice) for _, element := range slice { fmt.Println(element) } }
Output: nonna papera paperino pippo pluto
func (*StringSet) AddSlice ¶
AddSlice adds the elements of the slice to the set.
Example ¶
package main import ( "fmt" "github.com/axamon/stringset" ) func main() { slice := []string{"pluto", "paperino", "pluto"} s := stringset.New() s.AddSlice(slice) fmt.Println(s.Len()) }
Output: 2
func (*StringSet) Contains ¶
Contains returns true if the given set contains all elements from the other set.
Example ¶
package main import ( "fmt" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo") testSet2 := stringset.NewStringSet("pippo", "pluto") if ok := testSet.Contains(testSet2); ok { fmt.Println("Yes") } if ok := testSet2.Contains(testSet); !ok { fmt.Println("No") } }
Output: Yes No
func (*StringSet) Delete ¶
Delete removes a string from the set.
Example ¶
package main import ( "fmt" "sort" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo") testSet.Delete("pluto") slice := testSet.Strings() sort.Strings(slice) for _, element := range slice { fmt.Println(element) } }
Output: paperino pippo
func (*StringSet) Difference ¶
Difference returns a new set with all elements from the first set and no elements from the latter.
Example ¶
package main import ( "fmt" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo") testSet2 := stringset.NewStringSet("paperino", "pluto") diff := testSet.Difference(testSet2) fmt.Println(diff.Strings()[0]) }
Output: pippo
func (*StringSet) Exists ¶
Exists checks if string exists in the set.
Example ¶
package main import ( "fmt" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo") element := "pippo" if ok := testSet.Exists(element); ok { fmt.Printf("%s exists", element) } }
Output: pippo exists
func (*StringSet) Intersect ¶
Intersect returns a new set which contains only the elemets shared by both input sets.
Example ¶
package main import ( "fmt" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo", "poldo", "minnie") testSet2 := stringset.NewStringSet("paperino", "pluto", "nonna papera") inersect := testSet.Intersect(testSet2) list := inersect.Strings() for _, element := range list { fmt.Println(element) } }
Output: paperino pluto
Example (Second) ¶
package main import ( "fmt" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("paperino", "pluto", "nonna papera") testSet2 := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo", "poldo", "minnie") inersect := testSet.Intersect(testSet2) list := inersect.Strings() for _, element := range list { fmt.Println(element) } }
Output: paperino pluto
func (*StringSet) Len ¶
Len returns the number of items in the set. Cannot be used in for loops.
Example ¶
package main import ( "fmt" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo") testSet2 := stringset.NewStringSet("pippo", "pluto") testSet3 := stringset.NewStringSet() fmt.Println(testSet.Len()) fmt.Println(testSet2.Len()) fmt.Println(testSet3.Len()) }
Output: 3 2 0
func (*StringSet) Pop ¶
Pop removes and returns an arbitrary element from the set and removes it from the set. If the set was empty, this returns ("", false).
Example ¶
package main import ( "fmt" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo") num := testSet.Len() for i := 0; i <= num; i++ { //testSet.Len() cannot be used in for loops element, _ := testSet.Pop() fmt.Println(element) } empty, ok := testSet.Pop() fmt.Println(empty, ok) }
Output: paperino pippo pluto false
func (*StringSet) Strings ¶
Strings returns a slice of strings in the set.
Example ¶
package main import ( "fmt" "sort" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo") slice := testSet.Strings() sort.Strings(slice) for _, element := range slice { fmt.Println(element) } }
Output: paperino pippo pluto
Example (Second) ¶
package main import ( "fmt" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet() for _, element := range testSet.Strings() { fmt.Println(element) } }
Output:
func (*StringSet) Unify ¶ added in v1.0.0
Unify returns the first set which contains all elements of the two sets.
func (*StringSet) Union ¶
Union returns a new set which contains all elements of the previous ones.
Example ¶
package main import ( "fmt" "github.com/axamon/stringset" ) func main() { testSet := stringset.NewStringSet("pippo", "pluto", "paperino", "pippo") testSet2 := stringset.NewStringSet("pippo", "pluto", "minnie") u := testSet.Union(testSet2) slice := u.Strings() for _, element := range slice { fmt.Println(element) } }
Output: minnie paperino pippo pluto