File: example_test.go

package info (click to toggle)
golang-github-xtgo-set 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 148 kB
  • sloc: makefile: 2
file content (101 lines) | stat: -rw-r--r-- 3,084 bytes parent folder | download
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
// Copyright 2015 Kevin Gillette. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package set_test

import (
	"fmt"
	"sort"

	"github.com/xtgo/set"
)

func Example() {
	s := set.Strings([]string{"alpha", "gamma", "alpha"})
	fmt.Println("set:", s)

	s = set.StringsDo(set.Union, s, "beta")
	fmt.Println("set + beta:", s)

	fmt.Println(s, "contains any [alpha delta]:",
		set.StringsChk(set.IsInter, s, "alpha", "delta"))

	fmt.Println(s, "contains all [alpha delta]:",
		set.StringsChk(set.IsSuper, s, "alpha", "delta"))

	// Output:
	// set: [alpha gamma]
	// set + beta: [alpha beta gamma]
	// [alpha beta gamma] contains any [alpha delta]: true
	// [alpha beta gamma] contains all [alpha delta]: false
}

func ExampleUniq() {
	data := sort.IntSlice{5, 7, 3, 3, 5}

	sort.Sort(data)     // sort the data first
	n := set.Uniq(data) // Uniq returns the size of the set
	data = data[:n]     // trim the duplicate elements

	fmt.Println(data)
	// Output: [3 5 7]
}

func ExampleInter() {
	data := sort.IntSlice{3, 5, 7} // create a set (it must be sorted)
	pivot := len(data)             // store the length of our first set

	data = append(data, 1, 3, 5)   // append a second set (which also must be sorted)
	size := set.Inter(data, pivot) // perform set intersection

	// trim data to contain just the result set
	data = data[:size]

	fmt.Println("inter:", data)

	// Output:
	// inter: [3 5]
}

func ExampleIsSuper() {
	data := sort.StringSlice{"b", "c", "d"} // create a set (it must be sorted)
	pivot := len(data)                      // store the length of our first set

	data = append(data, "c", "d")         // append a second set (which also must be sorted)
	contained := set.IsSuper(data, pivot) // check the first set is a superset of the second

	fmt.Printf("%v superset of %v = %t\n", data[:pivot], data[pivot:], contained)

	data = data[:pivot] // trim off the second set

	data = append(data, "s")             // append a new singleton set to compare against
	contained = set.IsSuper(data, pivot) // check for membership

	fmt.Printf("%v superset of %v = %t\n", data[:pivot], data[pivot:], contained)

	// Output:
	// [b c d] superset of [c d] = true
	// [b c d] superset of [s] = false
}

func ExampleIsInter() {
	data := sort.StringSlice{"b", "c", "d"} // create a set (it must be sorted)
	pivot := len(data)                      // store the length of our first set

	data = append(data, "d", "z")         // append a second set (which also must be sorted)
	contained := set.IsInter(data, pivot) // check the first set is a superset of the second

	fmt.Printf("%v intersects %v = %t\n", data[:pivot], data[pivot:], contained)

	data = data[:pivot] // trim off the second set

	data = append(data, "s")             // append a new singleton set to compare against
	contained = set.IsInter(data, pivot) // check for membership

	fmt.Printf("%v intersects %v = %t\n", data[:pivot], data[pivot:], contained)

	// Output:
	// [b c d] intersects [d z] = true
	// [b c d] intersects [s] = false
}