File tree Expand file tree Collapse file tree 3 files changed +144
-0
lines changed
Expand file tree Collapse file tree 3 files changed +144
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Author: Juntaran
3+ * Email: Jacinthmail@gmail.com
4+ * Date: 2017/5/5 01:01
5+ */
6+
7+ package main
8+
9+ import (
10+ "Golang_Algorithm/Data_Structure/Set"
11+ "fmt"
12+ "log"
13+ )
14+
15+ func main () {
16+ testSet := Set .New ()
17+ testSet .PrintSet ()
18+ fmt .Println ("Length is" , testSet .LenSet ())
19+ fmt .Println ("isEmpty:" , testSet .IsEmpty ())
20+
21+ for i := 1 ; i <= 10 ; i ++ {
22+ testSet .Add (i )
23+ }
24+ testSet .PrintSet ()
25+ fmt .Println ("Length is" , testSet .LenSet ())
26+ fmt .Println ("isEmpty:" , testSet .IsEmpty ())
27+
28+ fmt .Println (testSet .IsExist (3 ))
29+ fmt .Println (testSet .IsExist (11 ))
30+
31+ err := testSet .Delete (3 )
32+ if err != nil {
33+ log .Println (err )
34+ }
35+
36+ fmt .Println (testSet .IsExist (3 ))
37+ fmt .Println ("Length is" , testSet .LenSet ())
38+ fmt .Println ("isEmpty:" , testSet .IsEmpty ())
39+
40+ err = testSet .Delete (3 )
41+ if err != nil {
42+ log .Println (err )
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Author: Juntaran
3+ * Email: Jacinthmail@gmail.com
4+ * Date: 2017/5/5 00:43
5+ */
6+
7+ package Set
8+
9+ import (
10+ "sync"
11+ "fmt"
12+ "errors"
13+ )
14+
15+ type Set struct {
16+ set map [interface {}]bool
17+ lock sync.Mutex
18+ }
19+
20+ // 创建一个集合
21+ func New () * Set {
22+ return & Set {
23+ set : map [interface {}]bool {},
24+ }
25+ }
26+
27+ // 添加元素
28+ func (set * Set ) Add (item interface {}) {
29+ set .lock .Lock ()
30+ defer set .lock .Unlock ()
31+ {
32+ set .set [item ] = true
33+ }
34+ }
35+
36+ // 删除元素
37+ func (set * Set ) Delete (item interface {}) error {
38+ if set .IsExist (item ) == false {
39+ return errors .New ("Error: Delete Not Find" )
40+ }
41+ set .lock .Lock ()
42+ defer set .lock .Unlock ()
43+ {
44+ delete (set .set , item )
45+ return nil
46+ }
47+ }
48+
49+ // 判断该元素是否存在
50+ func (set * Set ) IsExist (item interface {}) bool {
51+ set .lock .Lock ()
52+ defer set .lock .Unlock ()
53+ {
54+ _ , ok := set .set [item ]
55+ return ok
56+ }
57+ }
58+
59+ // 集合大小
60+ func (set * Set ) LenSet () int {
61+ return len (set .listSet ())
62+ }
63+
64+ // 判断集合是否为空
65+ func (set * Set ) IsEmpty () bool {
66+ if set .LenSet () == 0 {
67+ return true
68+ }
69+ return false
70+ }
71+
72+ // 输出集合中所有元素
73+ func (set * Set ) PrintSet () {
74+ setList := set .listSet ()
75+ fmt .Println (setList )
76+ }
77+
78+ // 集合元素存入切片
79+ func (set * Set ) listSet () []interface {} {
80+ set .lock .Lock ()
81+ defer set .lock .Unlock ()
82+ {
83+ list := []interface {}{}
84+ for item := range set .set {
85+ list = append (list , item )
86+ }
87+ return list
88+ }
89+ }
Original file line number Diff line number Diff line change @@ -16,3 +16,14 @@ Reference:
1616* [ 0xAX] ( https://github.com/0xAX/go-algorithms )
1717* [ arnauddri] ( https://github.com/arnauddri/algorithms )
1818* [ jackfhebert] ( https://github.com/jackfhebert/hashtable )
19+ * [ studyGolang.com] ( http://studygolang.com/articles/2335 )
20+
21+ 1.数组是 slice 和 map 的底层结构。
22+ 2.slice 是 Go 里面惯用的集合数据的方法,map 则是用来存储键值对。
23+ 3.内建函数 make 用来创建 slice 和 map,并且为它们指定长度和容量等等。slice 和 map 字面值也可以做同样的事。
24+ 4.slice 有容量的约束,不过可以通过内建函数 append 来增加元素。
25+ 5.map 没有容量一说,所以也没有任何增长限制。
26+ 6.内建函数 len 可以用来获得 slice 和 map 的长度。
27+ 7.内建函数 cap 只能作用在 slice 上。
28+ 8.可以通过组合方式来创建多维数组和 slice。map 的值可以是 slice 或者另一个 map。slice 不能作为 map 的键。
29+ 9.在函数之间传递 slice 和 map 是相当廉价的,因为他们不会传递底层数组的拷贝。
You can’t perform that action at this time.
0 commit comments