github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/intsets/oracle.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 intsets 12 13 // oracle implements the same API as Sparse using a map. It is only used for 14 // testing. 15 type oracle struct { 16 m map[int]struct{} 17 } 18 19 // Clear empties the set. 20 func (o *oracle) Clear() { 21 o.m = nil 22 } 23 24 // Add adds an integer to the set. 25 func (o *oracle) Add(i int) { 26 if o.m == nil { 27 o.m = make(map[int]struct{}) 28 } 29 o.m[i] = struct{}{} 30 } 31 32 // Remove removes an integer from the set. 33 func (o *oracle) Remove(i int) { 34 delete(o.m, i) 35 } 36 37 // Contains returns true if the set contains the given integer. 38 func (o oracle) Contains(i int) bool { 39 _, ok := o.m[i] 40 return ok 41 } 42 43 // Empty returns true if the set contains no integers. 44 func (o oracle) Empty() bool { 45 return len(o.m) == 0 46 } 47 48 // Len returns the number of integers in the set. 49 func (o oracle) Len() int { 50 return len(o.m) 51 } 52 53 // LowerBound returns the smallest element >= startVal, or MaxInt if there is no 54 // such element. 55 func (o *oracle) LowerBound(startVal int) int { 56 lb := MaxInt 57 for i := range o.m { 58 if i >= startVal && i < lb { 59 lb = i 60 } 61 } 62 return lb 63 } 64 65 // Min returns the minimum value in the set. If the set is empty, MaxInt is 66 // returned. 67 func (o *oracle) Min() int { 68 return o.LowerBound(MinInt) 69 } 70 71 // Copy sets the receiver to a copy of rhs, which can then be modified 72 // independently. 73 func (o *oracle) Copy(rhs *oracle) { 74 o.Clear() 75 for i := range rhs.m { 76 o.Add(i) 77 } 78 } 79 80 // UnionWith adds all the elements from rhs to this set. 81 func (o *oracle) UnionWith(rhs *oracle) { 82 for i := range rhs.m { 83 o.Add(i) 84 } 85 } 86 87 // IntersectionWith removes any elements not in rhs from this set. 88 func (o *oracle) IntersectionWith(rhs *oracle) { 89 for i := range o.m { 90 if !rhs.Contains(i) { 91 o.Remove(i) 92 } 93 } 94 } 95 96 // Intersects returns true if s has any elements in common with rhs. 97 func (o *oracle) Intersects(rhs *oracle) bool { 98 for i := range o.m { 99 if rhs.Contains(i) { 100 return true 101 } 102 } 103 return false 104 } 105 106 // DifferenceWith removes any elements in rhs from this set. 107 func (o *oracle) DifferenceWith(rhs *oracle) { 108 for i := range rhs.m { 109 o.Remove(i) 110 } 111 } 112 113 // Equals returns true if the two sets are identical. 114 func (o *oracle) Equals(rhs *oracle) bool { 115 if len(o.m) != len(rhs.m) { 116 return false 117 } 118 for i := range o.m { 119 if !rhs.Contains(i) { 120 return false 121 } 122 } 123 for i := range rhs.m { 124 if !o.Contains(i) { 125 return false 126 } 127 } 128 return true 129 } 130 131 // SubsetOf returns true if rhs contains all the elements in s. 132 func (o *oracle) SubsetOf(rhs *oracle) bool { 133 for i := range o.m { 134 if !rhs.Contains(i) { 135 return false 136 } 137 } 138 return true 139 }