aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2016-12-11 03:09:14 +0100
committerKatharina Fey <kookie@spacekookie.de>2019-06-04 20:21:03 +0200
commita4b6f559da2af7f1c035446a1ad8f01fd4d9dbf7 (patch)
tree7352aadae99d24f4df57c523f1f5015836f2e27f
parente3fe9401e4593c317ca967738d4c14e638a6ccd6 (diff)
Adding new function that gets the parent of a current node in a recursive structure
-rw-r--r--include/dtree/dtree.h13
-rw-r--r--lib/dtree.c42
-rw-r--r--lib/dtree_utils.c (renamed from lib/dyn_utils.c)0
3 files changed, 55 insertions, 0 deletions
diff --git a/include/dtree/dtree.h b/include/dtree/dtree.h
index 7662412..a315014 100644
--- a/include/dtree/dtree.h
+++ b/include/dtree/dtree.h
@@ -189,6 +189,19 @@ dt_err dtree_merge_trees(dtree *data, dtree *merge);
/**
+ * You can use this function to search the structure of a root node to find the
+ * parent of the node you provide as "data". It will leave the search pointer
+ * blanked if the node can't be found in the structure.
+ *
+ * @param root Root reference to search
+ * @param data The node we are searching for
+ * @param parent The node parent we are interested in
+ * @return
+ */
+dt_err dtree_parent(dtree *root, dtree *data, dtree **parent);
+
+
+/**
* Recursive tree search function that will return the first occurence match
* to a provided payload (with an exact type). If you have data duplication
* in your tree this _might_ return some false positives.
diff --git a/lib/dtree.c b/lib/dtree.c
index 466c8c0..e45d453 100644
--- a/lib/dtree.c
+++ b/lib/dtree.c
@@ -335,6 +335,48 @@ dt_err dtree_copy_deep(dtree *data, dtree *(*copy))
return err;
}
+
+dt_err dtree_parent(dtree *root, dtree *data, dtree **parent)
+{
+ if(root == NULL || data == NULL) return INVALID_PARAMS;
+
+ /* Blank the search pointer for easy error checking */
+ (*parent) = NULL;
+
+ switch(data->type) {
+
+ /* Dead-end data stores automatically return @{NODE_NOT_FOUND} */
+ case POINTER:
+ case LITERAL:
+ case NUMERIC:
+ return NODE_NOT_FOUND;
+
+ case PAIR:
+ case LIST:
+ {
+ int i;
+ for(i = 0; i < root->used; i++) {
+
+ /* Check if the node we're looking at is what we're searching for */
+ if(root->payload.list[i] == data) {
+ (*parent) = root;
+ return SUCCESS;
+ }
+
+ dt_err err = dtree_parent(root->payload.list[i], data, parent);
+ if(err == SUCCESS) return SUCCESS;
+ }
+ }
+ break;
+
+ default:
+ return INVALID_PAYLOAD;
+ }
+
+ return NODE_NOT_FOUND;
+}
+
+
dt_err dtree_copy(dtree *data, dtree *(*copy))
{
if(data == NULL) return INVALID_PARAMS;
diff --git a/lib/dyn_utils.c b/lib/dtree_utils.c
index 6912dd2..6912dd2 100644
--- a/lib/dyn_utils.c
+++ b/lib/dtree_utils.c