实现有序map之go

Go Map介绍

Go 中 Map是一种无序的键值对的集合。Map最重要的一点是通过key来快速检索数据,key类似于索引,指向数据的值。Map是一种集合,所以我们可以像迭代数组和切片那样迭代它。不过,Map是无序的,我们无法决定它的返回顺序,这是因为Map是使用链式hash表来实现的。

c++中的实现

在C++ STL 中map 采用红黑树实现,可以实现有序的Map.

Go 中实现

实现原理

这个实现方法的主要的方法是用空间换取时间。通过list 和 map 两种数据结构,保存相同的一份数据。list 用来做顺序遍历,map 用来做查找,删除操作

实现代码

package main

import (
    "container/list"
    "fmt"
)

type Keyer interface {
    GetKey() string
}

type MapList struct {
    dataMap  map[string]*list.Element
    dataList *list.List
}

func NewMapList() *MapList {
    return &MapList{
        dataMap:  make(map[string]*list.Element),
        dataList: list.New(),
    }
}

func (mapList *MapList) Exists(data Keyer) bool {
    _, exists := mapList.dataMap[string(data.GetKey())]
    return exists
}

func (mapList *MapList) Push(data Keyer) bool {
    if mapList.Exists(data) {
        return false
    }
    elem := mapList.dataList.PushBack(data)
    mapList.dataMap[data.GetKey()] = elem
    return true
}

func (mapList *MapList) Remove(data Keyer) {
    if !mapList.Exists(data) {
        return
    }
    mapList.dataList.Remove(mapList.dataMap[data.GetKey()])
    delete(mapList.dataMap, data.GetKey())
}

func (mapList *MapList) Size() int {
    return mapList.dataList.Len()
}

func (mapList *MapList) Walk(cb func(data Keyer)) {
    for elem := mapList.dataList.Front(); elem != nil; elem = elem.Next() {
        cb(elem.Value.(Keyer))
    }
}

type Elements struct {
    value string
}

func (e Elements) GetKey() string {
    return e.value
}

func main() {
    fmt.Println("Starting test...")
    ml := NewMapList()
    var a, b, c Keyer
    a = &Elements{"Alice"}
    b = &Elements{"Bob"}
    c = &Elements{"Conrad"}
    ml.Push(a)
    ml.Push(b)
    ml.Push(c)
    cb := func(data Keyer) {
        fmt.Println(ml.dataMap[data.GetKey()].Value.(*Elements).value)
    }
    fmt.Println("Print elements in the order of pushing:")
    ml.Walk(cb)
    fmt.Printf("Size of MapList: %d \n", ml.Size())
    ml.Remove(b)
    fmt.Println("After removing b:")
    ml.Walk(cb)
    fmt.Printf("Size of MapList: %d \n", ml.Size())
}

优点

红黑树的插入、删除、查找的复杂度都是 O(logn), 而这个实现插入查找删除的复杂度都是 O(1), 可以说是一种非常好的数据结构。

缺点

使用了两个数据结构,空间占用稍微大了一点。但是和树的实现比,这个占用也不算非常大

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值