stringset

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2020 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 allows the creation of sets for strings that are concurrecy safe.

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 creates a new instance of type *Stringset

func NewStringSet

func NewStringSet(strings ...string) *StringSet

NewStringSet creates a new set for strings.

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 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

func (s *StringSet) Delete(str string)

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

func (s *StringSet) Difference(other *StringSet) (diff *StringSet)

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

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

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

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

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

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() (str string, ok bool)

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

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

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

func (s *StringSet) Unify(other *StringSet)

Unify returns the first set which contains all elements of the two sets.

func (*StringSet) Union

func (s *StringSet) Union(other *StringSet) (union *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