Documentation ¶
Overview ¶
Package stringset permette di creare e gestire in parallelo set di stringhe.
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(o *StringSet) *StringSet
- func (s *StringSet) Exists(str string) bool
- func (s *StringSet) Intersect(other *StringSet) *StringSet
- func (s *StringSet) Len() int
- func (s *StringSet) Pop() (string, bool)
- func (s *StringSet) Strings() []string
- func (s *StringSet) Union(other *StringSet) *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 New ¶
func New() *StringSet
New crea una istanza di tipo *Stringset
Example ¶
package main import ( "fmt" "reflect" "github.com/axamon/stringset" ) func main() { s := stringset.New() fmt.Println(reflect.TypeOf(s)) }
Output: *stringset.StringSet
func NewStringSet ¶
NewStringSet crea una istanza *StringSet avente strings come contenuto.
Example ¶
package main import ( "fmt" "sort" "github.com/axamon/stringset" ) func main() { s := stringset.NewStringSet("pippo", "pluto", "minnie") slice := s.Strings() sort.Strings(slice) for _, element := range slice { fmt.Println(element) } }
Output: minnie pippo pluto
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 restituisce true se tutti gli elementi in other sono presenti in s.
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 elimina la stringa str da s.
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 restituisce un nuovo set con gli elementi in s meno quelli in o.
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 restituisce true se str è presente in s.
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 restituisce un nuovo *StringSet con solo gli elementi presenti in entrambi s e other: s ∩ other
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 returns and removes an arbitrary element from the set. If the set is empty it 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 restituisce la lista di stringhe contenute in s.
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) 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