Skip to content

Commit 680ba5c

Browse files
committed
Move the ringlist into own package
1 parent c544e6e commit 680ba5c

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

backend.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ package evcache
33
import (
44
"sync"
55
"time"
6+
7+
"github.com/mgnsk/evcache/v3/ringlist"
68
)
79

10+
type recordList[V any] struct {
11+
ringlist.List[record[V], *record[V]]
12+
}
13+
814
type backend[K comparable, V any] struct {
9-
list RingList[record[V], *record[V]]
15+
list recordList[V]
1016
timer *time.Timer
1117
done chan struct{}
1218
sync.Map

ringlist/doc.go

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package ringlist implements a circular doubly linked list.
3+
*/
4+
package ringlist

ringlist/element.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ringlist
2+
3+
// Element is a list element.
4+
type Element[V any] struct {
5+
Value V
6+
next, prev *Element[V]
7+
}
8+
9+
// NewElement creates a list element.
10+
func NewElement[V any](v V) *Element[V] {
11+
e := &Element[V]{
12+
Value: v,
13+
}
14+
e.next = e
15+
e.prev = e
16+
return e
17+
}
18+
19+
// Next returns the next element or nil.
20+
func (e *Element[V]) Next() *Element[V] {
21+
return e.next
22+
}
23+
24+
// Prev returns the previous element or nil.
25+
func (e *Element[V]) Prev() *Element[V] {
26+
return e.prev
27+
}
28+
29+
// Link inserts an element after this element.
30+
func (e *Element[V]) Link(s *Element[V]) {
31+
n := e.next
32+
e.next = s
33+
s.prev = e
34+
n.prev = s
35+
s.next = n
36+
}
37+
38+
// Unlink unlinks this element.
39+
func (e *Element[V]) Unlink() {
40+
e.prev.next = e.next
41+
e.next.prev = e.prev
42+
e.next = e
43+
e.prev = e
44+
}

ringlist.go ringlist/ringlist.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
package evcache
1+
package ringlist
22

3-
// ListElement is the constraint for a list element.
3+
// ElementList is a built in list type that uses the Element type as its element.
4+
// The zero value is a ready to use empty list.
5+
type ElementList[V any] struct {
6+
List[Element[V], *Element[V]]
7+
}
8+
9+
// ListElement is the constraint for a generic list element.
410
type ListElement[E any] interface {
511
Link(E)
612
Unlink()
713
Next() E
814
Prev() E
915
}
1016

11-
// RingList is a circular doubly linked list.
17+
// List is a generic circular doubly linked list.
1218
// The zero value is a ready to use empty list.
13-
type RingList[T any, E interface {
19+
type List[T any, E interface {
1420
*T
1521
ListElement[E]
1622
}] struct {
@@ -19,25 +25,25 @@ type RingList[T any, E interface {
1925
}
2026

2127
// Len returns the number of elements in the list.
22-
func (l RingList[T, E]) Len() int {
28+
func (l *List[T, E]) Len() int {
2329
return l.len
2430
}
2531

2632
// Front returns the first element of the list or nil.
27-
func (l *RingList[T, E]) Front() E {
33+
func (l *List[T, E]) Front() E {
2834
if l.len == 0 {
2935
return nil
3036
}
3137
return l.tail.Next()
3238
}
3339

3440
// Back returns the last element of the list or nil.
35-
func (l *RingList[T, E]) Back() E {
41+
func (l *List[T, E]) Back() E {
3642
return l.tail
3743
}
3844

39-
// PushBack inserts a new element v at the back of the list.
40-
func (l *RingList[T, E]) PushBack(e E) {
45+
// PushBack inserts a new element at the back of the list.
46+
func (l *List[T, E]) PushBack(e E) {
4147
if l.tail != nil {
4248
l.tail.Link(e)
4349
}
@@ -46,7 +52,7 @@ func (l *RingList[T, E]) PushBack(e E) {
4652
}
4753

4854
// Remove an element from the list.
49-
func (l *RingList[T, E]) Remove(e E) {
55+
func (l *List[T, E]) Remove(e E) {
5056
if e == l.tail {
5157
if l.len == 1 {
5258
l.tail = nil

0 commit comments

Comments
 (0)