Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qtreetbl improvements #107

Merged
merged 73 commits into from
Jul 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
9015d0c
code style of qtreetbl.h
wolkykim Jul 21, 2023
efecb49
implement qtreetbl_debug() & remove stdout prints
wolkykim Jul 21, 2023
2a61755
Improve unit tests with improved tree drawer for visual inspection
wolkykim Jul 21, 2023
7ce3d5d
Add more test cases
wolkykim Jul 21, 2023
e7f5278
comment updates
wolkykim Jul 21, 2023
e0d92ac
Add more insertion test
wolkykim Jul 21, 2023
06cb183
Make tree print prettier
wolkykim Jul 22, 2023
7e07065
Update print_tree
wolkykim Jul 22, 2023
833c064
update print_tree
wolkykim Jul 22, 2023
d5a47f4
update debug output example
wolkykim Jul 22, 2023
37e0c32
Add Stroh Snow's Rule 4 violation case
wolkykim Jul 22, 2023
3f61a5b
code style update
wolkykim Jul 22, 2023
e34dfbb
Combine free_obj and free_objs
wolkykim Jul 22, 2023
15f144d
code style
wolkykim Jul 22, 2023
2030873
Update LLRB rule checker
wolkykim Jul 22, 2023
831d4cd
Add ASSERT_TABLE_CHECK
wolkykim Jul 22, 2023
6128938
Add rule 6
wolkykim Jul 22, 2023
1be7215
pretty print
wolkykim Jul 22, 2023
72c3521
Comment updates
strohsnow Jul 22, 2023
6ac3900
Make tree print even prettier
strohsnow Jul 22, 2023
d5c21c7
Tree properties update
strohsnow Jul 22, 2023
4eac56e
Style fix
strohsnow Jul 22, 2023
b8231a5
Comments update
strohsnow Jul 22, 2023
c9d2863
Comments update
strohsnow Jul 22, 2023
383f2ff
Deletion algorithm fix
strohsnow Jul 22, 2023
49d3d43
Comments updare
strohsnow Jul 22, 2023
fb260f2
typo fix
wolkykim Jul 22, 2023
417e69d
code style
wolkykim Jul 22, 2023
c9a2e69
name update
wolkykim Jul 22, 2023
3cd2a03
Add performance test
wolkykim Jul 23, 2023
0415802
Add integrity check with random keys
wolkykim Jul 23, 2023
3a3ad93
test code
wolkykim Jul 23, 2023
3da8918
Remove test code
wolkykim Jul 23, 2023
9b3d1c0
fix unit tests and put back integrity tests
wolkykim Jul 23, 2023
180e462
Increase the duration of integrity check
wolkykim Jul 23, 2023
d9e6d17
Update comments
wolkykim Jul 23, 2023
7f58402
Add Stroh Snow to the contributors list
wolkykim Jul 23, 2023
58568ea
code style
wolkykim Jul 23, 2023
856193a
name change, _by_obj => obj
wolkykim Jul 24, 2023
3a49be4
qtreetbl to allow NULL data insertion
wolkykim Jul 24, 2023
b01ad19
Remove tailing whitespaces
wolkykim Jul 24, 2023
62c6871
Add more performance test
wolkykim Jul 24, 2023
a4d67f9
Add rotate counter metrics
wolkykim Jul 24, 2023
4c6baad
update perf test
wolkykim Jul 24, 2023
73d28fc
fix perf test
wolkykim Jul 24, 2023
1ce0342
style update
wolkykim Jul 24, 2023
2e4df59
code style
wolkykim Jul 24, 2023
901081f
Add drawing tree on perf test
wolkykim Jul 24, 2023
1944536
Update debug() to print integer type key names
wolkykim Jul 24, 2023
147348e
code style
wolkykim Jul 24, 2023
b921997
Add -v CLI parameter
wolkykim Jul 24, 2023
e8697e3
Reduce tree drawing but enable them with -v option
wolkykim Jul 24, 2023
b3f58c1
Performance tune
wolkykim Jul 24, 2023
bfad492
remove comment
wolkykim Jul 24, 2023
5fba0e7
Updated print_node
strohsnow Jul 24, 2023
84cf3cc
Updated find_min/max
strohsnow Jul 24, 2023
29b3d77
Code style
strohsnow Jul 24, 2023
fcee53a
code style
wolkykim Jul 24, 2023
544ef91
Merge branch 'tree' of github.com:wolkykim/qlibc into tree
wolkykim Jul 24, 2023
70f6781
Added 2-3 LLRB as comments
strohsnow Jul 25, 2023
f69288b
Removed repeated lock/unlock
strohsnow Jul 25, 2023
d38b35a
code readability
wolkykim Jul 25, 2023
c86abc9
Merge branch 'tree' of github.com:wolkykim/qlibc into tree
wolkykim Jul 25, 2023
601e534
Add LLRB234 directive to allow switching between 2-3-4 and 2-3 at the…
wolkykim Jul 25, 2023
8f48930
Typo fix
strohsnow Jul 25, 2023
15748c0
update unit tests
wolkykim Jul 25, 2023
fbdfb78
remove comment
wolkykim Jul 25, 2023
827d055
code readability
wolkykim Jul 26, 2023
406653d
Code style
strohsnow Jul 26, 2023
b3c503f
Merge remote-tracking branch 'origin/tree' into tree
strohsnow Jul 26, 2023
e419464
Comments update
strohsnow Jul 26, 2023
64a3ee3
code style
wolkykim Jul 26, 2023
544592e
regenerate docs
wolkykim Jul 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 67 additions & 31 deletions src/containers/qtreetbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,34 @@
* @code
* qtreetbl_t *tbl = qtreetbl(QTREETBL_THREADSAFE);
*
* tbl->put(tbl, "key", "DATA", 4); // use put_by_obj() for binary keys.
* void *data = tbl->get(tbl, "key", false); // use get_by_obj() for binary keys.
* tbl->remove(tbl, "key"); // use remove_by_key() for binary keys.
* tbl->put(tbl, "KEY", "DATA", 4); // use put_by_obj() for binary keys.
* void *data = tbl->get(tbl, "KEY", false); // use get_by_obj() for binary keys.
* tbl->remove(tbl, "KEY"); // use remove_by_key() for binary keys.
*
* // iteration example
* qtreetbl_obj_t obj;
* memset((void*)&obj, 0, sizeof(obj)); // must be cleared before call
* tbl->lock(tbl); // for thread safety
* while (tbl->getnext(tbl, &obj, false) == true) {
* ...
* }
* tbl->unlock(tbl);
*
* // find a nearest key then iterate from there
* tbl->lock(tbl);
* qtreetbl_obj_t obj = tbl->find_nearest(tbl, "k", 2, false);
* qtreetbl_obj_t obj = tbl->find_nearest(tbl, "K", sizeof("K"), false);
* tbl->lock(tbl); // for thread safety
* while (tbl->getnext(tbl, &obj, false) == true) {
* ...
* }
* tbl->unlock(tbl);
*
* tbl->set_compare(tbl, my_compare_func);
* size_t num = tbl->size(tbl);
* // find minimum value using custom comparator
* tbl->set_compare(tbl, my_compare_func); // default is byte comparator
* void *min = tbl->find_min(tbl, &keysize);
* qtree_clean();
*
* // get total number of objects in the table
* size_t num = tbl->size(tbl);
*
* tbl->free(tbl);
* @endcode
Expand Down Expand Up @@ -687,13 +699,10 @@ void *qtreetbl_find_max(qtreetbl_t *tbl, size_t *namesize) {
* - ENOMEM : Memory allocation failure.
*
* @code
* [Some examples here]
* Data Set : A B C D E I N R S X
* find_nearest("0") => "A" // no smaller key available, so "A"
* find_nearest("C") => "C" // matching key found
* find_nearest("F") => "E" // "E" is nearest smaller key from "F"
* find_nearest("M") => "N" // "N" is nearest smaller key from "M"
* find_nearest("Z") => "X" // "X" is nearest smaller key from "Z"
* @endcode
*
* @note
Expand Down Expand Up @@ -847,7 +856,6 @@ int qtreetbl_byte_cmp(const void *name1, size_t namesize1, const void *name2,
*
* @code
* Example output:
*
* .- 9
* | `=[8]
* .- 7
Expand Down Expand Up @@ -876,54 +884,72 @@ bool qtreetbl_debug(qtreetbl_t *tbl, FILE *out) {
}

/**
* Verifies that RULE 4 of the red-black tree is verified for the node pointed
* by `obj` and all its children.
* Verifies that property 2 of the red-black tree is conserved
*
* Rule 4 states that no red node shall have a red child.
* Property 2: The root node of the tree is always black
*
* @param tbl A pointer to the tree object.
*/
int node_check_rule2(qtreetbl_t *tbl) {
if (tbl == NULL) {
return 1;
}

if (is_red(tbl->root)) {
return 1;
}
return 0;
}

/**
* Verifies that property 3 of the red-black tree is conserved
*
* Property 3: Red nodes cannot have red children
*
* @param tbl A pointer to the tree object.
* @param obj A pointer to a node of the tree object.
*/
int node_check_rule4(qtreetbl_t *tbl, qtreetbl_obj_t *obj) {
if (obj == NULL) return 0;
int node_check_rule3(qtreetbl_t *tbl, qtreetbl_obj_t *obj) {
if (obj == NULL) {
return 0;
}

if (is_red(obj)) {
if (is_red(obj->right) || is_red(obj->left)) {
return 1;
}
}

if (node_check_rule4(tbl, obj->right)) {
if (node_check_rule3(tbl, obj->right)) {
return 1;
}
if (node_check_rule4(tbl, obj->left)) {
if (node_check_rule3(tbl, obj->left)) {
return 1;
}
return 0;
}

/**
* Verifies that RULE 5 of the red-black tree is verified for the node pointed
* by `obj` and all its children.
* Verifies that property 4 of the red-black tree is conserved
*
* Rule 5 states that every path from the root of the tree to any leaf of the
* tree has the same number of black nodes.
* Property 4: Every path from the root of the tree to any leaf of the
* tree has the same number of black nodes.
*
* @param tbl A pointer to the tree object.
* @param obj A pointer to a node of the tree object.
*/
int node_check_rule5(qtreetbl_t *tbl, qtreetbl_obj_t *obj, int *path_len) {
int node_check_rule4(qtreetbl_t *tbl, qtreetbl_obj_t *obj, int *path_len) {
if (obj == NULL) {
*path_len = 0;
*path_len = 1;
return 0;
}

int right_path_len;
if (node_check_rule5(tbl, obj->right, &right_path_len)) {
if (node_check_rule4(tbl, obj->right, &right_path_len)) {
return 1;
}
int left_path_len;
if (node_check_rule5(tbl, obj->left, &left_path_len)) {
if (node_check_rule4(tbl, obj->left, &left_path_len)) {
return 1;
}

Expand All @@ -938,19 +964,30 @@ int node_check_rule5(qtreetbl_t *tbl, qtreetbl_obj_t *obj, int *path_len) {
/**
* Verifies that the (some) invariants of the red-black tree are satisfied.
*
* Property 1. Every node is either red or black.
* Property 2. The root node is always black.
* Property 3: Red nodes cannot have red children (no consecutive red nodes).
* Property 4: Every leaf (null) node is considered black.
* Property 5: Every path from the root of the tree to any leaf of the tree
* has the same number of black nodes.
* Property 6: A red link is allowed to the left child. (LLRB specific)
strohsnow marked this conversation as resolved.
Show resolved Hide resolved

* @param tbl A pointer to the tree object to check.
*/
int qtreetbl_check(qtreetbl_t *tbl) {
if (tbl == NULL) {
return 0;
}

if (node_check_rule4(tbl, tbl->root)) {
return 4;
if (node_check_rule2(tbl)) {
return 2;
}
if (node_check_rule3(tbl, tbl->root)) {
return 3;
}
int path_len = 0;
if (node_check_rule5(tbl, tbl->root, &path_len)) {
return 5;
if (node_check_rule4(tbl, tbl->root, &path_len)) {
return 4;
}

return 0;
Expand Down Expand Up @@ -1266,7 +1303,6 @@ void print_node(qtreetbl_obj_t *obj, FILE *out, struct branch_obj_s *prev, bool
prev->s = prev_s;
}
branch.s = " |";

print_node(obj->left, out, &branch, true);
}

Expand Down