mirror of
https://github.com/fumiama/terasu-cloudflared.git
synced 2026-06-12 06:00:25 +08:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
106
vendor/github.com/golang-collections/collections/set/set.go
generated
vendored
Executable file
106
vendor/github.com/golang-collections/collections/set/set.go
generated
vendored
Executable file
@@ -0,0 +1,106 @@
|
||||
package set
|
||||
|
||||
type (
|
||||
Set struct {
|
||||
hash map[interface{}]nothing
|
||||
}
|
||||
|
||||
nothing struct{}
|
||||
)
|
||||
|
||||
// Create a new set
|
||||
func New(initial ...interface{}) *Set {
|
||||
s := &Set{make(map[interface{}]nothing)}
|
||||
|
||||
for _, v := range initial {
|
||||
s.Insert(v)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Find the difference between two sets
|
||||
func (this *Set) Difference(set *Set) *Set {
|
||||
n := make(map[interface{}]nothing)
|
||||
|
||||
for k, _ := range this.hash {
|
||||
if _, exists := set.hash[k]; !exists {
|
||||
n[k] = nothing{}
|
||||
}
|
||||
}
|
||||
|
||||
return &Set{n}
|
||||
}
|
||||
|
||||
// Call f for each item in the set
|
||||
func (this *Set) Do(f func(interface{})) {
|
||||
for k, _ := range this.hash {
|
||||
f(k)
|
||||
}
|
||||
}
|
||||
|
||||
// Test to see whether or not the element is in the set
|
||||
func (this *Set) Has(element interface{}) bool {
|
||||
_, exists := this.hash[element]
|
||||
return exists
|
||||
}
|
||||
|
||||
// Add an element to the set
|
||||
func (this *Set) Insert(element interface{}) {
|
||||
this.hash[element] = nothing{}
|
||||
}
|
||||
|
||||
// Find the intersection of two sets
|
||||
func (this *Set) Intersection(set *Set) *Set {
|
||||
n := make(map[interface{}]nothing)
|
||||
|
||||
for k, _ := range this.hash {
|
||||
if _, exists := set.hash[k]; exists {
|
||||
n[k] = nothing{}
|
||||
}
|
||||
}
|
||||
|
||||
return &Set{n}
|
||||
}
|
||||
|
||||
// Return the number of items in the set
|
||||
func (this *Set) Len() int {
|
||||
return len(this.hash)
|
||||
}
|
||||
|
||||
// Test whether or not this set is a proper subset of "set"
|
||||
func (this *Set) ProperSubsetOf(set *Set) bool {
|
||||
return this.SubsetOf(set) && this.Len() < set.Len()
|
||||
}
|
||||
|
||||
// Remove an element from the set
|
||||
func (this *Set) Remove(element interface{}) {
|
||||
delete(this.hash, element)
|
||||
}
|
||||
|
||||
// Test whether or not this set is a subset of "set"
|
||||
func (this *Set) SubsetOf(set *Set) bool {
|
||||
if this.Len() > set.Len() {
|
||||
return false
|
||||
}
|
||||
for k, _ := range this.hash {
|
||||
if _, exists := set.hash[k]; !exists {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Find the union of two sets
|
||||
func (this *Set) Union(set *Set) *Set {
|
||||
n := make(map[interface{}]nothing)
|
||||
|
||||
for k, _ := range this.hash {
|
||||
n[k] = nothing{}
|
||||
}
|
||||
for k, _ := range set.hash {
|
||||
n[k] = nothing{}
|
||||
}
|
||||
|
||||
return &Set{n}
|
||||
}
|
||||
74
vendor/github.com/golang-collections/collections/set/set_test.go
generated
vendored
Normal file
74
vendor/github.com/golang-collections/collections/set/set_test.go
generated
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
package set
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) {
|
||||
s := New()
|
||||
|
||||
s.Insert(5)
|
||||
|
||||
if s.Len() != 1 {
|
||||
t.Errorf("Length should be 1")
|
||||
}
|
||||
|
||||
if !s.Has(5) {
|
||||
t.Errorf("Membership test failed")
|
||||
}
|
||||
|
||||
s.Remove(5)
|
||||
|
||||
if s.Len() != 0 {
|
||||
t.Errorf("Length should be 0")
|
||||
}
|
||||
|
||||
if s.Has(5) {
|
||||
t.Errorf("The set should be empty")
|
||||
}
|
||||
|
||||
// Difference
|
||||
s1 := New(1,2,3,4,5,6)
|
||||
s2 := New(4,5,6)
|
||||
s3 := s1.Difference(s2)
|
||||
|
||||
if s3.Len() != 3 {
|
||||
t.Errorf("Length should be 3")
|
||||
}
|
||||
|
||||
if !(s3.Has(1) && s3.Has(2) && s3.Has(3)) {
|
||||
t.Errorf("Set should only contain 1, 2, 3")
|
||||
}
|
||||
|
||||
// Intersection
|
||||
s3 = s1.Intersection(s2)
|
||||
if s3.Len() != 3 {
|
||||
t.Errorf("Length should be 3 after intersection")
|
||||
}
|
||||
|
||||
if !(s3.Has(4) && s3.Has(5) && s3.Has(6)) {
|
||||
t.Errorf("Set should contain 4, 5, 6")
|
||||
}
|
||||
|
||||
// Union
|
||||
s4 := New(7,8,9)
|
||||
s3 = s2.Union(s4)
|
||||
|
||||
if s3.Len() != 6 {
|
||||
t.Errorf("Length should be 6 after union")
|
||||
}
|
||||
|
||||
if !(s3.Has(7)) {
|
||||
t.Errorf("Set should contain 4, 5, 6, 7, 8, 9")
|
||||
}
|
||||
|
||||
// Subset
|
||||
if !s1.SubsetOf(s1) {
|
||||
t.Errorf("set should be a subset of itself")
|
||||
}
|
||||
// Proper Subset
|
||||
if s1.ProperSubsetOf(s1) {
|
||||
t.Errorf("set should not be a subset of itself")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user