@cryptotaxi247 / kubo / commits / 49f2684c5

expose O(1) len

Brian Tiger Chow committed Jan 20, 2015 at 02:43 UTC 49f2684c531dc225199db8354574a81d0bee16bf
1 file changed +13 -2
exchange/bitswap/wantlist/wantlist.go
+13 -2
@@ -7,13 +7,14 @@ import (
7 )
8
9 type ThreadSafe struct {
10 - lk sync.RWMutex
11 - Wantlist
10 + lk sync.RWMutex
11 + Wantlist Wantlist
12 }
13
14 // not threadsafe
15 type Wantlist struct {
16 set map[u.Key]Entry
17 + // TODO provide O(1) len accessor if cost becomes an issue
18 }
19
20 type Entry struct {
@@ -74,6 +75,16 @@ func (w *ThreadSafe) SortedEntries() []Entry {
75 return w.Wantlist.SortedEntries()
76 }
77
78 +func (w *ThreadSafe) Len() int {
79 + w.lk.RLock()
80 + defer w.lk.RUnlock()
81 + return w.Wantlist.Len()
82 +}
83 +
84 +func (w *Wantlist) Len() int {
85 + return len(w.set)
86 +}
87 +
88 func (w *Wantlist) Add(k u.Key, priority int) {
89 if _, ok := w.set[k]; ok {
90 return