1
- package evcache
1
+ package ringlist
2
2
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.
4
10
type ListElement [E any ] interface {
5
11
Link (E )
6
12
Unlink ()
7
13
Next () E
8
14
Prev () E
9
15
}
10
16
11
- // RingList is a circular doubly linked list.
17
+ // List is a generic circular doubly linked list.
12
18
// The zero value is a ready to use empty list.
13
- type RingList [T any , E interface {
19
+ type List [T any , E interface {
14
20
* T
15
21
ListElement [E ]
16
22
}] struct {
@@ -19,25 +25,25 @@ type RingList[T any, E interface {
19
25
}
20
26
21
27
// 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 {
23
29
return l .len
24
30
}
25
31
26
32
// 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 {
28
34
if l .len == 0 {
29
35
return nil
30
36
}
31
37
return l .tail .Next ()
32
38
}
33
39
34
40
// 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 {
36
42
return l .tail
37
43
}
38
44
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 ) {
41
47
if l .tail != nil {
42
48
l .tail .Link (e )
43
49
}
@@ -46,7 +52,7 @@ func (l *RingList[T, E]) PushBack(e E) {
46
52
}
47
53
48
54
// Remove an element from the list.
49
- func (l * RingList [T , E ]) Remove (e E ) {
55
+ func (l * List [T , E ]) Remove (e E ) {
50
56
if e == l .tail {
51
57
if l .len == 1 {
52
58
l .tail = nil
0 commit comments