create wantlist object
Jeromy committed
Dec 10, 2014 at 02:00 UTC
12b296ee1a494fb2d74db7ceebb513e5b4479d39
1 file changed
+56
exchange/bitswap/wantlist/wantlist.go
new
+56
@@ -0,0 +1,56 @@
1
+package wantlist
2
+
3
+import (
4
+ u "github.com/jbenet/go-ipfs/util"
5
+ "sort"
6
+)
7
+
8
+type Wantlist struct {
9
+ set map[u.Key]*Entry
10
+}
11
+
12
+func NewWantlist() *Wantlist {
13
+ return &Wantlist{
14
+ set: make(map[u.Key]*Entry),
15
+ }
16
+}
17
+
18
+type Entry struct {
19
+ Value u.Key
20
+ Priority int
21
+}
22
+
23
+func (w *Wantlist) Add(k u.Key, priority int) {
24
+ if _, ok := w.set[k]; ok {
25
+ return
26
+ }
27
+ w.set[k] = &Entry{
28
+ Value: k,
29
+ Priority: priority,
30
+ }
31
+}
32
+
33
+func (w *Wantlist) Remove(k u.Key) {
34
+ delete(w.set, k)
35
+}
36
+
37
+func (w *Wantlist) Contains(k u.Key) bool {
38
+ _, ok := w.set[k]
39
+ return ok
40
+}
41
+
42
+type entrySlice []*Entry
43
+
44
+func (es entrySlice) Len() int { return len(es) }
45
+func (es entrySlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
46
+func (es entrySlice) Less(i, j int) bool { return es[i].Priority < es[j].Priority }
47
+
48
+func (w *Wantlist) Entries() []*Entry {
49
+ var es entrySlice
50
+
51
+ for _, e := range w.set {
52
+ es = append(es, e)
53
+ }
54
+ sort.Sort(es)
55
+ return es
56
+}