github.com/cilium/statedb@v0.3.2/index/keyset.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package index 5 6 import ( 7 "bytes" 8 ) 9 10 // Key is a byte slice describing a key used in an index by statedb. 11 type Key []byte 12 13 func (k Key) Equal(k2 Key) bool { 14 return bytes.Equal(k, k2) 15 } 16 17 type KeySet struct { 18 head Key 19 tail []Key 20 } 21 22 func (ks KeySet) First() Key { 23 return ks.head 24 } 25 26 func (ks KeySet) Foreach(fn func(Key)) { 27 if ks.head == nil { 28 return 29 } 30 fn(ks.head) 31 for _, k := range ks.tail { 32 fn(k) 33 } 34 } 35 36 func (ks KeySet) Exists(k Key) bool { 37 if ks.head.Equal(k) { 38 return true 39 } 40 for _, k2 := range ks.tail { 41 if k2.Equal(k) { 42 return true 43 } 44 } 45 return false 46 } 47 48 func NewKeySet(keys ...Key) KeySet { 49 if len(keys) == 0 { 50 return KeySet{} 51 } 52 return KeySet{keys[0], keys[1:]} 53 }