1
0
mirror of https://github.com/tursom/GoCollections.git synced 2025-04-27 05:50:27 +08:00

bloom support merge operation

This commit is contained in:
tursom 2023-04-25 10:19:20 +08:00
parent a62508af6f
commit accb2aa057
2 changed files with 80 additions and 1 deletions
util

View File

@ -1,6 +1,5 @@
package bloom
import "C"
import (
"encoding/binary"
"io"
@ -116,3 +115,15 @@ func Unmarshal(data []byte) *Bloom {
c: uint(c),
}
}
func (b *Bloom) Merge(bitMap []byte) bool {
if len(b.m) != len(bitMap) {
return false
}
for i := range b.m {
b.m[i] |= lang.UInt8(bitMap[i])
}
return true
}

68
util/mr/utils.go Normal file
View File

@ -0,0 +1,68 @@
package mr
import (
"github.com/tursom/GoCollections/collections"
"github.com/tursom/GoCollections/lang"
)
type MapEntry[K comparable, V any] struct {
key K
value V
}
func MapChannel[K comparable, V any](m map[K]V) lang.ReceiveChannel[*MapEntry[K, V]] {
ch := make(lang.RawChannel[*MapEntry[K, V]])
go func() {
for k, v := range m {
ch <- &MapEntry[K, V]{k, v}
}
close(ch)
}()
return ch
}
func ArrayChannel[E any](arr lang.Array[E]) lang.ReceiveChannel[E] {
ch := make(lang.RawChannel[E])
go func() {
for _, e := range arr {
ch <- e
}
close(ch)
}()
return ch
}
func SliceChannel[E any](arr []E) lang.ReceiveChannel[E] {
ch := make(lang.RawChannel[E])
go func() {
for _, e := range arr {
ch <- e
}
close(ch)
}()
return ch
}
func IteratorChannel[E any](iter collections.Iterator[E]) lang.ReceiveChannel[E] {
ch := make(lang.RawChannel[E])
go func() {
for iter.HasNext() {
next, e := iter.Next()
if e != nil {
panic(e)
}
ch <- next
}
close(ch)
}()
return ch
}