-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericMapWithPastesCounter.go
67 lines (56 loc) · 1.37 KB
/
GenericMapWithPastesCounter.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
package util
// Users of this map are expected to access it with a mutex.
type MapWithPastesCount[T MapItem] interface {
InsertNew(key string, value T) error
GetKey(key string) (T, error)
DeleteKey(key string)
NumPastes() int
NumItems() int
}
type MapWithPastesCount_impl[T MapItem] struct {
m map[string]T
pastes_count int
}
func NewMapWithPastesCount[T MapItem](size int64) MapWithPastesCount[T] {
return &MapWithPastesCount_impl[T]{
m: make(map[string]T, size),
pastes_count: 0,
}
}
func (mwpc *MapWithPastesCount_impl[T]) InsertNew(key string, value T) error {
_, ok := mwpc.m[key]
if ok {
return KeyAlreadyExistsError{}
}
mwpc.m[key] = value
if value.GetType().ValueType == TYPE_MAP_ITEM_PASTE {
mwpc.pastes_count++
}
return nil
}
func (mwpc *MapWithPastesCount_impl[T]) GetKey(key string) (T, error) {
val, ok := mwpc.m[key]
if ok {
return val, nil
} else {
var zero_value T
return zero_value, CPMNonExistentKeyError{}
}
}
func (mwpc *MapWithPastesCount_impl[T]) DeleteKey(key string) {
// check if key is already in map
val, ok := mwpc.m[key]
if !ok {
return
}
if val.GetType().ValueType == TYPE_MAP_ITEM_PASTE {
mwpc.pastes_count--
}
delete(mwpc.m, key)
}
func (mwpc *MapWithPastesCount_impl[T]) NumItems() int {
return len(mwpc.m)
}
func (mwpc *MapWithPastesCount_impl[T]) NumPastes() int {
return mwpc.pastes_count
}