aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/fatih/set.v0/README.md
blob: 23afdd98d316570f12cbe8b11eb910d626a4f319 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Set [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://godoc.org/gopkg.in/fatih/set.v0) [![Build Status](http://img.shields.io/travis/fatih/set.svg?style=flat-square)](https://travis-ci.org/fatih/set)

Set is a basic and simple, hash-based, **Set** data structure implementation
in Go (Golang).

Set provides both threadsafe and non-threadsafe implementations of a generic
set data structure. The thread safety encompasses all operations on one set.
Operations on multiple sets are consistent in that the elements of each set
used was valid at exactly one point in time between the start and the end of
the operation. Because it's thread safe, you can use it concurrently with your
goroutines.

For usage see examples below or click on the godoc badge.

## Install and Usage

Install the package with:

```bash
go get gopkg.in/fatih/set.v0
```

Import it with:

```go
import "gopkg.in/fatih/set.v0"
```

and use `set` as the package name inside the code.

## Examples

#### Initialization of a new Set

```go

// create a set with zero items
s := set.New()
s := set.NewNonTS() // non thread-safe version

// ... or with some initial values
s := set.New("istanbul", "frankfurt", 30.123, "san francisco", 1234)
s := set.NewNonTS("kenya", "ethiopia", "sumatra")

```

#### Basic Operations

```go
// add items
s.Add("istanbul")
s.Add("istanbul") // nothing happens if you add duplicate item

// add multiple items
s.Add("ankara", "san francisco", 3.14)

// remove item
s.Remove("frankfurt")
s.Remove("frankfurt") // nothing happes if you remove a nonexisting item

// remove multiple items
s.Remove("barcelona", 3.14, "ankara")

// removes an arbitary item and return it
item := s.Pop()

// create a new copy
other := s.Copy()

// remove all items
s.Clear()

// number of items in the set
len := s.Size()

// return a list of items
items := s.List()

// string representation of set
fmt.Printf("set is %s", s.String())

```

#### Check Operations

```go
// check for set emptiness, returns true if set is empty
s.IsEmpty()

// check for a single item exist
s.Has("istanbul")

// ... or for multiple items. This will return true if all of the items exist.
s.Has("istanbul", "san francisco", 3.14)

// create two sets for the following checks...
s := s.New("1", "2", "3", "4", "5")
t := s.New("1", "2", "3")


// check if they are the same
if !s.IsEqual(t) {
    fmt.Println("s is not equal to t")
}

// if s contains all elements of t
if s.IsSubset(t) {
    fmt.Println("t is a subset of s")
}

// ... or if s is a superset of t
if t.IsSuperset(s) {
    fmt.Println("s is a superset of t")
}


```

#### Set Operations


```go
// let us initialize two sets with some values
a := set.New("ankara", "berlin", "san francisco")
b := set.New("frankfurt", "berlin")

// creates a new set with the items in a and b combined.
// [frankfurt, berlin, ankara, san francisco]
c := set.Union(a, b)

// contains items which is in both a and b
// [berlin]
c := set.Intersection(a, b)

// contains items which are in a but not in b
// [ankara, san francisco]
c := set.Difference(a, b)

// contains items which are in one of either, but not in both.
// [frankfurt, ankara, san francisco]
c := set.SymmetricDifference(a, b)

```

```go
// like Union but saves the result back into a.
a.Merge(b)

// removes the set items which are in b from a and saves the result back into a.
a.Separate(b)

```

#### Multiple Set Operations

```go
a := set.New("1", "3", "4", "5")
b := set.New("2", "3", "4", "5")
c := set.New("4", "5", "6", "7")

// creates a new set with items in a, b and c
// [1 2 3 4 5 6 7]
u := set.Union(a, b, c)

// creates a new set with items in a but not in b and c
// [1]
u := set.Difference(a, b, c)

// creates a new set with items that are common to a, b and c
// [5]
u := set.Intersection(a, b, c)
```

#### Helper methods

The Slice functions below are a convenient way to extract or convert your Set data
into basic data types.


```go
// create a set of mixed types
s := set.New("ankara", "5", "8", "san francisco", 13, 21)


// convert s into a slice of strings (type is []string)
// [ankara 5 8 san francisco]
t := set.StringSlice(s)


// u contains a slice of ints (type is []int)
// [13, 21]
u := set.IntSlice(s)

```

#### Concurrent safe usage

Below is an example of a concurrent way that uses set. We call ten functions
concurrently and wait until they are finished. It basically creates a new
string for each goroutine and adds it to our set.

```go
package main

import (
    "fmt"
    "github.com/fatih/set"
    "strconv"
    "sync"
)

func main() {
    var wg sync.WaitGroup // this is just for waiting until all goroutines finish

    // Initialize our thread safe Set
    s := set.New()

    // Add items concurrently (item1, item2, and so on)
    for i := 0; i < 10; i++ {
        wg.Add(1)
        go func(i int) {
            item := "item" + strconv.Itoa(i)
            fmt.Println("adding", item)
            s.Add(item)
            wg.Done()
        }(i)
    }

    // Wait until all concurrent calls finished and print our set
    wg.Wait()
    fmt.Println(s)
}
```

## Credits

 * [Fatih Arslan](https://github.com/fatih)
 * [Arne Hormann](https://github.com/arnehormann)
 * [Sam Boyer](https://github.com/sdboyer)
 * [Ralph Loizzo](https://github.com/friartech)

## License

The MIT License (MIT) - see LICENSE.md for more details