zoukankan      html  css  js  c++  java
  • golang ctrie demo

    下载ctrie:

    go get -t github.com/Workiva/go-datastructures/trie/ctrie

    测试demo1:

    package main
    
    import (
        "fmt"
        "github.com/Workiva/go-datastructures/trie/ctrie"
        "strconv"
    )
    
    type dataStruct struct {
        id int64
        x  int64
        y  int64
        a  string
        b  string
    }
    
    func main() {
        rect1 := &dataStruct{1, 1, 2, "sss", "rrr"}
        rect2 := &dataStruct{2, 2, 4, "aaa", "xxx"}
        rect3 := &dataStruct{3, 3, 5, "bbb", "yyy"}
    
        ctrie := ctrie.New(nil)
        key1 := []byte(strconv.Itoa(int(rect1.id)))
        ctrie.Insert(key1, rect1)
    
        key2 := []byte(strconv.Itoa(int(rect2.id)))
        ctrie.Insert(key2, rect2)
    
        key3 := []byte(strconv.Itoa(int(rect3.id)))
        ctrie.Insert(key3, rect3)
    
        key := []byte(strconv.Itoa(int(rect3.id)))
    
        val, exists := ctrie.Lookup(key)
        if exists {
            data := val.(*dataStruct)
            fmt.Println(data.a)
            ctrie.Remove(key)
        }
    
        val, exists = ctrie.Lookup(key)
        if !exists {
            fmt.Println("Not find")
        }
    
        return
    }

    运行结果:

    [root@wangjq ctrie]# go run main.go 
    bbb
    Not find

    测试demo2:

    package main
    
    import (
        "fmt"
        "github.com/Workiva/go-datastructures/trie/ctrie"
        "bytes"
    )
    
    type dataStruct struct {
        id int64
        x  int64
        y  int64
        a  string
        b  string
    }
    
    func main() {
        rect := &dataStruct{1, 1, 2, "sss", "rrr"}
    
        buf := &bytes.Buffer{}
        buf.WriteString("123456")
        key := buf.Bytes()
    
        ctrie := ctrie.New(nil)
        ctrie.Insert(key, rect)
    
        buf_tmp := &bytes.Buffer{}
        buf_tmp.WriteString("123456")
        key_tmp := buf_tmp.Bytes()
    
        val, exists := ctrie.Lookup(key_tmp)
        if exists {
            data := val.(*dataStruct)
            fmt.Println(data.a)
            ctrie.Remove(key_tmp)
        }
    
        val, exists = ctrie.Lookup(key_tmp)
        if !exists {
            fmt.Println("Not find")
        }
    
        return
    }

    运行结果:

    [root@ctrie]# go run demo.go 
    sss
    Not find
  • 相关阅读:
    求Computational problems associated with Racah algebra
    病理情形
    扫描算法求最大子序列的一次简单非严格证明
    分治法求最大子序列,关于复杂度的一次弱推导
    VFP_等待 rar 命令结束
    Windows_解决win7开机画面变成vista画面的方法
    C#_WinForm的等待窗口
    C#_控件的缩写大全
    SQL Server 2008_基本安装说明
    C#_获取 SQL服务器列表
  • 原文地址:https://www.cnblogs.com/wangjq19920210/p/11757202.html
Copyright © 2011-2022 走看看