|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +// Copyright 2024 The Go Authors. All rights reserved. |
| 6 | +// Use of this source code is governed by a BSD-style |
| 7 | +// license that can be found in the LICENSE file. |
| 8 | + |
| 9 | +//go:build go1.22 && !go1.24 && !nomaptypehash |
| 10 | + |
| 11 | +//nolint:revive,govet,stylecheck |
| 12 | +package concurrent |
| 13 | + |
| 14 | +import ( |
| 15 | + "math/rand/v2" |
| 16 | + "unsafe" |
| 17 | +) |
| 18 | + |
| 19 | +// NewHashTrieMap creates a new HashTrieMap for the provided key and value. |
| 20 | +func NewHashTrieMap[K, V comparable]() *HashTrieMap[K, V] { |
| 21 | + var m map[K]V |
| 22 | + |
| 23 | + mapType := efaceMapOf(m) |
| 24 | + ht := &HashTrieMap[K, V]{ |
| 25 | + root: newIndirectNode[K, V](nil), |
| 26 | + keyHash: mapType._type.Hasher, |
| 27 | + seed: uintptr(rand.Uint64()), |
| 28 | + } |
| 29 | + return ht |
| 30 | +} |
| 31 | + |
| 32 | +// _MapType is runtime.maptype from runtime/type.go. |
| 33 | +type _MapType struct { |
| 34 | + _Type |
| 35 | + Key *_Type |
| 36 | + Elem *_Type |
| 37 | + Bucket *_Type // internal type representing a hash bucket |
| 38 | + // function for hashing keys (ptr to key, seed) -> hash |
| 39 | + Hasher func(unsafe.Pointer, uintptr) uintptr |
| 40 | + KeySize uint8 // size of key slot |
| 41 | + ValueSize uint8 // size of elem slot |
| 42 | + BucketSize uint16 // size of bucket |
| 43 | + Flags uint32 |
| 44 | +} |
| 45 | + |
| 46 | +// _Type is runtime._type from runtime/type.go. |
| 47 | +type _Type struct { |
| 48 | + Size_ uintptr |
| 49 | + PtrBytes uintptr // number of (prefix) bytes in the type that can contain pointers |
| 50 | + Hash uint32 // hash of type; avoids computation in hash tables |
| 51 | + TFlag uint8 // extra type information flags |
| 52 | + Align_ uint8 // alignment of variable with this type |
| 53 | + FieldAlign_ uint8 // alignment of struct field with this type |
| 54 | + Kind_ uint8 // enumeration for C |
| 55 | + // function for comparing objects of this type |
| 56 | + // (ptr to object A, ptr to object B) -> ==? |
| 57 | + Equal func(unsafe.Pointer, unsafe.Pointer) bool |
| 58 | + // GCData stores the GC type data for the garbage collector. |
| 59 | + // If the KindGCProg bit is set in kind, GCData is a GC program. |
| 60 | + // Otherwise it is a ptrmask bitmap. See mbitmap.go for details. |
| 61 | + GCData *byte |
| 62 | + Str int32 // string form |
| 63 | + PtrToThis int32 // type for pointer to this type, may be zero |
| 64 | +} |
| 65 | + |
| 66 | +// efaceMap is runtime.eface from runtime/runtime2.go. |
| 67 | +type efaceMap struct { |
| 68 | + _type *_MapType |
| 69 | + data unsafe.Pointer |
| 70 | +} |
| 71 | + |
| 72 | +func efaceMapOf(ep any) *efaceMap { |
| 73 | + return (*efaceMap)(unsafe.Pointer(&ep)) |
| 74 | +} |
0 commit comments