1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
// 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 hashing
import (
"github.com/apache/arrow-go/v18/arrow/bitutil"
"github.com/apache/arrow-go/v18/internal/utils"
)
{{range .In}}
type payload{{.Name}} struct {
val {{.name}}
memoIdx int32
}
type entry{{.Name}} struct {
h uint64
payload payload{{.Name}}
}
func (e entry{{.Name}}) Valid() bool { return e.h != sentinel }
// {{.Name}}HashTable is a hashtable specifically for {{.name}} that
// is utilized with the MemoTable to generalize interactions for easier
// implementation of dictionaries without losing performance.
type {{.Name}}HashTable struct {
cap uint64
capMask uint64
size uint64
entries []entry{{.Name}}
}
// New{{.Name}}HashTable returns a new hash table for {{.name}} values
// initialized with the passed in capacity or 32 whichever is larger.
func New{{.Name}}HashTable(cap uint64) *{{.Name}}HashTable {
initCap := uint64(bitutil.NextPowerOf2(int(max(cap, 32))))
ret := &{{.Name}}HashTable{cap: initCap, capMask: initCap - 1, size: 0}
ret.entries = make([]entry{{.Name}}, initCap)
return ret
}
// Reset drops all of the values in this hash table and re-initializes it
// with the specified initial capacity as if by calling New, but without having
// to reallocate the object.
func (h *{{.Name}}HashTable) Reset(cap uint64) {
h.cap = uint64(bitutil.NextPowerOf2(int(max(cap, 32))))
h.capMask = h.cap - 1
h.size = 0
h.entries = make([]entry{{.Name}}, h.cap)
}
// CopyValues is used for copying the values out of the hash table into the
// passed in slice, in the order that they were first inserted
func (h *{{.Name}}HashTable) CopyValues(out []{{.name}}) {
h.CopyValuesSubset(0, out)
}
// CopyValuesSubset copies a subset of the values in the hashtable out, starting
// with the value at start, in the order that they were inserted.
func (h *{{.Name}}HashTable) CopyValuesSubset(start int, out []{{.name}}) {
h.VisitEntries(func(e *entry{{.Name}}) {
idx := e.payload.memoIdx - int32(start)
if idx >= 0 {
out[idx] = e.payload.val
}
})
}
func (h *{{.Name}}HashTable) WriteOut(out []byte) {
h.WriteOutSubset(0, out)
}
func (h *{{.Name}}HashTable) WriteOutSubset(start int, out []byte) {
data := arrow.{{.Name}}Traits.CastFromBytes(out)
h.VisitEntries(func(e *entry{{.Name}}) {
idx := e.payload.memoIdx - int32(start)
if idx >= 0 {
{{if and (ne .Name "Int8") (ne .Name "Uint8") -}}
data[idx] = utils.ToLE{{.Name}}(e.payload.val)
{{else -}}
data[idx] = e.payload.val
{{end -}}
}
})
}
func (h *{{.Name}}HashTable) needUpsize() bool { return h.size*uint64(loadFactor) >= h.cap }
func ({{.Name}}HashTable) fixHash(v uint64) uint64 {
if v == sentinel {
return 42
}
return v
}
// Lookup retrieves the entry for a given hash value assuming it's payload value returns
// true when passed to the cmp func. Returns a pointer to the entry for the given hash value,
// and a boolean as to whether it was found. It is not safe to use the pointer if the bool is false.
func (h *{{.Name}}HashTable) Lookup(v uint64, cmp func({{.name}}) bool) (*entry{{.Name}}, bool) {
idx, ok := h.lookup(v, h.capMask, cmp)
return &h.entries[idx], ok
}
func (h *{{.Name}}HashTable) lookup(v uint64, szMask uint64, cmp func({{.name}}) bool) (uint64, bool) {
const perturbShift uint8 = 5
var (
idx uint64
perturb uint64
e *entry{{.Name}}
)
v = h.fixHash(v)
idx = v & szMask
perturb = (v >> uint64(perturbShift)) + 1
for {
e = &h.entries[idx]
if e.h == v && cmp(e.payload.val) {
return idx, true
}
if e.h == sentinel {
return idx, false
}
// perturbation logic inspired from CPython's set/dict object
// the goal is that all 64 bits of unmasked hash value eventually
// participate int he probing sequence, to minimize clustering
idx = (idx + perturb) & szMask
perturb = (perturb >> uint64(perturbShift)) + 1
}
}
func (h *{{.Name}}HashTable) upsize(newcap uint64) error {
newMask := newcap - 1
oldEntries := h.entries
h.entries = make([]entry{{.Name}}, newcap)
for _, e := range oldEntries {
if e.Valid() {
idx, _ := h.lookup(e.h, newMask, func({{.name}}) bool { return false })
h.entries[idx] = e
}
}
h.cap = newcap
h.capMask = newMask
return nil
}
// Insert updates the given entry with the provided hash value, payload value and memo index.
// The entry pointer must have been retrieved via lookup in order to actually insert properly.
func (h *{{.Name}}HashTable) Insert(e *entry{{.Name}}, v uint64, val {{.name}}, memoIdx int32) error {
e.h = h.fixHash(v)
e.payload.val = val
e.payload.memoIdx = memoIdx
h.size++
if h.needUpsize() {
h.upsize(h.cap * uint64(loadFactor) * 2)
}
return nil
}
// VisitEntries will call the passed in function on each *valid* entry in the hash table,
// a valid entry being one which has had a value inserted into it.
func (h *{{.Name}}HashTable) VisitEntries(visit func(*entry{{.Name}})) {
for _, e := range h.entries {
if e.Valid() {
visit(&e)
}
}
}
// {{.Name}}MemoTable is a wrapper over the appropriate hashtable to provide an interface
// conforming to the MemoTable interface defined in the encoding package for general interactions
// regarding dictionaries.
type {{.Name}}MemoTable struct {
tbl *{{.Name}}HashTable
nullIdx int32
}
// New{{.Name}}MemoTable returns a new memotable with num entries pre-allocated to reduce further
// allocations when inserting.
func New{{.Name}}MemoTable(num int64) *{{.Name}}MemoTable {
return &{{.Name}}MemoTable{tbl: New{{.Name}}HashTable(uint64(num)), nullIdx: KeyNotFound}
}
func ({{.Name}}MemoTable) TypeTraits() TypeTraits {
return arrow.{{.Name}}Traits
}
// Reset allows this table to be re-used by dumping all the data currently in the table.
func (s *{{.Name}}MemoTable) Reset() {
s.tbl.Reset(32)
s.nullIdx = KeyNotFound
}
// Size returns the current number of inserted elements into the table including if a null
// has been inserted.
func (s *{{.Name}}MemoTable) Size() int {
sz := int(s.tbl.size)
if _, ok := s.GetNull(); ok {
sz++
}
return sz
}
// GetNull returns the index of an inserted null or KeyNotFound along with a bool
// that will be true if found and false if not.
func (s *{{.Name}}MemoTable) GetNull() (int, bool) {
return int(s.nullIdx), s.nullIdx != KeyNotFound
}
// GetOrInsertNull will return the index of the null entry or insert a null entry
// if one currently doesn't exist. The found value will be true if there was already
// a null in the table, and false if it inserted one.
func (s *{{.Name}}MemoTable) GetOrInsertNull() (idx int, found bool) {
idx, found = s.GetNull()
if !found {
idx = s.Size()
s.nullIdx = int32(idx)
}
return
}
// CopyValues will copy the values from the memo table out into the passed in slice
// which must be of the appropriate type.
func (s *{{.Name}}MemoTable) CopyValues(out interface{}) {
s.CopyValuesSubset(0, out)
}
// CopyValuesSubset is like CopyValues but only copies a subset of values starting
// at the provided start index
func (s *{{.Name}}MemoTable) CopyValuesSubset(start int, out interface{}) {
s.tbl.CopyValuesSubset(start, out.([]{{.name}}))
}
func (s *{{.Name}}MemoTable) WriteOut(out []byte) {
s.tbl.CopyValues(arrow.{{.Name}}Traits.CastFromBytes(out))
}
func (s *{{.Name}}MemoTable) WriteOutSubset(start int, out []byte) {
s.tbl.CopyValuesSubset(start, arrow.{{.Name}}Traits.CastFromBytes(out))
}
func (s *{{.Name}}MemoTable) WriteOutLE(out []byte) {
s.tbl.WriteOut(out)
}
func (s *{{.Name}}MemoTable) WriteOutSubsetLE(start int, out []byte) {
s.tbl.WriteOutSubset(start, out)
}
// Get returns the index of the requested value in the hash table or KeyNotFound
// along with a boolean indicating if it was found or not.
func (s *{{.Name}}MemoTable) Get(val interface{}) (int, bool) {
{{if and (ne .Name "Float32") (ne .Name "Float64") }}
h := hashInt(uint64(val.({{.name}})), 0)
if e, ok := s.tbl.Lookup(h, func(v {{.name}}) bool { return val.({{.name}}) == v }); ok {
{{ else -}}
var cmp func({{.name}}) bool
{{if eq .Name "Float32"}}
if math.IsNaN(float64(val.(float32))) {
cmp = isNan32Cmp
// use consistent internal bit pattern for NaN regardless of the pattern
// that is passed to us. NaN is NaN is NaN
val = float32(math.NaN())
{{ else -}}
if math.IsNaN(val.(float64)) {
cmp = math.IsNaN
// use consistent internal bit pattern for NaN regardless of the pattern
// that is passed to us. NaN is NaN is NaN
val = math.NaN()
{{end -}}
} else {
cmp = func(v {{.name}}) bool { return val.({{.name}}) == v }
}
h := hash{{.Name}}(val.({{.name}}), 0)
if e, ok := s.tbl.Lookup(h, cmp); ok {
{{ end -}}
return int(e.payload.memoIdx), ok
}
return KeyNotFound, false
}
// GetOrInsert will return the index of the specified value in the table, or insert the
// value into the table and return the new index. found indicates whether or not it already
// existed in the table (true) or was inserted by this call (false).
func (s *{{.Name}}MemoTable) GetOrInsert(val interface{}) (idx int, found bool, err error) {
{{if and (ne .Name "Float32") (ne .Name "Float64") }}
h := hashInt(uint64(val.({{.name}})), 0)
e, ok := s.tbl.Lookup(h, func(v {{.name}}) bool {
return val.({{.name}}) == v
})
{{ else }}
var cmp func({{.name}}) bool
{{if eq .Name "Float32"}}
if math.IsNaN(float64(val.(float32))) {
cmp = isNan32Cmp
// use consistent internal bit pattern for NaN regardless of the pattern
// that is passed to us. NaN is NaN is NaN
val = float32(math.NaN())
{{ else -}}
if math.IsNaN(val.(float64)) {
cmp = math.IsNaN
// use consistent internal bit pattern for NaN regardless of the pattern
// that is passed to us. NaN is NaN is NaN
val = math.NaN()
{{end -}}
} else {
cmp = func(v {{.name}}) bool { return val.({{.name}}) == v }
}
h := hash{{.Name}}(val.({{.name}}), 0)
e, ok := s.tbl.Lookup(h, cmp)
{{ end }}
if ok {
idx = int(e.payload.memoIdx)
found = true
} else {
idx = s.Size()
s.tbl.Insert(e, h, val.({{.name}}), int32(idx))
}
return
}
// GetOrInsertBytes is unimplemented
func (s *{{.Name}}MemoTable) GetOrInsertBytes(val []byte) (idx int, found bool, err error) {
panic("unimplemented")
}
{{end}}
|