Skip to content

Commit

Permalink
Hide list root (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
JimChenWYU authored Aug 26, 2024
1 parent 354ff5a commit 94c9fbd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
12 changes: 11 additions & 1 deletion internal/list/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ type Entry[K comparable, V any] struct {
}

// PrevEntry returns the previous list element or nil.
// note: can't get root entry
func (e *Entry[K, V]) PrevEntry() *Entry[K, V] {
if p := e.prev; e.list != nil && p != e.list.Root() {
if p := e.prev; e.list != nil && p != &e.list.root {
return p
}
return nil
}

// NextEntry returns the next list element or nil.
// note: can't get root entry
func (e *Entry[K, V]) NextEntry() *Entry[K, V] {
if p := e.next; e.list != nil && p != &e.list.root {
return p
}
return nil
Expand Down
9 changes: 2 additions & 7 deletions internal/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,15 @@ func (l *DoublyLinkedList[K, V]) Back() *Entry[K, V] {
return l.root.prev
}

// Root returns the root of the list.
func (l *DoublyLinkedList[K, V]) Root() *Entry[K, V] {
return &l.root
}

// Root returns the root of the list.
func (l *DoublyLinkedList[K, V]) Debug() []string {
asc := []string{"root"}
for ent := l.Root().next; ent.list == l; ent = ent.next {
for ent := l.root.next; ent.list == l; ent = ent.next {
asc = append(asc, fmt.Sprintf("%v", ent.Key))
}
asc = append(asc, "root")
desc := []string{"root"}
for ent := l.Root().prev; ent.list == l; ent = ent.prev {
for ent := l.root.prev; ent.list == l; ent = ent.prev {
desc = append(desc, fmt.Sprintf("%v", ent.Key))
}
desc = append(desc, "root")
Expand Down
6 changes: 4 additions & 2 deletions internal/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestLruList_PushFront(t *testing.T) {
assert.EqualValues(t, 1, l.Len())

second := l.PushFront(2, 22)
assert.EqualValues(t, second.prev, l.Root()) // forbid root
assert.EqualValues(t, second.prev, &l.root) // forbid root
assert.Equal(t, first.PrevEntry(), second)

assert.EqualValues(t, 2, second.Key)
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestLruList_PushBack(t *testing.T) {
assert.EqualValues(t, 1, l.Len())

second := l.PushBack(2, 22)
assert.Equal(t, second.next, l.Root())
assert.Equal(t, second.next, &l.root)
assert.Equal(t, first.next, second)

assert.EqualValues(t, 2, second.Key)
Expand Down Expand Up @@ -155,5 +155,7 @@ func TestLruList_Entry(t *testing.T) {
first := l.PushFront(1, 1)
second := l.PushFront(2, 2)
assert.Equal(t, first.PrevEntry(), second)
assert.Equal(t, second.NextEntry(), first)
assert.Nil(t, second.PrevEntry())
assert.Nil(t, first.NextEntry())
}
7 changes: 4 additions & 3 deletions simplelfu/lfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,13 @@ func (c *Cache[K, V]) removeElement(ent *list.Entry[K, *LFUValue[V]]) {

// moveForward is used to move a given list element to the front of the cache
func (c *Cache[K, V]) moveForward(ent *list.Entry[K, *LFUValue[V]]) {
// if ent is root entry, ent.PrevEntry() is nil
for prev := ent.PrevEntry(); prev != nil; prev = ent.PrevEntry() {
if ent.Value.GetVisit() < prev.Value.GetVisit() {
break
}
if !c.evictList.MoveToAt(ent, prev.PrevEntry()) {
break
}
// because ent can't get root entry from PrevEntry(),
// so we don't need to check
_ = c.evictList.MoveToAt(ent, prev.PrevEntry())
}
}

0 comments on commit 94c9fbd

Please sign in to comment.