github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/evalgen/string_set.go (about)

     1  // Copyright 2022 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package main
    12  
    13  import "sort"
    14  
    15  type stringSet map[string]struct{}
    16  
    17  func (ss stringSet) add(s string) { ss[s] = struct{}{} }
    18  
    19  func (ss stringSet) removeAll(other stringSet) {
    20  	for s := range other {
    21  		delete(ss, s)
    22  	}
    23  }
    24  
    25  func (ss stringSet) addAll(other stringSet) {
    26  	for s := range other {
    27  		ss.add(s)
    28  	}
    29  }
    30  
    31  func (ss stringSet) ordered() []string {
    32  	list := make([]string, 0, len(ss))
    33  	for s := range ss {
    34  		list = append(list, s)
    35  	}
    36  	sort.Strings(list)
    37  	return list
    38  }
    39  
    40  func (ss stringSet) contains(name string) bool {
    41  	_, exists := ss[name]
    42  	return exists
    43  }
    44  
    45  func (ss stringSet) intersection(other stringSet) stringSet {
    46  	intersection := stringSet{}
    47  	for s := range ss {
    48  		if other.contains(s) {
    49  			intersection.add(s)
    50  		}
    51  	}
    52  	return intersection
    53  }