首页 > 其他分享 >hashmap oop in golang

hashmap oop in golang

时间:2023-05-07 12:23:06浏览次数:46  
标签:node insert hash hashmap nil fmt golang oop

package main

import (
    "fmt"
)

const HASH_BUCKET_SIZE = 3 //1023

type hash_node struct {
    key  interface{}
    val  interface{}
    next *hash_node
}

type HASH_BUCKET [HASH_BUCKET_SIZE]*hash_node

func hash(key interface{}) int {
    hash := 5381
    switch x := key.(type) {
    case string:
        // Time 33 hash
        for _, k := range x {
            hash += (hash << 5) + int(k)
        }

        return (hash % HASH_BUCKET_SIZE)

    case int:

        hash += (hash << 5) + x

        return (hash % HASH_BUCKET_SIZE)
    default:

        _ = x
        return 0
    }
}

func hash_init() HASH_BUCKET {
    //hash bucket to save node address

    hash_bucket_ptr := new(HASH_BUCKET)

    return *hash_bucket_ptr
}

func key_cmp(a interface{}, b interface{}) bool {
    typ := fmt.Sprintf("%T", a)

    switch x := b.(type) {
    case string:
        if typ == "string" {
            if b == a {
                return true
            }
        }
    case int:
        if typ == "int" {
            if b == a {
                return true
            }
        }
    default:
        _ = x
        return false
    }

    return false
}

func (hashmap *HASH_BUCKET) get(key interface{}) interface{} {
    hash := hash(key)

    for node := hashmap[hash]; node != nil; node = node.next {

        if key_cmp(node.key, key) {
            return node.val
        }
    }

    return nil
}

//insert new node addr in the head of linklist
func (hashmap *HASH_BUCKET) insert(node *hash_node) bool {
    val := hashmap.get(node.key)

    if val != nil {
        return false
    }

    hash := hash(node.key)
    head := hashmap[hash]

    node.next = head
    hashmap[hash] = node

    return true
}

func (hashmap *HASH_BUCKET) remove(item *hash_node) bool {
    hash := hash(item.key)

    cur := hashmap[hash]
    if cur == nil {
        return false
    }

    if key_cmp(cur.key, item.key) {
        hashmap[hash] = cur.next
        return true
    }

    for node := cur.next; node != nil; node = node.next {
        if key_cmp(node.key, item.key) {

            cur.next = node.next

            return true
        }

        cur = node
    }

    return false
}

func (hashmap *HASH_BUCKET) iterate() {
    for i := 0; i < HASH_BUCKET_SIZE; i++ {
        fmt.Printf("i %d ---------->  \n", i)
        for node := hashmap[i]; node != nil; node = node.next {
            fmt.Println("   ", node.key, node.val)
        }
    }
}

func main() {
    hashmap := hash_init()

    for i := 0; i < 5; i++ {
        hashmap.insert(&hash_node{i, i + 1, nil})
        hashmap.insert(&hash_node{fmt.Sprintf("key%d", i), fmt.Sprintf("val%d", i+1), nil})
    }

    hashmap.iterate()

    fmt.Println("---------------after del------------")

    rv := hashmap.insert(&hash_node{"key4", "val1", nil})
    if !rv {
        fmt.Println("insert key4 failed")
    }

    rv = hashmap.insert(&hash_node{"key5", "val1", nil})
    if rv {
        fmt.Println("insert key5 success")
    }

    hashmap.remove(&hash_node{"key4", nil, nil})

    hashmap.remove(&hash_node{1, nil, nil})

    hashmap.iterate()

    fmt.Println(hashmap.get(4))

    fmt.Println(hashmap.get("key0"))
}

output

i 0 ---------->  
    key3 val4
    3 4
    key0 val1
    0 1
i 1 ---------->  
    key4 val5
    4 5
    key1 val2
    1 2
i 2 ---------->  
    key2 val3
    2 3
---------------after del------------
insert key4 failed
insert key5 success
i 0 ---------->  
    key3 val4
    3 4
    key0 val1
    0 1
i 1 ---------->  
    4 5
    key1 val2
i 2 ---------->  
    key5 val1
    key2 val3
    2 3
5
val1

  

标签:node,insert,hash,hashmap,nil,fmt,golang,oop
From: https://www.cnblogs.com/wallywl/p/17379125.html

相关文章

  • golang的vscode环境搭建
    因为墙的原因,vscode安装go插件会有报错,需要切换成国内的镜像 GOPROXYhttps://proxy.golang.com.cn,direct ......
  • Golang基础--加锁与原子操作
    前言在实际项目开发中,有时会面临同一时刻将多个goroutine作用于同一个对象的情况,此时,他们之间会发生冲突,这种情况称为数据竞态问题。例如:packagemainimport("fmt""time")varcountintfuncmain(){goCountPlus(10000)goCountPlus(10000)......
  • golang hashmap
    packagemainimport("fmt")constHASH_BUCKET_SIZE=3//1023typehash_nodestruct{keyinterface{}valinterface{}next*hash_node}//hashbuckettosavenodeaddressvarhash_bucket[HASH_BUCKET_SIZE]*hash_nodefunc......
  • HashMap设置初始容量一直都用错了?
    1背景今天在代码审查的时候,发现一位离职的同事留下了这样一串代码:Map<String,String>map=newHashMap<>((int)(list.size()/0.75F+1));第一反应是:又在炫技,又在搞这些花里胡哨的东西。但是看到0.75的我却陷入了沉思,稍微深入了解过Map的应该都知道,Map中有个属性,叫做负载因......
  • golang模拟键盘输入字符串
    介绍仅供学习使用哈,不要用来开gua。代码仓库:https://github.com/GuoFlight/gkeybd(本人仓库,欢迎留言)注意事项只支持英文使用前请切换到英文输入法。因为本程序只支持英文(模拟的是按键,而不是传递字符串)。Mac中使用可能会报错需要用vendor,并将vendor/github.com/micmona......
  • golang基础--Goroutine与Channel
    什么是goroutine?goroutine是go特有的并发体,是一种轻量级的线程,由go关键字启动。goroutine是Go语言提供的一种用户态线程,有时我们也称之为协程。所谓的协程,某种程度上也可以叫做轻量线程,它不由os,而由应用程序创建和管理,因此使用开销较低(一般为4K)。我们可以创建很多的gorou......
  • 基于Hadoop3.1.3安装Hive3.1.2
    Hive是什么?\tHive是一个基于Hadoop的数据仓库工具,它提供了类似SQL的查询语言HQL(HiveQueryLanguage),使得开发人员可以使用类SQL语言来查询和处理存储在大规模分布式文件系统(如HDFS)中的数据。Hive有哪些功能?Hive的主要功能包括数据存储、查询和分析等。通过将SQL......
  • 简单说说HashMap和LinkedHashMap的区别
    HashMap和LinkedHashMap的区别我们知道HashMap的变量顺序是不可预测的,这意味着便利的输出顺序并不一定和HashMap的插入顺序是一致的。这个特性通常会对我们的工作造成一定的困扰。为了实现这个功能,我们可以使用LinkedHashMap。LinkedHashMap详解先看下LinkedHashMap的定义:pu......
  • Hashtable、synchronizedMap、ConcurrentHashMap 深度比较
    关键字:Hashtable、synchronizedMap、ConcurrentHashMap深度比较util.concurrent包除了包含许多其他有用的并发构造块之外,还包含了一些主要集合类型List和Map的高性能的、线程安全的实现。BrianGoetz向您展示了用ConcurrentHashMap替换Hashtable或synchro......
  • hadoop 3.3.5伪分布式集群部署
    hadoop包下载https://archive.apache.org/dist/hadoop/common/安装好jdk并配置环境变量下载hadoop压缩包并放至/data/hadoop目录解压tar-zxvfhadoop-3.3.5.tar.gz1配置1.1在Hadoop安装目录下进入到etc/hadoop目录,修改Hadoop相关配置文件。<property><name>f......