Module: gears.sort
Utilities to sort and arrange data.
lib.gears.sort.topological Functions
:append (node, dependencies) |
Ensure that node appears after all dependencies .
|
|
:prepend (node, subordinates) |
Ensure that node appears before all subordinates .
|
|
:clone () -> gears.sort.topological | Create a copy of this topological sort. | |
:remove (node) | Remove a node from the topological map. | |
:sort () -> table or nil | Try to sort the nodes. | |
gears.sort.topological () | A topological sorting class. |
lib.gears.sort.topological Functions
- 🔗 :append (node, dependencies)
-
Ensure that
node
appears after alldependencies
.Parameters:
Name Type(s) Description node The node that edges are added to. dependencies table List of nodes that have to appear before node
. - 🔗 :prepend (node, subordinates)
-
Ensure that
node
appears before allsubordinates
.Parameters:
Name Type(s) Description node The node that edges are added to. subordinates table List of nodes that have to appear after node
. - 🔗 :clone () -> gears.sort.topological
-
Create a copy of this topological sort.
This is useful to backup it before adding elements that can potentially
have circular dependencies and thus render the original useless.
Returns:
-
gears.sort.topological
The cloned sorter object.
- 🔗 :remove (node)
-
Remove a node from the topological map.
Parameters:
Name Type(s) Description node The node - 🔗 :sort () -> table or nil
-
Try to sort the nodes.
Returns:
-
table
A sorted list of nodes
Or
- nil
- A node around which a loop exists
- 🔗 gears.sort.topological ()
-
A topological sorting class.
The object returned by this function allows to create simple dependency graphs. It can be used for decision making or ordering of complex sequences.
Usage example output:
The position #1 is: a The position #2 is: b The position #3 is: c The position #4 is: d The position #5 is: e The position #6 is: f
Usage:
local tsort = gears.sort.topological() tsort:prepend('a', { 'b' }) tsort:prepend('b', { 'c' }) tsort:prepend('c', { 'd' }) tsort:append('e', { 'd' }) tsort:append('f', { 'e', 'd' }) local res = assert(tsort:sort()) for k, v in ipairs(res) do print("The position #"..k.." is: "..v) end