-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathstd.tree.html
More file actions
544 lines (446 loc) · 18 KB
/
std.tree.html
File metadata and controls
544 lines (446 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
<title>stdlib 41.2.2 Reference</title>
<link rel="stylesheet" href="../ldoc.css" type="text/css" />
</head>
<body>
<div id="container">
<div id="product">
<div id="product_logo"></div>
<div id="product_name"><big><b></b></big></div>
<div id="product_description"></div>
</div> <!-- id="product" -->
<div id="main">
<!-- Menu -->
<div id="navigation">
<br/>
<h1>stdlib 41.2.2</h1>
<ul>
<li><a href="../index.html">Index</a></li>
</ul>
<h2>Contents</h2>
<ul>
<li><a href="#Objects">Objects</a></li>
<li><a href="#Functions">Functions</a></li>
<li><a href="#Metamethods">Metamethods</a></li>
</ul>
<h2>Classes</h2>
<ul class="nowrap">
<li><a href="../classes/std.container.html">std.container</a></li>
<li><a href="../classes/std.list.html">std.list</a></li>
<li><a href="../classes/std.object.html">std.object</a></li>
<li><a href="../classes/std.optparse.html">std.optparse</a></li>
<li><a href="../classes/std.set.html">std.set</a></li>
<li><a href="../classes/std.strbuf.html">std.strbuf</a></li>
<li><strong>std.tree</strong></li>
</ul>
<h2>Modules</h2>
<ul class="nowrap">
<li><a href="../modules/std.html">std</a></li>
<li><a href="../modules/std.debug.html">std.debug</a></li>
<li><a href="../modules/std.functional.html">std.functional</a></li>
<li><a href="../modules/std.io.html">std.io</a></li>
<li><a href="../modules/std.math.html">std.math</a></li>
<li><a href="../modules/std.operator.html">std.operator</a></li>
<li><a href="../modules/std.package.html">std.package</a></li>
<li><a href="../modules/std.strict.html">std.strict</a></li>
<li><a href="../modules/std.string.html">std.string</a></li>
<li><a href="../modules/std.table.html">std.table</a></li>
</ul>
</div>
<div id="content">
<h1>Class <code>std.tree</code></h1>
<p>Tree container prototype.</p>
<p>
<p> Note that Functions listed below are only available from the Tree
prototype returned by requiring this module, because Container objects
cannot have object methods.</p>
<h2> Prototype Chain</h2>
<pre>
<span class="global">table</span>
<span class="backtick">`-> Object
`</span>-> Container
`-> Tree
</pre>
</p>
<h3>See also:</h3>
<ul>
<a href="../classes/std.container.html#">std.container</a>
</ul>
<h2><a href="#Objects">Objects</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#std.tree.Tree">std.tree.Tree</a></td>
<td class="summary">Tree prototype object.</td>
</tr>
</table>
<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#std.tree.clone">std.tree.clone (t, nometa)</a></td>
<td class="summary">Make a deep copy of a tree, including any metatables.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#std.tree.ileaves">std.tree.ileaves (tr)</a></td>
<td class="summary">Tree iterator which returns just numbered leaves, in order.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#std.tree.inodes">std.tree.inodes (tr)</a></td>
<td class="summary">Tree iterator over numbered nodes, in order.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#std.tree.leaves">std.tree.leaves (t)</a></td>
<td class="summary">Tree iterator which returns just leaves.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#std.tree.merge">std.tree.merge (t, u)</a></td>
<td class="summary">Destructively deep-merge one tree into another.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#std.tree.nodes">std.tree.nodes (tr)</a></td>
<td class="summary">Tree iterator over all nodes.</td>
</tr>
</table>
<h2><a href="#Metamethods">Metamethods</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#std.tree.__index">std.tree.__index (tr, i)</a></td>
<td class="summary">Deep retrieval.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#std.tree.__newindex">std.tree.__newindex (tr, i[, v])</a></td>
<td class="summary">Deep insertion.</td>
</tr>
</table>
<br/>
<br/>
<h2 class="section-header "><a name="Objects"></a>Objects</h2>
<dl class="function">
<dt>
<a name = "std.tree.Tree"></a>
<strong>std.tree.Tree</strong>
</dt>
<dd>
Tree prototype object.
<h3>Fields:</h3>
<ul>
<li><span class="parameter">_type</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.4">string</a></span>
object name
(<em>default</em> "Tree")
</li>
</ul>
<h3>See also:</h3>
<ul>
<li><a href="../classes/std.container.html#">std.container</a></li>
<li><a href="../classes/std.object.html#std.object:__call">std.object.__call</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> std = <span class="global">require</span> <span class="string">"std"</span>
<span class="keyword">local</span> Tree = std.tree {}
<span class="keyword">local</span> tr = Tree {}
tr[{<span class="string">"branch1"</span>, <span class="number">1</span>}] = <span class="string">"leaf1"</span>
tr[{<span class="string">"branch1"</span>, <span class="number">2</span>}] = <span class="string">"leaf2"</span>
tr[{<span class="string">"branch2"</span>, <span class="number">1</span>}] = <span class="string">"leaf3"</span>
<span class="global">print</span> (tr[{<span class="string">"branch1"</span>}]) <span class="comment">--> Tree {leaf1, leaf2}
</span><span class="global">print</span> (tr[{<span class="string">"branch1"</span>, <span class="number">2</span>}]) <span class="comment">--> leaf2
</span><span class="global">print</span> (tr[{<span class="string">"branch1"</span>, <span class="number">3</span>}]) <span class="comment">--> nil
</span><span class="comment">--> leaf1 leaf2 leaf3
</span><span class="keyword">for</span> leaf <span class="keyword">in</span> std.tree.leaves (tr) <span class="keyword">do</span>
<span class="global">io</span>.write (leaf .. <span class="string">"\t"</span>)
<span class="keyword">end</span></pre>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="Functions"></a>Functions</h2>
Methods
<dl class="function">
<dt>
<a name = "std.tree.clone"></a>
<strong>std.tree.clone (t, nometa)</strong>
</dt>
<dd>
Make a deep copy of a tree, including any metatables.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
tree or tree-like table
</li>
<li><span class="parameter">nometa</span>
<span class="types"><span class="type">boolean</span></span>
if non-<code>nil</code> don't copy metatables
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="../classes/std.tree.html#std.tree.Tree">Tree</a> or <a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
a deep copy of <em>tr</em>
</ol>
<h3>See also:</h3>
<ul>
<a href="../modules/std.table.html#clone">std.table.clone</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example">tr = {<span class="string">"one"</span>, {two=<span class="number">2</span>}, {{<span class="string">"three"</span>}, four=<span class="number">4</span>}}
copy = clone (tr)
copy[<span class="number">2</span>].two=<span class="number">5</span>
<span class="global">assert</span> (tr[<span class="number">2</span>].two == <span class="number">2</span>)</pre>
</ul>
</dd>
<dt>
<a name = "std.tree.ileaves"></a>
<strong>std.tree.ileaves (tr)</strong>
</dt>
<dd>
Tree iterator which returns just numbered leaves, in order.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">tr</span>
<span class="types"><a class="type" href="../classes/std.tree.html#std.tree.Tree">Tree</a> or <a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
tree or tree-like table
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
<span class="types"><span class="type">function</span></span>
iterator function</li>
<li>
<span class="types"><a class="type" href="../classes/std.tree.html#std.tree.Tree">Tree</a> or <a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
the tree <em>tr</em></li>
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../classes/std.tree.html#std.tree.inodes">inodes</a></li>
<li><a href="../classes/std.tree.html#std.tree.leaves">leaves</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">--> t = {"one", "three", "five"}
</span><span class="keyword">for</span> leaf <span class="keyword">in</span> ileaves {<span class="string">"one"</span>, {two=<span class="number">2</span>}, {{<span class="string">"three"</span>}, four=<span class="number">4</span>}}, foo=<span class="string">"bar"</span>, <span class="string">"five"</span>}
<span class="keyword">do</span>
t[#t + <span class="number">1</span>] = leaf
<span class="keyword">end</span></pre>
</ul>
</dd>
<dt>
<a name = "std.tree.inodes"></a>
<strong>std.tree.inodes (tr)</strong>
</dt>
<dd>
Tree iterator over numbered nodes, in order. </p>
<p> The iterator function behaves like <a href="../classes/std.tree.html#std.tree.nodes">nodes</a>, but only traverses the
array part of the nodes of <em>tr</em>, ignoring any others.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">tr</span>
<span class="types"><a class="type" href="../classes/std.tree.html#std.tree.Tree">Tree</a> or <a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
tree or tree-like table to iterate over
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
<span class="types"><span class="type">function</span></span>
iterator function</li>
<li>
<span class="types"><a class="type" href="../classes/std.tree.html">tree</a> or <a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
the tree, <em>tr</em></li>
</ol>
<h3>See also:</h3>
<ul>
<a href="../classes/std.tree.html#std.tree.nodes">nodes</a>
</ul>
</dd>
<dt>
<a name = "std.tree.leaves"></a>
<strong>std.tree.leaves (t)</strong>
</dt>
<dd>
Tree iterator which returns just leaves.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
tree or tree-like table
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
<span class="types"><span class="type">function</span></span>
iterator function</li>
<li>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
<em>t</em></li>
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../classes/std.tree.html#std.tree.ileaves">ileaves</a></li>
<li><a href="../classes/std.tree.html#std.tree.nodes">nodes</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">for</span> leaf <span class="keyword">in</span> leaves {<span class="string">"one"</span>, {two=<span class="number">2</span>}, {{<span class="string">"three"</span>}, four=<span class="number">4</span>}}, foo=<span class="string">"bar"</span>, <span class="string">"five"</span>}
<span class="keyword">do</span>
t[#t + <span class="number">1</span>] = leaf
<span class="keyword">end</span>
<span class="comment">--> t = {2, 4, "five", "foo", "one", "three"}
</span><span class="global">table</span>.sort (t, lambda <span class="string">"=tostring(_1) < tostring(_2)"</span>)</pre>
</ul>
</dd>
<dt>
<a name = "std.tree.merge"></a>
<strong>std.tree.merge (t, u)</strong>
</dt>
<dd>
Destructively deep-merge one tree into another.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
destination tree
</li>
<li><span class="parameter">u</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
table with nodes to merge
</li>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
<em>t</em> with nodes from <em>u</em> merged in
</ol>
<h3>See also:</h3>
<ul>
<a href="../modules/std.table.html#merge">std.table.merge</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example">merge (dest, {{exists=<span class="number">1</span>}, {{<span class="keyword">not</span> = {present = { inside = <span class="string">"dest"</span> }}}}})</pre>
</ul>
</dd>
<dt>
<a name = "std.tree.nodes"></a>
<strong>std.tree.nodes (tr)</strong>
</dt>
<dd>
Tree iterator over all nodes. </p>
<p> The returned iterator function performs a depth-first traversal of
<code>tr</code>, and at each node it returns <code>{node-type, tree-path, tree-node}</code>
where <code>node-type</code> is <code>branch</code>, <code>join</code> or <code>leaf</code>; <code>tree-path</code> is a
list of keys used to reach this node, and <code>tree-node</code> is the current
node.</p>
<p> Note that the <code>tree-path</code> reuses the same table on each iteration, so
you must <code>table.clone</code> a copy if you want to take a snap-shot of the
current state of the <code>tree-path</code> list before the next iteration
changes it.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">tr</span>
<span class="types"><a class="type" href="../classes/std.tree.html#std.tree.Tree">Tree</a> or <a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
tree or tree-like table to iterate over
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
<span class="types"><span class="type">function</span></span>
iterator function</li>
<li>
<span class="types"><a class="type" href="../classes/std.tree.html#std.tree.Tree">Tree</a> or <a class="type" href="https://www.lua.org/manual/5.3/manual.html#6.6">table</a></span>
the tree, <em>tr</em></li>
</ol>
<h3>See also:</h3>
<ul>
<a href="../classes/std.tree.html#std.tree.inodes">inodes</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">-- tree = +-- node1
</span><span class="comment">-- | +-- leaf1
</span><span class="comment">-- | '-- leaf2
</span><span class="comment">-- '-- leaf 3
</span>tree = Tree { Tree { <span class="string">"leaf1"</span>, <span class="string">"leaf2"</span>}, <span class="string">"leaf3"</span> }
<span class="keyword">for</span> node_type, path, node <span class="keyword">in</span> nodes (tree) <span class="keyword">do</span>
<span class="global">print</span> (node_type, path, node)
<span class="keyword">end</span>
<span class="comment">--> "branch" {} {{"leaf1", "leaf2"}, "leaf3"}
</span><span class="comment">--> "branch" {1} {"leaf1", "leaf"2")
</span><span class="comment">--> "leaf" {1,1} "leaf1"
</span><span class="comment">--> "leaf" {1,2} "leaf2"
</span><span class="comment">--> "join" {1} {"leaf1", "leaf2"}
</span><span class="comment">--> "leaf" {2} "leaf3"
</span><span class="comment">--> "join" {} {{"leaf1", "leaf2"}, "leaf3"}
</span><span class="global">os</span>.exit (<span class="number">0</span>)</pre>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="Metamethods"></a>Metamethods</h2>
<dl class="function">
<dt>
<a name = "std.tree.__index"></a>
<strong>std.tree.__index (tr, i)</strong>
</dt>
<dd>
Deep retrieval.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">tr</span>
<span class="types"><a class="type" href="../classes/std.tree.html#std.tree.Tree">Tree</a></span>
a tree
</li>
<li><span class="parameter">i</span>
non-table, or list of keys <code>{i1, ...i_n}</code>
</li>
</ul>
<h3>Returns:</h3>
<ol>
<code>tr[i1]...[i_n]</code> if <em>i</em> is a key list, <code>tr[i]</code> otherwise
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example">del_other_window = keymap[{<span class="string">"C-x"</span>, <span class="string">"4"</span>, KEY_DELETE}]</pre>
</ul>
</dd>
<dt>
<a name = "std.tree.__newindex"></a>
<strong>std.tree.__newindex (tr, i[, v])</strong>
</dt>
<dd>
Deep insertion.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">tr</span>
<span class="types"><a class="type" href="../classes/std.tree.html#std.tree.Tree">Tree</a></span>
a tree
</li>
<li><span class="parameter">i</span>
non-table, or list of keys <code>{i1, ...i_n}</code>
</li>
<li><span class="parameter">v</span>
value
(<em>optional</em>)
</li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">function</span> bindkey (keylist, fn) keymap[keylist] = fn <span class="keyword">end</span></pre>
</ul>
</dd>
</dl>
</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2018-09-16 19:20:25 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>