Fastest/shortest way to build unique tree in Ruby? -
The fastest / least / one-liner (not possible: P) ways to make a unique tree tree Where many elements are repeated / missing in some nodes, there is a set of nodes in the tree (which we use this algorithm to find out that we do not have to manually do it).
This can be XML / JSON (hash), or anything like this:
root {nodes {node {} nodeb {all node { }}} Nodes {node a {subnode {}} nodeb {all nodex {}}}}
... it has been modified:
root {nodes {node A {subnode {}} nodeb {all node {subNodeX}}}}
< P> With XML: & lt; Root & gt; & Lt; Nodes & gt; & Lt; NodeA / & gt; & Lt; NodeB & gt; & Lt; SubNodeA / & gt; & Lt; / NodeB & gt; & Lt; / Nodes & gt; & Lt; Nodes & gt; & Lt; NodeA & gt; & Lt; SubNodeA / & gt; & Lt; / NodeA & gt; & Lt; NodeB & gt; & Lt; SubNodeX / & gt; & Lt; / NodeB & gt; & Lt; / Nodes & gt; & Lt; / Root & gt;
& lt; Root & gt; & Lt; Nodes & gt; & Lt; NodeA & gt; & Lt; SubNodeA / & gt; & Lt; / NodeA & gt; & Lt; NodeB & gt; & Lt; SubNodeA / & gt; & Lt; SubNodeX / & gt; & Lt; / NodeB & gt; & Lt; / Nodes & gt;
The xml / json files may be well (1 MB +), so each element moves deeper It seems to be - before or something like it may take some time, it can also be small as an example above.
This requires a set of unique paths:
'' Set 'xml = Nokogirl :: XML.parse (your_data) path = Set.new xml.traverse {| Node | Next if node.Text? Path & lt; & Lt; Node.path.gsub (/ \ [\ d + \] /, ""). Sub (/ \ / $ /, "")}
Do you have to start this?
[Answer to question in comment]
Adding an attribute-path is also easy, but let's at least a bit-line:
Xml .traverse do | Node | Next if node.Text? Path & lt; & Lt; (Npath = node.path.gsub (/ \ [\ d + \] /, ""). Sub (/ \ / $ /, "")) path + = node.dates.map {| K, v | "# {Npath} @ # {k}"} End
Comments
Post a Comment