Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
func New ¶
func New[K comparable, V any](size int) *Map[K, V]
New creates a new Swiss map with the specified initial size. It preallocates the necessary number of groups and sets up the hash function. The control bytes of each group are initialized to an empty state (kEmpty). The hash function and seed are also initialized. The capacity is calculated based on the number of groups and the load factor.
func (*Map[K, V]) Cap ¶
Cap returns the map’s capacity, which is based on the number of groups and the load factor.
func (*Map[K, V]) Clear ¶
func (m *Map[K, V]) Clear()
Clear removes all key-value pairs from the map, resetting all groups to an empty state. The capacity remains unchanged, but the length and tombstones are reset to zero.
func (*Map[K, V]) Delete ¶
func (m *Map[K, V]) Delete(key K)
Delete removes a key-value pair from the map. If the key is found, the slot is cleared, and the control byte is marked as either empty or deleted (tombstone). This optimization helps avoid wasting slots if there are empty slots available in the group. Tombstones are tracked and used to trigger rehashing when necessary.
func (*Map[K, V]) Get ¶
Get retrieves the value associated with a given key. It calculates the hash of the key and uses h1 to find the corresponding group. The function checks the control bytes of the group for a matching h2. If a match is found, it compares the key and returns the value. If the key is not found or an empty slot is encountered, the function returns false.
func (*Map[K, V]) Len ¶
Len returns the number of key-value pairs currently stored in the map, excluding deleted (tombstone) entries.
func (*Map[K, V]) Put ¶
func (m *Map[K, V]) Put(key K, value V)
Put inserts or updates a key-value pair in the map. It calculates the hash of the key and uses h1 to locate the appropriate group. The function probes the group for a matching key or an empty/deleted slot. If the key is found, its value is updated. If an empty or deleted slot is found, the key-value pair is inserted. Rehashing occurs if the map's load exceeds the capacity.