go实现set
package main import (
"fmt"
"sync"
) type object interface{}
type Set struct {
m map[object]bool
sync.RWMutex //线程安全实现,记录下
} //初始化
func New() *Set {
return &Set{
m: map[object]bool{},
}
} func (s *Set) Add(item object) {
s.Lock()
defer s.Unlock()
s.m[item] = true
} func (s *Set) Remove(item object) {
s.Lock()
defer s.Unlock()
delete(s.m, item)
} func (s *Set) Clean() {
s.m = map[object]bool{}
}
func (s *Set) Len() int {
return len(s.m)
} func (s *Set) Contains(item object) bool {
_, ok := s.m[item];
return ok
} func (s *Set) IsEmpty() bool {
fmt.Println(len(s.m))
return len(s.m) > 0
} func main() {
s := New()
fmt.Println(s.IsEmpty())
s.Add("aaa")
fmt.Println(s.IsEmpty())
s.Add("1")
s.Add("2")
s.Add("3")
fmt.Println(s.Contains("3"))
//s.Clean()
for key, _ := range s.m {
fmt.Println("Key:", key)
}
}
随机推荐
- ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)
Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...
- [转]Mac技巧——让Mac轻松访问Windows网络共享
Mac技巧——让Mac轻松访问Windows网络共享 用Mac(MacBook Pro)有段时间了,用一个字概括,那就是“爽”!当然,也有不爽的时候,比如说键盘键位变了,用eclipse的快捷键让 ...
- Out of resources when opening file ‘./xxx.MYD’ (Errcode: 24)解决方法
今天朋友向我反映网站出现错误:Out of resources when opening file './xxx.MYD' (Errcode: 24)错误是因为打开的文件数超过了my.cnf的--op ...
- ie下的布局(layout)和拥有布局(hasLayout)
我们都知道ie浏览器和其他一些浏览器有很多表现不同的地方,这确实让人头疼,ie的表现与其他浏览器不同的原因之一就是我们今天要说的这个熟悉又陌生的东西:layout是一个专门针对显示引擎内部工作方式的概 ...
- 分布式一致性协议之:Zab(Zookeeper的分布式一致性算法)
Zookeeper使用了一种称为Zab(Zookeeper Atomic Broadcast)的协议作为其一致性复制的核心,据其作者说这是一种新发算法,其特点是充分考虑了Yahoo的具体情况:高吞吐量 ...
- c# 正则表达式移除html文本前面的空格
var val = Regex.Replace(text, @"^( | )+(?<value>.*)", "${value}", RegexOpt ...
- 《Java多线程编程核心技术》读后感(十二)
类ThreadLocal的使用 主要解决的是每个线程绑定自己的值,可以将ThreadLocal类比喻成全局存放数据的盒子,盒子中可以存储每个线程私有数据. 类ThreadLocal解决的是变量在不同线 ...
- day4 函数重载
函数的重载 1.函数重载的定义:在同一个类中,有一个以上的同名函数,只要函数的参数列表或参数类型不一样即可,与返回值无关, 这些统称为方法的重载. 2.函数的重载存在的原因:为了增强方法的阅读性,优化 ...
- CodeForces 484B Maximum Value (数学,其实我也不知道咋分类)
B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- C#Timer停不住
System.Timers.Timer timer1 = new System.Timers.Timer(); timer1.Interval = ; //1天循环一次 timer1.Elapsed ...