Skip to content

Commit f188137

Browse files
committed
Fix some minor issues in deletion process - Binary Tree and make the cjlib_json_set as it must be
1 parent ba91dc4 commit f188137

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

include/cjlib.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ static inline int cjlib_json_array_get(struct cjlib_json_data *restrict dst, int
246246
* @return 0 on success, otherwise -1.
247247
*/
248248
extern int cjlib_json_object_set
249-
(cjlib_json_object *src, const char *restrict key,
249+
(cjlib_json_object **src, const char *restrict key,
250250
struct cjlib_json_data *restrict value, enum cjlib_json_datatypes datatype);
251251

252252
/**
@@ -269,7 +269,7 @@ extern int cjlib_json_object_get
269269
* @return 0 on success, otherwise -1.
270270
*/
271271
extern int cjlib_json_object_remove
272-
(struct cjlib_json_data *restrict dst, cjlib_json_object *src,
272+
(struct cjlib_json_data *restrict dst, cjlib_json_object **src,
273273
const char *key);
274274

275275
/**
@@ -286,7 +286,7 @@ static CJLIB_ALWAYS_INLINE int cjlib_json_set
286286
(struct cjlib_json *restrict src, const char *restrict key,
287287
struct cjlib_json_data *restrict value, enum cjlib_json_datatypes datatype)
288288
{
289-
return cjlib_json_object_set(src->c_dict, key, value, datatype);
289+
return cjlib_json_object_set(&src->c_dict, key, value, datatype);
290290
}
291291

292292
/**
@@ -313,10 +313,10 @@ static CJLIB_ALWAYS_INLINE int cjlib_json_get
313313
* @return 0 on success, otherwise -1.
314314
*/
315315
static CJLIB_ALWAYS_INLINE int cjlib_json_remove
316-
(struct cjlib_json_data *restrict dst, const struct cjlib_json *restrict src,
316+
(struct cjlib_json_data *restrict dst, struct cjlib_json *restrict src,
317317
const char *restrict key)
318318
{
319-
return cjlib_json_object_remove(dst, src->c_dict, key);
319+
return cjlib_json_object_remove(dst, &src->c_dict, key);
320320
}
321321

322322
/**

src/cjlib.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,18 @@ static inline char *incomplete_property_str_expand_state
120120
}
121121

122122
int cjlib_json_object_set
123-
(cjlib_json_object *src, const char *restrict key,
123+
(cjlib_json_object **src, const char *restrict key,
124124
struct cjlib_json_data *restrict value, enum cjlib_json_datatypes datatype)
125125
{
126+
struct cjlib_json_data dummy;
127+
128+
// Remove the previous contents (if exists).
129+
(void) cjlib_json_object_remove(&dummy, src, key);
130+
cjlib_json_data_destroy(&dummy);
131+
132+
// (change/set) the record.
126133
value->c_datatype = datatype;
127-
if (-1 == cjlib_dict_insert(value, &src, key)) return -1;
134+
if (-1 == cjlib_dict_insert(value, src, key)) return -1;
128135
return 0;
129136
}
130137

@@ -144,16 +151,16 @@ int cjlib_json_object_get
144151
}
145152

146153
int cjlib_json_object_remove
147-
(struct cjlib_json_data *restrict dst, cjlib_json_object *src,
154+
(struct cjlib_json_data *restrict dst, cjlib_json_object **src,
148155
const char *restrict key)
149156
{
150157
// dst == NULL, then you can skip the return value.
151158
if (NULL == dst) goto perform_deletion;
152159

153-
if (-1 == cjlib_json_object_get(dst, src, key)) return -1;
160+
if (-1 == cjlib_json_object_get(dst, *src, key)) return -1;
154161

155162
perform_deletion:
156-
if (-1 == cjlib_dict_remove(&src, key)) return -1;
163+
if (-1 == cjlib_dict_remove(src, key)) return -1;
157164

158165
return 0;
159166
}

src/cjlib_dictionary.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,10 @@ int cjlib_dict_remove(struct avl_bs_tree_node **dict, const char *restrict key)
712712
largest_key_of_left_subtree_parent = search_node(*dict, largest_key_of_left_subtree->avl_key,
713713
S_RETRIEVE_KEY_NODE_PARENT);
714714

715-
removed->avl_key = largest_key_of_left_subtree->avl_key;
716-
(void) memcpy(largest_key_of_left_subtree->avl_data, removed->avl_data, sizeof(struct cjlib_json_data));
715+
free(removed->avl_key);
716+
removed->avl_key = strdup(largest_key_of_left_subtree->avl_key);
717+
(void) memcpy(removed->avl_data, largest_key_of_left_subtree->avl_data, sizeof(struct cjlib_json_data));
718+
//(void) memcpy(largest_key_of_left_subtree->avl_data, removed->avl_data, sizeof(struct cjlib_json_data));
717719

718720
removed = largest_key_of_left_subtree;
719721
parent = largest_key_of_left_subtree_parent;
@@ -723,11 +725,21 @@ int cjlib_dict_remove(struct avl_bs_tree_node **dict, const char *restrict key)
723725
if (removed->avl_left) child_of_removed = removed->avl_left;
724726
else child_of_removed = removed->avl_right;
725727

728+
if (is_root) {
729+
*dict = child_of_removed;
730+
} else {
731+
if (parent->avl_left == removed) {
732+
parent->avl_left = child_of_removed;
733+
} else {
734+
parent->avl_right = child_of_removed;
735+
}
736+
}
737+
/*
726738
if (parent->avl_left == removed) {
727739
parent->avl_left = child_of_removed;
728740
} else {
729741
parent->avl_right = child_of_removed;
730-
}
742+
}*/
731743

732744
perform_rotation_after_delete(removed, dict);
733745

tests/src/main.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <stdio.h>
2222
#include <stdlib.h>
2323

24+
#include <string.h>
25+
2426
#include "cjlib.h"
2527

2628
#include "cjlib_list.h"
@@ -50,11 +52,14 @@ int main(void)
5052

5153
cjlib_json_read(&json_file);
5254

53-
/*if (-1 == cjlib_json_get(&dst, &json_file, "programming_languages")) {
55+
if (-1 == cjlib_json_get(&dst, &json_file, "version")) {
5456
(void) printf("Error\n");
5557
exit(-1);
5658
}
5759

60+
(void) printf("%s\n", dst.c_value.c_str);
61+
62+
/*
5863
struct cjlib_json_data test;
5964
CJLIB_LIST_FOR_EACH(test, dst.c_value.c_arr, struct cjlib_json_data) {
6065
(void) printf("%s\n", test.c_value.c_str);
@@ -86,8 +91,9 @@ int main(void)
8691
(void) printf("%s\n", dst.c_value.c_str);*/
8792

8893

89-
free((void *) cjlib_json_stringtify(&json_file));
94+
//free((void *) cjlib_json_stringtify(&json_file));
9095

96+
//cjlib_json_dump(&json_file);
9197
// // Close the json file.
9298
cjlib_json_close(&json_file);
9399
}

0 commit comments

Comments
 (0)