Created
February 2, 2023 14:05
-
-
Save dalexeev/fb6faa24d91b78b785a4ccd84e63c198 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DataNode extends Object: | |
var index: int | |
var parent: DataNode | |
var children: Array[DataNode] | |
class DataHeap extends RefCounted: | |
var _heap := {} # id: int -> node: DataNode | |
var _max_id := -1 | |
func add_node(id: int, parent_id: int) -> void: | |
assert(id > _max_id) | |
assert(parent_id == -1 || has_node(parent_id)) | |
var node := DataNode.new() | |
if parent_id != -1: | |
var parent_node: DataNode = _heap[parent_id] | |
node.index = parent_node.children.size() | |
node.parent = parent_node | |
parent_node.children.append(node) | |
_heap[id] = node | |
_max_id = id | |
func has_node(id: int) -> bool: | |
return _heap.has(id) | |
func get_node(id: int) -> DataNode: | |
assert(has_node(id)) | |
return _heap[id] | |
func _notification(what: int) -> void: | |
if what == NOTIFICATION_PREDELETE: | |
for id in _heap: | |
_heap[id].free() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DataHeap extends RefCounted: | |
var _index := {} # id: int -> index: int | |
var _parent := {} # id: int -> parent_id: int | |
var _children := {} # id: int -> children_ids: Array[int] | |
var _max_id := -1 | |
func add_node(id: int, parent_id: int) -> void: | |
assert(id > _max_id) | |
assert(parent_id == -1 || has_node(parent_id)) | |
_parent[id] = parent_id | |
_children[id] = Array([], TYPE_INT, &'', null) # Array[int]() | |
if parent_id == -1: | |
_index[id] = 0 | |
else: | |
_index[id] = _children[parent_id].size() | |
_children[parent_id].append(id) | |
_max_id = id | |
func has_node(id: int) -> bool: | |
return _index.has(id) | |
func get_index(id: int) -> int: | |
assert(has_node(id)) | |
return _index[id] | |
func get_parent_id(id: int) -> int: | |
assert(has_node(id)) | |
return _parent[id] | |
func get_children_ids(id: int) -> Array[int]: | |
assert(has_node(id)) | |
return _children[id].duplicate() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DataHeap extends RefCounted: | |
var _index: PackedInt32Array | |
var _parent: PackedInt32Array | |
var _children: Array[Array] # Array[Array[int]] | |
var _max_id := -1 | |
func add_node(parent_id: int) -> void: | |
assert(parent_id == -1 || has_node(parent_id)) | |
var id := _max_id + 1 | |
_parent.append(parent_id) | |
_children.append(Array([], TYPE_INT, &'', null)) # Array[int]() | |
if parent_id == -1: | |
_index.append(0) | |
else: | |
_index.append(_children[parent_id].size()) | |
_children[parent_id].append(id) | |
_max_id = id | |
func has_node(id: int) -> bool: | |
return 0 <= id && id < _index.size() | |
func get_index(id: int) -> int: | |
assert(has_node(id)) | |
return _index[id] | |
func get_parent_id(id: int) -> int: | |
assert(has_node(id)) | |
return _parent[id] | |
func get_children_ids(id: int) -> Array[int]: | |
assert(has_node(id)) | |
return _children[id].duplicate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment