Commit fe4f8ad2 authored by Brian Tiger Chow's avatar Brian Tiger Chow

expose O(1) len

parent 3c044d23
...@@ -8,12 +8,13 @@ import ( ...@@ -8,12 +8,13 @@ import (
type ThreadSafe struct { type ThreadSafe struct {
lk sync.RWMutex lk sync.RWMutex
Wantlist Wantlist Wantlist
} }
// not threadsafe // not threadsafe
type Wantlist struct { type Wantlist struct {
set map[u.Key]Entry set map[u.Key]Entry
// TODO provide O(1) len accessor if cost becomes an issue
} }
type Entry struct { type Entry struct {
...@@ -74,6 +75,16 @@ func (w *ThreadSafe) SortedEntries() []Entry { ...@@ -74,6 +75,16 @@ func (w *ThreadSafe) SortedEntries() []Entry {
return w.Wantlist.SortedEntries() return w.Wantlist.SortedEntries()
} }
func (w *ThreadSafe) Len() int {
w.lk.RLock()
defer w.lk.RUnlock()
return w.Wantlist.Len()
}
func (w *Wantlist) Len() int {
return len(w.set)
}
func (w *Wantlist) Add(k u.Key, priority int) { func (w *Wantlist) Add(k u.Key, priority int) {
if _, ok := w.set[k]; ok { if _, ok := w.set[k]; ok {
return return
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment