2020-03-10 12:20:34 -07:00
|
|
|
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
|
|
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
|
|
|
|
package ids
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
2020-06-16 12:17:13 -07:00
|
|
|
const (
|
|
|
|
minShortSetSize = 16
|
|
|
|
)
|
|
|
|
|
2020-03-10 12:20:34 -07:00
|
|
|
// ShortSet is a set of ShortIDs
|
|
|
|
type ShortSet map[[20]byte]bool
|
|
|
|
|
|
|
|
func (ids *ShortSet) init(size int) {
|
|
|
|
if *ids == nil {
|
2020-06-16 12:17:13 -07:00
|
|
|
if minShortSetSize > size {
|
|
|
|
size = minShortSetSize
|
|
|
|
}
|
2020-03-10 12:20:34 -07:00
|
|
|
*ids = make(map[[20]byte]bool, size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all the ids to this set, if the id is already in the set, nothing happens
|
|
|
|
func (ids *ShortSet) Add(idList ...ShortID) {
|
|
|
|
ids.init(2 * len(idList))
|
|
|
|
for _, id := range idList {
|
|
|
|
(*ids)[id.Key()] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Union adds all the ids from the provided sets to this set.
|
|
|
|
func (ids *ShortSet) Union(idSet ShortSet) {
|
|
|
|
ids.init(2 * idSet.Len())
|
|
|
|
for id := range idSet {
|
|
|
|
(*ids)[id] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains returns true if the set contains this id, false otherwise
|
|
|
|
func (ids *ShortSet) Contains(id ShortID) bool {
|
|
|
|
ids.init(1)
|
|
|
|
return (*ids)[id.Key()]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of ids in this set
|
|
|
|
func (ids ShortSet) Len() int { return len(ids) }
|
|
|
|
|
|
|
|
// Remove all the id from this set, if the id isn't in the set, nothing happens
|
|
|
|
func (ids *ShortSet) Remove(idList ...ShortID) {
|
|
|
|
ids.init(1)
|
|
|
|
for _, id := range idList {
|
|
|
|
delete(*ids, id.Key())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear empties this set
|
|
|
|
func (ids *ShortSet) Clear() { *ids = nil }
|
|
|
|
|
2020-06-22 13:50:31 -07:00
|
|
|
// CappedList returns a list of length at most [size].
|
2020-06-23 13:44:02 -07:00
|
|
|
// Size should be >= 0. If size < 0, returns nil.
|
2020-03-10 12:20:34 -07:00
|
|
|
func (ids ShortSet) CappedList(size int) []ShortID {
|
2020-06-22 13:50:31 -07:00
|
|
|
if size < 0 {
|
2020-06-23 13:44:02 -07:00
|
|
|
return nil
|
2020-06-22 13:50:31 -07:00
|
|
|
}
|
|
|
|
if l := ids.Len(); l < size {
|
|
|
|
size = l
|
|
|
|
}
|
|
|
|
i := 0
|
|
|
|
idList := make([]ShortID, size)
|
2020-03-10 12:20:34 -07:00
|
|
|
for id := range ids {
|
2020-06-22 13:50:31 -07:00
|
|
|
if i >= size {
|
2020-03-10 12:20:34 -07:00
|
|
|
break
|
|
|
|
}
|
2020-06-22 13:50:31 -07:00
|
|
|
idList[i] = NewShortID(id)
|
|
|
|
i++
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
return idList
|
|
|
|
}
|
|
|
|
|
|
|
|
// List converts this set into a list
|
|
|
|
func (ids ShortSet) List() []ShortID {
|
2020-06-16 12:17:13 -07:00
|
|
|
idList := make([]ShortID, len(ids), len(ids))
|
|
|
|
i := 0
|
2020-03-10 12:20:34 -07:00
|
|
|
for id := range ids {
|
2020-06-16 12:17:13 -07:00
|
|
|
idList[i] = NewShortID(id)
|
|
|
|
i++
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
return idList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals returns true if the sets contain the same elements
|
|
|
|
func (ids ShortSet) Equals(oIDs ShortSet) bool {
|
|
|
|
if ids.Len() != oIDs.Len() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for key := range oIDs {
|
|
|
|
if !ids[key] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string representation of a set
|
|
|
|
func (ids ShortSet) String() string {
|
|
|
|
sb := strings.Builder{}
|
|
|
|
sb.WriteString("{")
|
|
|
|
first := true
|
|
|
|
for idBytes := range ids {
|
|
|
|
if !first {
|
|
|
|
sb.WriteString(", ")
|
|
|
|
}
|
|
|
|
first = false
|
|
|
|
sb.WriteString(NewShortID(idBytes).String())
|
|
|
|
}
|
|
|
|
sb.WriteString("}")
|
|
|
|
return sb.String()
|
|
|
|
}
|