github.com/songzhibin97/gkit@v1.2.13/structure/hashset/hashset.go (about) 1 // Copyright 2021 ByteDance 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 hashset 16 17 type Int64Set map[int64]struct{} 18 19 // NewInt64 returns an empty int64 set 20 func NewInt64() Int64Set { 21 return make(map[int64]struct{}) 22 } 23 24 // NewInt64WithSize returns an empty int64 set initialized with specific size 25 func NewInt64WithSize(size int) Int64Set { 26 return make(map[int64]struct{}, size) 27 } 28 29 // Add adds the specified element to this set 30 // Always returns true due to the build-in map doesn't indicate caller whether the given element already exists 31 // Reserves the return type for future extension 32 func (s Int64Set) Add(value int64) bool { 33 s[value] = struct{}{} 34 return true 35 } 36 37 // Contains returns true if this set contains the specified element 38 func (s Int64Set) Contains(value int64) bool { 39 if _, ok := s[value]; ok { 40 return true 41 } 42 return false 43 } 44 45 // Remove removes the specified element from this set 46 // Always returns true due to the build-in map doesn't indicate caller whether the given element already exists 47 // Reserves the return type for future extension 48 func (s Int64Set) Remove(value int64) bool { 49 delete(s, value) 50 return true 51 } 52 53 // Range calls f sequentially for each value present in the hashset. 54 // If f returns false, range stops the iteration. 55 func (s Int64Set) Range(f func(value int64) bool) { 56 for k := range s { 57 if !f(k) { 58 break 59 } 60 } 61 } 62 63 // Len returns the number of elements of this set 64 func (s Int64Set) Len() int { 65 return len(s) 66 }