-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict.go
More file actions
102 lines (88 loc) · 1.84 KB
/
Copy pathdict.go
File metadata and controls
102 lines (88 loc) · 1.84 KB
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
/* Go maps with additional methods
- Set
- Delete
- Has
- Get
- Clear
- Size
- Keys
- Values
*/
package dictionary
import (
"sync"
"github.com/cheekybits/genny/generic"
)
// Key is a generic type allowing substitution
type Key generic.Type
// Value is a generic type allowing substitution
type Value generic.Type
// Dictionary the set of Items
type Dictionary struct {
items map[Key]Value
lock sync.RWMutex
}
// Set adds a new item to the dictionary
func (d *Dictionary) Set(k Key, v Value) {
d.lock.Lock()
defer d.lock.Unlock()
if d.items == nil {
d.items = make(map[Key]Value)
}
d.items[k] = v
}
// Delete a value from a Dictionary, given key
func (d *Dictionary) Delete(k Key) bool {
d.lock.Lock()
defer d.lock.Unlock()
_, ok := d.items[k]
if ok {
delete(d.items, k)
}
return ok
}
// Has returns true if the key exists in the dictionary
func (d *Dictionary) Has(k Key) bool {
d.lock.RLock()
defer d.lock.RUnlock()
_, ok := d.items[k]
return ok
}
// Get returns the value associated with the key
func (d *Dictionary) Get(k Key) Value {
d.lock.RLock()
defer d.lock.RUnlock()
return d.items[k]
}
// Clear removes all the items from the dictionary
func (d *Dictionary) Clear() {
d.lock.Lock()
defer d.lock.Unlock()
d.items = make(map[Key]Value)
}
// Size returns the amount of elements in the dictionary
func (d *Dictionary) Size() int {
d.lock.Lock()
defer d.lock.Unlock()
return len(d.items)
}
// Keys returns a slice of all the keys present
func (d *Dictionary) Keys() []Key {
d.lock.RLock()
defer d.lock.RUnlock()
keys := []Key{}
for i := range d.items {
keys = append(keys, i)
}
return keys
}
// Values returns a slice of all the values present
func (d *Dictionary) Values() []Value {
d.lock.RLock()
defer d.lock.RUnlock()
values := []Value{}
for i := range d.items {
values = append(values, d.items[i])
}
return values
}