Skip to content

Commit 18745c6

Browse files
committed
Restore list constructor
1 parent fc6e369 commit 18745c6

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

internal/backend/backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (b *Backend[K, V]) StoreTTL(key K, value V, ttl time.Duration) {
147147
// Note: unlike Fetch, Store never lets map readers
148148
// see uninitialized elements.
149149

150-
new := b.list.NewElement(Record[K, V]{})
150+
new := list.NewElement(Record[K, V]{})
151151
deadline := b.prepareDeadline(ttl)
152152
new.Value.Initialize(key, value, deadline)
153153
b.list.PushBackElem(new)
@@ -180,7 +180,7 @@ tryLoadStore:
180180
goto tryLoadStore
181181
}
182182

183-
new := b.list.NewElement(Record[K, V]{})
183+
new := list.NewElement(Record[K, V]{})
184184
new.Value.wg.Add(1)
185185
b.xmap[key] = new
186186
b.mu.Unlock()

list/element.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ type Element[V any] struct {
77
Value V
88
}
99

10+
// NewElement creates a new list element.
11+
func NewElement[V any](v V) *Element[V] {
12+
e := &Element[V]{
13+
Value: v,
14+
}
15+
e.next = e
16+
e.prev = e
17+
return e
18+
}
19+
1020
// Next returns the next element or nil if e is the last element in its list.
1121
func (e *Element[V]) Next() *Element[V] {
1222
if e != e.next && e.list != nil && e != e.list.tail {

list/list.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ type List[V any] struct {
88
len int
99
}
1010

11-
// NewElement creates a new element belonging to this list.
12-
func (l *List[V]) NewElement(v V) *Element[V] {
13-
e := &Element[V]{
14-
Value: v,
15-
list: l,
16-
}
17-
e.next = e
18-
e.prev = e
19-
return e
20-
}
21-
2211
// Len returns the number of elements in the list.
2312
func (l *List[V]) Len() int {
2413
return l.len
@@ -39,13 +28,14 @@ func (l *List[V]) Back() *Element[V] {
3928

4029
// PushBack inserts a value at the back of list l and returns the new element.
4130
func (l *List[V]) PushBack(value V) *Element[V] {
42-
e := l.NewElement(value)
31+
e := NewElement(value)
4332
l.PushBackElem(e)
4433
return e
4534
}
4635

4736
// PushBackElem inserts a new element at the back of list l.
4837
func (l *List[V]) PushBackElem(e *Element[V]) {
38+
e.list = l
4939
if l.tail != nil {
5040
l.tail.link(e)
5141
}
@@ -55,13 +45,14 @@ func (l *List[V]) PushBackElem(e *Element[V]) {
5545

5646
// PushFront inserts a value at the front of list l and returns the new element.
5747
func (l *List[V]) PushFront(value V) *Element[V] {
58-
e := l.NewElement(value)
48+
e := NewElement(value)
5949
l.PushFrontElem(e)
6050
return e
6151
}
6252

6353
// PushFrontElem inserts a new element at the front of list l.
6454
func (l *List[V]) PushFrontElem(e *Element[V]) {
55+
e.list = l
6556
if l.tail != nil {
6657
l.tail.link(e)
6758
} else {

0 commit comments

Comments
 (0)