Skip to content

Commit 2c8b4e4

Browse files
committed
Document LinkedList
1 parent da4df2a commit 2c8b4e4

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

doc/LinkedList.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<class name="LinkedList" inherits="Reference" version="3.2">
33
<brief_description>
4+
A doubly linked list data structure.
45
</brief_description>
56
<description>
7+
A data structure which consists of a set of sequentially linked elements called nodes. Uses [ListNode] as a basic building block. Each node contains a reference to the previous node, the next node, and the data associated with the node. Insertion and deletion operations are faster [code]O(1)[/code] compared to [Array], but performs worse for random access [code]O(n)[/code].
8+
[ListNode]s are constructed by inserting values to the list, and are not meant to be instantiated directly:
9+
[codeblock]
10+
var list = LinkedList.new()
11+
var node = list.push_back("Goost")
12+
var same_node = list.find("Goost")
13+
[/codeblock]
14+
Traversing a list can be done using a [code]for[/code] loop:
15+
[codeblock]
16+
for node in list:
17+
print(node)
18+
[/codeblock]
19+
or by manually walking the list using the nodes themselves:
20+
[codeblock]
21+
# Forward!
22+
var node = list.front
23+
while node:
24+
print(node)
25+
node = node.next
26+
27+
# Backward!
28+
var node = list.back
29+
while node:
30+
print(node)
31+
node = node.prev
32+
[/codeblock]
33+
Nodes can be passed around throughout the code, and values can be changed dynamically for nodes which are already inserted into the list.
634
</description>
735
<tutorials>
836
</tutorials>
@@ -11,6 +39,7 @@
1139
<return type="void">
1240
</return>
1341
<description>
42+
Erases all nodes from the list.
1443
</description>
1544
</method>
1645
<method name="create_from">
@@ -19,12 +48,18 @@
1948
<argument index="0" name="value" type="Variant">
2049
</argument>
2150
<description>
51+
Initializes the list from a [Variant] compatible type. Clears all nodes before copying.
52+
If [code]value[/code] is [code]null[/code], just clears the contents of the list.
53+
If [code]value[/code] is [Array], each element in the array is converted to a [ListNode]. Pool*Arrays are converted similarly to [Array].
54+
If [code]value[/code] is [Dictionary], each key in the dictionary is converted to a [ListNode], and the values are encoded as [ListNode] meta variables using [method Object.set_meta]. Values can be retrieved later with [code]node.get_meta("value")[/code] for each node.
55+
Any other type is simply pushed back to the list.
2256
</description>
2357
</method>
2458
<method name="empty" qualifiers="const">
2559
<return type="bool">
2660
</return>
2761
<description>
62+
Returns [code]true[/code] if the list doesn't contain any nodes.
2863
</description>
2964
</method>
3065
<method name="erase">
@@ -33,6 +68,7 @@
3368
<argument index="0" name="value" type="Variant">
3469
</argument>
3570
<description>
71+
Erases (deletes) the first found node with a matching value in the list.
3672
</description>
3773
</method>
3874
<method name="find">
@@ -41,18 +77,21 @@
4177
<argument index="0" name="value" type="Variant">
4278
</argument>
4379
<description>
80+
Returns a node if a list contains a node with specified value, otherwise returns [code]null[/code].
4481
</description>
4582
</method>
4683
<method name="get_elements">
4784
<return type="Array">
4885
</return>
4986
<description>
87+
An alias for [method get_nodes].
5088
</description>
5189
</method>
5290
<method name="get_nodes">
5391
<return type="Array">
5492
</return>
5593
<description>
94+
Returns all nodes as an [Array], preserving front-to-back order.
5695
</description>
5796
</method>
5897
<method name="insert_after">
@@ -63,6 +102,7 @@
63102
<argument index="1" name="value" type="Variant">
64103
</argument>
65104
<description>
105+
Constructs a new [ListNode] and places it [i]after[/i] existing node in the list. If [code]node[/code] is [code]null[/code], then the value is pushed at the end of the list, making the behavior equivalent to [method push_back].
66106
</description>
67107
</method>
68108
<method name="insert_before">
@@ -73,12 +113,14 @@
73113
<argument index="1" name="value" type="Variant">
74114
</argument>
75115
<description>
116+
Constructs a new [ListNode] and places it [i]before[/i] existing node in the list. If [code]node[/code] is [code]null[/code], then the value is pushed at the end of the list, making the behavior equivalent to [method push_back].
76117
</description>
77118
</method>
78119
<method name="invert">
79120
<return type="void">
80121
</return>
81122
<description>
123+
Inverts the order of nodes in the list.
82124
</description>
83125
</method>
84126
<method name="move_before">
@@ -89,6 +131,7 @@
89131
<argument index="1" name="before_node" type="ListNode">
90132
</argument>
91133
<description>
134+
Moves a node [i]before[/i] the other one within the list.
92135
</description>
93136
</method>
94137
<method name="move_to_back">
@@ -97,6 +140,7 @@
97140
<argument index="0" name="node" type="ListNode">
98141
</argument>
99142
<description>
143+
Moves a node to the back of the list ([member back] node will point to [code]node[/code]).
100144
</description>
101145
</method>
102146
<method name="move_to_front">
@@ -105,18 +149,29 @@
105149
<argument index="0" name="node" type="ListNode">
106150
</argument>
107151
<description>
152+
Moves a node to the front of the list (the [member front] node will point to [code]node[/code]).
108153
</description>
109154
</method>
110155
<method name="pop_back">
111156
<return type="void">
112157
</return>
113158
<description>
159+
Erases the last node of the list. Make sure to preserve the [member ListNode.value] if you're interested in the data associated with the node:
160+
[codeblock]
161+
var value = list.back.value
162+
list.pop_back()
163+
[/codeblock]
114164
</description>
115165
</method>
116166
<method name="pop_front">
117167
<return type="void">
118168
</return>
119169
<description>
170+
Erases the first node of the list. Make sure to preserve the [member ListNode.value] if you're interested in the data associated with the node:
171+
[codeblock]
172+
var value = list.front.value
173+
list.pop_front()
174+
[/codeblock]
120175
</description>
121176
</method>
122177
<method name="push_back">
@@ -125,6 +180,7 @@
125180
<argument index="0" name="value" type="Variant">
126181
</argument>
127182
<description>
183+
Constructs a new [ListNode] and pushes it at the [i]end[/i] of the list.
128184
</description>
129185
</method>
130186
<method name="push_front">
@@ -133,6 +189,7 @@
133189
<argument index="0" name="value" type="Variant">
134190
</argument>
135191
<description>
192+
Constructs a new [ListNode] and pushes it at the [i]beginning[/i] of the list.
136193
</description>
137194
</method>
138195
<method name="remove">
@@ -141,18 +198,21 @@
141198
<argument index="0" name="node" type="ListNode">
142199
</argument>
143200
<description>
201+
Removes (and deletes) an existing node from the list.
144202
</description>
145203
</method>
146204
<method name="size" qualifiers="const">
147205
<return type="int">
148206
</return>
149207
<description>
208+
Returns the total number of nodes in the list.
150209
</description>
151210
</method>
152211
<method name="sort">
153212
<return type="void">
154213
</return>
155214
<description>
215+
Sorts the list in alphabetical order if the list contains [String]s. If the list contains nodes with different types of values, these are sorted according to the order of [enum @GlobalScope.Variant.Type].
156216
</description>
157217
</method>
158218
<method name="swap">
@@ -163,13 +223,16 @@
163223
<argument index="1" name="node_B" type="ListNode">
164224
</argument>
165225
<description>
226+
Moves [code]node_A[/code] to the position of [code]node_B[/code], and moves [code]node_B[/code] to the original position of [code]node_A[/code]. If [code]node_A == node_B[/code], does nothing.
166227
</description>
167228
</method>
168229
</methods>
169230
<members>
170231
<member name="back" type="ListNode" setter="" getter="get_back">
232+
The last node in the list. Can be [code]null[/code] if the list is [method empty].
171233
</member>
172234
<member name="front" type="ListNode" setter="" getter="get_front">
235+
The first node in the list. Can be [code]null[/code] if the list is [method empty].
173236
</member>
174237
</members>
175238
<constants>

tests/project/goost/core/types/test_list.gd

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ func test_insert_after_back():
136136
assert_eq(list.back.value, "Godot")
137137

138138

139+
func test_insert_after_null():
140+
populate_test_data(list)
141+
list.insert_after(null, "Godot")
142+
assert_eq(list.back.value, "Godot")
143+
144+
145+
func test_insert_before_null():
146+
populate_test_data(list)
147+
list.insert_before(null, "Godot")
148+
# Not sure about this, see issue upstream from which this was ported:
149+
# https://github.com/godotengine/godot/issues/42116
150+
# But consistency with builtin List<Variant> is more important currently.
151+
assert_eq(list.back.value, "Godot")
152+
153+
139154
func test_size():
140155
var nodes = populate_test_data(list)
141156
var original_size = nodes.size()
@@ -443,7 +458,7 @@ func test_custom_iterators():
443458
gut.p(node)
444459

445460

446-
func tets_sort():
461+
func tets_sort_strings():
447462
var n: ListNode
448463
n = list.push_back("B")
449464
assert_eq(n.value, "B")
@@ -469,6 +484,28 @@ func tets_sort():
469484
assert_eq(abcd[i], abcd_list[i])
470485

471486

487+
func test_sort_variants():
488+
list.push_back("Goost")
489+
list.push_back(Color.blue)
490+
list.push_back(37)
491+
list.push_back(Vector2.ONE)
492+
list.push_back(true)
493+
list.push_back(null)
494+
list.push_back(100.0)
495+
496+
list.sort()
497+
498+
var nodes = list.get_nodes()
499+
500+
assert_eq(nodes[0].value, null)
501+
assert_eq(nodes[1].value, true)
502+
assert_eq(nodes[2].value, 37)
503+
assert_eq(nodes[3].value, 100.0)
504+
assert_eq(nodes[4].value, "Goost")
505+
assert_eq(nodes[5].value, Vector2.ONE)
506+
assert_eq(nodes[6].value, Color.blue)
507+
508+
472509
func test_print_list_node():
473510
var node = list.push_back("Goost")
474511
gut.p(node)

0 commit comments

Comments
 (0)