dict

package module
v0.0.0-...-02d34f5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 7 Imported by: 0

README

Go Redis Dictionary Implementation

This is an implementation of the dict data structure in Redis using golang. The implementation uses a hash table to store key-value pairs.

Features

  • Efficient key-value lookup and storage
  • Support for key and value types of any kind
  • Automatic resizing of hash table for optimal performance

Usage

The dictionary can be used like any other map in Go:

import "dict"

func main() {
    d := dict.New()
    d.Store("key", "value")
    val, ok := d.Load("key")
    if ok {
        fmt.Println(val)
    }
}

Implementation Details

The dictionary is implemented using a hash table, which provides efficient key-value lookups and storage. The hash table is made up of a series of buckets, each containing a linked list of key-value pairs.

When a key-value pair is added to the dictionary, the key is hashed and the resulting hash value is used to determine the appropriate bucket in the hash table. If the bucket is empty, a new linked list is created and the key-value pair is added to the list. If the bucket already contains a linked list, the key is searched for in the list. If the key is found, the value is updated. If the key is not found, a new node is added to the linked list containing the key-value pair.

To ensure optimal performance, the hash table is automatically resized when it becomes too full. The resizing process involves creating a new, larger hash table and copying all key-value pairs from the old table to the new one.

License

This code is licensed under the MIT License.

Go对Redis中Dictionary数据结构的实现

这是一个使用Go实现的Dictionary数据结构,用于在Redis中存储键值对。该实现使用哈希表来存储键值对。

特点

  • 高效的键值对查找和存储
  • 支持任何类型的键和值
  • 自动调整哈希表大小以达到最佳性能

使用方法

该Dictionary可以像Go中的任何其他map一样使用:

import "dict"

func main() {
    d := dict.New()
    d.Store("key", "value")
    val, ok := d.Load("key")
    if ok {
        fmt.Println(val)
    }
}

实现细节

该Dictionary使用哈希表来实现,从而提供高效的键值对查找和存储。哈希表由一系列桶组成,每个桶都包含一个键值对的链表。

当将键值对添加到Dictionary中时,将对键进行哈希处理,并使用生成的哈希值确定哈希表中的适当桶。如果桶为空,则创建一个新的链表,并将键值对添加到链表中。如果桶中已经包含一个链表,则在该链表中查找键。如果找到了键,则更新其值。如果没有找到键,则添加一个包含键值对的新节点到链表中。

为确保最佳性能,哈希表在变得太满时会自动调整大小。调整大小的过程涉及创建一个新的、更大的哈希表,并将所有键值对从旧表复制到新表。

许可证

该代码使用MIT许可证授权。

Documentation

Overview

* @Author: zengzh * @Date: 2023-04-23 19:48:01 * @Last Modified by: zengzh * @Last Modified time: 2023-04-23 19:56:55

* @Author: zengzh * @Date: 2023-04-23 19:48:08 * @Last Modified by: zengzh * @Last Modified time: 2023-04-23 19:48:08

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SipHash

func SipHash(v interface{}) uint64

Types

type Dict

type Dict struct {
	// contains filtered or unexported fields
}

func New

func New() *Dict

new init a dict

func (*Dict) Cap

func (d *Dict) Cap() uint64

Cap return the capacity of dict

func (*Dict) Delete

func (d *Dict) Delete(key interface{})

Delete delete a key-value pair by key

func (*Dict) Len

func (d *Dict) Len() uint64

Range range all key-value pairs

func (*Dict) Load

func (d *Dict) Load(key interface{}) (value interface{}, ok bool)

Load get a value by key

func (*Dict) Range

func (d *Dict) Range(fn func(key, value interface{}) bool)

Range range all key-value pairs

func (*Dict) RangeSafely

func (d *Dict) RangeSafely(fn func(key, value interface{}) bool)

RangeSafely range all key-value pairs safely

func (*Dict) RehashForAWhile

func (d *Dict) RehashForAWhile(duration time.Duration) int64

RehashForAWhile rehash for a while

func (*Dict) Resize

func (d *Dict) Resize() error

Resize resize the dict

func (*Dict) Store

func (d *Dict) Store(key, value interface{})

Store add a key-value pair to dict

func (*Dict) String

func (d *Dict) String() string

String return the string of dict

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址