proost commented on code in PR #51:
URL: https://github.com/apache/datasketches-go/pull/51#discussion_r2445188776


##########
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:
   
[0395c56](https://github.com/apache/datasketches-go/pull/51/commits/0395c562e5ca25b37c8741430526d3545fe5ddb2)
   
   You are right. explicit returning error when hash value is zero is more clear



-- 
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]

Reply via email to