该笔记参考《Go并发编程实战》

  • 首先实现一个自定义的HashSet

利用interface{}作为键,布尔型作为值。

package main

import (
"bytes"
"fmt"
) type HashSet struct {
m map[interface{}]bool
} func NewHashSet() {
return &HashSet{m: make(map[interface{}]bool)}
} func (set *HashSet) Add(e interface{}) bool {
if !set.m[e] {
set.m[e] = true
return true
}
return false
} func (set *HashSet) Remove(e interface{}) {
delete(set.m, e)
} func (set *HashSet) Clear() {
set.m = make(map[interface{}]bool)
} func (set *HashSet) Contains(e interface{}) bool {
return set.m[e]
} func (set *HashSet) Len() int {
return len(set.m)
} func (set *HashSet) Same(other Set) bool {
if other == nil {
return false
}
if set.Len() != other.Len() {
return false
}
for k := range set.m {
if !other.Contains(k) {
return false
}
}
return true
} func (set *HashSet) Elements() []interface{} {
initLen := len(set.m)
actualLen := 0
snapshot := make([]interface{}, initLen) for k := range set.m {
if actualLen < initLen {
snapshot[actualLen] = k
} else {
snapshot = append(snapshot, k)
}
actualLen++
}
if actualLen < initLen {
snapshot = snapshot[:actualLen]
}
return snapshot
} func (set *HashSet) String() string {
var buf bytes.Buffer
buf.WriteString("HastSet{")
first := true
for k := range set.m {
if first {
first = false
} else {
buf.WriteString(" ")
}
buf.WriteString(fmt.Sprintf("%v", k))
}
buf.WriteString("}")
}
  • 实现Set的基本特性
package main

type Set interface {
Add(e interface{}) bool
Remove(e interface{})
Clear()
Same(outher Set) bool
Elements() []interface{}
String() string
Len() int
Contains(e interface{}) bool
} func IsSuperSet(one Set, other Set) bool {
if one == nil || other == nil {
return false
}
oneLen := one.Len()
otherLen := other.Len()
if oneLen > 0 && otherLen == 0 {
return true
}
if oneLen == 0 && oneLen == otherLen {
return false
} for v := range other.Elements() {
if !one.Contains(v) {
return false
}
}
return true
} func Union(one Set, other Set) Set {
if one == nil || other == nil {
return false
}
unionedSet := NewSimpleSet()
for _, v := range one.Elements() {
unionedSet.Add(v)
}
if other.Len() == 0 {
return unionedSet
}
for v := range one.Elements() {
unionedSet.Add(v)
}
return unionedSet
} func Intersect(one Set, other Set) Set {
if one == nil || other == nil {
return false
}
intersectedSet := NewSimpleSet()
if other.Len() == 0 {
return intersectedSet
}
if one.Len() < other.Len() {
for _, v := range one.Elements() {
if other.Contains(v) {
intersectedSet.Add(v)
}
}
} else { for _, v := range other.Elements() {
if one.Contains(v) {
intersectedSet.Add(v)
}
}
}
return intersectedSet
} func NewSimpleSet() Set {
return NewHashSet()
}
func IsSet(value interface{}) bool {
if _, ok := value.(Set); ok {
return true
}
return false
}

至此,一个简单的Set集合就完成了。

Go-利用Map实现类似Python的Set数据结构的更多相关文章

  1. Go-常识补充-切片-map(类似字典)-字符串-指针-结构体

    目录 Go 常识补充 Go 命名 打印变量类型科普 _ 关键字 命名规范相关 包目录规范 切片 多维切片 切片初始化的方法 多维切片初始化 切片删除元素(会略微影响效率 ,少用) copy 函数 打散 ...

  2. 【Python】无须numpy,利用map函数与zip(*)函数对数组转置(转)

    http://blog.csdn.net/yongh701/article/details/50283689 在Python的numpy中,对类似array=[[1,2,3],[4,5,6],[7,8 ...

  3. 1034 Head of a Gang (30分)(dfs 利用map)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  4. Java-map-第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。 附:世界杯冠军以及对应的夺冠年份,请参考本章附录。 附录

    第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...

  5. map/reduce of python

    [map/reduce of python] 参考: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac92 ...

  6. zk框架中利用map类型传值来创建window,并且传值

    @Command @NotifyChange("accList") public void clear(@BindingParam("id") String a ...

  7. [前端引用] 利用ajax实现类似php include require 等命令的功能

    利用ajax实现类似php中的include.require等命令的功能 最新文件下载: https://github.com/myfancy/ajaxInclude 建议去这里阅读readme-2. ...

  8. C#利用API制作类似QQ一样的右下角弹出窗体

    C#利用API制作类似QQ一样的右下角弹出窗体 (2009-03-21 15:02:49) 转载▼ 标签: 杂谈 分类: .NET using System;using System.Collecti ...

  9. [DevExpress]利用LookUpEdit实现类似自动提示效果

    原文:[DevExpress]利用LookUpEdit实现类似自动提示效果 关键代码: public static void BindWithAutoCompletion(this LookUpEdi ...

随机推荐

  1. input type="file"去掉取消默认原来选择的文件

    很多时候我们上传文件点击取消后或我们制定了内容格式上传不符合,再次点击input="file"按钮时,选择的文件还是原来的文件,却又上传不.当时想在旁边多添加个按钮清除file里面 ...

  2. jprofiler8使用小贴士

    说明:本文的小贴士是针对jprofiler8的,其他版本上可能有不适用的地方 贴士一:使用jpenable监控,无需增加jvm参数和重启 贴士一:使用jpenable监控,无需增加jvm参数和重启 j ...

  3. C# 数据库连接测试以及备份

    现在我们要做一个如图5.1的数据库连接配置,从界面上看有三个功能需要实现:从配置文件中读取数据库连接的相关属性.备份数据库.测试连接. 现在我们就一个一个开始讲解. 图5.1 1.从配置文件中读取数据 ...

  4. mybatis 入门进阶之 mapper

    由于上节 <mybatis 入门优化>中的dao实现类耦合了user.xml中的statment的id,例如:src.main.resource.userMapper.findUserBy ...

  5. 某大神C#框架后台发送信息的查找及破解

    最近在博客园瞎逛的时候,发现了某个大神发布的一个c#框架,一看框架,叫牛逼框架,嗯,装B效果太好了,界面很炫,虽然有很多的组件还是不怎么完善,但是,已经可以初步运用于项目了. 先来看看界面:   在进 ...

  6. Python 函数简介 之二

    1.当函数有多个返回值时, 其多个返回值将以元组的形式出现 def test1(): print("in the test1") return 'end' def test2(): ...

  7. action = "#" 是什么意思 在HTML语言中

    action = "#" 是form标签的属性,代表提交数据到本页,如:// 提交数据到a.aspx页面<form action="a.aspx"> ...

  8. top batch output

    echo 3 > sudo tee /proc/sys/vm/drop_caches top -d30 -bn20 > a

  9. jmeter命令行运行-分布式测试

    上一篇文章我们说到了jmeter命令行运行但是是单节点下的, jmeter底层用java开发,耗内存.cpu,如果项目要求大并发去压测服务端的话,jmeter单节点难以完成大并发的请求,这时就需要对j ...

  10. Unity2D开发小细节

    当某个触碰物体挂在父组件时,如果当前子组件不加rigidbody,会默认使用父组件的rigidbody