Jump to content

Tree datastructures: Difference between revisions

(→‎{{header|Perl}}: native format / JSON)
Line 457:
- - comparison
...</pre>
 
=={{header|Phix}}==
The standard Phix sequence is perfect for handling all of these kinds of structures.
<lang Phix>function text_to_indent(string plain_text)
sequence lines = split(plain_text,"\n",no_empty:=true),
parents = {}
for i=1 to length(lines) do
string line = trim_tail(lines[i]),
text = trim_head(line)
integer indent = length(line)-length(text)
-- remove any completed parents
while length(parents) and indent<=parents[$] do
parents = parents[1..$-1]
end while
-- append potential new parent
parents &= indent
integer depth = length(parents)
lines[i] = {depth,text}
end for
return lines
end function
 
function indent_to_nested(sequence indent, integer idx=1, level=1)
sequence res = {}
while idx<=length(indent) do
{integer lvl, string text} = indent[idx]
if lvl<level then exit end if
{sequence children,idx} = indent_to_nested(indent,idx+1,level+1)
res = append(res,{text,children})
end while
return {res,idx}
end function
 
function nested_to_indent(sequence nested, integer level=1)
sequence res = {}
for i=1 to length(nested) do
{string text, sequence children} = nested[i]
res = append(res,{level,text})
res &= nested_to_indent(children,level+1)
end for
return res
end function
 
constant text = """
RosettaCode
encourages
code
diversity
comparison
discourages
emphasising execution speed
code-golf.io
encourages
golfing
discourages
comparison""",
indent = text_to_indent(text),
nested = indent_to_nested(indent)[1]
 
puts(1,"Indent form:\n")
pp(indent,{pp_Nest,1})
puts(1,"\nNested form:\n")
pp(nested,{pp_Nest,8})
sequence r2i = nested_to_indent(nested)
printf(1,"\nNested to indent:%s\n",{iff(r2i==indent?"same":"***ERROR***")})</lang>
{{out}}
<pre>
Indent form:
{{1, `RosettaCode`},
{2, `encourages`},
{3, `code`},
{4, `diversity`},
{4, `comparison`},
{2, `discourages`},
{3, `emphasising execution speed`},
{1, `code-golf.io`},
{2, `encourages`},
{3, `golfing`},
{2, `discourages`},
{3, `comparison`}}
 
Nested form:
{{`RosettaCode`,
{{`encourages`,
{{`code`,
{{`diversity`,
{}},
{`comparison`,
{}}}}}},
{`discourages`,
{{`emphasising execution speed`,
{}}}}}},
{`code-golf.io`,
{{`encourages`,
{{`golfing`,
{}}}},
{`discourages`,
{{`comparison`,
{}}}}}}}
 
Nested to indent:same
</pre>
You can also strictly enforce these structures, which is obviously useful for debugging:
<lang Phix>type indent_struct(object o)
if sequence(o) then
for i=1 to length(o) do
object oi = o[i]
if not sequence(oi)
or length(oi)!=2
or not integer(oi[1])
or not string(oi[2]) then
return false
end if
end for
return true
end if
return false
end type
 
type nested_struct(object o)
if sequence(o) then
for i=1 to length(o) do
object oi = o[i]
if not sequence(oi)
or length(oi)!=2
or not string(oi[1])
or not nested_struct(oi[2]) then
return false
end if
end for
return true
end if
return false
end type
 
-- and as above except:
function indent_to_nested(indent_struct indent, integer idx=1, level=1)
function nested_to_indent(nested_struct nested, integer level=1)
-- (the next two are the constant declarations, now typed)
indent_struct indent = text_to_indent(text),
nested_struct nested = indent_to_nested(indent)[1]
indent_struct r2i = nested_to_indent(nested)</lang>
 
=={{header|Python}}==
7,820

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.