Go-利用Map实现类似Python的Set数据结构
该笔记参考《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数据结构的更多相关文章
- Go-常识补充-切片-map(类似字典)-字符串-指针-结构体
目录 Go 常识补充 Go 命名 打印变量类型科普 _ 关键字 命名规范相关 包目录规范 切片 多维切片 切片初始化的方法 多维切片初始化 切片删除元素(会略微影响效率 ,少用) copy 函数 打散 ...
- 【Python】无须numpy,利用map函数与zip(*)函数对数组转置(转)
http://blog.csdn.net/yongh701/article/details/50283689 在Python的numpy中,对类似array=[[1,2,3],[4,5,6],[7,8 ...
- 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 ...
- Java-map-第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。 附:世界杯冠军以及对应的夺冠年份,请参考本章附录。 附录
第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...
- map/reduce of python
[map/reduce of python] 参考: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac92 ...
- zk框架中利用map类型传值来创建window,并且传值
@Command @NotifyChange("accList") public void clear(@BindingParam("id") String a ...
- [前端引用] 利用ajax实现类似php include require 等命令的功能
利用ajax实现类似php中的include.require等命令的功能 最新文件下载: https://github.com/myfancy/ajaxInclude 建议去这里阅读readme-2. ...
- C#利用API制作类似QQ一样的右下角弹出窗体
C#利用API制作类似QQ一样的右下角弹出窗体 (2009-03-21 15:02:49) 转载▼ 标签: 杂谈 分类: .NET using System;using System.Collecti ...
- [DevExpress]利用LookUpEdit实现类似自动提示效果
原文:[DevExpress]利用LookUpEdit实现类似自动提示效果 关键代码: public static void BindWithAutoCompletion(this LookUpEdi ...
随机推荐
- DataOutputStream的writeBytes(String s)
最近,在关于网络请求中有用到DataOutputStraem中的writeBytes()方法,然而就是这个问题,导致了传输中文时就出现问题,着实困扰了很长一段时间. 后来,服务器端同事建议我使用Dat ...
- gcc与g++的区别与联系
gcc和g++都是GNU(组织)的编译器. 一.误区详解 误区一:gcc只能编译c代码,g++只能编译c++代码 两者都可以,但是请注意: 1.后缀为.c的文件, gcc把它当作是C程序,而g++ ...
- ubuntu安装 cober 笔记
设置原始Ubuntu原始Root密码 安装Mysql 安装Jdk 拷贝Cober到Linux,启动服务(看日志) 设置原始Ubuntu原始Root密码 $ sudo passwd root 修改系统文 ...
- Android开发学习——自定义View
转载自: http://blog.csdn.net/xmxkf/article/details/51454685 本文出自:[openXu的博客]
- python下实现汉诺塔
汉诺塔是印度一个古老传说的益智玩具.汉诺塔的移动也可以看做是递归函数. 我们对柱子编号为a, b, c,将所有圆盘从a移到c可以描述为: 如果a只有一个圆盘,可以直接移动到c: 如果a有N个圆盘,可以 ...
- To the end
身为一名初二狗的我也走过了半年.不管怎么说人生中也没有几个半年嘛.从九月到现在快四个月了,我也离中考越来越近了/郁闷/.但是还是要好好过唔.不过我想起这半学期还是挺充实的,至少没有浪费太多的时间.有些 ...
- 遍历Map集合的四中方法
->有这样一个Map集合 Map<String, String> map = new HashMap<String, String>(); map.put(", ...
- 最简单的MFC
#include <SDKDDKVer.h> #include <afxwin.h> #include <afxext.h> #include <iostre ...
- jQ内容的强大,后面继续跟进...
<script type="text/javascript" src="jQ/jquery.js"></script> <scri ...
- python3 验证用户名密码
输入用户名,密码,匹配通过,不匹配报错 import getpass user = input('input username: ') pwd = getpass.getpass('input pas ...