status-go/vendor/github.com/anacrolix/dht/v2/containers/addr-maybe-ids-by-distance.go

45 lines
1.1 KiB
Go
Raw Normal View History

2022-03-10 09:44:48 +00:00
package containers
import (
"github.com/anacrolix/dht/v2/int160"
"github.com/anacrolix/dht/v2/types"
"github.com/anacrolix/missinggo/v2/iter"
"github.com/anacrolix/stm/stmutil"
)
type addrMaybeId = types.AddrMaybeId
type AddrMaybeIdsByDistance interface {
Add(addrMaybeId) AddrMaybeIdsByDistance
Next() addrMaybeId
Delete(addrMaybeId) AddrMaybeIdsByDistance
Len() int
}
type stmSettishWrapper struct {
set stmutil.Settish
}
func (me stmSettishWrapper) Next() addrMaybeId {
first, _ := iter.First(me.set.Iter)
return first.(addrMaybeId)
}
func (me stmSettishWrapper) Delete(x addrMaybeId) AddrMaybeIdsByDistance {
return stmSettishWrapper{me.set.Delete(x)}
}
func (me stmSettishWrapper) Len() int {
return me.set.Len()
}
func (me stmSettishWrapper) Add(x addrMaybeId) AddrMaybeIdsByDistance {
return stmSettishWrapper{me.set.Add(x)}
}
func NewImmutableAddrMaybeIdsByDistance(target int160.T) AddrMaybeIdsByDistance {
return stmSettishWrapper{stmutil.NewSortedSet(func(l, r interface{}) bool {
return l.(addrMaybeId).CloserThan(r.(addrMaybeId), target)
})}
}