github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/set/uint64set.go (about) 1 // Copyright 2020 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package set 16 17 import "sort" 18 19 type Uint64Set struct { 20 uints map[uint64]bool 21 } 22 23 func NewUint64Set(uints []uint64) *Uint64Set { 24 s := &Uint64Set{make(map[uint64]bool, len(uints))} 25 26 for _, b := range uints { 27 s.uints[b] = true 28 } 29 30 return s 31 } 32 33 func (us *Uint64Set) Contains(i uint64) bool { 34 _, present := us.uints[i] 35 return present 36 } 37 38 func (us *Uint64Set) ContainsAll(uints []uint64) bool { 39 for _, b := range uints { 40 if _, present := us.uints[b]; !present { 41 return false 42 } 43 } 44 45 return true 46 } 47 48 func (us *Uint64Set) Add(i uint64) { 49 us.uints[i] = true 50 } 51 52 func (us *Uint64Set) Remove(i uint64) { 53 delete(us.uints, i) 54 } 55 56 func (us *Uint64Set) Intersection(other *Uint64Set) *Uint64Set { 57 inter := &Uint64Set{uints: make(map[uint64]bool)} 58 for member := range us.uints { 59 if other.Contains(member) { 60 inter.Add(member) 61 } 62 } 63 return inter 64 } 65 66 func (us *Uint64Set) AsSlice() []uint64 { 67 sl := make([]uint64, 0, us.Size()) 68 for k := range us.uints { 69 sl = append(sl, k) 70 } 71 sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] }) 72 return sl 73 } 74 75 func (us *Uint64Set) Size() int { 76 return len(us.uints) 77 }