• Stars
    star
    804
  • Rank 54,406 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Type-safe, zero-allocation sets for Go

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 sets 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 *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(sets ...*Set) *Set

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

func New

func New(ts ...T) *Set

New creates and initializes a new Set.

func NewWithSize

func NewWithSize(size int) *Set

NewWithSize creates a new Set and gives make map a size hint.

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(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 ...T)

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 T) 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 ...T) 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) 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() []T

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() T

Pop deletes and returns an item from the Set. The underlying Set s is modified. If Set is empty, the zero value is returned.

func (*Set) Pop2

func (s *Set) Pop2() (T, bool)

Pop2 tries to delete and return an item from the Set. The underlying Set s is modified. The second value is a bool that is true if the item existed in the set, and false if not. If Set is empty, the zero value and false are returned.

func (*Set) Remove

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

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

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:

./gen_set.sh SomeType sometypeset

this would generate a new directory sometypeset in current working directory.

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!

More Repositories

1

scylladb

NoSQL data store using the seastar framework, compatible with Apache Cassandra
C++
12,344
star
2

seastar

High performance server-side application framework
C++
7,604
star
3

gocqlx

All-In-One: CQL query builder, ORM and migration tool
Go
868
star
4

scylla-rust-driver

Async CQL driver for Rust, optimized for Scylla!
Rust
517
star
5

scylla-operator

The Kubernetes Operator for ScyllaDB
Go
312
star
6

charybdefs

ScyllaDB fault injection filesystem
C++
241
star
7

scylla-code-samples

Code samples for working with ScyllaDB
Python
222
star
8

diskplorer

Python
210
star
9

scylla-monitoring

Simple monitoring of Scylla with Grafana
Shell
203
star
10

dpdk

Mirror of Data Plane Development Kit, git://dpdk.org/dpdk (http://dpdk.org)
C++
173
star
11

scylla-go-driver

Experimental, high performance Scylla Driver, University of Warsaw students' project
Go
146
star
12

termtables

Fork of github.com/apcera/termtables
Go
55
star
13

scylla-migrator

Migrate data extract using Spark to Scylla, normally from Cassandra
Scala
50
star
14

scylla-cluster-tests

Tests for Scylla Clusters
Python
50
star
15

scylla-tools-java

Apache Cassandra, supplying tools for Scylla
Java
49
star
16

gaming-leaderboard-demo

Learn how to use ScyllaDB to generate a monstrously fast leadearboard for your application!
TypeScript
48
star
17

scylla-manager

The Scylla Manager
Go
42
star
18

kafka-connect-scylladb

Kafka Connect Scylladb Sink
Java
42
star
19

scylla-ansible-roles

Ansible roles for deploying and managing Scylla, Scylla-Manager and Scylla-Monitoring
Python
39
star
20

scylla-bench

Go
39
star
21

scylla-cdc-source-connector

A Kafka source connector capturing Scylla CDC changes
Java
38
star
22

scylla-cloud-getting-started

Rust
38
star
23

scylla-cdc-go

Go
37
star
24

care-pet

Care Pet IoT ScyllaDB example
JavaScript
37
star
25

cassandra-test-and-deploy

Python
30
star
26

scylla-cdc-rust

Rust
27
star
27

gemini

Test data integrity by comparing against an Oracle running in parallel
Go
27
star
28

scylla-seastar

Stable-branch fork of seatar, for scylla use
C++
26
star
29

scylla-jmx

Scylla JMX proxy
Java
25
star
30

go-reflectx

Go reflection library to find struct field by its tag
Go
24
star
31

scylla-graphite-monitoring

Python
20
star
32

scylla-cdc-java

Java
20
star
33

scylla-ccm

Cassandra Cluster Manager, modified for Scylla
Python
18
star
34

scylla-machine-image

Python
17
star
35

scylla-grafana-datasource

A grafana backend plugin for ScyllaDB
TypeScript
17
star
36

scylladb-feature-store

A feature store sample application built with ScyllaDB
Jupyter Notebook
14
star
37

jepsen

scylladb jepsen clone
Clojure
14
star
38

alternator-load-balancing

Various tricks, scripts, and libraries, for load balancing multiple Alternator nodes
Java
14
star
39

sphinx-scylladb-theme

The documentation toolchain for Scylla projects.
Python
12
star
40

cpp-rust-driver

API-compatible rewrite of https://github.com/scylladb/cpp-driver as a wrapper for Rust driver.
C++
11
star
41

go-sshtools

Go SSH client wrapper and other tools
Go
11
star
42

video-streaming

Video streaming sample app with NextJs + ScyllaDB
TypeScript
10
star
43

terraform-provider-scylladbcloud

Terraform provider for ScyllaDB Cloud.
Go
10
star
44

scylla-rust-udf

Rust
10
star
45

scylla-cqlsh

A fork of the cqlsh code
Python
9
star
46

sstable-tools

Python
7
star
47

scylla-ami

Python
7
star
48

1m-ops-demo

1 million operations/second ScyllaDB demo
HCL
6
star
49

scylla-swagger-ui

Private fork of https://github.com/swagger-api/swagger-ui for scylla
JavaScript
6
star
50

scylla-python3

Python
6
star
51

scylla-stress-orchestrator

Python
5
star
52

cql-stress

Rust
5
star
53

scylla-price-calculator

Compare Scylla Cloud cost to estimations of other DBaaS services cost. Data is served with no guarantees. Please validate with each DBaaS price list per region.
Vue
5
star
54

argus

Svelte
4
star
55

go-log

Wrapper over Uber Zap
Go
4
star
56

scylla-api-client

Scylla CLI Python module and command line utility
Python
4
star
57

jenkins-slave

Jenkins slaves as docker images
4
star
58

k8s-local-volume-provisioner

Go
3
star
59

python-driver-matrix

Groovy
3
star
60

seastar-website

Seastar static website
HTML
3
star
61

scylladb-web-install

Shell
3
star
62

scylla-java-driver-matrix

Groovy
3
star
63

scylla-doc-issues

Repository for reporting issues about Scylla documentation (Deprecated)
2
star
64

scylla-artifact-tests

Tests for the scylla distro packages
Python
2
star
65

scylla-cluster-autoscaler

Go
2
star
66

scylla-operator-images

This repository contains the source for shared images used by Scylla Operator projects. We do not provide any support or compatibility guaranties for these images as their sole purpose is only to provide a common base in our projects.
Dockerfile
2
star
67

scylla-cpp-driver-matrix

Groovy
1
star
68

scylla-build-dependencies-docker

Dockerfiles for generating images with scylla build dependencies
1
star
69

scylladb-cloud-doc-issues

A repo for Scylla Cloud docs issues
1
star