Skip to content

Instantly share code, notes, and snippets.

@mvlootman
Last active August 20, 2019 13:53
Show Gist options
  • Save mvlootman/a707f3aab5e671ca9ccd7c1dd7dac38a to your computer and use it in GitHub Desktop.
Save mvlootman/a707f3aab5e671ca9ccd7c1dd7dac38a to your computer and use it in GitHub Desktop.
tree - V code
struct TNode {
pub mut:
id int
children []*TNode
}
struct Record{
pub:
id int
parent int
}
fn compare_records(a, b *Record) int {
// println('sorting a.id:$a.id b.id$b.id')
if a.id < b.id {
return -1
}
if a.id > b.id {
return 1
}
return 0
}
fn build(records []Record) *TNode{
// sort incoming records on IDs
mut records_sorted := records.clone()
records_sorted.sort_with_compare(compare_records)
println('length of records_sorted:${records_sorted.len}')
mut result := [TNode{}; records_sorted.len]
for _, rec in records_sorted{
if rec.id != 0 {
mut parent := rec.parent // avoid `rec` is immutable (can't <<)
result[parent].children << &result[rec.id] //add REFERENENCE to existing node
}
result[rec.id] = TNode{id: rec.id}
}
return result[0]
}
fn main(){
records := [
Record{id: 5, parent: 1},
Record{id: 3, parent: 2},
Record{id: 2, parent: 0},
Record{id: 4, parent: 1},
Record{id: 1, parent: 0},
Record{id: 0},
Record{id: 6, parent: 2}
]
res := build(records) or {
println('err:$err')
return
}
println(res.id)
}
// Output:
// bad arr array_TNode*
// bad arr array_TNode*
// .tree.c:155:1: error: unknown type name 'array_TNode'
// array_TNode* children;
// ^
// .tree.c:3534:1: error: use of undeclared identifier 'array_TNode'; did you mean 'array_clone'?
// array_TNode result= array_repeat(&tmp2, records_sorted .len , sizeof(TNode) ) ;
// ^~~~~~~~~~~
// array_clone
// .tree.c:613:24: note: 'array_clone' declared here
// /* returns 0 */ array array_clone(array a) {
// ^
// .tree.c:3534:12: error: expected ';' after expression
// array_TNode result= array_repeat(&tmp2, records_sorted .len , sizeof(TNode) ) ;
// ^
// ;
// .tree.c:3534:13: error: use of undeclared identifier 'result'
// array_TNode result= array_repeat(&tmp2, records_sorted .len , sizeof(TNode) ) ;
// ^
// .tree.c:3546:101: error: use of undeclared identifier 'result'
// _PUSH( ( *(TNode*) array__get( result , parent) ) .children , ( & /*vvar*/ ( *(TNode*) array__get( result , rec .id) ) ), tmp8, TNode*) ;
// ^
// .tree.c:3546:32: error: use of undeclared identifier 'result'
// _PUSH( ( *(TNode*) array__get( result , parent) ) .children , ( & /*vvar*/ ( *(TNode*) array__get( result , rec .id) ) ), tmp8, TNode*) ;
// ^
// .tree.c:3553:18: error: use of undeclared identifier 'result'
// array_set(&/*q*/ result , rec .id , & tmp11) ;
// ^
// .tree.c:3558:33: error: use of undeclared identifier 'result'
// return ( *(TNode*) array__get( result , 0) ) ;
// ^
// .tree.c:3568:13: error: member reference type 'TNode *' (aka 'struct TNode *') is a pointer; did you mean to use '->'?
// if (!tmp15 .ok) {
// ~~~~~ ^
// ->
// .tree.c:3568:14: error: no member named 'ok' in 'struct TNode'
// if (!tmp15 .ok) {
// ~~~~~ ^
// .tree.c:3569:21: error: member reference type 'TNode *' (aka 'struct TNode *') is a pointer; did you mean to use '->'?
// string err = tmp15 . error;
// ~~~~~ ^
// ->
// .tree.c:3569:23: error: no member named 'error' in 'struct TNode'
// string err = tmp15 . error;
// ~~~~~ ^
// .tree.c:3576:32: error: member reference type 'TNode *' (aka 'struct TNode *') is a pointer; did you mean to use '->'?
// TNode* res = *(TNode**) tmp15 . data;
// ~~~~~ ^
// ->
// .tree.c:3576:34: error: no member named 'data' in 'struct TNode'
// TNode* res = *(TNode**) tmp15 . data;
// ~~~~~ ^
// 14 errors generated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment