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 ...
随机推荐
- 12503 - Robot Instructions
Robot Instructions You have a robot standing on the origin of x axis. The robot will be given som ...
- JavaScript中ActiveXObject操作本地文件夹
在Windows平台上, js可以调用很多Windows提供的ActivexObject,本文就使用js来实现文档处理, 和使用js编写ActiveX做一个简单介绍. <!DOCTYPE HTM ...
- 基于数据库的自动化生成工具,自动生成JavaBean、自动生成数据库文档等(v4.1.2版)
目录: 第1版:http://blog.csdn.net/vipbooks/article/details/51912143 第2版:htt ...
- 结构-行为-样式-Js函数节流
最近一个面试官问了我一个函数节流的问题,然后感觉自己工作中遇到过这个问题,但是不知道这种形式就是函数节流.下面我来说下这个Js的高级问题,思路:函数节流就是防止用户高频调用某个事件而做的Js层面的处理 ...
- 部署 instance 到 OVS vlan100 - 每天5分钟玩转 OpenStack(138)
上一节创建了 OVS vlan network vlan100,今天部署 instance 到该网络.launch 新的 instance “cirros-vm1”,网络选择 vlan100. cir ...
- List转换为DataTable
public static DataTable ListToDataTable(IList list) { DataTable result = new DataTable(); if (list.C ...
- Some Error
0x01 E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution). sudo ...
- 微信小程序前置课程:Flex 布局教程(一):语法篇
原文:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool 网页布局(layout)是CSS的一个重点 ...
- CFRound#379(div2)
题目链接:http://codeforces.com/contest/734 A:SB题. #include<cstdio> #include<cstring> #includ ...
- sql语句实现隐藏手机号码中间四位的方法
1、select REPLACE(mobile,SUBSTR(mobile,4,4), '****') as mobile from tableName 2、select INSERT(mobile, ...