aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty/cty/set/set.go
blob: b4fb316f1ccd13036efb41e5b45058f38a11c5d5 (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
package set

import (
	"fmt"
)

// Set is an implementation of the concept of a set: a collection where all
// values are conceptually either in or out of the set, but the members are
// not ordered.
//
// This type primarily exists to be the internal type of sets in cty, but
// it is considered to be at the same level of abstraction as Go's built in
// slice and map collection types, and so should make no cty-specific
// assumptions.
//
// Set operations are not thread safe. It is the caller's responsibility to
// provide mutex guarantees where necessary.
//
// Set operations are not optimized to minimize memory pressure. Mutating
// a set will generally create garbage and so should perhaps be avoided in
// tight loops where memory pressure is a concern.
type Set struct {
	vals  map[int][]interface{}
	rules Rules
}

// NewSet returns an empty set with the membership rules given.
func NewSet(rules Rules) Set {
	return Set{
		vals:  map[int][]interface{}{},
		rules: rules,
	}
}

func NewSetFromSlice(rules Rules, vals []interface{}) Set {
	s := NewSet(rules)
	for _, v := range vals {
		s.Add(v)
	}
	return s
}

func sameRules(s1 Set, s2 Set) bool {
	return s1.rules == s2.rules
}

func mustHaveSameRules(s1 Set, s2 Set) {
	if !sameRules(s1, s2) {
		panic(fmt.Errorf("incompatible set rules: %#v, %#v", s1.rules, s2.rules))
	}
}

// HasRules returns true if and only if the receiving set has the given rules
// instance as its rules.
func (s Set) HasRules(rules Rules) bool {
	return s.rules == rules
}

// Rules returns the receiving set's rules instance.
func (s Set) Rules() Rules {
	return s.rules
}