graph/simple: factor id handling out of graphs

This commit is contained in:
kortschak
2017-05-27 19:13:28 +09:30
committed by Dan Kortschak
parent d0fc9efef3
commit 1a18034e61
3 changed files with 46 additions and 49 deletions

View File

@@ -9,6 +9,8 @@ package simple // import "gonum.org/v1/gonum/graph/simple"
import (
"math"
"golang.org/x/tools/container/intsets"
"gonum.org/v1/gonum/graph"
)
@@ -43,3 +45,37 @@ const maxInt int = int(^uint(0) >> 1)
func isSame(a, b float64) bool {
return a == b || (math.IsNaN(a) && math.IsNaN(b))
}
// idSet implements available ID storage.
type idSet struct {
used, free intsets.Sparse
}
// newID returns a new unique ID.
func (s *idSet) newID() int {
var id int
if s.free.TakeMin(&id) {
return id
}
if id = s.used.Max(); id < maxInt {
return id + 1
}
for id = 0; id < maxInt; id++ {
if !s.used.Has(id) {
return id
}
}
panic("unreachable")
}
// use adds the id to the used IDs in the idSet.
func (s *idSet) use(id int) {
s.free.Remove(id)
s.used.Insert(id)
}
// free frees the id for reuse.
func (s *idSet) release(id int) {
s.free.Insert(id)
s.used.Remove(id)
}