freakyzoidberg commented on code in PR #51: URL: https://github.com/apache/datasketches-go/pull/51#discussion_r2444408373
########## theta/hashtable.go: ########## @@ -0,0 +1,292 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package theta + +import ( + "errors" + "fmt" + "math" + + "github.com/apache/datasketches-go/internal" +) + +const ( + resizeThreshold = 0.5 + rebuildThreshold = 15.0 / 16.0 +) + +const ( + strideHashBits = 7 + strideMask = (1 << strideHashBits) - 1 +) + +var ( + ErrKeyNotFound = errors.New("key not found") + ErrKeyNotFoundAndNoEmptySlots = errors.New("key not found and no empty slots") +) + +type Hashtable struct { + entries []uint64 + theta uint64 + seed uint64 + numEntries uint32 + p float32 + lgCurSize uint8 + lgNomSize uint8 + rf ResizeFactor + isEmpty bool +} + +// NewHashtable creates a new hash table +func NewHashtable(lgCurSize, lgNomSize uint8, rf ResizeFactor, p float32, theta, seed uint64, isEmpty bool) *Hashtable { + sketch := &Hashtable{ + isEmpty: isEmpty, + lgCurSize: lgCurSize, + lgNomSize: lgNomSize, + rf: rf, + p: p, + numEntries: 0, + theta: theta, + seed: seed, + entries: nil, + } + + if lgCurSize > 0 { + size := 1 << lgCurSize + sketch.entries = make([]uint64, size) + } + + return sketch +} + +// Copy creates a deep copy of the sketch +func (t *Hashtable) Copy() *Hashtable { + c := &Hashtable{ + isEmpty: t.isEmpty, + lgCurSize: t.lgCurSize, + lgNomSize: t.lgNomSize, + rf: t.rf, + p: t.p, + numEntries: t.numEntries, + theta: t.theta, + seed: t.seed, + entries: nil, + } + + if t.entries != nil { + size := 1 << t.lgCurSize + c.entries = make([]uint64, size) + + copy(c.entries, t.entries) + } + + return c +} + +// HashStringAndScreen computes the hash of string and checks if it passes theta threshold +func (t *Hashtable) HashStringAndScreen(data string) (uint64, error) { + t.isEmpty = false + h1, _ := internal.HashCharSliceMurmur3([]byte(data), 0, len(data), t.seed) + hash := h1 >> 1 + if hash >= t.theta { + // hash == 0 is reserved to mark empty slots in the table + return 0, fmt.Errorf("hash %d is greater than or equal to theta %d", hash, t.theta) + } + return hash, nil +} + +// HashInt32AndScreen computes the hash of int32 and checks if it passes theta threshold +func (t *Hashtable) HashInt32AndScreen(data int32) (uint64, error) { + t.isEmpty = false + h1, _ := internal.HashInt32SliceMurmur3([]int32{data}, 0, 1, t.seed) + hash := h1 >> 1 + if hash >= t.theta { Review Comment: I wonder that now that we return explicit errors instead of conflating the 0 value as an invalid case (the way its done in c++), if we should also check for 0 value as an error case ? in c++ version all return equal to 0 would be ignored - but here we should ignore error and nil:error if hash is 0. https://github.com/apache/datasketches-cpp/blob/dddc4a668cdc47ad8a221cf7d4cb5054e53a40ee/tuple/include/tuple_sketch_impl.hpp#L215 It is fine to handle that call side - maybe more error prone -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
