-
Notifications
You must be signed in to change notification settings - Fork 52
/
cmdset.go
142 lines (125 loc) · 2.82 KB
/
cmdset.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
package data
import (
"container/list"
"sync"
)
type cmdElement struct {
cmd Command
proposed bool
}
// CommandSet is a linkedhashset for Commands
type CommandSet struct {
mut sync.Mutex
set map[Command]*list.Element
order list.List
}
func NewCommandSet() *CommandSet {
c := &CommandSet{
set: make(map[Command]*list.Element),
}
c.order.Init()
return c
}
// Add adds cmds to the set. Duplicate entries are ignored
func (s *CommandSet) Add(cmds ...Command) {
s.mut.Lock()
defer s.mut.Unlock()
for _, cmd := range cmds {
// avoid duplicates
if _, ok := s.set[cmd]; ok {
continue
}
e := s.order.PushBack(&cmdElement{cmd: cmd})
s.set[cmd] = e
}
}
// Remove removes cmds from the set
func (s *CommandSet) Remove(cmds ...Command) {
s.mut.Lock()
defer s.mut.Unlock()
for _, cmd := range cmds {
if e, ok := s.set[cmd]; ok {
// remove e from list and set
delete(s.set, cmd)
s.order.Remove(e)
}
}
}
// GetFirst returns the n first non-proposed commands in the set
func (s *CommandSet) GetFirst(n int) []Command {
s.mut.Lock()
defer s.mut.Unlock()
if len(s.set) == 0 {
return nil
}
cmds := make([]Command, 0, n)
i := 0
e := s.order.Front()
for i < n {
if e == nil {
break
}
if c := e.Value.(*cmdElement); !c.proposed {
cmds = append(cmds, c.cmd)
i++
}
e = e.Next()
}
return cmds
}
// Contains returns true if the set contains cmd, false otherwise
func (s *CommandSet) Contains(cmd Command) bool {
s.mut.Lock()
defer s.mut.Unlock()
_, ok := s.set[cmd]
return ok
}
// Len returns the length of the set
func (s *CommandSet) Len() int {
s.mut.Lock()
defer s.mut.Unlock()
return len(s.set)
}
// TrimToLen will try to remove proposed elements from the set until its length is equal to or less than 'length'
func (s *CommandSet) TrimToLen(length int) {
s.mut.Lock()
defer s.mut.Unlock()
e := s.order.Front()
for length < len(s.set) {
if e == nil {
break
}
n := e.Next()
c := e.Value.(*cmdElement)
if c.proposed {
s.order.Remove(e)
delete(s.set, c.cmd)
}
e = n
}
}
// IsProposed will return true if the given command is marked as proposed
func (s *CommandSet) IsProposed(cmd Command) bool {
s.mut.Lock()
defer s.mut.Unlock()
if e, ok := s.set[cmd]; ok {
return e.Value.(*cmdElement).proposed
}
return false
}
// MarkProposed will mark the given commands as proposed and move them to the back of the queue
func (s *CommandSet) MarkProposed(cmds ...Command) {
s.mut.Lock()
defer s.mut.Unlock()
for _, cmd := range cmds {
if e, ok := s.set[cmd]; ok {
e.Value.(*cmdElement).proposed = true
// Move to back so that it's not immediately deleted by a call to TrimToLen()
s.order.MoveToBack(e)
} else {
// We don't have the command locally yet, so let's store it
e := s.order.PushBack(&cmdElement{cmd: cmd, proposed: true})
s.set[cmd] = e
}
}
}