• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 11393106485

17 Oct 2024 09:10PM UTC coverage: 57.848% (-1.0%) from 58.81%
11393106485

Pull #9148

github

ProofOfKeags
lnwire: convert DynPropose and DynCommit to use typed tlv records
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

142 of 177 new or added lines in 4 files covered. (80.23%)

18983 existing lines in 242 files now uncovered.

99003 of 171143 relevant lines covered (57.85%)

36968.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.74
/fn/list.go
1
// Copyright (c) 2009 The Go Authors. All rights reserved.
2
// Copyright (c) 2024 Lightning Labs and the Lightning Network Developers
3

4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
6
// met:
7

8
//    * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
//    * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
13
// distribution.
14
//    * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
17

18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
package fn
30

31
type Node[A any] struct {
32
        // prev is a pointer to the previous node in the List.
33
        prev *Node[A]
34

35
        // next is a pointer to the next node in the List.
36
        next *Node[A]
37

38
        // list is the root pointer to the List in which this node is located.
39
        list *List[A]
40

41
        // Value is the actual data contained within the Node.
42
        Value A
43
}
44

45
// Next returns the next list node or nil.
46
func (e *Node[A]) Next() *Node[A] {
5,705,557✔
47
        if e.list == nil {
5,705,558✔
48
                return nil
1✔
49
        }
1✔
50

51
        if e.next == &e.list.root {
5,762,310✔
52
                return nil
56,754✔
53
        }
56,754✔
54

55
        return e.next
5,648,802✔
56
}
57

58
// Prev returns the previous list node or nil.
59
func (e *Node[A]) Prev() *Node[A] {
13,074✔
60
        if e.list == nil {
13,075✔
61
                return nil
1✔
62
        }
1✔
63

64
        if e.prev == &e.list.root {
13,204✔
65
                return nil
131✔
66
        }
131✔
67

68
        return e.prev
12,942✔
69
}
70

71
// List represents a doubly linked list.
72
// The zero value for List is an empty list ready to use.
73
type List[A any] struct {
74
        // root is a sentinal Node to identify the head and tail of the list.
75
        // root.prev is the tail, root.next is the head. For the purposes of
76
        // elegance, the absence of a next or prev node is encoded as the
77
        // address of the root node.
78
        root Node[A]
79

80
        // len is the current length of the list.
81
        len int
82
}
83

84
// Init intializes or clears the List l.
85
func (l *List[A]) Init() *List[A] {
5,141✔
86
        l.root.next = &l.root
5,141✔
87
        l.root.prev = &l.root
5,141✔
88
        l.len = 0
5,141✔
89

5,141✔
90
        return l
5,141✔
91
}
5,141✔
92

93
// lazyInit lazily initializes a zero List value. It is called by other public
94
// functions that could feasibly be called on a List that was initialized by the
95
// raw List[A]{} constructor.
96
func (l *List[A]) lazyInit() {
256,782✔
97
        if l.root.next == nil {
256,790✔
98
                l.Init()
8✔
99
        }
8✔
100
}
101

102
// insert inserts n after predecessor, increments l.len, and returns n.
103
func (l *List[A]) insert(n *Node[A], predecessor *Node[A]) *Node[A] {
283,067✔
104
        // Make n point to correct neighborhood.
283,067✔
105
        n.prev = predecessor
283,067✔
106
        n.next = predecessor.next
283,067✔
107

283,067✔
108
        // Make neighborhood point to n.
283,067✔
109
        n.prev.next = n
283,067✔
110
        n.next.prev = n
283,067✔
111

283,067✔
112
        // Make n part of the list.
283,067✔
113
        n.list = l
283,067✔
114

283,067✔
115
        // Increment list length.
283,067✔
116
        l.len++
283,067✔
117

283,067✔
118
        return n
283,067✔
119
}
283,067✔
120

121
// insertVal is a convenience wrapper for
122
// insert(&Node[A]{Value: v}, predecessor).
123
func (l *List[A]) insertVal(a A, predecessor *Node[A]) *Node[A] {
283,067✔
124
        return l.insert(&Node[A]{Value: a}, predecessor)
283,067✔
125
}
283,067✔
126

127
// move removes n from its current position and inserts it as the successor to
128
// predecessor.
129
func (l *List[A]) move(n *Node[A], predecessor *Node[A]) {
808✔
130
        if n == predecessor {
817✔
131
                return // Can't move after itself.
9✔
132
        }
9✔
133

134
        if predecessor.next == n {
813✔
135
                return // Nothing to be done.
14✔
136
        }
14✔
137

138
        // Bind previous and next to each other.
139
        n.prev.next = n.next
785✔
140
        n.next.prev = n.prev
785✔
141

785✔
142
        // Make n point to new neighborhood.
785✔
143
        n.prev = predecessor
785✔
144
        n.next = predecessor.next
785✔
145

785✔
146
        // Make new neighborhood point to n.
785✔
147
        n.prev.next = n
785✔
148
        n.next.prev = n
785✔
149
}
150

151
// New returns an initialized List.
152
func NewList[A any]() *List[A] {
5,133✔
153
        l := List[A]{}
5,133✔
154
        return l.Init()
5,133✔
155
}
5,133✔
156

157
// Len returns the number of elements of List l.
158
// The complexity is O(1).
159
func (l *List[A]) Len() int {
55,023✔
160
        return l.len
55,023✔
161
}
55,023✔
162

163
// Front returns the first Node of List l or nil if the list is empty.
164
func (l *List[A]) Front() *Node[A] {
144,652✔
165
        if l.len == 0 {
166,031✔
166
                return nil
21,379✔
167
        }
21,379✔
168

169
        return l.root.next
123,273✔
170
}
171

172
// Back returns the last Node of List l or nil if the list is empty.
173
func (l *List[A]) Back() *Node[A] {
227,506✔
174
        if l.len == 0 {
227,509✔
175
                return nil
3✔
176
        }
3✔
177

178
        return l.root.prev
227,503✔
179
}
180

181
// Remove removes Node n from List l if n is an element of List l.
182
// It returns the Node value e.Value.
183
// The Node must not be nil.
184
func (l *List[A]) Remove(n *Node[A]) A {
9,414✔
185
        if n.list == l {
18,826✔
186
                n.prev.next = n.next
9,412✔
187
                n.next.prev = n.prev
9,412✔
188
                l.len--
9,412✔
189

9,412✔
190
                v := n.Value
9,412✔
191
                // Set all node data to nil to prevent dangling references.
9,412✔
192
                *n = Node[A]{Value: v}
9,412✔
193

9,412✔
194
                return v
9,412✔
195
        }
9,412✔
196

197
        return n.Value
2✔
198
}
199

200
// PushFront inserts a new Node n with value a at the front of List l and
201
// returns n.
202
func (l *List[A]) PushFront(a A) *Node[A] {
101✔
203
        l.lazyInit()
101✔
204
        return l.insertVal(a, &l.root)
101✔
205
}
101✔
206

207
// PushBack inserts a new Node n with value a at the back of List l and returns
208
// n.
209
func (l *List[A]) PushBack(a A) *Node[A] {
256,471✔
210
        l.lazyInit()
256,471✔
211
        return l.insertVal(a, l.root.prev)
256,471✔
212
}
256,471✔
213

214
// InsertBefore inserts a new Node n with value a immediately before successor
215
// and returns n. If successor is not an element of l, the list is not
216
// modified. The successor must not be nil.
217
func (l *List[A]) InsertBefore(a A, successor *Node[A]) *Node[A] {
205✔
218
        if successor == nil {
205✔
UNCOV
219
                return l.insertVal(a, &l.root)
×
UNCOV
220
        }
×
221

222
        if successor.list != l {
206✔
223
                return nil
1✔
224
        }
1✔
225

226
        return l.insertVal(a, successor.prev)
204✔
227
}
228

229
// InsertAfter inserts a new Node n with value a immediately after  and returns
230
// e. If predecessor is not an element of l, the list is not modified. The
231
// predecessor must not be nil.
232
func (l *List[A]) InsertAfter(a A, predecessor *Node[A]) *Node[A] {
204✔
233
        if predecessor == nil {
206✔
234
                return l.insertVal(a, l.root.prev)
2✔
235
        }
2✔
236

237
        if predecessor.list != l {
203✔
238
                return nil
1✔
239
        }
1✔
240

241
        return l.insertVal(a, predecessor)
201✔
242
}
243

244
// MoveToFront moves Node n to the front of List l.
245
// If n is not an element of l, the list is not modified.
246
// The Node must not be nil.
247
func (l *List[A]) MoveToFront(n *Node[A]) {
203✔
248
        if n.list == l {
406✔
249
                l.move(n, &l.root)
203✔
250
        }
203✔
251
}
252

253
// MoveToBack moves Node n to the back of List l.
254
// If n is not an element of l, the list is not modified.
255
// The Node must not be nil.
256
func (l *List[A]) MoveToBack(n *Node[A]) {
201✔
257
        if n.list == l {
402✔
258
                l.move(n, l.root.prev)
201✔
259
        }
201✔
260
}
261

262
// MoveBefore moves Node n to its new position before successor.
263
// If n or successor is not an element of l, or n == successor, the list is not
264
// modified. The Node and successor must not be nil.
265
func (l *List[A]) MoveBefore(n, successor *Node[A]) {
203✔
266
        if n.list == l && successor.list == l {
405✔
267
                l.move(n, successor.prev)
202✔
268
        }
202✔
269
}
270

271
// MoveAfter moves Node n to its new position after predecessor.
272
// If n or predecessor is not an element of l, or n == predecessor, the list is
273
// not modified. The Node and predecessor must not be nil.
274
func (l *List[A]) MoveAfter(n, predecessor *Node[A]) {
203✔
275
        if n.list == l && predecessor.list == l {
405✔
276
                l.move(n, predecessor)
202✔
277
        }
202✔
278
}
279

280
// PushBackList inserts a copy of List other at the back of List l.
281
// The Lists l and other may be the same. They must not be nil.
282
func (l *List[A]) PushBackList(other *List[A]) {
105✔
283
        l.lazyInit()
105✔
284
        n := other.Front()
105✔
285
        sz := other.Len()
105✔
286
        for i := 0; i < sz; i++ {
13,308✔
287
                l.insertVal(n.Value, l.root.prev)
13,203✔
288
                n = n.Next()
13,203✔
289
        }
13,203✔
290
}
291

292
// PushFrontList inserts a copy of List other at the front of List l.
293
// The Lists l and other may be the same. They must not be nil.
294
func (l *List[A]) PushFrontList(other *List[A]) {
105✔
295
        l.lazyInit()
105✔
296
        n := other.Back()
105✔
297
        sz := other.Len()
105✔
298
        for i := 0; i < sz; i++ {
12,990✔
299
                l.insertVal(n.Value, &l.root)
12,885✔
300
                n = n.Prev()
12,885✔
301
        }
12,885✔
302
}
303

304
// Filter gives a slice of all of the node values that satisfy the given
305
// predicate.
306
func (l *List[A]) Filter(f Pred[A]) []A {
400✔
307
        var acc []A
400✔
308

400✔
309
        for cursor := l.Front(); cursor != nil; cursor = cursor.Next() {
52,008✔
310
                a := cursor.Value
51,608✔
311
                if f(a) {
78,280✔
312
                        acc = append(acc, a)
26,672✔
313
                }
26,672✔
314
        }
315

316
        return acc
400✔
317
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc