File: typeVarSet.go

package info (click to toggle)
golang-github-chewxy-hm 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: makefile: 2
file content (94 lines) | stat: -rw-r--r-- 1,587 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
package hm

import (
	"sort"

	"github.com/xtgo/set"
)

// TypeVarSet is a set of TypeVariable
type TypeVarSet []TypeVariable

// TypeVariables are orderable, so we fulfil the interface for sort.Interface

func (s TypeVarSet) Len() int           { return len(s) }
func (s TypeVarSet) Less(i, j int) bool { return s[i] < s[j] }
func (s TypeVarSet) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }

func (s TypeVarSet) Set() TypeVarSet {
	sort.Sort(s)
	n := set.Uniq(s)
	s = s[:n]
	return s
}

func (s TypeVarSet) Union(other TypeVarSet) TypeVarSet {
	if other == nil {
		return s
	}

	sort.Sort(s)
	sort.Sort(other)
	s2 := append(s, other...)
	n := set.Union(s2, len(s))
	return s2[:n]
}

func (s TypeVarSet) Intersect(other TypeVarSet) TypeVarSet {
	if len(s) == 0 || len(other) == 0 {
		return nil
	}

	sort.Sort(s)
	sort.Sort(other)
	s2 := append(s, other...)
	n := set.Inter(s2, len(s))
	return s2[:n]
}

func (s TypeVarSet) Difference(other TypeVarSet) TypeVarSet {
	sort.Sort(s)
	sort.Sort(other)
	s2 := append(s, other...)
	n := set.Diff(s2, len(s))
	return s2[:n]
}

func (s TypeVarSet) Contains(tv TypeVariable) bool {
	for _, v := range s {
		if v == tv {
			return true
		}
	}
	return false
}

func (s TypeVarSet) Index(tv TypeVariable) int {
	for i, v := range s {
		if v == tv {
			return i
		}
	}
	return -1
}

func (s TypeVarSet) Equals(other TypeVarSet) bool {
	if len(s) != len(other) {
		return false
	}

	if len(s) == 0 {
		return true
	}

	if &s[0] == &other[0] {
		return true
	}

	for _, v := range s {
		if !other.Contains(v) {
			return false
		}
	}
	return true
}