gxset

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2018 License: Apache-2.0, Apache-2.0 Imports: 17 Imported by: 0

README

Set GoDoc Go Report Card Build Status

Package set is a type-safe, zero-allocation port of the excellent package fatih/set. It contains sets for most of the basic types and you can generate set for your own types with ease.

Example

Example code using the generated string set:

import "github.com/scylladb/go-set/strset"

s1 := strset.New("entry 1", "entry 2")
s2 := strset.New("entry 2", "entry 3")
s3 := strset.Intersection(s1, s2)
// s3 now contains only "entry 2"

The library exposes a number of top level factory functions that can be used to create a specific instances of the set type you want to use. For example to create a set to store int you could do like this:

import "github.com/scylladb/go-set"

s := set.NewIntSet()
// use the set...

Usage

In every subpackage Set is the main set structure that holds all the data and methods used to working with the set.

func Difference
func Difference(set1, set2 *Set, sets ...*Set) *Set

Difference returns a new set which contains items which are in in the first set but not in the others.

func Intersection
func Intersection(set1, set2 *Set, sets ...*Set) *Set

Intersection returns a new set which contains items that only exist in all given sets.

func New
func New(ts ...int) *Set

New creates and initializes a new Set interface. Its single parameter denotes the type of set to create. Either ThreadSafe or NonThreadSafe. The default is ThreadSafe.

func SymmetricDifference
func SymmetricDifference(s *Set, t *Set) *Set

SymmetricDifference returns a new set which s is the difference of items which are in one of either, but not in both.

func Union
func Union(set1, set2 *Set, sets ...*Set) *Set

Union is the merger of multiple sets. It returns a new set with all the elements present in all the sets that are passed.

func (*Set) Add
func (s *Set) Add(items ...int)

Add includes the specified items (one or more) to the Set. The underlying Set s is modified. If passed nothing it silently returns.

func (*Set) Clear
func (s *Set) Clear()

Clear removes all items from the Set.

func (*Set) Copy
func (s *Set) Copy() *Set

Copy returns a new Set with a copy of s.

func (*Set) Each
func (s *Set) Each(f func(item int) bool)

Each traverses the items in the Set, calling the provided function for each Set member. Traversal will continue until all items in the Set have been visited, or if the closure returns false.

func (*Set) Has
func (s *Set) Has(items ...int) bool

Has looks for the existence of items passed. It returns false if nothing is passed. For multiple items it returns true only if all of the items exist.

func (*Set) IsEmpty
func (s *Set) IsEmpty() bool

IsEmpty reports whether the Set is empty.

func (*Set) IsEqual
func (s *Set) IsEqual(t *Set) bool

IsEqual test whether s and t are the same in size and have the same items.

func (*Set) IsSubset
func (s *Set) IsSubset(t *Set) (subset bool)

IsSubset tests whether t is a subset of s.

func (*Set) IsSuperset
func (s *Set) IsSuperset(t *Set) bool

IsSuperset tests whether t is a superset of s.

func (*Set) List
func (s *Set) List() []int

List returns a slice of all items. There is also StringSlice() and IntSlice() methods for returning slices of type string or int.

func (*Set) Merge
func (s *Set) Merge(t *Set)

Merge is like Union, however it modifies the current Set it's applied on with the given t Set.

func (*Set) Pop
func (s *Set) Pop() int

Pop deletes and return an item from the Set. The underlying Set s is modified. If Set is empty, nil is returned.

func (*Set) Remove
func (s *Set) Remove(items ...int)

Remove deletes the specified items from the Set. The underlying Set s is modified. If passed nothing it silently returns.

func (*Set) Separate
func (s *Set) Separate(t *Set)

Separate removes the Set items containing in t from Set s. Please aware that it's not the opposite of Merge.

func (*Set) Size
func (s *Set) Size() int

Size returns the number of items in a Set.

func (*Set) String
func (s *Set) String() string

String returns a string representation of s

Performance

The improvement in performance by using concrete types over interface{} is notable. Below you will find benchmark results comparing type-safe sets to fatih/set counterparts for string, int64, int32, float64 and float32 on a local machine, Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz.

pkg: github.com/scylladb/go-set/strset
BenchmarkTypeSafeSetHasNonExisting-4            200000000                7.02 ns/op            0 B/op          0 allocs/op
BenchmarkInterfaceSetHasNonExisting-4           20000000                60.0 ns/op            32 B/op          2 allocs/op
BenchmarkTypeSafeSetHasExisting-4               200000000                9.02 ns/op            0 B/op          0 allocs/op
BenchmarkInterfaceSetHasExisting-4              20000000                97.0 ns/op            32 B/op          2 allocs/op
BenchmarkTypeSafeSetHasExistingMany-4           100000000               16.8 ns/op             0 B/op          0 allocs/op
BenchmarkInterfaceSetHasExistingMany-4          30000000               106 ns/op              32 B/op          2 allocs/op
BenchmarkTypeSafeSetAdd-4                        3000000               469 ns/op              58 B/op          0 allocs/op
BenchmarkInterfaceSetAdd-4                       2000000               909 ns/op             117 B/op          2 allocs/op

pkg: github.com/scylladb/go-set/i64set
BenchmarkTypeSafeSetHasNonExisting-4            300000000                5.51 ns/op            0 B/op          0 allocs/op
BenchmarkInterfaceSetHasNonExisting-4           30000000                49.4 ns/op            24 B/op          2 allocs/op
BenchmarkTypeSafeSetHasExisting-4               200000000                7.53 ns/op            0 B/op          0 allocs/op
BenchmarkInterfaceSetHasExisting-4              20000000                68.5 ns/op            24 B/op          2 allocs/op
BenchmarkTypeSafeSetHasExistingMany-4           100000000               11.0 ns/op             0 B/op          0 allocs/op
BenchmarkInterfaceSetHasExistingMany-4          20000000                74.5 ns/op            24 B/op          2 allocs/op
BenchmarkTypeSafeSetAdd-4                       10000000               225 ns/op              40 B/op          0 allocs/op
BenchmarkInterfaceSetAdd-4                       3000000               403 ns/op              82 B/op          2 allocs/op

pkg: github.com/scylladb/go-set/i32set
BenchmarkTypeSafeSetHasNonExisting-4            300000000                5.61 ns/op            0 B/op          0 allocs/op
BenchmarkInterfaceSetHasNonExisting-4           30000000                48.8 ns/op            20 B/op          2 allocs/op
BenchmarkTypeSafeSetHasExisting-4               200000000                7.07 ns/op            0 B/op          0 allocs/op
BenchmarkInterfaceSetHasExisting-4              20000000                69.3 ns/op            20 B/op          2 allocs/op
BenchmarkTypeSafeSetHasExistingMany-4           100000000               11.7 ns/op             0 B/op          0 allocs/op
BenchmarkInterfaceSetHasExistingMany-4          20000000                71.1 ns/op            20 B/op          2 allocs/op
BenchmarkTypeSafeSetAdd-4                       10000000               206 ns/op              25 B/op          0 allocs/op
BenchmarkInterfaceSetAdd-4                       3000000               394 ns/op              78 B/op          2 allocs/op

pkg: github.com/scylladb/go-set/f64set
BenchmarkTypeSafeSetHasNonExisting-4            300000000                5.82 ns/op            0 B/op          0 allocs/op
BenchmarkInterfaceSetHasNonExisting-4           30000000                49.8 ns/op            24 B/op          2 allocs/op
BenchmarkTypeSafeSetHasExisting-4               50000000                26.8 ns/op             0 B/op          0 allocs/op
BenchmarkInterfaceSetHasExisting-4              20000000                77.6 ns/op            24 B/op          2 allocs/op
BenchmarkTypeSafeSetHasExistingMany-4           50000000                27.6 ns/op             0 B/op          0 allocs/op
BenchmarkInterfaceSetHasExistingMany-4          20000000                82.3 ns/op            24 B/op          2 allocs/op
BenchmarkTypeSafeSetAdd-4                       10000000               270 ns/op              40 B/op          0 allocs/op
BenchmarkInterfaceSetAdd-4                       3000000               428 ns/op              82 B/op          2 allocs/op

pkg: github.com/scylladb/go-set/f32set
BenchmarkTypeSafeSetHasNonExisting-4            300000000                5.78 ns/op            0 B/op          0 allocs/op
BenchmarkInterfaceSetHasNonExisting-4           30000000                49.3 ns/op            20 B/op          2 allocs/op
BenchmarkTypeSafeSetHasExisting-4               50000000                24.9 ns/op             0 B/op          0 allocs/op
BenchmarkInterfaceSetHasExisting-4              20000000                78.6 ns/op            20 B/op          2 allocs/op
BenchmarkTypeSafeSetHasExistingMany-4           50000000                25.1 ns/op             0 B/op          0 allocs/op
BenchmarkInterfaceSetHasExistingMany-4          20000000                81.1 ns/op            20 B/op          2 allocs/op
BenchmarkTypeSafeSetAdd-4                       10000000               246 ns/op              24 B/op          0 allocs/op
BenchmarkInterfaceSetAdd-4                       3000000               408 ns/op              78 B/op          2 allocs/op

Code generation

For code generation we use Google go_generics tool that we forked to provide bazel-free installation, to install run:

go get -u github.com/mmatczuk/go_generics/cmd/go_generics
go get -u github.com/mmatczuk/go_generics/cmd/go_merge

Once you have go_generics installed properly you can regenerate the code using go generate in the top level directory.

Your custom types

If you have types that you would like to use but the are not amenable for inclusion in this library you can simply generate code on your own and put it in your package.

For example, to generate a set for SomeType in package sometypeset call:

go_generics -i internal/set/set.go -t T=SomeType -o path/to/outputfile.go -p sometypeset
go_generics -i internal/set/set_test.go -t P=SomeType -o path/to/outputfile_test.go -p sometypeset

If you think your addition belongs here we are open to accept pull requests.

License

Copyright (C) 2018 ScyllaDB

This project is distributed under the Apache 2.0 license. See the LICENSE file for details. It contains software from:

GitHub star is always appreciated!

Documentation

Overview

Package set is a type-safe, zero-allocation port of the excellent package fatih/set. It contains sets for most of the basic types and you can generate set for your own types with ease.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewByte16Set added in v0.3.1

func NewByte16Set() *b16set.Set

NewByte16Set is a convenience function to create a new b16set.Set

func NewByte32Set added in v0.3.1

func NewByte32Set() *b32set.Set

NewByte32Set is a convenience function to create a new b32set.Set

func NewByte64Set added in v0.3.1

func NewByte64Set() *b64set.Set

NewByte64Set is a convenience function to create a new b64set.Set

func NewByte8Set added in v0.3.1

func NewByte8Set() *b8set.Set

NewByte8Set is a convenience function to create a new b16set.Set

func NewFloat32Set added in v0.3.1

func NewFloat32Set() *f32set.Set

NewFloat32Set is a convenience function to create a new f32set.Set

func NewFloat64Set added in v0.3.1

func NewFloat64Set() *f64set.Set

NewFloat64Set is a convenience function to create a new f64set.Set

func NewInt16Set added in v0.3.1

func NewInt16Set() *i16set.Set

NewInt16Set is a convenience function to create a new i16set.Set

func NewInt32Set added in v0.3.1

func NewInt32Set() *i32set.Set

NewInt32Set is a convenience function to create a new i32set.Set

func NewInt64Set added in v0.3.1

func NewInt64Set() *i64set.Set

NewInt64Set is a convenience function to create a new i64set.Set

func NewInt8Set added in v0.3.1

func NewInt8Set() *i8set.Set

NewInt8Set is a convenience function to create a new i8set.Set

func NewIntSet added in v0.3.1

func NewIntSet() *iset.Set

NewIntSet is a convenience function to create a new iset.Set

func NewStringSet added in v0.3.1

func NewStringSet() *strset.Set

NewStringSet is a convenience function to create a new strset.Set

func NewUint16Set added in v0.3.1

func NewUint16Set() *u16set.Set

NewUint16Set is a convenience function to create a new u16set.Set

func NewUint32Set added in v0.3.1

func NewUint32Set() *u32set.Set

NewUint32Set is a convenience function to create a new u32set.Set

func NewUint64Set added in v0.3.1

func NewUint64Set() *u64set.Set

NewUint64Set is a convenience function to create a new u64set.Set

func NewUint8Set added in v0.3.1

func NewUint8Set() *u8set.Set

NewUint8Set is a convenience function to create a new u8set.Set

func NewUintSet added in v0.3.1

func NewUintSet() *uset.Set

NewUintSet is a convenience function to create a new uset.Set

Types

This section is empty.

Directories

Path Synopsis
internal
set

Jump to

Keyboard shortcuts

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