stringset

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2021 License: MIT Imports: 1 Imported by: 5

README

stringset

GoDoc Build Status Go Report Card codecov Maintainability HitCount

stringset creates sets for strings in golang that are concurrency safe

Installation

go get -u github.com/axamon/stringset

Usage


package main

import (
    "fmt"
    "github.com/axamon/stringset"
)

func main() {
    testSet := NewStringSet("pippo", "pluto", "paperino", "pippo")
      
	slice := testSet.Strings()
	sort.Strings(slice)
	for _, element := range slice {
		fmt.Println(element)
	}
	// Output:
	// paperino
	// pippo
	// pluto
}

Benchmarks

BenchmarkAdd-16          	 3000000	       497 ns/op

BenchmarkDelete-16       	 3000000	       539 ns/op

BenchmarkIntersect-16    	 1000000	      2168 ns/op

BenchmarkUnion-16        	 2000000	      1825 ns/op

Documentation

Overview

Package stringset permette di creare e gestire in parallelo set di stringhe.

Index

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

func NewStringSet(strings ...string) *StringSet

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

func (s *StringSet) Add(str string)

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

func (s *StringSet) AddSlice(slice []string) *StringSet

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

func (s *StringSet) Contains(other *StringSet) bool

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

func (s *StringSet) Delete(str string)

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

func (s *StringSet) Difference(o *StringSet) *StringSet

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

func (s *StringSet) Exists(str string) bool

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

func (s *StringSet) Intersect(other *StringSet) *StringSet

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

func (s *StringSet) Len() int

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

func (s *StringSet) Pop() (string, bool)

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

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

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

func (s *StringSet) Union(other *StringSet) *StringSet

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

Jump to

Keyboard shortcuts

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