Skip to content

add parent field to sexp_node for O(1) parent lookups - #7714

Open
Goober5000 wants to merge 1 commit into
scp-fs2open:masterfrom
Goober5000:sexp_node_parent
Open

add parent field to sexp_node for O(1) parent lookups#7714
Goober5000 wants to merge 1 commit into
scp-fs2open:masterfrom
Goober5000:sexp_node_parent

Conversation

@Goober5000

Copy link
Copy Markdown
Contributor

The sexp_node struct used first/rest pointers but had no parent pointer, requiring O(n) linear scans of the entire Sexp_nodes array to find a node's parent. Add an int parent field (-1 for root/unlinked) and maintain it at all assignment sites: alloc_sexp, free_sexp, get_sexp parse loop, and save_branch. Refactor find_sexp_list(), find_parent_operator(), and is_sexp_top_level() to use the parent field directly; and add find_sexp_antecedent() and find_sexp_root().

Also add parent_node guards to several functions to allow them to gracefully fail on nodes with no parents.

Also add documentation on how SEXP nodes and lists work, particularly the gotchas for top-level nodes.

@Goober5000 Goober5000 added enhancement A new feature or upgrade of an existing feature to add additional functionality. sexps A feature or issue related to SEXPs refactor A cleanup/restructure of a feature for speed, simplicity, and/or maintainability labels Aug 17, 2026
The sexp_node struct used first/rest pointers but had no parent pointer, requiring O(n) linear scans of the entire Sexp_nodes array to find a node's parent.  Add an int parent field (-1 for root/unlinked) and maintain it at all
assignment sites: alloc_sexp, free_sexp, get_sexp parse loop, and save_branch.  Refactor find_sexp_list(), find_parent_operator(), and is_sexp_top_level() to use the parent field directly; and add find_sexp_antecedent() and find_sexp_root().

Also add parent_node guards to several functions to allow them to gracefully fail on nodes with no parents.

Also add documentation on how SEXP nodes and lists work, particularly the gotchas for top-level nodes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement A new feature or upgrade of an existing feature to add additional functionality. refactor A cleanup/restructure of a feature for speed, simplicity, and/or maintainability sexps A feature or issue related to SEXPs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant