-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhset_map.go
152 lines (131 loc) · 3.38 KB
/
hset_map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2017 The go-vgo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://github.com/go-vgo/gt/blob/master/LICENSE
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
package hset
import (
"fmt"
"strings"
"sync"
)
// Hset holds elements in go's native map
type Hset struct {
items map[interface{}]struct{}
// items sync.Map[interface{}]struct{}
sync.RWMutex
}
// Hset holds elements in go's native map
var itemExists = struct{}{}
// New instantiates a new empty hset
func New() *Hset {
return &Hset{items: make(map[interface{}]struct{})}
}
// Add adds the items (one or more) to the hset.
func (hset *Hset) Add(items ...interface{}) {
hset.Lock()
// defer hset.Unlock()
for _, item := range items {
hset.items[item] = itemExists
}
hset.Unlock()
}
// Remove removes the items (one or more) from the hset.
func (hset *Hset) Remove(items ...interface{}) {
hset.Lock()
// defer hset.Unlock()
for _, item := range items {
delete(hset.items, item)
}
hset.Unlock()
}
// Contains check if items (one or more) are present in the hset.
// All items have to be present in the hset for the method to return true.
// Returns true if no arguments are passed at all,
// i.e. hset is always superhset of empty hset.
func (hset *Hset) Contains(items ...interface{}) bool {
for _, item := range items {
if _, contains := hset.items[item]; !contains {
return false
}
}
return true
}
// Clear clears all values in the hset.
func (hset *Hset) Clear() {
hset.Lock()
// defer hset.Unlock()
hset.items = make(map[interface{}]struct{})
hset.Unlock()
}
// Exists returns a bool indicating if the given item exists in the set.
func (hset *Hset) Exists(item interface{}) bool {
hset.RLock()
_, ok := hset.items[item]
hset.RUnlock()
return ok
}
// Empty returns true if hset does not contain any elements.
func (hset *Hset) Empty() bool {
// return hset.Size() == 0
return hset.Len() == 0
}
// Size returns number of elements within the hset.
// func (hset *Hset) Size() int {
// return len(hset.items)
// }
// Len returns number of elements within the hset.
func (hset *Hset) Len() int {
hset.RLock()
size := len(hset.items)
hset.RUnlock()
return size
}
// Values returns all items in the hset.
// List()
func (hset *Hset) Values() []interface{} {
hset.RLock()
defer hset.RUnlock()
// values := make([]interface{}, hset.Size())
values := make([]interface{}, hset.Len())
count := 0
for item := range hset.items {
values[count] = item
count++
}
return values
}
// Same to determine whether the two hset type values are the same.
func (hset *Hset) Same(other Set) bool {
hset.RLock()
defer hset.RUnlock()
if other == nil {
return false
}
if hset.Len() != other.Len() {
return false
}
for key := range hset.items {
if !other.Contains(key) {
return false
}
}
return true
}
// String returns a string representation of container
func (hset *Hset) String(pre ...bool) string {
str := ""
if len(pre) > 0 {
str = "Has Hset:\n"
}
items := []string{}
for k := range hset.items {
items = append(items, fmt.Sprintf("%v", k))
}
str += strings.Join(items, ", ")
return str
}