Skip to content

Commit

Permalink
feat: add RangeNoOrder function to quickly traverse the elements in t…
Browse files Browse the repository at this point in the history
…he Splay tree without caring about the order
  • Loading branch information
slipegg committed Oct 17, 2024
1 parent e25741f commit 954cfc5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/framework/api/nodeinfo_podinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (m *PodInfoMaintainer) Len() int {
func (m *PodInfoMaintainer) GetPods() []*PodInfo {
pods := make([]*PodInfo, 0, m.bePodsMayBePreempted.Len()+m.gtPodsMayBePreempted.Len()+len(m.neverBePreempted))
rangeSplay := func(s splay.Splay) {
s.Range(func(so splay.StoredObj) {
s.RangeNoOrder(func(so splay.StoredObj) {
pods = append(pods, so.(*PodInfo))
})
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/util/splay/splay.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ type Splay interface {
Clone() Splay
// PrintTree outputs splay in the form of a tree diagram.
PrintTree() string
// Range traverses the entire splay quickly. Note that the order of traversal is not guaranteed
RangeNoOrder(RangeFunc)
}

type splay struct {
Expand Down Expand Up @@ -387,6 +389,14 @@ func (s *splay) PrintTree() string {
return output.String()
}

func (s *splay) RangeNoOrder(f RangeFunc) {
for i, node := range s.items {
if i != s.minv && i != s.maxv && i != 0 {
f(node.obj)
}
}
}

// getChildIndex indicates whether `x` is the right child of `y`.
func (s *splay) getChildIndex(x, y int) int {
if y != 0 && s.items[y].rchild == x {
Expand Down
38 changes: 38 additions & 0 deletions pkg/util/splay/splay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,44 @@ func Test_Splay(t *testing.T) {
t.Logf("After j=%v got splay: %s\n", j, s)
}
}
func Test_Splay_List_vs_Range(t *testing.T) {
s := NewSplay()

for i := 1; i < 100; i++ {
s.Insert(makeObj(i, i))
}

var noOrderObjs []StoredObj
s.RangeNoOrder(func(so StoredObj) {
noOrderObjs = append(noOrderObjs, so)
})

var orderObjs []StoredObj
s.Range(func(so StoredObj) {
orderObjs = append(orderObjs, so)
})

if len(noOrderObjs) != len(orderObjs) {
t.Errorf("Length mismatch: List has %d items, Range has %d items", len(noOrderObjs), len(orderObjs))
}

// Tests if elements are identical (order doesn't matter)
counts := make(map[StoredObj]int)

for _, obj := range noOrderObjs {
counts[obj]++
}
for _, obj := range orderObjs {
counts[obj]--
}

for _, count := range counts {
if count != 0 {
t.Error("Mismatch between onOrderedObjs and rangeObjs elements")
return
}
}
}

func Test_Rotate(t *testing.T) {
s := NewSplay()
Expand Down

0 comments on commit 954cfc5

Please sign in to comment.