Asset - Scripts Tree data structure

Dmi7ry

Member


ds_tree

Marketplace link

Simple tree data structure for GameMaker Studio.

Feel free to contact me if any issues or requests.

GMS1.4 Note: add constants.txt to Macros (Resources → Define Macros → Load)

See documentation in wiki.

Function list

Basic
  • ds_tree_create(id)
  • ds_tree_add(node, name, value, [is_node])
  • ds_tree_add_tree(target_node, node)
  • ds_tree_clear(tree)
  • ds_tree_cut_node(node)
  • ds_tree_destroy(id, [only_clear])
  • ds_tree_find_name(tree, name)
  • ds_tree_find_value(tree, name)
  • ds_tree_height(tree)
  • ds_tree_insert_node()
  • ds_tree_size()
  • ds_tree_split_node()
Properties
  • ds_tree_property_childs(tree, [new_value])
  • ds_tree_property_is_node(tree, [new_value])
  • ds_tree_property_name(tree, [new_value])
  • ds_tree_property_parent(tree, [new_value])
  • ds_tree_property_value(tree, [new_value])
Extra
  • ds_tree_destroy_childs()
  • ds_tree_get_ids()
  • ds_tree_is_last_child(index)
  • ds_tree_print(tree)
  • ds_tree_to_array(tree)
  • ds_tree_to_string(tree)
  • ds_tree_print_values(node, level)
  • ds_tree_to_DOT(tree, [colored])
  • ds_tree_print_DOT(tree, [colored])
Reference counter for data structures
  • rc_initialize()
  • rc_flush()
  • rc_list_create()
  • rc_list_destroy(map)
  • rc_list_size()
  • rc_map_create()
  • rc_map_destroy(map)
  • rc_map_size()
  • rc_print_state()

tree_add.png
 

zbox

Member
GMC Elder
I meant the whole system :p Memory leaks are avoided with DS structures too if you are chaining them properly?
 

Dmi7ry

Member
I meant the whole system :p
Do you understand what is tree data structure?

Memory leaks are avoided with DS structures too if you are chaining them properly?
Reference counter system allows to avoid memory leaks for any data structures, whether they are chained or not - it does not matter.

P.S. I used "reference counter" name, but actually this is not a reference counter, but just a list of all data structures.
 

zbox

Member
GMC Elder
I absolutely do... They are Nodes that can contain data, or, another node. Like maps and lists.

And you use ds_tree_add() to add a node right, so that it can be linked and managed to avoid memory leaks... like how you can use ds_map_add map to add a node to a ds map that is destroyed when its parent is also destroyed to avoid memory leaks.

Not trying to be rude I was just interested in what the difference was if any :)
 

Dmi7ry

Member
And you use ds_tree_add() to add a node right, so that it can be linked and managed to avoid memory leaks... like how you can use ds_map_add map to add a node to a ds map that is destroyed when its parent is also destroyed to avoid memory leaks.
Why there is used rc_... Because any node can be split from tree, so it won't be chained with anything. And then user may forget about that node. Or user even may forget to destroy whole tree. Or he can create node and do not add it to another. Etc. There are lot of cases why it may be missed/not chained. In this case RC alerts that there is problem, so user can fix it, if wants.

Why there is not used ds_map_add/etc. The asset is just part of system I used in my project. And in some cases it uses different data structures besides ds_list and ds_map (ds_priority for ordered children, as example) and different assets. So I just use one way for all data structures - no reason to do ds_map_add/etc because I anyway should check each child for destroying other data structures or assets.

If you need simple tree, not need difficult manipulations of nodes, etc, then you can use ds_map_add/etc functions, of course. And then tree's code will be few shorter.
 
Top