mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-06 15:22:47 +01:00
57
caql/set.go
57
caql/set.go
@@ -22,19 +22,18 @@
|
||||
|
||||
package caql
|
||||
|
||||
import "sort"
|
||||
|
||||
type (
|
||||
Set struct {
|
||||
hash map[interface{}]nothing
|
||||
}
|
||||
|
||||
nothing struct{}
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Create a new set
|
||||
func New(initial ...interface{}) *Set {
|
||||
s := &Set{make(map[interface{}]nothing)}
|
||||
type Set struct {
|
||||
hash map[any]nothing
|
||||
}
|
||||
|
||||
type nothing struct{}
|
||||
|
||||
func NewSet(initial ...any) *Set {
|
||||
s := &Set{make(map[any]nothing)}
|
||||
|
||||
for _, v := range initial {
|
||||
s.Insert(v)
|
||||
@@ -43,9 +42,8 @@ func New(initial ...interface{}) *Set {
|
||||
return s
|
||||
}
|
||||
|
||||
// Find the difference between two sets
|
||||
func (s *Set) Difference(set *Set) *Set {
|
||||
n := make(map[interface{}]nothing)
|
||||
n := make(map[any]nothing)
|
||||
|
||||
for k := range s.hash {
|
||||
if _, exists := set.hash[k]; !exists {
|
||||
@@ -56,27 +54,18 @@ func (s *Set) Difference(set *Set) *Set {
|
||||
return &Set{n}
|
||||
}
|
||||
|
||||
// Call f for each item in the set
|
||||
func (s *Set) Do(f func(interface{})) {
|
||||
for k := range s.hash {
|
||||
f(k)
|
||||
}
|
||||
}
|
||||
|
||||
// Test to see whether or not the element is in the set
|
||||
func (s *Set) Has(element interface{}) bool {
|
||||
func (s *Set) Has(element any) bool {
|
||||
_, exists := s.hash[element]
|
||||
|
||||
return exists
|
||||
}
|
||||
|
||||
// Add an element to the set
|
||||
func (s *Set) Insert(element interface{}) {
|
||||
func (s *Set) Insert(element any) {
|
||||
s.hash[element] = nothing{}
|
||||
}
|
||||
|
||||
// Find the intersection of two sets
|
||||
func (s *Set) Intersection(set *Set) *Set {
|
||||
n := make(map[interface{}]nothing)
|
||||
n := make(map[any]nothing)
|
||||
|
||||
for k := range s.hash {
|
||||
if _, exists := set.hash[k]; exists {
|
||||
@@ -87,23 +76,20 @@ func (s *Set) Intersection(set *Set) *Set {
|
||||
return &Set{n}
|
||||
}
|
||||
|
||||
// Return the number of items in the set
|
||||
func (s *Set) Len() int {
|
||||
return len(s.hash)
|
||||
}
|
||||
|
||||
// Test whether or not this set is a proper subset of "set"
|
||||
func (s *Set) ProperSubsetOf(set *Set) bool {
|
||||
return s.SubsetOf(set) && s.Len() < set.Len()
|
||||
}
|
||||
|
||||
// Remove an element from the set
|
||||
func (s *Set) Remove(element interface{}) {
|
||||
func (s *Set) Remove(element any) {
|
||||
delete(s.hash, element)
|
||||
}
|
||||
|
||||
func (s *Set) Minus(set *Set) *Set {
|
||||
n := make(map[interface{}]nothing)
|
||||
n := make(map[any]nothing)
|
||||
for k := range s.hash {
|
||||
n[k] = nothing{}
|
||||
}
|
||||
@@ -115,7 +101,6 @@ func (s *Set) Minus(set *Set) *Set {
|
||||
return &Set{n}
|
||||
}
|
||||
|
||||
// Test whether or not this set is a subset of "set"
|
||||
func (s *Set) SubsetOf(set *Set) bool {
|
||||
if s.Len() > set.Len() {
|
||||
return false
|
||||
@@ -125,12 +110,12 @@ func (s *Set) SubsetOf(set *Set) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Find the union of two sets
|
||||
func (s *Set) Union(set *Set) *Set {
|
||||
n := make(map[interface{}]nothing)
|
||||
n := make(map[any]nothing)
|
||||
|
||||
for k := range s.hash {
|
||||
n[k] = nothing{}
|
||||
@@ -142,8 +127,8 @@ func (s *Set) Union(set *Set) *Set {
|
||||
return &Set{n}
|
||||
}
|
||||
|
||||
func (s *Set) Values() []interface{} {
|
||||
values := []interface{}{}
|
||||
func (s *Set) Values() []any {
|
||||
values := []any{}
|
||||
|
||||
for k := range s.hash {
|
||||
values = append(values, k)
|
||||
|
||||
Reference in New Issue
Block a user