From 6b30396e46b387d59266ca62496911d553da96f3 Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 14 May 2025 11:35:51 -0700 Subject: [PATCH 01/43] adding set --- bflibc/makefile | 2 +- bflibc/src/set.c | 29 +++++++++++++++++++++++++++++ bflibc/src/set.h | 15 +++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 bflibc/src/set.c create mode 100644 bflibc/src/set.h diff --git a/bflibc/makefile b/bflibc/makefile index 5d6e2310..d693ea7e 100644 --- a/bflibc/makefile +++ b/bflibc/makefile @@ -27,7 +27,7 @@ filesystem coreutils stringutils \ bftime thread bfmath \ lock filewriter hash \ rand map tree internal/tree \ -hashmap +hashmap set ### Release settings ifeq ($(CONFIG),release) # release diff --git a/bflibc/src/set.c b/bflibc/src/set.c new file mode 100644 index 00000000..542df064 --- /dev/null +++ b/bflibc/src/set.c @@ -0,0 +1,29 @@ +/** + * author: brando + * date: 5/14/25 + */ + +#include "set.h" +#include "tree.h" +#include "free.h" + +typedef struct _BFSet { + BFTree tree; +} _BFSet; + +BFSet BFSetCreate() { + _BFSet * res = (_BFSet *) malloc(sizeof(_BFSet)); + if (!res) return NULL; + + res->tree = BFTreeCreate(); + return (BFSet) res; +} + +void BFSetRelease(BFSet _set) { + _BFSet * set = (_BFSet *) _set; + if (!set) return; + + BFTreeRelease(set->tree); + BFFree(set); +} + diff --git a/bflibc/src/set.h b/bflibc/src/set.h new file mode 100644 index 00000000..0eea309a --- /dev/null +++ b/bflibc/src/set.h @@ -0,0 +1,15 @@ +/** + * author: brando + * date: 5/14/25 + */ + +#ifndef SET_H +#define SET_H + +typedef void * BFSet; + +BFSet BFSetCreate(); +void BFSetRelease(BFSet set); + +#endif // SET_H + From eb5f7974a5904d94fdd2dde9a167a23fcee5d69f Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 14 May 2025 21:22:15 -0700 Subject: [PATCH 02/43] Saving work --- bflibc/src/set.c | 34 +++++++++++++++++++++++++ bflibc/src/set.h | 8 ++++++ bflibc/testbench/set_tests.h | 49 ++++++++++++++++++++++++++++++++++++ bflibc/testbench/tests.c | 4 +++ 4 files changed, 95 insertions(+) create mode 100644 bflibc/testbench/set_tests.h diff --git a/bflibc/src/set.c b/bflibc/src/set.c index 542df064..82cfc5ee 100644 --- a/bflibc/src/set.c +++ b/bflibc/src/set.c @@ -19,6 +19,20 @@ BFSet BFSetCreate() { return (BFSet) res; } +void BFSetSetCompare(BFSet _set, int (*compare)(BFSetValue a, BFSetValue b)) { + _BFSet * set = (_BFSet *) _set; + if (!set) return; + + BFTreeSetCompare(set->tree, compare); +} + +void BFSetSetRelease(BFSet _set, void (*release)(BFSetValue value)) { + _BFSet * set = (_BFSet *) _set; + if (!set) return; + + BFTreeSetRelease(set->tree, release); +} + void BFSetRelease(BFSet _set) { _BFSet * set = (_BFSet *) _set; if (!set) return; @@ -27,3 +41,23 @@ void BFSetRelease(BFSet _set) { BFFree(set); } +int BFSetInsert(BFSet _set, BFSetValue value) { + _BFSet * set = (_BFSet *) _set; + if (!set) return -1; + + return BFTreeInsert(set->tree, value); +} + +int BFSetRemove(BFSet _set, BFSetValue value) { + _BFSet * set = (_BFSet *) _set; + if (!set) return -1; + + return BFTreeRemove(set->tree, value); +} + +bool BFSetContains(BFSet _set, BFSetValue value) { + _BFSet * set = (_BFSet *) _set; + if (!set) return -1; + + return BFTreeContains(set->tree, value); +} diff --git a/bflibc/src/set.h b/bflibc/src/set.h index 0eea309a..01699899 100644 --- a/bflibc/src/set.h +++ b/bflibc/src/set.h @@ -6,10 +6,18 @@ #ifndef SET_H #define SET_H +#include + typedef void * BFSet; +typedef void * BFSetValue; BFSet BFSetCreate(); +void BFSetSetCompare(BFSet set, int (*compare)(BFSetValue a, BFSetValue b)); +void BFSetSetRelease(BFSet set, void (*release)(BFSetValue value)); void BFSetRelease(BFSet set); +int BFSetInsert(BFSet set, BFSetValue value); +int BFSetRemove(BFSet set, BFSetValue value); +bool BFSetContains(BFSet set, BFSetValue value); #endif // SET_H diff --git a/bflibc/testbench/set_tests.h b/bflibc/testbench/set_tests.h new file mode 100644 index 00000000..4ebb6d63 --- /dev/null +++ b/bflibc/testbench/set_tests.h @@ -0,0 +1,49 @@ +/** + * author: Brando + * date: 5/14/25 + */ + +#ifndef SET_TESTS_H +#define SET_TESTS_H + +#include "clib_tests.h" +#include "set.h" + +BFTEST_UNIT_FUNC(test_setinit, 2<<10, { + BFSet set = BFSetCreate(); + BF_ASSERT(set, "null set"); + BFSetRelease(set); +}) + +int BFTestSetCompareInteger(BFSetValue aobj, BFSetValue bobj) { + int a = (intptr_t) aobj; + int b = (intptr_t) bobj; + return a - b; +} + +BFTEST_UNIT_FUNC(test_setcontains, 2<<10, { + BFSet set = BFSetCreate(); + BF_ASSERT(set, "null set"); + + BFSetSetCompare(set, BFTestSetCompareInteger); + + int max = 2 << 9; + for (int i = 0; i < max; i++) { + if (i % 2 == 0) { + BF_ASSERT(BFSetInsert(set, (BFSetValue) (intptr_t) i), "could not insert %d", i); + } + } + + for (int i = 0; i < max; i++) { + BF_ASSERT(BFSetContains(set, (BFSetValue) (intptr_t) i) == (i % 2 == 0 ? true : false), "contains(%d) != %s", (i % 2 == 0 ? "true" : "false")); + } + BFSetRelease(set); +}) + +BFTEST_COVERAGE_FUNC(set_tests, { + BFTEST_LAUNCH(test_setinit); + BFTEST_LAUNCH(test_setcontains); +}) + +#endif // SET_TESTS_H + diff --git a/bflibc/testbench/tests.c b/bflibc/testbench/tests.c index b2d3908a..9fee4a98 100644 --- a/bflibc/testbench/tests.c +++ b/bflibc/testbench/tests.c @@ -18,11 +18,13 @@ #include "map_tests.h" #include "tree_tests.h" #include "hashmap_tests.h" +#include "set_tests.h" #include BFTEST_SUITE_FUNC({ //BFTEST_SUITE_LAUNCH(checksum_tests); + /* BFTEST_SUITE_LAUNCH(coreutils_tests); BFTEST_SUITE_LAUNCH(lock_tests); BFTEST_SUITE_LAUNCH(filesystem_tests); @@ -37,6 +39,8 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(tree_tests); + */ + BFTEST_SUITE_LAUNCH(set_tests); }) From d6dcc4c2207f5b4bce8210eb087aaeebcec43c8c Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 15 May 2025 09:26:38 -0700 Subject: [PATCH 03/43] Accepting a 0 object --- bflibc/src/internal/tree.c | 4 ++-- bflibc/testbench/set_tests.h | 4 ++-- bflibc/testbench/tests.c | 2 -- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/bflibc/src/internal/tree.c b/bflibc/src/internal/tree.c index 85f796eb..f3309770 100644 --- a/bflibc/src/internal/tree.c +++ b/bflibc/src/internal/tree.c @@ -275,8 +275,8 @@ bool _BFTreeNodeSearch( ) { if (!node) { return false; - } else if (!obj) { - return false; + //} else if (!obj) { + // return false; } int comp = compare(obj, node->object); diff --git a/bflibc/testbench/set_tests.h b/bflibc/testbench/set_tests.h index 4ebb6d63..d77118fe 100644 --- a/bflibc/testbench/set_tests.h +++ b/bflibc/testbench/set_tests.h @@ -30,12 +30,12 @@ BFTEST_UNIT_FUNC(test_setcontains, 2<<10, { int max = 2 << 9; for (int i = 0; i < max; i++) { if (i % 2 == 0) { - BF_ASSERT(BFSetInsert(set, (BFSetValue) (intptr_t) i), "could not insert %d", i); + BF_ASSERT(BFSetInsert(set, (BFSetValue) (intptr_t) i) == 0, "could not insert %d", i); } } for (int i = 0; i < max; i++) { - BF_ASSERT(BFSetContains(set, (BFSetValue) (intptr_t) i) == (i % 2 == 0 ? true : false), "contains(%d) != %s", (i % 2 == 0 ? "true" : "false")); + BF_ASSERT(BFSetContains(set, (BFSetValue) (intptr_t) i) == (i % 2 == 0 ? true : false), "contains(%d) != %s", i, (i % 2 == 0 ? "true" : "false")); } BFSetRelease(set); }) diff --git a/bflibc/testbench/tests.c b/bflibc/testbench/tests.c index 9fee4a98..5e60077a 100644 --- a/bflibc/testbench/tests.c +++ b/bflibc/testbench/tests.c @@ -24,7 +24,6 @@ BFTEST_SUITE_FUNC({ //BFTEST_SUITE_LAUNCH(checksum_tests); - /* BFTEST_SUITE_LAUNCH(coreutils_tests); BFTEST_SUITE_LAUNCH(lock_tests); BFTEST_SUITE_LAUNCH(filesystem_tests); @@ -39,7 +38,6 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(tree_tests); - */ BFTEST_SUITE_LAUNCH(set_tests); }) From d2597b2bb012415c2e30b2e7def958b2a89773df Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 15 May 2025 13:00:30 -0700 Subject: [PATCH 04/43] set size --- bflibc/src/set.c | 10 +++++++++- bflibc/src/set.h | 2 ++ bflibc/testbench/set_tests.h | 17 +++++++++++++++++ bflibc/testbench/tests.c | 2 ++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/bflibc/src/set.c b/bflibc/src/set.c index 82cfc5ee..892c9292 100644 --- a/bflibc/src/set.c +++ b/bflibc/src/set.c @@ -57,7 +57,15 @@ int BFSetRemove(BFSet _set, BFSetValue value) { bool BFSetContains(BFSet _set, BFSetValue value) { _BFSet * set = (_BFSet *) _set; - if (!set) return -1; + if (!set) return false; return BFTreeContains(set->tree, value); } + +size_t BFSetGetSize(BFSet _set) { + _BFSet * set = (_BFSet *) _set; + if (!set) return 0; + + return BFTreeSize(set->tree); +} + diff --git a/bflibc/src/set.h b/bflibc/src/set.h index 01699899..8084aa8b 100644 --- a/bflibc/src/set.h +++ b/bflibc/src/set.h @@ -7,6 +7,7 @@ #define SET_H #include +#include typedef void * BFSet; typedef void * BFSetValue; @@ -18,6 +19,7 @@ void BFSetRelease(BFSet set); int BFSetInsert(BFSet set, BFSetValue value); int BFSetRemove(BFSet set, BFSetValue value); bool BFSetContains(BFSet set, BFSetValue value); +size_t BFSetGetSize(BFSet set); #endif // SET_H diff --git a/bflibc/testbench/set_tests.h b/bflibc/testbench/set_tests.h index d77118fe..46cd36e8 100644 --- a/bflibc/testbench/set_tests.h +++ b/bflibc/testbench/set_tests.h @@ -40,9 +40,26 @@ BFTEST_UNIT_FUNC(test_setcontains, 2<<10, { BFSetRelease(set); }) +BFTEST_UNIT_FUNC(test_setsize, 2<<10, { + BFSet set = BFSetCreate(); + BF_ASSERT(set, "null set"); + + BFSetSetCompare(set, BFTestSetCompareInteger); + + srand(time(0)); + int max = rand() % 2<<9; + for (int i = 0; i < max; i++) { + BF_ASSERT(BFSetInsert(set, (BFSetValue) (intptr_t) i) == 0, "could not insert %d", i); + } + + BF_ASSERT(BFSetGetSize(set) == max, "size(%ld) != %ld", BFSetGetSize(set), max); + BFSetRelease(set); +}) + BFTEST_COVERAGE_FUNC(set_tests, { BFTEST_LAUNCH(test_setinit); BFTEST_LAUNCH(test_setcontains); + BFTEST_LAUNCH(test_setsize); }) #endif // SET_TESTS_H diff --git a/bflibc/testbench/tests.c b/bflibc/testbench/tests.c index 5e60077a..9fee4a98 100644 --- a/bflibc/testbench/tests.c +++ b/bflibc/testbench/tests.c @@ -24,6 +24,7 @@ BFTEST_SUITE_FUNC({ //BFTEST_SUITE_LAUNCH(checksum_tests); + /* BFTEST_SUITE_LAUNCH(coreutils_tests); BFTEST_SUITE_LAUNCH(lock_tests); BFTEST_SUITE_LAUNCH(filesystem_tests); @@ -38,6 +39,7 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(tree_tests); + */ BFTEST_SUITE_LAUNCH(set_tests); }) From 3e38e1f1c447f611d1069b13df54bdeee3c66597 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 15 May 2025 13:37:56 -0700 Subject: [PATCH 05/43] documenting --- bflibc/src/set.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/bflibc/src/set.h b/bflibc/src/set.h index 8084aa8b..73ba3076 100644 --- a/bflibc/src/set.h +++ b/bflibc/src/set.h @@ -12,13 +12,46 @@ typedef void * BFSet; typedef void * BFSetValue; +/** + * creates set + * + * caller owns memory + */ BFSet BFSetCreate(); + +/** + * sets how the values are compared to one another + */ void BFSetSetCompare(BFSet set, int (*compare)(BFSetValue a, BFSetValue b)); + +/** + * how each value memory is handled once set is released + */ void BFSetSetRelease(BFSet set, void (*release)(BFSetValue value)); + +/** + * releases set from memory + */ void BFSetRelease(BFSet set); + +/** + * inserts value into set + */ int BFSetInsert(BFSet set, BFSetValue value); + +/** + * removes value from set + */ int BFSetRemove(BFSet set, BFSetValue value); + +/** + * checks if value is in set + */ bool BFSetContains(BFSet set, BFSetValue value); + +/** + * returns the current size of set + */ size_t BFSetGetSize(BFSet set); #endif // SET_H From 60237851185db296eb7cca28e7196acf151c3576 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 15 May 2025 14:30:02 -0700 Subject: [PATCH 06/43] init --- bflibc/src/internal/tree.c | 5 +++-- bflibc/testbench/tests.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/bflibc/src/internal/tree.c b/bflibc/src/internal/tree.c index f3309770..1f0a5022 100644 --- a/bflibc/src/internal/tree.c +++ b/bflibc/src/internal/tree.c @@ -96,11 +96,12 @@ _BFTreeNode * _BFTreeNodeInsert( return node; } - if (compare(object, node->object) < 0) { + int cmpval = compare(object, node->object); + if (cmpval < 0) { node->left = _BFTreeNodeInsert( node->left, object, compare, error ); - } else if (compare(object, node->object) > 0) { + } else if (cmpval > 0) { node->right = _BFTreeNodeInsert( node->right, object, compare, error ); diff --git a/bflibc/testbench/tests.c b/bflibc/testbench/tests.c index 9fee4a98..181ca535 100644 --- a/bflibc/testbench/tests.c +++ b/bflibc/testbench/tests.c @@ -36,10 +36,10 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(rand_tests); BFTEST_SUITE_LAUNCH(hash_tests); BFTEST_SUITE_LAUNCH(math_tests); + */ BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(tree_tests); - */ BFTEST_SUITE_LAUNCH(set_tests); }) From ad640233219fcd06523f0dc0f86ec23212c5d968 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 15 May 2025 14:44:43 -0700 Subject: [PATCH 07/43] incorporate flags --- bflibc/src/internal/tree.c | 7 +++-- bflibc/src/internal/tree.h | 12 ++++++++ bflibc/src/tree.c | 8 ++++++ bflibc/src/tree.h | 9 ++++++ bflibc/todo.md | 58 +++++++++++++++++++------------------- 5 files changed, 63 insertions(+), 31 deletions(-) diff --git a/bflibc/src/internal/tree.c b/bflibc/src/internal/tree.c index 1f0a5022..43db8e72 100644 --- a/bflibc/src/internal/tree.c +++ b/bflibc/src/internal/tree.c @@ -87,6 +87,7 @@ _BFTreeNode * _BFTreeNodeInsert( _BFTreeNode * node, BFTreeObject object, int (*compare)(BFTreeObject a, BFTreeObject b), + unsigned char flags, int * error ) { // 1. Perform the normal BST insertion @@ -99,13 +100,15 @@ _BFTreeNode * _BFTreeNodeInsert( int cmpval = compare(object, node->object); if (cmpval < 0) { node->left = _BFTreeNodeInsert( - node->left, object, compare, error + node->left, object, compare, flags, error ); } else if (cmpval > 0) { node->right = _BFTreeNodeInsert( - node->right, object, compare, error + node->right, object, compare, flags, error ); } else { // Equal keys are not allowed in BST + bool allowDuplicates = flags & (0x01 << _BFTREE_FLAG_ALLOW_DUPLICATES); + if (error) *error = -1; return node; } diff --git a/bflibc/src/internal/tree.h b/bflibc/src/internal/tree.h index a78a3867..e5615161 100644 --- a/bflibc/src/internal/tree.h +++ b/bflibc/src/internal/tree.h @@ -35,18 +35,30 @@ typedef struct _BFTree { * BFTreeRelease() is called */ void (*release)(BFTreeObject object); + + /** + * |x|x|x|x|x|x|x|| + * + * : default value == 0. 0: not allow. 1: allow + */ + unsigned char flags; } _BFTree; +#define _BFTREE_FLAG_ALLOW_DUPLICATES 0 + /** * error: will nonzero if object couldn't be inserted. * one reason is there may be a duplicate * + * flags: _BFTree::flags + * * returns node */ _BFTreeNode * _BFTreeNodeInsert( _BFTreeNode * node, BFTreeObject object, int (*compare)(BFTreeObject a, BFTreeObject b), + unsigned char flags, int * error ); diff --git a/bflibc/src/tree.c b/bflibc/src/tree.c index fe8ab2c0..9e2d2254 100644 --- a/bflibc/src/tree.c +++ b/bflibc/src/tree.c @@ -27,6 +27,13 @@ void BFTreeSetRelease(BFTree tree, void (*release)(BFTreeObject object)) { ((_BFTree *) tree)->release = release; } +void BFTreeSetAllowDuplicates(BFTree _tree, bool allow) { + _BFTree * tree = (_BFTree *) _tree; + if (!tree) return; + + tree->flags = tree->flags ^ (0x01 << _BFTREE_FLAG_ALLOW_DUPLICATES); +} + // left->right->node void BFTreeReleaseNode(_BFTree * tree, _BFTreeNode * node) { if (!node) return; @@ -64,6 +71,7 @@ int BFTreeInsert(BFTree _tree, BFTreeObject object) { tree->root, object, tree->compare, + tree->flags, &err ); diff --git a/bflibc/src/tree.h b/bflibc/src/tree.h index 9f6484a7..766ba962 100644 --- a/bflibc/src/tree.h +++ b/bflibc/src/tree.h @@ -1,6 +1,8 @@ /** * author: brando * date: 11/22/24 + * + * binary search tree (avl implementation) */ #ifndef TREE_H @@ -32,6 +34,13 @@ void BFTreeSetCompare(BFTree tree, int (*compare)(BFTreeObject a, BFTreeObject b */ void BFTreeSetRelease(BFTree tree, void (*release)(BFTreeObject object)); +/** + * allow == true to permit the use of duplicates in this binary tree + * + * by default, this binary tree does NOT allow duplicates. + */ +void BFTreeSetAllowDuplicates(BFTree tree, bool allow); + /** * frees tree * diff --git a/bflibc/todo.md b/bflibc/todo.md index 5bc6fa43..dc5cba1c 100644 --- a/bflibc/todo.md +++ b/bflibc/todo.md @@ -1,29 +1,29 @@ -``` -[] queue object - [] add to the traversal test case for trees "Level Order Traversal" -[x] add documentation to hash, map, tree, hashmap -[x] remove the pair from map header -[x] add node accessor to tree class -[] sprintf is deprecated -[] improve sqrt, it sometimes stalls - [x] use binary search - [] store all known square root in a map -[x] BFMapContains -[x] map - [x] make a tree object so data store is close to O(logn) - [x] make a hash function good enough to use for O(1) operations -[] improve prime num getter using hash map -[] remove checksum -[x] add tests to nonexistent entries in tree, map, and hashmap -[x] link math lib for linux only -[x] put all these flags into the libs file and couple it -[x] make hashing function for bflibcpp library hash maps -[x] make a max and min function like python3 using macros -[x] random generator -[x] abs() in bfmath -[x] tree - [x] insertion - [x] removal - [x] getter -[x] consider allowing duplicate values into tree - no -``` +- [ ] queue object + - [ ] add to the traversal test case for trees "Level Order Traversal" +- [x] add documentation to hash, map, tree, hashmap +- [x] remove the pair from map header +- [x] add node accessor to tree class +- [ ] sprintf is deprecated +- [ ] improve sqrt, it sometimes stalls + - [x] use binary search + - [ ] store all known square root in a map +- [x] BFMapContains +- [x] map + - [x] make a tree object so data store is close to O(logn) + - [x] make a hash function good enough to use for O(1) operations +- [ ] improve prime num getter using hash map +- [ ] remove checksum +- [x] add tests to nonexistent entries in tree, map, and hashmap +- [x] link math lib for linux only +- [x] put all these flags into the libs file and couple it +- [x] make hashing function for bflibcpp library hash maps +- [x] make a max and min function like python3 using macros +- [x] random generator +- [x] abs() in bfmath +- [x] tree + - [x] insertion + - [x] removal + - [x] getter +- [x] consider allowing duplicate values into tree - no +- [ ] Set object + - [ ] allow tree to permit duplicates From 2fc1038d3390aec161420277a44b49e6f0e58078 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 15 May 2025 19:02:09 -0700 Subject: [PATCH 08/43] making sure I initialize correctly --- bflibc/src/internal/tree.c | 32 +++++++++++++++------- bflibc/src/internal/tree.h | 11 +++++++- bflibc/src/tree.c | 14 ++++++++-- bflibc/src/tree.h | 1 + bflibc/testbench/tests.c | 2 +- bflibc/testbench/tree_tests.h | 51 ++++++++++++++++++++++++++++------- 6 files changed, 88 insertions(+), 23 deletions(-) diff --git a/bflibc/src/internal/tree.c b/bflibc/src/internal/tree.c index 43db8e72..f0008694 100644 --- a/bflibc/src/internal/tree.c +++ b/bflibc/src/internal/tree.c @@ -16,6 +16,7 @@ _BFTreeNode * _BFTreeNodeCreate() { res->right = NULL; res->object = NULL; res->height = 1; + res->count = 0; return res; } @@ -107,9 +108,11 @@ _BFTreeNode * _BFTreeNodeInsert( node->right, object, compare, flags, error ); } else { // Equal keys are not allowed in BST - bool allowDuplicates = flags & (0x01 << _BFTREE_FLAG_ALLOW_DUPLICATES); - - if (error) *error = -1; + if ((flags & (0x01 << _BFTREE_FLAG_ALLOW_DUPLICATES)) != 0) { + node->count++; + } else { + if (error) *error = -11; + } return node; } @@ -174,7 +177,8 @@ _BFTreeNode * _BFTreeNodeRemove( _BFTreeNode * root, BFTreeObject object, int (*compare)(BFTreeObject a, BFTreeObject b), - void (*release)(BFTreeObject object) + void (*release)(BFTreeObject object), + unsigned char flags ) { // STEP 1: PERFORM STANDARD BST DELETE @@ -184,19 +188,29 @@ _BFTreeNode * _BFTreeNodeRemove( // If the key to be deleted is smaller than the // root's key, then it lies in left subtree - if (compare(object, root->object) < 0) { + int cmpval = compare(object, root->object); + if (cmpval < 0) { root->left = _BFTreeNodeRemove( - root->left, object, compare, release + root->left, object, compare, release, flags ); // If the key to be deleted is greater than the // root's key, then it lies in right subtree - } else if (compare(object, root->object) > 0) { + } else if (cmpval > 0) { root->right = _BFTreeNodeRemove( - root->right, object, compare, release + root->right, object, compare, release, flags ); // if key is same as root's key, then This is // the node to be deleted + // + // unless duplicates are allowed } else { + if (flags & (0x01 << _BFTREE_FLAG_ALLOW_DUPLICATES)) { + if (root->count > 0) { + root->count--; + return root; + } + } + // node with only one child or no child if (root->left == NULL || root->right == NULL) { _BFTreeNode * temp = root->left ? root->left : root->right; @@ -227,7 +241,7 @@ _BFTreeNode * _BFTreeNodeRemove( // Delete the inorder successor root->right = _BFTreeNodeRemove( - root->right, temp->object, compare, NULL + root->right, temp->object, compare, NULL, flags ); } } diff --git a/bflibc/src/internal/tree.h b/bflibc/src/internal/tree.h index e5615161..5cb5ef5e 100644 --- a/bflibc/src/internal/tree.h +++ b/bflibc/src/internal/tree.h @@ -13,6 +13,12 @@ typedef struct _BFTreeNode { struct _BFTreeNode * right; size_t height; BFTreeObject object; + + /** + * IIF duplicates are allowed this will hold the count of + * instances of object in the tree + */ + size_t count; } _BFTreeNode; _BFTreeNode * _BFTreeNodeCreate(); @@ -63,13 +69,16 @@ _BFTreeNode * _BFTreeNodeInsert( ); /** + * flags: _BFTree::flags + * * returns node */ _BFTreeNode * _BFTreeNodeRemove( _BFTreeNode * node, BFTreeObject object, int (*compare)(BFTreeObject a, BFTreeObject b), - void (*release)(BFTreeObject object) + void (*release)(BFTreeObject object), + unsigned char flags ); /** diff --git a/bflibc/src/tree.c b/bflibc/src/tree.c index 9e2d2254..63abd0b0 100644 --- a/bflibc/src/tree.c +++ b/bflibc/src/tree.c @@ -14,6 +14,7 @@ BFTree BFTreeCreate() { res->compare = NULL; res->release = NULL; res->size = 0; + res->flags = 0x00; return (BFTree) res; } @@ -27,11 +28,20 @@ void BFTreeSetRelease(BFTree tree, void (*release)(BFTreeObject object)) { ((_BFTree *) tree)->release = release; } +bool BFTreeGetAllowDuplicates(BFTree _tree) { + _BFTree * tree = (_BFTree *) _tree; + if (!tree) return false; + + return (tree->flags & (0x01 << _BFTREE_FLAG_ALLOW_DUPLICATES)) != 0; +} + void BFTreeSetAllowDuplicates(BFTree _tree, bool allow) { _BFTree * tree = (_BFTree *) _tree; if (!tree) return; - tree->flags = tree->flags ^ (0x01 << _BFTREE_FLAG_ALLOW_DUPLICATES); + unsigned char bit = allow ? 0x01 : 0x00; + + tree->flags = tree->flags ^ (bit << _BFTREE_FLAG_ALLOW_DUPLICATES); } // left->right->node @@ -88,7 +98,7 @@ int BFTreeRemove(BFTree _tree, BFTreeObject object) { } _BFTree * tree = (_BFTree *) _tree; tree->root = _BFTreeNodeRemove( - tree->root, object, tree->compare, tree->release + tree->root, object, tree->compare, tree->release, tree->flags ); tree->size--; diff --git a/bflibc/src/tree.h b/bflibc/src/tree.h index 766ba962..08163b20 100644 --- a/bflibc/src/tree.h +++ b/bflibc/src/tree.h @@ -40,6 +40,7 @@ void BFTreeSetRelease(BFTree tree, void (*release)(BFTreeObject object)); * by default, this binary tree does NOT allow duplicates. */ void BFTreeSetAllowDuplicates(BFTree tree, bool allow); +bool BFTreeGetAllowDuplicates(BFTree tree); /** * frees tree diff --git a/bflibc/testbench/tests.c b/bflibc/testbench/tests.c index 181ca535..efd554a3 100644 --- a/bflibc/testbench/tests.c +++ b/bflibc/testbench/tests.c @@ -36,9 +36,9 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(rand_tests); BFTEST_SUITE_LAUNCH(hash_tests); BFTEST_SUITE_LAUNCH(math_tests); - */ BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(map_tests); + */ BFTEST_SUITE_LAUNCH(tree_tests); BFTEST_SUITE_LAUNCH(set_tests); diff --git a/bflibc/testbench/tree_tests.h b/bflibc/testbench/tree_tests.h index 41a55f55..31a18301 100644 --- a/bflibc/testbench/tree_tests.h +++ b/bflibc/testbench/tree_tests.h @@ -132,7 +132,6 @@ BFTEST_UNIT_FUNC(test_InsertNodesAndSearch, 2<<10, { }) void BFTestTreeFree(BFTreeObject object) { - //printf("freeing: %p\n", object); free(object); } @@ -156,7 +155,6 @@ BFTEST_UNIT_FUNC(test_InsertAndRemovingNodes, 2<<10, { *objects[i] = i; // insert into tree - //printf("inserting: %p\n", objects[i]); int err = BFTreeInsert(tree, objects[i]); BF_ASSERT(err == 0, "node insertion failed, node(obj=%d)", i); } @@ -166,15 +164,10 @@ BFTEST_UNIT_FUNC(test_InsertAndRemovingNodes, 2<<10, { // remove nodes int randNumSearch = 20; while (randNumSearch--) { - //for (int i = 0; i < randNumSearch; i++) { - //for (int i = treesize - 1; i >= treesize - randNumSearch; i--) { int index = abs(BFRand()) % treesize; - //int index = i; int * object = objects[index]; - //printf("can we remove %p\n", object); if (object && BFTreeContains(tree, object)) { - //printf("removing %p\n", object); int err = BFTreeRemove(tree, object); BF_ASSERT(err == 0, "couldn't remove node for object=%d", *object); @@ -187,11 +180,48 @@ BFTEST_UNIT_FUNC(test_InsertAndRemovingNodes, 2<<10, { //BFTestTreePrint(tree->root); } - //printf("freeing tree\n"); BFTreeRelease(tree); }) -BFTEST_UNIT_FUNC(test_InsertDuplicates, 2<<10, { +BFTEST_UNIT_FUNC(test_InsertDuplicatesAllowed, 2<<10, { + if (BFTEST_UNIT_FUNC_ITR == 0) { + BFRandInit(time(0)); + } + + // create trees + BFTree tree = BFTreeCreate(); + BF_ASSERT(tree, "a null tree was returned"); + BFTreeSetCompare(tree, BFTestTreeCompare); + BFTreeSetRelease(tree, free); + BFTreeSetAllowDuplicates(tree, true); + + BF_ASSERT(BFTreeGetAllowDuplicates(tree)); + + int val = BFRand(); + + // object 1 + int * object = (int *) malloc(sizeof(int)); + *object = val; + int err = BFTreeInsert(tree, object); + BF_ASSERT(err == 0, "error inserting %d", err); + + // object 2 using the same value + object = (int *) malloc(sizeof(int)); + *object = val; + err = BFTreeInsert(tree, object); + BF_ASSERT(err == 0, "should not be an error inserting %d because we allow duplicates", err); + + // now free since the tree doesn't hold onto it + //BFFree(object); + + if (BFTEST_UNIT_FUNC_ITR == 0) { + //BFTestTreePrint(tree->root); + } + + BFTreeRelease(tree); +}) + +BFTEST_UNIT_FUNC(test_InsertDuplicatesNotAllowed, 2<<10, { if (BFTEST_UNIT_FUNC_ITR == 0) { BFRandInit(time(0)); } @@ -373,7 +403,8 @@ BFTEST_COVERAGE_FUNC(tree_tests, { BFTEST_LAUNCH(test_InsertNodes); BFTEST_LAUNCH(test_InsertNodesAndSearch); BFTEST_LAUNCH(test_InsertAndRemovingNodes); - BFTEST_LAUNCH(test_InsertDuplicates); + BFTEST_LAUNCH(test_InsertDuplicatesAllowed); + BFTEST_LAUNCH(test_InsertDuplicatesNotAllowed); BFTEST_LAUNCH(test_InsertAndRemovingNoPointers); BFTEST_LAUNCH(test_treeGettingNonexistentValues); BFTEST_LAUNCH(test_treeTraversal); From c0d2cd5183aa74e664c4db82ec99752eaf83edd3 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 15 May 2025 19:40:33 -0700 Subject: [PATCH 09/43] testing to allow duplicates --- bflibc/src/tree.h | 4 ++++ bflibc/testbench/tests.c | 2 -- bflibc/testbench/tree_tests.h | 26 ++++++++++++++++++++++++++ bflibc/todo.md | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/bflibc/src/tree.h b/bflibc/src/tree.h index 08163b20..b29ae60a 100644 --- a/bflibc/src/tree.h +++ b/bflibc/src/tree.h @@ -40,6 +40,10 @@ void BFTreeSetRelease(BFTree tree, void (*release)(BFTreeObject object)); * by default, this binary tree does NOT allow duplicates. */ void BFTreeSetAllowDuplicates(BFTree tree, bool allow); + +/** + * returns true if duplicates are allowed + */ bool BFTreeGetAllowDuplicates(BFTree tree); /** diff --git a/bflibc/testbench/tests.c b/bflibc/testbench/tests.c index efd554a3..5e60077a 100644 --- a/bflibc/testbench/tests.c +++ b/bflibc/testbench/tests.c @@ -24,7 +24,6 @@ BFTEST_SUITE_FUNC({ //BFTEST_SUITE_LAUNCH(checksum_tests); - /* BFTEST_SUITE_LAUNCH(coreutils_tests); BFTEST_SUITE_LAUNCH(lock_tests); BFTEST_SUITE_LAUNCH(filesystem_tests); @@ -38,7 +37,6 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(math_tests); BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(map_tests); - */ BFTEST_SUITE_LAUNCH(tree_tests); BFTEST_SUITE_LAUNCH(set_tests); diff --git a/bflibc/testbench/tree_tests.h b/bflibc/testbench/tree_tests.h index 31a18301..5b38bafa 100644 --- a/bflibc/testbench/tree_tests.h +++ b/bflibc/testbench/tree_tests.h @@ -221,6 +221,31 @@ BFTEST_UNIT_FUNC(test_InsertDuplicatesAllowed, 2<<10, { BFTreeRelease(tree); }) +int BFTestTreeCompareInteger(BFTreeObject aobj, BFTreeObject bobj) { + int a = (intptr_t) aobj; + int b = (intptr_t) bobj; + return a - b; +} + +BFTEST_UNIT_FUNC(test_InsertDuplicatesAllowedEvenMore, 2<<10, { + // create trees + BFTree tree = BFTreeCreate(); + BF_ASSERT(tree, "a null tree was returned"); + BFTreeSetCompare(tree, BFTestTreeCompareInteger); + BFTreeSetAllowDuplicates(tree, true); + + BF_ASSERT(BFTreeGetAllowDuplicates(tree)); + + int max = 2 << 9; + for (int i = 0; i < max; i++) { + BFTreeInsert(tree, (BFTreeObject) (intptr_t) 0xFF); + } + + BF_ASSERT(BFTreeSize(tree) == max); + + BFTreeRelease(tree); +}) + BFTEST_UNIT_FUNC(test_InsertDuplicatesNotAllowed, 2<<10, { if (BFTEST_UNIT_FUNC_ITR == 0) { BFRandInit(time(0)); @@ -404,6 +429,7 @@ BFTEST_COVERAGE_FUNC(tree_tests, { BFTEST_LAUNCH(test_InsertNodesAndSearch); BFTEST_LAUNCH(test_InsertAndRemovingNodes); BFTEST_LAUNCH(test_InsertDuplicatesAllowed); + BFTEST_LAUNCH(test_InsertDuplicatesAllowedEvenMore); BFTEST_LAUNCH(test_InsertDuplicatesNotAllowed); BFTEST_LAUNCH(test_InsertAndRemovingNoPointers); BFTEST_LAUNCH(test_treeGettingNonexistentValues); diff --git a/bflibc/todo.md b/bflibc/todo.md index dc5cba1c..ab846a0f 100644 --- a/bflibc/todo.md +++ b/bflibc/todo.md @@ -26,4 +26,4 @@ - [x] getter - [x] consider allowing duplicate values into tree - no - [ ] Set object - - [ ] allow tree to permit duplicates + - [x] allow tree to permit duplicates From 65bd4a7683caf609a49f03e540b332c9a0a5e841 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 15 May 2025 20:11:43 -0700 Subject: [PATCH 10/43] init --- bflibc/src/hashset.c | 71 ++++++++++++++++++++++++++++++++++++++++++++ bflibc/src/hashset.h | 58 ++++++++++++++++++++++++++++++++++++ bflibc/todo.md | 3 ++ 3 files changed, 132 insertions(+) create mode 100644 bflibc/src/hashset.c create mode 100644 bflibc/src/hashset.h diff --git a/bflibc/src/hashset.c b/bflibc/src/hashset.c new file mode 100644 index 00000000..329c5e13 --- /dev/null +++ b/bflibc/src/hashset.c @@ -0,0 +1,71 @@ +/** + * author: brando + * date: 5/14/25 + */ + +#include "hashset.h" +#include "tree.h" +#include "free.h" + +typedef struct _BFHashSet { + BFHashMap tree; +} _BFHashSet; + +BFHashSet BFHashSetCreate() { + _BFHashSet * res = (_BFHashSet *) malloc(sizeof(_BFHashSet)); + if (!res) return NULL; + + res->tree = BFHashMapCreate(); + return (BFHashSet) res; +} + +void BFHashSetSetCompare(BFHashSet _set, int (*compare)(BFHashSetValue a, BFHashSetValue b)) { + _BFHashSet * set = (_BFHashSet *) _set; + if (!set) return; + + BFHashMapSetCompare(set->tree, compare); +} + +void BFHashSetSetRelease(BFHashSet _set, void (*release)(BFHashSetValue value)) { + _BFHashSet * set = (_BFHashSet *) _set; + if (!set) return; + + BFHashMapSetRelease(set->tree, release); +} + +void BFHashSetRelease(BFHashSet _set) { + _BFHashSet * set = (_BFHashSet *) _set; + if (!set) return; + + BFHashMapRelease(set->tree); + BFFree(set); +} + +int BFHashSetInsert(BFHashSet _set, BFHashSetValue value) { + _BFHashSet * set = (_BFHashSet *) _set; + if (!set) return -1; + + return BFHashMapInsert(set->tree, value); +} + +int BFHashSetRemove(BFHashSet _set, BFHashSetValue value) { + _BFHashSet * set = (_BFHashSet *) _set; + if (!set) return -1; + + return BFHashMapRemove(set->tree, value); +} + +bool BFHashSetContains(BFHashSet _set, BFHashSetValue value) { + _BFHashSet * set = (_BFHashSet *) _set; + if (!set) return false; + + return BFHashMapContains(set->tree, value); +} + +size_t BFHashSetGetSize(BFHashSet _set) { + _BFHashSet * set = (_BFHashSet *) _set; + if (!set) return 0; + + return BFHashMapSize(set->tree); +} + diff --git a/bflibc/src/hashset.h b/bflibc/src/hashset.h new file mode 100644 index 00000000..7214ef88 --- /dev/null +++ b/bflibc/src/hashset.h @@ -0,0 +1,58 @@ +/** + * author: brando + * date: 5/14/25 + */ + +#ifndef HASHSET_H +#define HASHSET_H + +#include +#include + +typedef void * BFHashSet; +typedef void * BFHashSetValue; + +/** + * creates set + * + * caller owns memory + */ +BFHashSet BFHashSetCreate(); + +/** + * sets how the values are compared to one another + */ +void BFHashSetSetCompare(BFHashSet set, int (*compare)(BFHashSetValue a, BFHashSetValue b)); + +/** + * how each value memory is handled once set is released + */ +void BFHashSetSetRelease(BFHashSet set, void (*release)(BFHashSetValue value)); + +/** + * releases set from memory + */ +void BFHashSetRelease(BFHashSet set); + +/** + * inserts value into set + */ +int BFHashSetInsert(BFHashSet set, BFHashSetValue value); + +/** + * removes value from set + */ +int BFHashSetRemove(BFHashSet set, BFHashSetValue value); + +/** + * checks if value is in set + */ +bool BFHashSetContains(BFHashSet set, BFHashSetValue value); + +/** + * returns the current size of set + */ +size_t BFHashSetGetSize(BFHashSet set); + +#endif // HASHSET_H + diff --git a/bflibc/todo.md b/bflibc/todo.md index ab846a0f..482bd28b 100644 --- a/bflibc/todo.md +++ b/bflibc/todo.md @@ -27,3 +27,6 @@ - [x] consider allowing duplicate values into tree - no - [ ] Set object - [x] allow tree to permit duplicates + - [x] tree set + - [ ] hash set + From 5eccdf3d21b6f7e2f716401f72ba9aeeb4e4ca88 Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 16 May 2025 08:11:11 -0700 Subject: [PATCH 11/43] setting up hashset --- bflibc/makefile | 2 +- bflibc/src/hashset.c | 45 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/bflibc/makefile b/bflibc/makefile index d693ea7e..908501b3 100644 --- a/bflibc/makefile +++ b/bflibc/makefile @@ -27,7 +27,7 @@ filesystem coreutils stringutils \ bftime thread bfmath \ lock filewriter hash \ rand map tree internal/tree \ -hashmap set +hashmap set hashset ### Release settings ifeq ($(CONFIG),release) # release diff --git a/bflibc/src/hashset.c b/bflibc/src/hashset.c index 329c5e13..f56f8907 100644 --- a/bflibc/src/hashset.c +++ b/bflibc/src/hashset.c @@ -4,18 +4,43 @@ */ #include "hashset.h" -#include "tree.h" +#include "hashmap.h" #include "free.h" typedef struct _BFHashSet { - BFHashMap tree; + /** + * we will use a hash map with this schema: + * : -> : + */ + BFHashMap map; + + void (*release)(BFHashSetValue value); } _BFHashSet; +/** + * value: hashset object + * key: hashset value + */ +void _BFHashSetReleaseHashMap(BFHashSetValue value, BFHashSet _set) { + _BFHashSet * set = (_BFHashSet *) _set; + if (!set && !set->release) return; + + set->release(value); +} + BFHashSet BFHashSetCreate() { _BFHashSet * res = (_BFHashSet *) malloc(sizeof(_BFHashSet)); if (!res) return NULL; - res->tree = BFHashMapCreate(); + res->release = NULL; + + res->map = BFHashMapCreate(); + if (!res->map) { + return NULL; + } + + BFHashMapSetRelease(res->map, _BFHashSetReleaseHashMap); + return (BFHashSet) res; } @@ -23,21 +48,21 @@ void BFHashSetSetCompare(BFHashSet _set, int (*compare)(BFHashSetValue a, BFHash _BFHashSet * set = (_BFHashSet *) _set; if (!set) return; - BFHashMapSetCompare(set->tree, compare); + BFHashMapSetCompare(set->map, compare); } void BFHashSetSetRelease(BFHashSet _set, void (*release)(BFHashSetValue value)) { _BFHashSet * set = (_BFHashSet *) _set; if (!set) return; - BFHashMapSetRelease(set->tree, release); + set->release = release; } void BFHashSetRelease(BFHashSet _set) { _BFHashSet * set = (_BFHashSet *) _set; if (!set) return; - BFHashMapRelease(set->tree); + BFHashMapRelease(set->map); BFFree(set); } @@ -45,27 +70,27 @@ int BFHashSetInsert(BFHashSet _set, BFHashSetValue value) { _BFHashSet * set = (_BFHashSet *) _set; if (!set) return -1; - return BFHashMapInsert(set->tree, value); + return BFHashMapInsert(set->map, value, set); } int BFHashSetRemove(BFHashSet _set, BFHashSetValue value) { _BFHashSet * set = (_BFHashSet *) _set; if (!set) return -1; - return BFHashMapRemove(set->tree, value); + return BFHashMapRemove(set->map, value); } bool BFHashSetContains(BFHashSet _set, BFHashSetValue value) { _BFHashSet * set = (_BFHashSet *) _set; if (!set) return false; - return BFHashMapContains(set->tree, value); + return BFHashMapContains(set->map, value); } size_t BFHashSetGetSize(BFHashSet _set) { _BFHashSet * set = (_BFHashSet *) _set; if (!set) return 0; - return BFHashMapSize(set->tree); + return BFHashMapGetSize(set->map); } From bc7f7cae7a7218c547d3af5b148a4da63f92dc50 Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 16 May 2025 09:13:58 -0700 Subject: [PATCH 12/43] Hash set tests --- bflibc/src/hashset.c | 7 +++ bflibc/src/hashset.h | 5 +++ bflibc/testbench/hashset_tests.h | 75 ++++++++++++++++++++++++++++++++ bflibc/testbench/tests.c | 5 ++- 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 bflibc/testbench/hashset_tests.h diff --git a/bflibc/src/hashset.c b/bflibc/src/hashset.c index f56f8907..ebd389d6 100644 --- a/bflibc/src/hashset.c +++ b/bflibc/src/hashset.c @@ -51,6 +51,13 @@ void BFHashSetSetCompare(BFHashSet _set, int (*compare)(BFHashSetValue a, BFHash BFHashMapSetCompare(set->map, compare); } +void BFHashSetSetHashFunction(BFHashSet _set, unsigned long (*hash)(BFHashSetValue value)) { + _BFHashSet * set = (_BFHashSet *) _set; + if (!set) return; + + BFHashMapSetHashFunction(set->map, hash); +} + void BFHashSetSetRelease(BFHashSet _set, void (*release)(BFHashSetValue value)) { _BFHashSet * set = (_BFHashSet *) _set; if (!set) return; diff --git a/bflibc/src/hashset.h b/bflibc/src/hashset.h index 7214ef88..b535218d 100644 --- a/bflibc/src/hashset.h +++ b/bflibc/src/hashset.h @@ -29,6 +29,11 @@ void BFHashSetSetCompare(BFHashSet set, int (*compare)(BFHashSetValue a, BFHashS */ void BFHashSetSetRelease(BFHashSet set, void (*release)(BFHashSetValue value)); +/** + * sets the hash function + */ +void BFHashSetSetHashFunction(BFHashSet set, unsigned long (*hash)(BFHashSetValue key)); + /** * releases set from memory */ diff --git a/bflibc/testbench/hashset_tests.h b/bflibc/testbench/hashset_tests.h new file mode 100644 index 00000000..a6f80287 --- /dev/null +++ b/bflibc/testbench/hashset_tests.h @@ -0,0 +1,75 @@ +/** + * author: Brando + * date: 5/16/25 + */ + +#ifndef HASHSET_TESTS_H +#define HASHSET_TESTS_H + +#include "clib_tests.h" +#include "hashset.h" +#include "hash.h" +#include + +unsigned long BFTestHashSetHashString(BFHashSetValue val) { + return BFHashDjb2((unsigned char *) val); +} + +int BFTestHashSetKeyCompare(BFHashSetValue aval, BFHashSetValue bval) { + const char * a = (const char *) aval; + const char * b = (const char *) bval; + return strcmp(a, b); +} + +char * _CreateRandomWord() { + const char charset[] = "abcdefghijklmnopqrstuvwxyz"; + size_t charset_size = strlen(charset); + + srand(time(0)); + + size_t reslen = 2 << 3; + char * res = (char *) malloc(sizeof(char) * (reslen + 1)); + for (int i = 0; i < reslen; i++) { + res[i] = charset[rand() % charset_size]; + } + + res[reslen] = '\0'; + + return res; +} + +BFTEST_UNIT_FUNC(test_hashsetinit, 2<<10, { + BFHashSet set = BFHashSetCreate(); + BF_ASSERT(set, "null hash set"); + BFHashSetSetHashFunction(set, BFTestHashSetHashString); + BFHashSetSetCompare(set, BFTestHashSetKeyCompare); + BFHashSetSetRelease(set, free); + BFHashSetRelease(set); +}) + +BFTEST_UNIT_FUNC(test_hashsetsize, 2<<10, { + BFHashSet set = BFHashSetCreate(); + BF_ASSERT(set, "null set"); + + BFHashSetSetHashFunction(set, BFTestHashSetHashString); + BFHashSetSetCompare(set, BFTestHashSetKeyCompare); + BFHashSetSetRelease(set, free); + + srand(time(0)); + int max = rand() % 2<<9; + for (int i = 0; i < max; i++) { + char * randword = _CreateRandomWord(); + BF_ASSERT(BFHashSetInsert(set, randword) == 0, "could not insert '%s'", randword); + } + + BF_ASSERT(BFHashSetGetSize(set) == max, "size(%ld) != %ld", BFHashSetGetSize(set), max); + BFHashSetRelease(set); +}) + +BFTEST_COVERAGE_FUNC(hashset_tests, { + BFTEST_LAUNCH(test_hashsetinit); + BFTEST_LAUNCH(test_hashsetsize); +}) + +#endif // HASHSET_TESTS_H + diff --git a/bflibc/testbench/tests.c b/bflibc/testbench/tests.c index 5e60077a..bc3eb152 100644 --- a/bflibc/testbench/tests.c +++ b/bflibc/testbench/tests.c @@ -19,11 +19,13 @@ #include "tree_tests.h" #include "hashmap_tests.h" #include "set_tests.h" +#include "hashset_tests.h" #include BFTEST_SUITE_FUNC({ //BFTEST_SUITE_LAUNCH(checksum_tests); + /* BFTEST_SUITE_LAUNCH(coreutils_tests); BFTEST_SUITE_LAUNCH(lock_tests); BFTEST_SUITE_LAUNCH(filesystem_tests); @@ -39,6 +41,7 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(tree_tests); BFTEST_SUITE_LAUNCH(set_tests); - + */ + BFTEST_SUITE_LAUNCH(hashset_tests); }) From 5ead2d791fd92c87457bd28e5c75c271959f4c38 Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 16 May 2025 12:43:42 -0700 Subject: [PATCH 13/43] clean up --- bflibc/testbench/set_tests.h | 1 + bflibc/testbench/tests.c | 2 +- bftest/src/bftest.h | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/bflibc/testbench/set_tests.h b/bflibc/testbench/set_tests.h index 46cd36e8..33f9ac2b 100644 --- a/bflibc/testbench/set_tests.h +++ b/bflibc/testbench/set_tests.h @@ -37,6 +37,7 @@ BFTEST_UNIT_FUNC(test_setcontains, 2<<10, { for (int i = 0; i < max; i++) { BF_ASSERT(BFSetContains(set, (BFSetValue) (intptr_t) i) == (i % 2 == 0 ? true : false), "contains(%d) != %s", i, (i % 2 == 0 ? "true" : "false")); } + BFSetRelease(set); }) diff --git a/bflibc/testbench/tests.c b/bflibc/testbench/tests.c index bc3eb152..edfc237a 100644 --- a/bflibc/testbench/tests.c +++ b/bflibc/testbench/tests.c @@ -40,8 +40,8 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(tree_tests); - BFTEST_SUITE_LAUNCH(set_tests); */ + BFTEST_SUITE_LAUNCH(set_tests); BFTEST_SUITE_LAUNCH(hashset_tests); }) diff --git a/bftest/src/bftest.h b/bftest/src/bftest.h index eba2f627..3e969a99 100644 --- a/bftest/src/bftest.h +++ b/bftest/src/bftest.h @@ -95,6 +95,8 @@ void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t buf int result = 0;\ time_t startTime = __BFTestGetCurrentTimeNS__(); +// the total time executed might be innaccurate considering +// i am running usleep(50) in the loop #define BFTEST_UNIT_END \ time_t endTime = __BFTestGetCurrentTimeNS__();\ if (result == 0) { printf("PASS"); }\ From 0b7c35c6c0f5ab0b57ee46a172085f2bb7a04a05 Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 16 May 2025 14:42:42 -0700 Subject: [PATCH 14/43] adding todo --- bflibcpp/todo.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index 13691c3f..66024c99 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -31,3 +31,8 @@ - [x] Ability to key a HashMap without defining a compare callback, at least for our standard objects - [ ] URL url = "this"; url = "this"; crashes - [x] Atomically figure out hash function without manually defining one for HashMap +- [ ] Set + - [ ] basic map + - [ ] fix compare callback to functor + - [ ] fix release callback to functor + From 4e75e686b61c73550bedcf10ee686f2debacf494 Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 16 May 2025 17:01:18 -0700 Subject: [PATCH 15/43] using compare functor --- bflibcpp/src/compare.hpp | 21 +++++++++++++++++++++ bflibcpp/src/implecompare.hpp | 16 ++++++++++++++++ bflibcpp/src/tree.hpp | 12 +++++++++++- bflibcpp/testbench/tests.cpp | 4 ++++ bflibcpp/testbench/tree_tests.hpp | 9 +++++---- bflibcpp/todo.md | 1 + 6 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 bflibcpp/src/compare.hpp create mode 100644 bflibcpp/src/implecompare.hpp diff --git a/bflibcpp/src/compare.hpp b/bflibcpp/src/compare.hpp new file mode 100644 index 00000000..9cfa4b0a --- /dev/null +++ b/bflibcpp/src/compare.hpp @@ -0,0 +1,21 @@ +/** + * author: brando + * date: 5/16/25 + */ + +#ifndef COMPARE_HPP +#define COMPARE_HPP + +namespace BF { + +template +struct Compare { + int operator()(const T & a, const T & b) { + return a - b; + } +}; + +} + +#endif // COMPARE_HPP + diff --git a/bflibcpp/src/implecompare.hpp b/bflibcpp/src/implecompare.hpp new file mode 100644 index 00000000..162a6bde --- /dev/null +++ b/bflibcpp/src/implecompare.hpp @@ -0,0 +1,16 @@ +/** + * author: brando + * date: 5/16/25 + */ + +#ifndef IMPL_LESSER_HPP +#define IMPL_LESSER_HPP + +#include "compare.hpp" + +namespace BF { + +} + +#endif // IMPL_LESSER_HPP + diff --git a/bflibcpp/src/tree.hpp b/bflibcpp/src/tree.hpp index 51eeae5c..6094b471 100644 --- a/bflibcpp/src/tree.hpp +++ b/bflibcpp/src/tree.hpp @@ -9,6 +9,7 @@ #include "collection.hpp" #include "release.hpp" #include "exception.hpp" +#include "compare.hpp" extern "C" { #include @@ -19,7 +20,7 @@ namespace BF { /** * AVL Tree */ -template +template> class Tree : public Collection { /** * holds container of type T @@ -83,6 +84,7 @@ class Tree : public Collection { * * see _compare */ + [[deprecated("Please use BF::Compare functor")]] void setCompare(int (*compare)(const T & a, const T & b)) { this->_compare = compare; } @@ -145,9 +147,17 @@ class Tree : public Collection { */ int (*_compare)(const T & a, const T & b); static int _BFTreeCompare(BFTreeObject a, BFTreeObject b) { + /* Container * acont = (Container *) a; Container * bcont = (Container *) b; return acont->_treeRef->_compare(acont->_obj, bcont->_obj); + */ + //if (!a || !b) return 0; + Container * acont = (Container *) a; + Container * bcont = (Container *) b; + + C cmp; + return cmp(acont->_obj, bcont->_obj); } void (*_release)(T obj); diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 77ef3eac..25f984fa 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -22,6 +22,7 @@ #include "defer_tests.hpp" BFTEST_SUITE_FUNC({ + /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); @@ -35,8 +36,11 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(list_tests); BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(exception_tests); + */ BFTEST_SUITE_LAUNCH(tree_tests); + /* BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(defer_tests); + */ }) diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index 49fdbb47..7abdc015 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -36,7 +36,8 @@ BFTEST_UNIT_FUNC(test_treeInsert, 2<<10, { int treesize = 2<<8; for (int i = 0; i < treesize; i++) { - BF_ASSERT(!tree.insert(i)); + int err = tree.insert(i); + BF_ASSERT(err == 0, "itr=%d, couldn't insert %d, err=%d", BFTEST_UNIT_FUNC_ITR, i, err); } BF_ASSERT(tree.size() == treesize, "%ld != %ld", tree.size(), treesize); }) @@ -51,7 +52,7 @@ BFTEST_UNIT_FUNC(test_treeRemove, 2<<10, { int treesize = 2<<8; for (int i = 0; i < treesize; i++) { - BF_ASSERT(!tree.insert(i)); + BF_ASSERT(!tree.insert(i), "couldn't insert %d", i); } BF_ASSERT(tree.size() == treesize, "%ld != %ld", tree.size(), treesize); @@ -71,7 +72,7 @@ BFTEST_UNIT_FUNC(test_treeContains, 2<<10, { int treesize = 2<<8; for (int i = 0; i < treesize; i += 2) { - BF_ASSERT(!tree.insert(i)); + BF_ASSERT(!tree.insert(i), "couldn't insert %d", i); } BF_ASSERT(tree.size() == treesize/2, "%ld != %ld", tree.size(), treesize); @@ -122,7 +123,7 @@ BFTEST_UNIT_FUNC(test_treeTraversing, 2<<10, { int treesize = 2<<8; for (int i = 0; i < treesize; i += 2) { - BF_ASSERT(!tree.insert(i)); + BF_ASSERT(!tree.insert(i), "couldn't insert %d", i); } BF_ASSERT(tree.size() == treesize/2, "%ld != %ld", tree.size(), treesize); diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index 66024c99..70da1a12 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -32,6 +32,7 @@ - [ ] URL url = "this"; url = "this"; crashes - [x] Atomically figure out hash function without manually defining one for HashMap - [ ] Set + - [ ] tree compare functor - [ ] basic map - [ ] fix compare callback to functor - [ ] fix release callback to functor From 99c98922c94a534bf823bf0884be3ed3989e308e Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 16 May 2025 17:18:27 -0700 Subject: [PATCH 16/43] good functor for compare --- bflibcpp/src/tree.hpp | 17 +++++------------ bflibcpp/testbench/tests.cpp | 4 ---- bflibcpp/testbench/tree_tests.hpp | 6 ------ 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/bflibcpp/src/tree.hpp b/bflibcpp/src/tree.hpp index 6094b471..c3f63825 100644 --- a/bflibcpp/src/tree.hpp +++ b/bflibcpp/src/tree.hpp @@ -70,7 +70,7 @@ class Tree : public Collection { Tree() : Collection() { this->_tree = BFTreeCreate(); if (!this->_tree) return; - BFTreeSetCompare(this->_tree, this->_BFTreeCompare); + BFTreeSetCompare(this->_tree, this->_compare); BFTreeSetRelease(this->_tree, this->_BFTreeRelease); } @@ -85,9 +85,7 @@ class Tree : public Collection { * see _compare */ [[deprecated("Please use BF::Compare functor")]] - void setCompare(int (*compare)(const T & a, const T & b)) { - this->_compare = compare; - } + void setCompare(int (*compare)(const T & a, const T & b)) { } /** * defines how objects are released @@ -145,14 +143,9 @@ class Tree : public Collection { * a > b -> result > 0 * a == b -> result == 0 */ - int (*_compare)(const T & a, const T & b); - static int _BFTreeCompare(BFTreeObject a, BFTreeObject b) { - /* - Container * acont = (Container *) a; - Container * bcont = (Container *) b; - return acont->_treeRef->_compare(acont->_obj, bcont->_obj); - */ - //if (!a || !b) return 0; + //int (*_compare)(const T & a, const T & b); + static int _compare(BFTreeObject a, BFTreeObject b) { + if (!a || !b) return 0; Container * acont = (Container *) a; Container * bcont = (Container *) b; diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 25f984fa..77ef3eac 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -22,7 +22,6 @@ #include "defer_tests.hpp" BFTEST_SUITE_FUNC({ - /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); @@ -36,11 +35,8 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(list_tests); BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(exception_tests); - */ BFTEST_SUITE_LAUNCH(tree_tests); - /* BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(defer_tests); - */ }) diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index 7abdc015..4787af02 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -25,13 +25,11 @@ void BFTestTreeRelease(int object) { } BFTEST_UNIT_FUNC(test_treeInit, 2<<10, { Tree tree; - tree.setCompare(BFTestTreeCompare); tree.setRelease(BFTestTreeRelease); }) BFTEST_UNIT_FUNC(test_treeInsert, 2<<10, { Tree tree; - tree.setCompare(BFTestTreeCompare); tree.setRelease(BFTestTreeRelease); int treesize = 2<<8; @@ -47,7 +45,6 @@ BFTEST_UNIT_FUNC(test_treeRemove, 2<<10, { BFRandInit(time(0)); } Tree tree; - tree.setCompare(BFTestTreeCompare); tree.setRelease(BFTestTreeRelease); int treesize = 2<<8; @@ -67,7 +64,6 @@ BFTEST_UNIT_FUNC(test_treeRemove, 2<<10, { BFTEST_UNIT_FUNC(test_treeContains, 2<<10, { Tree tree; - tree.setCompare(BFTestTreeCompare); tree.setRelease(BFTestTreeRelease); int treesize = 2<<8; @@ -118,7 +114,6 @@ void _BFTestTreeTraversePostorder(const typename Tree::Node node) { BFTEST_UNIT_FUNC(test_treeTraversing, 2<<10, { Tree tree; - tree.setCompare(BFTestTreeCompare); tree.setRelease(BFTestTreeRelease); int treesize = 2<<8; @@ -149,7 +144,6 @@ BFTEST_UNIT_FUNC(test_treeWithMallocObjects, 2<<10, { BFRandInit(time(0)); } Tree tree; - tree.setCompare(BFTestTreeComparePointers); tree.setRelease(BFTestTreeReleasePointer); int treesize = 2<<8; From 7de9afdb9cb07ef09a9e2ca406e0ed66f189690a Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 16 May 2025 17:24:04 -0700 Subject: [PATCH 17/43] using - operator --- bflibcpp/src/string.cpp | 4 ++++ bflibcpp/src/string.hpp | 1 + 2 files changed, 5 insertions(+) diff --git a/bflibcpp/src/string.cpp b/bflibcpp/src/string.cpp index d83cc770..93c1f969 100644 --- a/bflibcpp/src/string.cpp +++ b/bflibcpp/src/string.cpp @@ -220,6 +220,10 @@ String & String::operator=(const String & str) { return *this; } +int String::operator-(const String & str) const { + return strcmp(this->cString(), str.cString()); +} + int String::toi(const String & s) { return atoi(s.cString()); } diff --git a/bflibcpp/src/string.hpp b/bflibcpp/src/string.hpp index 3b1f7d17..961dad79 100644 --- a/bflibcpp/src/string.hpp +++ b/bflibcpp/src/string.hpp @@ -141,6 +141,7 @@ class String : public Array { virtual bool operator!=(const String & s) const; virtual bool operator!=(const char * s) const; virtual String & operator=(const String & str); + virtual int operator-(const String & str) const; // Conversions public: From 0b85ae8dd5dbe9fc72e6450c7af486f5af322a33 Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 16 May 2025 17:45:07 -0700 Subject: [PATCH 18/43] cleaning up --- bflibcpp/src/tree.hpp | 12 ++++++++++- bflibcpp/testbench/tests.cpp | 4 ++++ bflibcpp/testbench/tree_tests.hpp | 33 ++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/bflibcpp/src/tree.hpp b/bflibcpp/src/tree.hpp index c3f63825..f1596cdc 100644 --- a/bflibcpp/src/tree.hpp +++ b/bflibcpp/src/tree.hpp @@ -74,6 +74,10 @@ class Tree : public Collection { BFTreeSetRelease(this->_tree, this->_BFTreeRelease); } + Tree(bool allowDuplicates) : Tree() { + BFTreeSetAllowDuplicates(this->_tree, allowDuplicates); + } + virtual ~Tree() { BFTreeRelease(this->_tree); } @@ -94,6 +98,13 @@ class Tree : public Collection { this->_release = release; } + /** + * true if tree can allow duplicates + */ + bool allowDuplicates() const { + return BFTreeGetAllowDuplicates(this->_tree); + } + /** * returns amount of tree nodes */ @@ -143,7 +154,6 @@ class Tree : public Collection { * a > b -> result > 0 * a == b -> result == 0 */ - //int (*_compare)(const T & a, const T & b); static int _compare(BFTreeObject a, BFTreeObject b) { if (!a || !b) return 0; Container * acont = (Container *) a; diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 77ef3eac..25f984fa 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -22,6 +22,7 @@ #include "defer_tests.hpp" BFTEST_SUITE_FUNC({ + /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); @@ -35,8 +36,11 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(list_tests); BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(exception_tests); + */ BFTEST_SUITE_LAUNCH(tree_tests); + /* BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(defer_tests); + */ }) diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index 4787af02..0dc0b43a 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -163,6 +163,37 @@ BFTEST_UNIT_FUNC(test_treeWithMallocObjects, 2<<10, { } }) +String _BFTreeTestsCreateRandomWord() { + const char charset[] = "abcdefghijklmnopqrstuvwxyz"; + size_t charset_size = strlen(charset); + + srand(time(0)); + + size_t reslen = 2 << 3; + char word[reslen + 1]; + for (int i = 0; i < reslen; i++) { + word[i] = charset[rand() % charset_size]; + } + + word[reslen] = '\0'; + + return word; +} + +BFTEST_UNIT_FUNC(test_treeWithStrings, 1, { + Tree tree(true); + + BF_ASSERT(tree.allowDuplicates()); + + int treesize = 2<<8; + for (int i = 0; i < treesize; i++) { + String word = _BFTreeTestsCreateRandomWord(); + int err = tree.insert(word); + BF_ASSERT(err == 0, "itr=%d, couldn't insert '%s', err=%d", BFTEST_UNIT_FUNC_ITR, word.cString(), err); + } + BF_ASSERT(tree.size() == treesize, "%ld != %ld", tree.size(), treesize); +}) + BFTEST_COVERAGE_FUNC(tree_tests, { BFTEST_LAUNCH(test_treeInit); BFTEST_LAUNCH(test_treeInsert); @@ -170,7 +201,7 @@ BFTEST_COVERAGE_FUNC(tree_tests, { BFTEST_LAUNCH(test_treeContains); BFTEST_LAUNCH(test_treeTraversing); BFTEST_LAUNCH(test_treeWithMallocObjects); - + BFTEST_LAUNCH(test_treeWithStrings); }) #endif // TREE_TESTS_HPP From a2e94c1b0c6075ae831532bb66a415c6fc77c2a3 Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 19 May 2025 08:58:24 -0700 Subject: [PATCH 19/43] update todo --- bflibcpp/todo.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index 70da1a12..22c1aeb9 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -32,7 +32,8 @@ - [ ] URL url = "this"; url = "this"; crashes - [x] Atomically figure out hash function without manually defining one for HashMap - [ ] Set - - [ ] tree compare functor + - [x] tree compare functor + - [ ] tree release functor - [ ] basic map - [ ] fix compare callback to functor - [ ] fix release callback to functor From a740ad94f00f9a88aae59ffe0d0a88df660c189c Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 19 May 2025 10:11:32 -0700 Subject: [PATCH 20/43] init allocator --- bflibcpp/src/allocator.hpp | 25 +++++++++++++++++++++++++ bflibcpp/src/tree.hpp | 3 ++- bflibcpp/testbench/tree_tests.hpp | 23 +++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 bflibcpp/src/allocator.hpp diff --git a/bflibcpp/src/allocator.hpp b/bflibcpp/src/allocator.hpp new file mode 100644 index 00000000..d029512d --- /dev/null +++ b/bflibcpp/src/allocator.hpp @@ -0,0 +1,25 @@ +/** + * author: brando + * date: 5/19/25 + */ + +#ifndef ALLOCATOR_HPP +#define ALLOCATOR_HPP + +namespace BF { + +template +struct Allocator { + bool canRelease() const { + return false; + } + + bool canCreate() const { + return false; + } +}; + +} + +#endif // ALLOCATOR_HPP + diff --git a/bflibcpp/src/tree.hpp b/bflibcpp/src/tree.hpp index f1596cdc..d043ab0a 100644 --- a/bflibcpp/src/tree.hpp +++ b/bflibcpp/src/tree.hpp @@ -10,6 +10,7 @@ #include "release.hpp" #include "exception.hpp" #include "compare.hpp" +#include "allocator.hpp" extern "C" { #include @@ -20,7 +21,7 @@ namespace BF { /** * AVL Tree */ -template> +template, class A = Allocator> class Tree : public Collection { /** * holds container of type T diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index 0dc0b43a..583b722e 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -194,14 +194,37 @@ BFTEST_UNIT_FUNC(test_treeWithStrings, 1, { BF_ASSERT(tree.size() == treesize, "%ld != %ld", tree.size(), treesize); }) +void _BFTestTreeRelease(char * str) { + free(str); +} + +BFTEST_UNIT_FUNC(test_treeWithCustomCompare, 1, { + Tree tree(true); + tree.setRelease(_BFTestTreeRelease); + + BF_ASSERT(tree.allowDuplicates()); + + int treesize = 2<<8; + for (int i = 0; i < treesize; i++) { + char * word = _BFTreeTestsCreateRandomWord().cStringCopy(); + int err = tree.insert(word); + BF_ASSERT(err == 0, "itr=%d, couldn't insert '%s', err=%d", BFTEST_UNIT_FUNC_ITR, word, err); + } + BF_ASSERT(tree.size() == treesize, "%ld != %ld", tree.size(), treesize); +}) + BFTEST_COVERAGE_FUNC(tree_tests, { + /* BFTEST_LAUNCH(test_treeInit); BFTEST_LAUNCH(test_treeInsert); BFTEST_LAUNCH(test_treeRemove); BFTEST_LAUNCH(test_treeContains); BFTEST_LAUNCH(test_treeTraversing); BFTEST_LAUNCH(test_treeWithMallocObjects); + */ BFTEST_LAUNCH(test_treeWithStrings); + //BFTEST_LAUNCH(test_treeWithCustomCompare); + }) #endif // TREE_TESTS_HPP From 6fc2200fb93864682d71a9a97bef01632007b38d Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 21 May 2025 13:00:30 -0700 Subject: [PATCH 21/43] save work --- bflibc/src/tree.c | 6 ------ bflibc/todo.md | 2 ++ bflibcpp/makefile | 2 +- bflibcpp/src/tree.hpp | 2 +- bflibcpp/testbench/tree_tests.hpp | 3 ++- 5 files changed, 6 insertions(+), 9 deletions(-) diff --git a/bflibc/src/tree.c b/bflibc/src/tree.c index 63abd0b0..d1f44959 100644 --- a/bflibc/src/tree.c +++ b/bflibc/src/tree.c @@ -50,12 +50,6 @@ void BFTreeReleaseNode(_BFTree * tree, _BFTreeNode * node) { BFTreeReleaseNode(tree, node->left); BFTreeReleaseNode(tree, node->right); - /* - if (tree->release) { - tree->release(node->object); - } - */ - _BFTreeNodeRelease(node, tree->release); tree->size--; } diff --git a/bflibc/todo.md b/bflibc/todo.md index 482bd28b..73e37632 100644 --- a/bflibc/todo.md +++ b/bflibc/todo.md @@ -25,6 +25,8 @@ - [x] removal - [x] getter - [x] consider allowing duplicate values into tree - no +- [ ] reconsider duplicates + - [ ] handle how memory is freed if duplicates are found - [ ] Set object - [x] allow tree to permit duplicates - [x] tree set diff --git a/bflibcpp/makefile b/bflibcpp/makefile index fe5f1626..acc56369 100644 --- a/bflibcpp/makefile +++ b/bflibcpp/makefile @@ -43,7 +43,7 @@ FLAGS = $(CPPFLAGS) -DDEBUG -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -I../bflibc/bi ### Test settings else ifeq ($(CONFIG),test) # test BIN_NAME = libbfcpp-test -ADDR_SANITIZER = -fsanitize=address +#ADDR_SANITIZER = -fsanitize=address FLAGS = $(CPPFLAGS) -DDEBUG -DTESTING -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -I../bflibc/bin/debug -I../bftest/bin/debug LIBRARIES = ../bflibc/bin/debug/bflibc/libbfc-debug.a ../bftest/bin/debug/bftest/libbftest-debug.a LINKS = $(BF_LIB_C_FLAGS) -lpthread diff --git a/bflibcpp/src/tree.hpp b/bflibcpp/src/tree.hpp index d043ab0a..a72e30cc 100644 --- a/bflibcpp/src/tree.hpp +++ b/bflibcpp/src/tree.hpp @@ -68,7 +68,7 @@ class Tree : public Collection { bool isNull() const { return this->_node == NULL; } }; - Tree() : Collection() { + Tree() : _tree(NULL), _release(NULL), Collection() { this->_tree = BFTreeCreate(); if (!this->_tree) return; BFTreeSetCompare(this->_tree, this->_compare); diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index 583b722e..a6dc374a 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -185,9 +185,10 @@ BFTEST_UNIT_FUNC(test_treeWithStrings, 1, { BF_ASSERT(tree.allowDuplicates()); - int treesize = 2<<8; + int treesize = 2; for (int i = 0; i < treesize; i++) { String word = _BFTreeTestsCreateRandomWord(); + BFTestPrint("inserting %s", word.cString()); int err = tree.insert(word); BF_ASSERT(err == 0, "itr=%d, couldn't insert '%s', err=%d", BFTEST_UNIT_FUNC_ITR, word.cString(), err); } From 52f63060d8be35f0c627fd48662d434d96854bd1 Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 21 May 2025 15:05:28 -0700 Subject: [PATCH 22/43] making sure the object is released when duplicate is found --- bflibc/src/internal/tree.c | 8 ++++++-- bflibc/src/internal/tree.h | 5 +++++ bflibc/src/tree.c | 1 + bflibc/src/tree.h | 4 +++- bflibc/testbench/tests.c | 2 -- bflibcpp/makefile | 2 +- bflibcpp/testbench/tree_tests.hpp | 7 ++----- 7 files changed, 18 insertions(+), 11 deletions(-) diff --git a/bflibc/src/internal/tree.c b/bflibc/src/internal/tree.c index f0008694..381ba8b5 100644 --- a/bflibc/src/internal/tree.c +++ b/bflibc/src/internal/tree.c @@ -88,6 +88,7 @@ _BFTreeNode * _BFTreeNodeInsert( _BFTreeNode * node, BFTreeObject object, int (*compare)(BFTreeObject a, BFTreeObject b), + void (*release)(BFTreeObject object), unsigned char flags, int * error ) { @@ -101,15 +102,18 @@ _BFTreeNode * _BFTreeNodeInsert( int cmpval = compare(object, node->object); if (cmpval < 0) { node->left = _BFTreeNodeInsert( - node->left, object, compare, flags, error + node->left, object, compare, release, flags, error ); } else if (cmpval > 0) { node->right = _BFTreeNodeInsert( - node->right, object, compare, flags, error + node->right, object, compare, release, flags, error ); } else { // Equal keys are not allowed in BST if ((flags & (0x01 << _BFTREE_FLAG_ALLOW_DUPLICATES)) != 0) { node->count++; + if (release) { + release(object); + } } else { if (error) *error = -11; } diff --git a/bflibc/src/internal/tree.h b/bflibc/src/internal/tree.h index 5cb5ef5e..feb30071 100644 --- a/bflibc/src/internal/tree.h +++ b/bflibc/src/internal/tree.h @@ -56,6 +56,10 @@ typedef struct _BFTree { * error: will nonzero if object couldn't be inserted. * one reason is there may be a duplicate * + * object: pointer or integer value. value=0 is allowed. If this tree + * is allowed to have duplicates, any duplicates found will be counted + * and released using its release callback + * flags: _BFTree::flags * * returns node @@ -64,6 +68,7 @@ _BFTreeNode * _BFTreeNodeInsert( _BFTreeNode * node, BFTreeObject object, int (*compare)(BFTreeObject a, BFTreeObject b), + void (*release)(BFTreeObject object), unsigned char flags, int * error ); diff --git a/bflibc/src/tree.c b/bflibc/src/tree.c index d1f44959..81631191 100644 --- a/bflibc/src/tree.c +++ b/bflibc/src/tree.c @@ -75,6 +75,7 @@ int BFTreeInsert(BFTree _tree, BFTreeObject object) { tree->root, object, tree->compare, + tree->release, tree->flags, &err ); diff --git a/bflibc/src/tree.h b/bflibc/src/tree.h index b29ae60a..88f9b799 100644 --- a/bflibc/src/tree.h +++ b/bflibc/src/tree.h @@ -63,7 +63,9 @@ size_t BFTreeSize(BFTree tree); /** * no duplicates allowed * - * object: pointer or integer value. value=0 is allowed + * object: pointer or integer value. value=0 is allowed. If this tree + * is allowed to have duplicates, any duplicates found will be counted + * and released using its release callback * * returns: -1 if object couldn't be inserted. Object may be a duplicate */ diff --git a/bflibc/testbench/tests.c b/bflibc/testbench/tests.c index edfc237a..e7027daa 100644 --- a/bflibc/testbench/tests.c +++ b/bflibc/testbench/tests.c @@ -25,7 +25,6 @@ BFTEST_SUITE_FUNC({ //BFTEST_SUITE_LAUNCH(checksum_tests); - /* BFTEST_SUITE_LAUNCH(coreutils_tests); BFTEST_SUITE_LAUNCH(lock_tests); BFTEST_SUITE_LAUNCH(filesystem_tests); @@ -40,7 +39,6 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(tree_tests); - */ BFTEST_SUITE_LAUNCH(set_tests); BFTEST_SUITE_LAUNCH(hashset_tests); }) diff --git a/bflibcpp/makefile b/bflibcpp/makefile index acc56369..fe5f1626 100644 --- a/bflibcpp/makefile +++ b/bflibcpp/makefile @@ -43,7 +43,7 @@ FLAGS = $(CPPFLAGS) -DDEBUG -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -I../bflibc/bi ### Test settings else ifeq ($(CONFIG),test) # test BIN_NAME = libbfcpp-test -#ADDR_SANITIZER = -fsanitize=address +ADDR_SANITIZER = -fsanitize=address FLAGS = $(CPPFLAGS) -DDEBUG -DTESTING -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -I../bflibc/bin/debug -I../bftest/bin/debug LIBRARIES = ../bflibc/bin/debug/bflibc/libbfc-debug.a ../bftest/bin/debug/bftest/libbftest-debug.a LINKS = $(BF_LIB_C_FLAGS) -lpthread diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index a6dc374a..cbac62b4 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -180,15 +180,14 @@ String _BFTreeTestsCreateRandomWord() { return word; } -BFTEST_UNIT_FUNC(test_treeWithStrings, 1, { +BFTEST_UNIT_FUNC(test_treeWithStrings, 2<<8, { Tree tree(true); BF_ASSERT(tree.allowDuplicates()); - int treesize = 2; + int treesize = 2<<8; for (int i = 0; i < treesize; i++) { String word = _BFTreeTestsCreateRandomWord(); - BFTestPrint("inserting %s", word.cString()); int err = tree.insert(word); BF_ASSERT(err == 0, "itr=%d, couldn't insert '%s', err=%d", BFTEST_UNIT_FUNC_ITR, word.cString(), err); } @@ -215,14 +214,12 @@ BFTEST_UNIT_FUNC(test_treeWithCustomCompare, 1, { }) BFTEST_COVERAGE_FUNC(tree_tests, { - /* BFTEST_LAUNCH(test_treeInit); BFTEST_LAUNCH(test_treeInsert); BFTEST_LAUNCH(test_treeRemove); BFTEST_LAUNCH(test_treeContains); BFTEST_LAUNCH(test_treeTraversing); BFTEST_LAUNCH(test_treeWithMallocObjects); - */ BFTEST_LAUNCH(test_treeWithStrings); //BFTEST_LAUNCH(test_treeWithCustomCompare); From 7d2b0a8b350b2f2470f147a91251f874e35d994c Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 21 May 2025 15:39:08 -0700 Subject: [PATCH 23/43] updating todo --- bflibc/todo.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bflibc/todo.md b/bflibc/todo.md index 73e37632..c601227b 100644 --- a/bflibc/todo.md +++ b/bflibc/todo.md @@ -25,10 +25,10 @@ - [x] removal - [x] getter - [x] consider allowing duplicate values into tree - no -- [ ] reconsider duplicates - - [ ] handle how memory is freed if duplicates are found -- [ ] Set object +- [x] reconsider duplicates + - [x] handle how memory is freed if duplicates are found +- [x] Set object - [x] allow tree to permit duplicates - [x] tree set - - [ ] hash set + - [x] hash set From eb292c11d9c86eb102af2cd883e73bf841591855 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 22 May 2025 10:51:04 -0700 Subject: [PATCH 24/43] update --- bflibcpp/src/allocator.hpp | 10 ++++++++-- bflibcpp/testbench/tree_tests.hpp | 7 ------- bflibcpp/todo.md | 8 +++----- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/bflibcpp/src/allocator.hpp b/bflibcpp/src/allocator.hpp index d029512d..a727905d 100644 --- a/bflibcpp/src/allocator.hpp +++ b/bflibcpp/src/allocator.hpp @@ -10,13 +10,19 @@ namespace BF { template struct Allocator { - bool canRelease() const { + bool canCreate() const { return false; } - bool canCreate() const { + T create() const { + return 0; + } + + bool canRelease() const { return false; } + + void release(T obj) const { } }; } diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index cbac62b4..a6fc8090 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -21,16 +21,12 @@ int BFTestTreeCompare(const int & a, const int & b) { return a - b; } -void BFTestTreeRelease(int object) { } - BFTEST_UNIT_FUNC(test_treeInit, 2<<10, { Tree tree; - tree.setRelease(BFTestTreeRelease); }) BFTEST_UNIT_FUNC(test_treeInsert, 2<<10, { Tree tree; - tree.setRelease(BFTestTreeRelease); int treesize = 2<<8; for (int i = 0; i < treesize; i++) { @@ -45,7 +41,6 @@ BFTEST_UNIT_FUNC(test_treeRemove, 2<<10, { BFRandInit(time(0)); } Tree tree; - tree.setRelease(BFTestTreeRelease); int treesize = 2<<8; for (int i = 0; i < treesize; i++) { @@ -64,7 +59,6 @@ BFTEST_UNIT_FUNC(test_treeRemove, 2<<10, { BFTEST_UNIT_FUNC(test_treeContains, 2<<10, { Tree tree; - tree.setRelease(BFTestTreeRelease); int treesize = 2<<8; for (int i = 0; i < treesize; i += 2) { @@ -114,7 +108,6 @@ void _BFTestTreeTraversePostorder(const typename Tree::Node node) { BFTEST_UNIT_FUNC(test_treeTraversing, 2<<10, { Tree tree; - tree.setRelease(BFTestTreeRelease); int treesize = 2<<8; for (int i = 0; i < treesize; i += 2) { diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index 22c1aeb9..0b88952b 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -32,9 +32,7 @@ - [ ] URL url = "this"; url = "this"; crashes - [x] Atomically figure out hash function without manually defining one for HashMap - [ ] Set - - [x] tree compare functor - - [ ] tree release functor - - [ ] basic map - - [ ] fix compare callback to functor - - [ ] fix release callback to functor + - [ ] release & compare functor work + - [x] tree + - [ ] basic map From 24de49750e3d3cec194a0986491c5cfa1eaeb7e4 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 22 May 2025 16:29:20 -0700 Subject: [PATCH 25/43] can work --- bflibcpp/src/tree.hpp | 18 +++++++++++++----- bflibcpp/testbench/tree_tests.hpp | 20 +++++++++++++++++++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/bflibcpp/src/tree.hpp b/bflibcpp/src/tree.hpp index a72e30cc..6db2b309 100644 --- a/bflibcpp/src/tree.hpp +++ b/bflibcpp/src/tree.hpp @@ -30,15 +30,23 @@ class Tree : public Collection { public: T _obj; const Tree * _treeRef; - void (*_release)(T obj); - Container(T obj, const Tree * treeRef, void (*release)(T obj)) + //void (*_release)(T obj); + bool _release; + //Container(T obj, const Tree * treeRef, void (*release)(T obj)) + Container(T obj, const Tree * treeRef, bool release) : _obj(obj), _treeRef(treeRef), _release(release), Object() { BFRetain(this->_treeRef); } virtual ~Container() { + A allocator; + if (allocator.canRelease() && this->_release) { + allocator.release(this->_obj); + } + /* if (this->_release) { this->_release(this->_obj); } + */ BFRelease(this->_treeRef); } }; @@ -118,7 +126,7 @@ class Tree : public Collection { */ int insert(T object) { if (!this->_tree) return -1; - Container * c = new Container(object, this, this->_release); + Container * c = new Container(object, this, true); return BFTreeInsert(this->_tree, c); } @@ -127,7 +135,7 @@ class Tree : public Collection { */ int remove(T object) { if (!this->_tree) return -1; - Container c(object, this, NULL); + Container c(object, this, false); return BFTreeRemove(this->_tree, &c); } @@ -136,7 +144,7 @@ class Tree : public Collection { */ bool contains(T object) const { if (!this->_tree) return -1; - Container c(object, this, NULL); + Container c(object, this, false); return BFTreeContains(this->_tree, &c); } diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index a6fc8090..493eb1fd 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -132,12 +132,30 @@ void BFTestTreeReleasePointer(int * object) { BFFree(object); } +template<> struct BF::Allocator { + bool canCreate() const { + return false; + } + + int * create() const { + return 0; + } + + bool canRelease() const { + return true; + } + + void release(int * obj) const { + BFFree(obj); + } +}; + BFTEST_UNIT_FUNC(test_treeWithMallocObjects, 2<<10, { if (BFTEST_UNIT_FUNC_ITR == 0) { BFRandInit(time(0)); } Tree tree; - tree.setRelease(BFTestTreeReleasePointer); + //tree.setRelease(BFTestTreeReleasePointer); int treesize = 2<<8; for (int i = 0; i < treesize; i++) { From 7ad15419f0fe3aeeea895a5b28a8359146d52f13 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 22 May 2025 16:36:36 -0700 Subject: [PATCH 26/43] cleaning up --- bflibcpp/src/allocator.hpp | 8 -------- bflibcpp/src/tree.hpp | 2 +- bflibcpp/testbench/tree_tests.hpp | 8 -------- 3 files changed, 1 insertion(+), 17 deletions(-) diff --git a/bflibcpp/src/allocator.hpp b/bflibcpp/src/allocator.hpp index a727905d..b4766008 100644 --- a/bflibcpp/src/allocator.hpp +++ b/bflibcpp/src/allocator.hpp @@ -10,18 +10,10 @@ namespace BF { template struct Allocator { - bool canCreate() const { - return false; - } - T create() const { return 0; } - bool canRelease() const { - return false; - } - void release(T obj) const { } }; diff --git a/bflibcpp/src/tree.hpp b/bflibcpp/src/tree.hpp index 6db2b309..00595e34 100644 --- a/bflibcpp/src/tree.hpp +++ b/bflibcpp/src/tree.hpp @@ -39,7 +39,7 @@ class Tree : public Collection { } virtual ~Container() { A allocator; - if (allocator.canRelease() && this->_release) { + if (this->_release) { allocator.release(this->_obj); } /* diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index 493eb1fd..0bfd3a1e 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -133,18 +133,10 @@ void BFTestTreeReleasePointer(int * object) { } template<> struct BF::Allocator { - bool canCreate() const { - return false; - } - int * create() const { return 0; } - bool canRelease() const { - return true; - } - void release(int * obj) const { BFFree(obj); } From 4d3f48b31bdeffb2b9a7d47d6343ef423fcd2082 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 22 May 2025 16:50:01 -0700 Subject: [PATCH 27/43] compare method --- bflibcpp/testbench/tree_tests.hpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index 0bfd3a1e..fd96ef56 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -128,10 +128,6 @@ int BFTestTreeComparePointers(int * const &ap, int * const &bp) { return a - b; } -void BFTestTreeReleasePointer(int * object) { - BFFree(object); -} - template<> struct BF::Allocator { int * create() const { return 0; @@ -147,7 +143,6 @@ BFTEST_UNIT_FUNC(test_treeWithMallocObjects, 2<<10, { BFRandInit(time(0)); } Tree tree; - //tree.setRelease(BFTestTreeReleasePointer); int treesize = 2<<8; for (int i = 0; i < treesize; i++) { @@ -201,9 +196,25 @@ void _BFTestTreeRelease(char * str) { free(str); } +template <> +struct BF::Compare { + int operator()(const char * const & a, const char * const & b) { + return strcmp(a, b); + } +}; + +template<> struct BF::Allocator { + char * create() const { + return 0; + } + + void release(char * obj) const { + BFFree(obj); + } +}; + BFTEST_UNIT_FUNC(test_treeWithCustomCompare, 1, { Tree tree(true); - tree.setRelease(_BFTestTreeRelease); BF_ASSERT(tree.allowDuplicates()); @@ -224,8 +235,7 @@ BFTEST_COVERAGE_FUNC(tree_tests, { BFTEST_LAUNCH(test_treeTraversing); BFTEST_LAUNCH(test_treeWithMallocObjects); BFTEST_LAUNCH(test_treeWithStrings); - //BFTEST_LAUNCH(test_treeWithCustomCompare); - + BFTEST_LAUNCH(test_treeWithCustomCompare); }) #endif // TREE_TESTS_HPP From b59fc07df879ebb11d0695714229963bd601948d Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 22 May 2025 16:50:47 -0700 Subject: [PATCH 28/43] changing freq --- bflibcpp/testbench/tree_tests.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index fd96ef56..f23172b1 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -213,7 +213,7 @@ template<> struct BF::Allocator { } }; -BFTEST_UNIT_FUNC(test_treeWithCustomCompare, 1, { +BFTEST_UNIT_FUNC(test_treeWithCustomCompare, 2<<10, { Tree tree(true); BF_ASSERT(tree.allowDuplicates()); From 165bae2502af01253e05596115240ba6c8179ba6 Mon Sep 17 00:00:00 2001 From: Brando Date: Thu, 22 May 2025 17:36:40 -0700 Subject: [PATCH 29/43] clean up --- bflibcpp/src/tree.hpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/bflibcpp/src/tree.hpp b/bflibcpp/src/tree.hpp index 00595e34..ca45f793 100644 --- a/bflibcpp/src/tree.hpp +++ b/bflibcpp/src/tree.hpp @@ -76,11 +76,11 @@ class Tree : public Collection { bool isNull() const { return this->_node == NULL; } }; - Tree() : _tree(NULL), _release(NULL), Collection() { + Tree() : _tree(NULL), Collection() { this->_tree = BFTreeCreate(); if (!this->_tree) return; BFTreeSetCompare(this->_tree, this->_compare); - BFTreeSetRelease(this->_tree, this->_BFTreeRelease); + BFTreeSetRelease(this->_tree, this->_release); } Tree(bool allowDuplicates) : Tree() { @@ -103,9 +103,8 @@ class Tree : public Collection { /** * defines how objects are released */ - void setRelease(void (*release)(T obj)) { - this->_release = release; - } + [[deprecated("Please use BF::Allocator functor")]] + void setRelease(void (*release)(T obj)) { } /** * true if tree can allow duplicates @@ -172,8 +171,8 @@ class Tree : public Collection { return cmp(acont->_obj, bcont->_obj); } - void (*_release)(T obj); - static void _BFTreeRelease(BFTreeObject object) { + //void (*_release)(T obj); + static void _release(BFTreeObject object) { Container * cont = (Container *) object; BFRelease(cont); } From a365d870daac8d04e2a42870d6c07832a63c54c2 Mon Sep 17 00:00:00 2001 From: Brando Date: Fri, 23 May 2025 13:19:23 -0700 Subject: [PATCH 30/43] using compare --- bflibcpp/src/basicmap.hpp | 21 ++++++++++++++++++--- bflibcpp/src/compare.hpp | 7 +++++++ bflibcpp/src/tree.hpp | 8 -------- bflibcpp/testbench/hashmap_tests.hpp | 10 +++++----- bflibcpp/testbench/map_tests.hpp | 10 +++++----- bflibcpp/testbench/tests.cpp | 4 ++++ bflibcpp/testbench/tree_tests.hpp | 7 ------- 7 files changed, 39 insertions(+), 28 deletions(-) diff --git a/bflibcpp/src/basicmap.hpp b/bflibcpp/src/basicmap.hpp index c38a6322..8d3e22f0 100644 --- a/bflibcpp/src/basicmap.hpp +++ b/bflibcpp/src/basicmap.hpp @@ -10,6 +10,8 @@ #include "release.hpp" #include "retain.hpp" #include "exception.hpp" +#include "compare.hpp" +#include "allocator.hpp" #include @@ -25,7 +27,14 @@ namespace BF { * This is formatted to fit the bflibc implementation of * HashMap and Map */ -template +template < + typename K, + typename V, + typename S = size_t, + class C = Compare, + class AK = Allocator, + class AV = Allocator +> class BasicMap : public Collection { public: virtual const char * className() const { @@ -98,6 +107,7 @@ class BasicMap : public Collection { * * similar behavior to strcmp and memcmp */ + [[deprecated("Please use BF::Compare functor")]] void setCompare(int (*compare)(K & a, K & b)) { this->_compare = compare; } @@ -190,9 +200,10 @@ class BasicMap : public Collection { Key * akey = (Key *) a; Key * bkey = (Key *) b; if (!akey || !bkey) { - return -1; + return 0; } - + + /* if (!akey->_mapRef->_compare && akey->isBFObject() && bkey->isBFObject()) { BF::Object * obja = (BF::Object *) &akey->_obj; BF::Object * objb = (BF::Object *) &bkey->_obj; @@ -201,6 +212,10 @@ class BasicMap : public Collection { } return akey->_mapRef->_compare(akey->_obj, bkey->_obj); + */ + + C cmp; + return cmp(akey->_obj, bkey->_obj); } /** diff --git a/bflibcpp/src/compare.hpp b/bflibcpp/src/compare.hpp index 9cfa4b0a..55265fd8 100644 --- a/bflibcpp/src/compare.hpp +++ b/bflibcpp/src/compare.hpp @@ -15,6 +15,13 @@ struct Compare { } }; +template <> +struct Compare { + int operator()(const char * const & a, const char * const & b) { + return strcmp(a, b); + } +}; + } #endif // COMPARE_HPP diff --git a/bflibcpp/src/tree.hpp b/bflibcpp/src/tree.hpp index ca45f793..4854736f 100644 --- a/bflibcpp/src/tree.hpp +++ b/bflibcpp/src/tree.hpp @@ -30,9 +30,7 @@ class Tree : public Collection { public: T _obj; const Tree * _treeRef; - //void (*_release)(T obj); bool _release; - //Container(T obj, const Tree * treeRef, void (*release)(T obj)) Container(T obj, const Tree * treeRef, bool release) : _obj(obj), _treeRef(treeRef), _release(release), Object() { BFRetain(this->_treeRef); @@ -42,11 +40,6 @@ class Tree : public Collection { if (this->_release) { allocator.release(this->_obj); } - /* - if (this->_release) { - this->_release(this->_obj); - } - */ BFRelease(this->_treeRef); } }; @@ -171,7 +164,6 @@ class Tree : public Collection { return cmp(acont->_obj, bcont->_obj); } - //void (*_release)(T obj); static void _release(BFTreeObject object) { Container * cont = (Container *) object; BFRelease(cont); diff --git a/bflibcpp/testbench/hashmap_tests.hpp b/bflibcpp/testbench/hashmap_tests.hpp index 142548ba..b0af205f 100644 --- a/bflibcpp/testbench/hashmap_tests.hpp +++ b/bflibcpp/testbench/hashmap_tests.hpp @@ -27,7 +27,7 @@ int BFTestHashMapCompareString(String & a, String & b) { BFTEST_UNIT_FUNC(test_hashMapInsert, 2<<10, { HashMap map; - map.setCompare(BFTestHashMapCompareString); + //map.setCompare(BFTestHashMapCompareString); int mapsize = 2<<7; for (int i = 0; i < mapsize; i++) { @@ -39,7 +39,7 @@ BFTEST_UNIT_FUNC(test_hashMapInsert, 2<<10, { BFTEST_UNIT_FUNC(test_hashMapGet, 2<<10, { HashMap map; - map.setCompare(BFTestHashMapCompareString); + //map.setCompare(BFTestHashMapCompareString); // insert int mapsize = 2<<7; @@ -66,7 +66,7 @@ BFTEST_UNIT_FUNC(test_hashMapRemove, 2<<10, { BFRandInit(time(0)); } HashMap map; - map.setCompare(BFTestHashMapCompareString); + //map.setCompare(BFTestHashMapCompareString); // insert int mapsize = 2<<7; @@ -105,7 +105,7 @@ BFTEST_UNIT_FUNC(test_hashMapWithAllocMem, 2<<10, { BFRandInit(time(0)); } HashMap map; - map.setCompare(BFTestHashMapCompareCString); + //map.setCompare(BFTestHashMapCompareCString); map.setRelease(BFTestHashMapReleaseKeyCString, BFTestHashMapReleaseValueInteger); // insert @@ -122,7 +122,7 @@ BFTEST_UNIT_FUNC(test_hashMapWithAllocMem, 2<<10, { BFTEST_UNIT_FUNC(test_hashMapGetterWithSubscript, 2<<10, { HashMap map; - map.setCompare(BFTestHashMapCompareString); + //map.setCompare(BFTestHashMapCompareString); // insert int mapsize = 2<<7; diff --git a/bflibcpp/testbench/map_tests.hpp b/bflibcpp/testbench/map_tests.hpp index 2b6a07e8..1a409299 100644 --- a/bflibcpp/testbench/map_tests.hpp +++ b/bflibcpp/testbench/map_tests.hpp @@ -26,7 +26,7 @@ int BFTestMapCompareString(String & a, String & b) { BFTEST_UNIT_FUNC(test_mapInsert, 2<<10, { Map map; - map.setCompare(BFTestMapCompareString); + //map.setCompare(BFTestMapCompareString); int mapsize = 2<<7; for (int i = 0; i < mapsize; i++) { @@ -38,7 +38,7 @@ BFTEST_UNIT_FUNC(test_mapInsert, 2<<10, { BFTEST_UNIT_FUNC(test_mapGet, 2<<10, { Map map; - map.setCompare(BFTestMapCompareString); + //map.setCompare(BFTestMapCompareString); // insert int mapsize = 2<<7; @@ -65,7 +65,7 @@ BFTEST_UNIT_FUNC(test_mapRemove, 2<<10, { BFRandInit(time(0)); } Map map; - map.setCompare(BFTestMapCompareString); + //map.setCompare(BFTestMapCompareString); // insert int mapsize = 2<<7; @@ -104,7 +104,7 @@ BFTEST_UNIT_FUNC(test_mapWithAllocMem, 2<<10, { BFRandInit(time(0)); } Map map; - map.setCompare(BFTestMapCompareCString); + //map.setCompare(BFTestMapCompareCString); map.setRelease(BFTestMapReleaseKeyCString, BFTestMapReleaseValueInteger); // insert @@ -121,7 +121,7 @@ BFTEST_UNIT_FUNC(test_mapWithAllocMem, 2<<10, { BFTEST_UNIT_FUNC(test_mapGetWithSubscript, 2<<10, { Map map; - map.setCompare(BFTestMapCompareString); + //map.setCompare(BFTestMapCompareString); // insert int mapsize = 2<<7; diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 25f984fa..d2cc0f2f 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -31,10 +31,14 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(data_tests); BFTEST_SUITE_LAUNCH(array_tests); BFTEST_SUITE_LAUNCH(vector_tests); + */ BFTEST_SUITE_LAUNCH(map_tests); + /* BFTEST_SUITE_LAUNCH(object_tests); BFTEST_SUITE_LAUNCH(list_tests); + */ BFTEST_SUITE_LAUNCH(hashmap_tests); + /* BFTEST_SUITE_LAUNCH(exception_tests); */ BFTEST_SUITE_LAUNCH(tree_tests); diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index f23172b1..3340a61a 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -196,13 +196,6 @@ void _BFTestTreeRelease(char * str) { free(str); } -template <> -struct BF::Compare { - int operator()(const char * const & a, const char * const & b) { - return strcmp(a, b); - } -}; - template<> struct BF::Allocator { char * create() const { return 0; From 0288970cfa486a739e927f3cdc7d020a670cda0d Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 26 May 2025 22:43:27 -0700 Subject: [PATCH 31/43] good implementation --- bflibcpp/src/basicmap.hpp | 18 +++++++++++------- bflibcpp/testbench/cpplib_tests.hpp | 23 +++++++++++++++++++++++ bflibcpp/testbench/hashmap_tests.hpp | 6 ------ bflibcpp/testbench/map_tests.hpp | 6 ------ bflibcpp/testbench/tree_tests.hpp | 20 -------------------- bflibcpp/todo.md | 4 ++-- 6 files changed, 36 insertions(+), 41 deletions(-) diff --git a/bflibcpp/src/basicmap.hpp b/bflibcpp/src/basicmap.hpp index 8d3e22f0..45543d30 100644 --- a/bflibcpp/src/basicmap.hpp +++ b/bflibcpp/src/basicmap.hpp @@ -74,9 +74,13 @@ class BasicMap : public Collection { Key(T obj, const BasicMap * mapRef) : Container(obj, mapRef) { } virtual ~Key() { + /* if (this->_mapRef->_releaseKey) { this->_mapRef->_releaseKey(this->_obj); } + */ + AK allocator; + allocator.release(this->_obj); } }; @@ -86,9 +90,13 @@ class BasicMap : public Collection { Value(T obj, const BasicMap * mapRef) : Container(obj, mapRef) { } virtual ~Value() { + /* if (this->_mapRef->_releaseValue) { this->_mapRef->_releaseValue(this->_obj); } + */ + AV allocator; + allocator.release(this->_obj); } }; @@ -108,17 +116,13 @@ class BasicMap : public Collection { * similar behavior to strcmp and memcmp */ [[deprecated("Please use BF::Compare functor")]] - void setCompare(int (*compare)(K & a, K & b)) { - this->_compare = compare; - } + void setCompare(int (*compare)(K & a, K & b)) { } /** * defines how Key and values are released */ - void setRelease(void (*releaseKey)(K obj), void (*releaseValue)(V obj)) { - this->_releaseKey = releaseKey; - this->_releaseValue = releaseValue; - } + [[deprecated("Please use BF::Allocatorfunctor")]] + void setRelease(void (*releaseKey)(K obj), void (*releaseValue)(V obj)) { } /** * adds key and value into map diff --git a/bflibcpp/testbench/cpplib_tests.hpp b/bflibcpp/testbench/cpplib_tests.hpp index f71498bd..496c43bd 100644 --- a/bflibcpp/testbench/cpplib_tests.hpp +++ b/bflibcpp/testbench/cpplib_tests.hpp @@ -6,9 +6,32 @@ #ifndef CPPLIB_TESTS_HPP #define CPPLIB_TESTS_HPP +#include "allocator.hpp" + extern "C" { #include +#include } +template<> struct BF::Allocator { + char * create() const { + return 0; + } + + void release(char * obj) const { + BFFree(obj); + } +}; + +template<> struct BF::Allocator { + int * create() const { + return 0; + } + + void release(int * obj) const { + BFFree(obj); + } +}; + #endif // CPPLIB_TESTS_HPP diff --git a/bflibcpp/testbench/hashmap_tests.hpp b/bflibcpp/testbench/hashmap_tests.hpp index b0af205f..48e9ee99 100644 --- a/bflibcpp/testbench/hashmap_tests.hpp +++ b/bflibcpp/testbench/hashmap_tests.hpp @@ -27,7 +27,6 @@ int BFTestHashMapCompareString(String & a, String & b) { BFTEST_UNIT_FUNC(test_hashMapInsert, 2<<10, { HashMap map; - //map.setCompare(BFTestHashMapCompareString); int mapsize = 2<<7; for (int i = 0; i < mapsize; i++) { @@ -39,7 +38,6 @@ BFTEST_UNIT_FUNC(test_hashMapInsert, 2<<10, { BFTEST_UNIT_FUNC(test_hashMapGet, 2<<10, { HashMap map; - //map.setCompare(BFTestHashMapCompareString); // insert int mapsize = 2<<7; @@ -66,7 +64,6 @@ BFTEST_UNIT_FUNC(test_hashMapRemove, 2<<10, { BFRandInit(time(0)); } HashMap map; - //map.setCompare(BFTestHashMapCompareString); // insert int mapsize = 2<<7; @@ -105,8 +102,6 @@ BFTEST_UNIT_FUNC(test_hashMapWithAllocMem, 2<<10, { BFRandInit(time(0)); } HashMap map; - //map.setCompare(BFTestHashMapCompareCString); - map.setRelease(BFTestHashMapReleaseKeyCString, BFTestHashMapReleaseValueInteger); // insert int mapsize = 2<<7; @@ -122,7 +117,6 @@ BFTEST_UNIT_FUNC(test_hashMapWithAllocMem, 2<<10, { BFTEST_UNIT_FUNC(test_hashMapGetterWithSubscript, 2<<10, { HashMap map; - //map.setCompare(BFTestHashMapCompareString); // insert int mapsize = 2<<7; diff --git a/bflibcpp/testbench/map_tests.hpp b/bflibcpp/testbench/map_tests.hpp index 1a409299..6ea317e2 100644 --- a/bflibcpp/testbench/map_tests.hpp +++ b/bflibcpp/testbench/map_tests.hpp @@ -26,7 +26,6 @@ int BFTestMapCompareString(String & a, String & b) { BFTEST_UNIT_FUNC(test_mapInsert, 2<<10, { Map map; - //map.setCompare(BFTestMapCompareString); int mapsize = 2<<7; for (int i = 0; i < mapsize; i++) { @@ -38,7 +37,6 @@ BFTEST_UNIT_FUNC(test_mapInsert, 2<<10, { BFTEST_UNIT_FUNC(test_mapGet, 2<<10, { Map map; - //map.setCompare(BFTestMapCompareString); // insert int mapsize = 2<<7; @@ -65,7 +63,6 @@ BFTEST_UNIT_FUNC(test_mapRemove, 2<<10, { BFRandInit(time(0)); } Map map; - //map.setCompare(BFTestMapCompareString); // insert int mapsize = 2<<7; @@ -104,8 +101,6 @@ BFTEST_UNIT_FUNC(test_mapWithAllocMem, 2<<10, { BFRandInit(time(0)); } Map map; - //map.setCompare(BFTestMapCompareCString); - map.setRelease(BFTestMapReleaseKeyCString, BFTestMapReleaseValueInteger); // insert int mapsize = 2<<7; @@ -121,7 +116,6 @@ BFTEST_UNIT_FUNC(test_mapWithAllocMem, 2<<10, { BFTEST_UNIT_FUNC(test_mapGetWithSubscript, 2<<10, { Map map; - //map.setCompare(BFTestMapCompareString); // insert int mapsize = 2<<7; diff --git a/bflibcpp/testbench/tree_tests.hpp b/bflibcpp/testbench/tree_tests.hpp index 3340a61a..5cafcc28 100644 --- a/bflibcpp/testbench/tree_tests.hpp +++ b/bflibcpp/testbench/tree_tests.hpp @@ -128,16 +128,6 @@ int BFTestTreeComparePointers(int * const &ap, int * const &bp) { return a - b; } -template<> struct BF::Allocator { - int * create() const { - return 0; - } - - void release(int * obj) const { - BFFree(obj); - } -}; - BFTEST_UNIT_FUNC(test_treeWithMallocObjects, 2<<10, { if (BFTEST_UNIT_FUNC_ITR == 0) { BFRandInit(time(0)); @@ -196,16 +186,6 @@ void _BFTestTreeRelease(char * str) { free(str); } -template<> struct BF::Allocator { - char * create() const { - return 0; - } - - void release(char * obj) const { - BFFree(obj); - } -}; - BFTEST_UNIT_FUNC(test_treeWithCustomCompare, 2<<10, { Tree tree(true); diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index 0b88952b..aab00d94 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -32,7 +32,7 @@ - [ ] URL url = "this"; url = "this"; crashes - [x] Atomically figure out hash function without manually defining one for HashMap - [ ] Set - - [ ] release & compare functor work + - [x] release & compare functor work - [x] tree - - [ ] basic map + - [x] basic map From 63c88bdac2f74193cdd6daf08df8e6a68e6d42f2 Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 26 May 2025 23:44:56 -0700 Subject: [PATCH 32/43] cleaning up --- bflibcpp/src/basicmap.hpp | 48 ++++----------------------------------- 1 file changed, 5 insertions(+), 43 deletions(-) diff --git a/bflibcpp/src/basicmap.hpp b/bflibcpp/src/basicmap.hpp index 45543d30..d58a7bc6 100644 --- a/bflibcpp/src/basicmap.hpp +++ b/bflibcpp/src/basicmap.hpp @@ -55,30 +55,15 @@ class BasicMap : public Collection { public: T _obj; const BasicMap * _mapRef; - Container(T obj, const BasicMap * mapRef) - : _obj(obj), _mapRef(mapRef), Object() { - BFRetain(this->_mapRef); - } - virtual ~Container() { - BFRelease(this->_mapRef); - } - - bool isBFObject() const { - return std::is_base_of_v; - } + Container(T obj, const BasicMap * mapRef) : _obj(obj), _mapRef(mapRef), Object() { } + virtual ~Container() { } }; template class Key : public Container { public: - Key(T obj, const BasicMap * mapRef) - : Container(obj, mapRef) { } + Key(T obj, const BasicMap * mapRef) : Container(obj, mapRef) { } virtual ~Key() { - /* - if (this->_mapRef->_releaseKey) { - this->_mapRef->_releaseKey(this->_obj); - } - */ AK allocator; allocator.release(this->_obj); } @@ -87,14 +72,8 @@ class BasicMap : public Collection { template class Value : public Container { public: - Value(T obj, const BasicMap * mapRef) - : Container(obj, mapRef) { } + Value(T obj, const BasicMap * mapRef) : Container(obj, mapRef) { } virtual ~Value() { - /* - if (this->_mapRef->_releaseValue) { - this->_mapRef->_releaseValue(this->_obj); - } - */ AV allocator; allocator.release(this->_obj); } @@ -104,9 +83,7 @@ class BasicMap : public Collection { friend class Value; public: - BasicMap() - : _compare(NULL), _releaseKey(NULL), - _releaseValue(NULL), Collection() { } + BasicMap() : Collection() { } virtual ~BasicMap() { } @@ -188,10 +165,6 @@ class BasicMap : public Collection { */ virtual Value * _getValueForKey(void * key) const = 0; - int (*_compare)(K & a, K & b); - void (*_releaseKey)(K obj); - void (*_releaseValue)(V obj); - protected: /** * compares a with b @@ -206,17 +179,6 @@ class BasicMap : public Collection { if (!akey || !bkey) { return 0; } - - /* - if (!akey->_mapRef->_compare && akey->isBFObject() && bkey->isBFObject()) { - BF::Object * obja = (BF::Object *) &akey->_obj; - BF::Object * objb = (BF::Object *) &bkey->_obj; - if (!obja || !objb) return -1; - return obja->compare(*objb); - } - - return akey->_mapRef->_compare(akey->_obj, bkey->_obj); - */ C cmp; return cmp(akey->_obj, bkey->_obj); From 57bd667b2ad97df2548a7879a55c48d2100db8dc Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 26 May 2025 23:55:20 -0700 Subject: [PATCH 33/43] moving allocator --- bflibcpp/src/basicmap.hpp | 55 ++++++++++++--------------------------- bflibcpp/src/hashmap.hpp | 8 +++--- bflibcpp/src/map.hpp | 6 ++--- 3 files changed, 24 insertions(+), 45 deletions(-) diff --git a/bflibcpp/src/basicmap.hpp b/bflibcpp/src/basicmap.hpp index d58a7bc6..61643d33 100644 --- a/bflibcpp/src/basicmap.hpp +++ b/bflibcpp/src/basicmap.hpp @@ -31,9 +31,7 @@ template < typename K, typename V, typename S = size_t, - class C = Compare, - class AK = Allocator, - class AV = Allocator + class C = Compare > class BasicMap : public Collection { public: @@ -50,38 +48,19 @@ class BasicMap : public Collection { * this object will get injected into the * map implementations */ - template + template> class Container : public Object { public: T _obj; const BasicMap * _mapRef; - Container(T obj, const BasicMap * mapRef) : _obj(obj), _mapRef(mapRef), Object() { } - virtual ~Container() { } - }; - - template - class Key : public Container { - public: - Key(T obj, const BasicMap * mapRef) : Container(obj, mapRef) { } - virtual ~Key() { - AK allocator; + Container(T obj, const BasicMap * mapRef) + : _obj(obj), _mapRef(mapRef), Object() { } + virtual ~Container() { + A allocator; allocator.release(this->_obj); } }; - template - class Value : public Container { - public: - Value(T obj, const BasicMap * mapRef) : Container(obj, mapRef) { } - virtual ~Value() { - AV allocator; - allocator.release(this->_obj); - } - }; - - friend class Key; - friend class Value; - public: BasicMap() : Collection() { } @@ -105,8 +84,8 @@ class BasicMap : public Collection { * adds key and value into map */ int insert(K k, V v) { - Key * key = new Key(k, this); - Value * value = new Value(v, this); + Container * key = new Container(k, this); + Container * value = new Container(v, this); return this->_insert(key, value); } @@ -116,9 +95,9 @@ class BasicMap : public Collection { * throws an exception if no value could be found for key */ V & getValueForKey(K k) const { - Key key(k, this); + Container key(k, this); - Value * value = this->_getValueForKey(&key); + Container * value = this->_getValueForKey(&key); if (value) { return value->_obj; } else { @@ -143,7 +122,7 @@ class BasicMap : public Collection { * removes key/value pair with key */ int remove(K k) { - Key key(k, this); + Container key(k, this); return this->_remove(&key); } @@ -151,7 +130,7 @@ class BasicMap : public Collection { * true if there is an entry with key=k */ bool contains(K k) const { - Key key(k, this); + Container key(k, this); return this->_contains(&key); } @@ -163,7 +142,7 @@ class BasicMap : public Collection { /** * returns NULL if there is no value for key */ - virtual Value * _getValueForKey(void * key) const = 0; + virtual Container * _getValueForKey(void * key) const = 0; protected: /** @@ -174,8 +153,8 @@ class BasicMap : public Collection { * default return(-1) */ static int _BFMapCompare(void * a, void * b) { - Key * akey = (Key *) a; - Key * bkey = (Key *) b; + Container * akey = (Container *) a; + Container * bkey = (Container *) b; if (!akey || !bkey) { return 0; } @@ -188,8 +167,8 @@ class BasicMap : public Collection { * Releases the Key & Value objects */ static void _BFMapRelease(void * k, void * v) { - Key * key = (Key *) k; - Value * value = (Value *) v; + Container * key = (Container *) k; + Container * value = (Container *) v; BFRelease(key); BFRelease(value); } diff --git a/bflibcpp/src/hashmap.hpp b/bflibcpp/src/hashmap.hpp index 214f5c5a..31fad58c 100644 --- a/bflibcpp/src/hashmap.hpp +++ b/bflibcpp/src/hashmap.hpp @@ -48,13 +48,13 @@ class HashMap : public BasicMap { return BFHashMapInsert(this->_map, key, value); } - virtual typename BasicMap::template Value * _getValueForKey(void * key) const { + virtual typename BasicMap::template Container * _getValueForKey(void * key) const { if (!this->_map) { return NULL; } - typename BasicMap::template Value * value = - (typename BasicMap::template Value *) BFHashMapGetValue(this->_map, key, NULL); + typename BasicMap::template Container * value = + (typename BasicMap::template Container *) BFHashMapGetValue(this->_map, key, NULL); return value; } @@ -70,7 +70,7 @@ class HashMap : public BasicMap { } static unsigned long _BFHashMapHashFunction(void * k) { - typename BasicMap::template Key * key = (typename BasicMap::template Key *) k; + typename BasicMap::template Container * key = (typename BasicMap::template Container *) k; if (!key) { return 0; } diff --git a/bflibcpp/src/map.hpp b/bflibcpp/src/map.hpp index fa40c026..9615e42e 100644 --- a/bflibcpp/src/map.hpp +++ b/bflibcpp/src/map.hpp @@ -42,13 +42,13 @@ class Map : public BasicMap { return BFMapInsert(this->_map, key, value); } - virtual typename BasicMap::template Value * _getValueForKey(void * key) const { + virtual typename BasicMap::template Container * _getValueForKey(void * key) const { if (!this->_map) { return NULL; } - typename BasicMap::template Value * value = - (typename BasicMap::template Value *) BFMapGetValue(this->_map, key, NULL); + typename BasicMap::template Container * value = + (typename BasicMap::template Container *) BFMapGetValue(this->_map, key, NULL); return value; } From ed99ee9954818ee132531a584f89c8b94c201fab Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 27 May 2025 00:21:12 -0700 Subject: [PATCH 34/43] init Set and HashSet --- bflibcpp/src/basicset.hpp | 123 ++++++++++++++++++++++++++++++++++++++ bflibcpp/src/bflibcpp.hpp | 2 + bflibcpp/src/hashset.hpp | 79 ++++++++++++++++++++++++ bflibcpp/src/set.hpp | 60 +++++++++++++++++++ 4 files changed, 264 insertions(+) create mode 100644 bflibcpp/src/basicset.hpp create mode 100644 bflibcpp/src/hashset.hpp create mode 100644 bflibcpp/src/set.hpp diff --git a/bflibcpp/src/basicset.hpp b/bflibcpp/src/basicset.hpp new file mode 100644 index 00000000..8fcc06b8 --- /dev/null +++ b/bflibcpp/src/basicset.hpp @@ -0,0 +1,123 @@ +/** + * author: brando + * date: 5/27/25 + */ + +#ifndef BASIC_SET_HPP +#define BASIC_SET_HPP + +#include "collection.hpp" +#include "release.hpp" +#include "retain.hpp" +#include "exception.hpp" +#include "compare.hpp" +#include "allocator.hpp" + +#include + +namespace BF { + +/** + * General implementation of a Map + * + * This is formatted to fit the bflibc implementation of + * HashMap and Map + */ +template < + typename V, + typename S = size_t, + class C = Compare +> +class BasicSet : public Collection { +public: + virtual const char * className() const { + return "BF::BasicSet"; + } + +protected: + + /** + * template on how Key and Value objects are + * held + * + * this object will get injected into the + * map implementations + */ + template> + class Container : public Object { + public: + T _obj; + const BasicSet * _setRef; + Container(T obj, const BasicSet * mapRef) + : _obj(obj), _setRef(mapRef), Object() { } + virtual ~Container() { + A allocator; + allocator.release(this->_obj); + } + }; + +public: + BasicSet() : Collection() { } + + virtual ~BasicSet() { } + + /** + * adds key and value into map + */ + int insert(V value) { + Container * v = new Container(value, this); + return this->_insert(v); + } + + /** + * removes key/value pair with key + */ + int remove(V value) { + Container v(value, this); + return this->_remove(&v); + } + + /** + * true if there is an entry with key=k + */ + bool contains(V value) const { + Container v(value, this); + return this->_contains(&v); + } + +private: + virtual int _insert(void * value) = 0; + virtual int _remove(void * value) = 0; + virtual bool _contains(void * value) const = 0; + +protected: + /** + * compares a with b + * + * we assume similar behavior to strcmp or memcmp + * + * default return(-1) + */ + static int _BFSetCompare(void * a, void * b) { + Container * aval = (Container *) a; + Container * bval = (Container *) b; + if (!aval || !bval) { + return 0; + } + + C cmp; + return cmp(aval->_obj, bval->_obj); + } + + /** + * Releases the Key & Value objects + */ + static void _BFSetRelease(void * v) { + Container * value = (Container *) v; + BFRelease(value); + } +}; +} + +#endif // BASIC_SET_HPP + diff --git a/bflibcpp/src/bflibcpp.hpp b/bflibcpp/src/bflibcpp.hpp index d3a15998..65ab9749 100644 --- a/bflibcpp/src/bflibcpp.hpp +++ b/bflibcpp/src/bflibcpp.hpp @@ -32,6 +32,8 @@ #include "hash.hpp" #include "defer.hpp" #include "sort.hpp" +#include "set.hpp" +#include "hashset.hpp" #endif // BFLIBCPP_HPP diff --git a/bflibcpp/src/hashset.hpp b/bflibcpp/src/hashset.hpp new file mode 100644 index 00000000..80448637 --- /dev/null +++ b/bflibcpp/src/hashset.hpp @@ -0,0 +1,79 @@ +/** + * author: brando + * date: 5/27/25 + */ + +#ifndef HASH_SET_HPP +#define HASH_SET_HPP + +#include "basicset.hpp" +#include "release.hpp" +#include "retain.hpp" +#include "hash.hpp" + +extern "C" { +#include +} + +namespace BF { + +/** + * Map implemented using self-balancing tree. See bflibc/map.h + */ +template > +class HashSet : public BasicSet { +public: + HashSet() : _set(NULL), BasicSet() { + this->_set = BFHashSetCreate(); + if (!this->_set) return; + BFHashSetSetCompare(this->_set, this->_BFMapCompare); + BFHashSetSetRelease(this->_set, this->_BFMapRelease); + BFHashSetSetHashFunction(this->_set, this->_BFHashSetHashFunction); + } + + virtual ~HashSet() { + BFHashSetRelease(this->_set); + } + +private: + size_t size() const { + return BFHashSetGetSize(this->_set); + } + + virtual int _insert(void * value) { + if (!this->_set) return -1; + return BFHashSetInsert(this->_set, value); + } + + virtual int _remove(void * value) { + if (!this->_set) return -1; + return BFHashSetRemove(this->_set, value); + } + + virtual bool _contains(void * value) const { + if (!this->_set) return false; + return BFHashSetContains(this->_set, value); + } + + static unsigned long _BFHashSetHashFunction(void * value) { + typename BasicSet::template Container * v = (typename BasicSet::template Container *) value; + if (!v) { + return 0; + } + + HashSet * set = (HashSet *) v->_setRef; + if (!set) { + return 0; + } + + V obj = v->_obj; + return set->_hash(obj); + } + + H _hash; + BFHashSet _set; +}; +} + +#endif // MAP_HPP + diff --git a/bflibcpp/src/set.hpp b/bflibcpp/src/set.hpp new file mode 100644 index 00000000..1362c34f --- /dev/null +++ b/bflibcpp/src/set.hpp @@ -0,0 +1,60 @@ +/** + * author: brando + * date: 5/27/25 + */ + +#ifndef SET_HPP +#define SET_HPP + +#include "basicset.hpp" +#include "release.hpp" + +extern "C" { +#include +} + +namespace BF { + +/** + * Set implemented using self-balancing tree. See bflibc/map.h + */ +template +class Set : public BasicSet { +public: + Set() : _set(NULL), BasicSet() { + this->_set = BFSetCreate(); + if (!this->_set) return; + BFSetSetCompare(this->_set, this->_BFSetCompare); + BFSetSetRelease(this->_set, this->_BFSetRelease); + } + + virtual ~Set() { + BFSetRelease(this->_set); + } + +private: + size_t size() const { + return BFSetGetSize(this->_set); + } + + virtual int _insert(void * value) { + if (!this->_set) return -1; + return BFSetInsert(this->_set, value); + } + + virtual int _remove(void * value) { + if (!this->_set) return -1; + return BFSetRemove(this->_set, value); + } + + virtual bool _contains(void * value) const { + if (!this->_set) return false; + return BFSetContains(this->_set, value); + } + + BFSet _set; +}; +} + +#endif // SET_HPP + From 7df64ecc2ae137da62439171f33e24c4e01cefab Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 27 May 2025 00:21:42 -0700 Subject: [PATCH 35/43] update todo --- bflibcpp/todo.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index aab00d94..9f18f541 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -32,6 +32,8 @@ - [ ] URL url = "this"; url = "this"; crashes - [x] Atomically figure out hash function without manually defining one for HashMap - [ ] Set + - [x] implementation + - [ ] tests - [x] release & compare functor work - [x] tree - [x] basic map From 9301a2e571d59c496b64d846f48b711edbb07743 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 27 May 2025 01:10:56 -0700 Subject: [PATCH 36/43] adding set tests --- bflibcpp/testbench/set_tests.hpp | 107 +++++++++++++++++++++++++++++++ bflibcpp/testbench/tests.cpp | 2 + 2 files changed, 109 insertions(+) create mode 100644 bflibcpp/testbench/set_tests.hpp diff --git a/bflibcpp/testbench/set_tests.hpp b/bflibcpp/testbench/set_tests.hpp new file mode 100644 index 00000000..eba58b95 --- /dev/null +++ b/bflibcpp/testbench/set_tests.hpp @@ -0,0 +1,107 @@ +/** + * author: Brando + * date: 11/26/24 + */ + +#ifndef SET_TESTS_HPP +#define SET_TESTS_HPP + +#define ASSERT_PUBLIC_MEMBER_ACCESS + +#include "set.hpp" + +extern "C" { +#include +} + +using namespace BF; + +BFTEST_UNIT_FUNC(test_setInit, 2<<10, { + Set set; +}) + +BFTEST_UNIT_FUNC(test_setInsert, 2<<10, { + Set set; + + int setsize = 2<<7; + for (int i = 0; i < setsize; i++) { + String value("%d", i); + int err = set.insert(value); + BF_ASSERT(err == 0, "error inserting %d", err); + } +}) + +BFTEST_UNIT_FUNC(test_setContains, 2<<10, { + Set set; + + // insert + int setsize = 2<<7; + for (int i = 0; i < setsize; i++) { + String value("%d", i); + int err = set.insert(value); + BF_ASSERT(err == 0, "error inserting %d", err); + } + + // get + for (int i = 0; i < setsize; i++) { + String value("%d", i); + try { + BF_ASSERT(set.contains(value), "!contains(%s)", value.cString()); + } catch (Exception & e) { + BF_ASSERT(false, "%s", e.what()); + } + } +}) + +BFTEST_UNIT_FUNC(test_setRemove, 2<<10, { + if (BFTEST_UNIT_FUNC_ITR == 0) { + BFRandInit(time(0)); + } + Set set; + + // insert + int setsize = 2<<7; + for (int i = 0; i < setsize; i++) { + String value("%d", i); + int err = set.insert(value); + BF_ASSERT(err == 0, "error inserting %d", err); + } + + // remove + int removeCount = 10; + while (removeCount) { + String value("%d", BFMathAbs(BFRand()) % setsize); + if (set.contains(value)) { + int err = set.remove(value); + BF_ASSERT(err == 0, "error removing for key=%s: %d", value.c_str(), err); + removeCount--; + } + } +}) + +BFTEST_UNIT_FUNC(test_setWithAllocMem, 2<<10, { + if (BFTEST_UNIT_FUNC_ITR == 0) { + BFRandInit(time(0)); + } + Set set; + + // insert + int setsize = 2<<7; + for (int i = 0; i < setsize; i++) { + char * value = (char *) malloc(sizeof(char) * 32); + snprintf(value, 32, "%d", i); + int err = set.insert(value); + BF_ASSERT(err == 0, "error inserting %d", err); + } +}) + +BFTEST_COVERAGE_FUNC(set_tests, { + BFTEST_LAUNCH(test_setInit); + BFTEST_LAUNCH(test_setInsert); + BFTEST_LAUNCH(test_setContains); + BFTEST_LAUNCH(test_setRemove); + BFTEST_LAUNCH(test_setWithAllocMem); +}) + +#endif // SET_TESTS_HPP + diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index d2cc0f2f..92c6d4ec 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -20,6 +20,7 @@ #include "tree_tests.hpp" #include "url_tests.hpp" #include "defer_tests.hpp" +#include "set_tests.hpp" BFTEST_SUITE_FUNC({ /* @@ -46,5 +47,6 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(defer_tests); */ + BFTEST_SUITE_LAUNCH(set_tests); }) From 65b23aae487c39012e33aaefd4a767eabe6403fe Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 27 May 2025 01:12:26 -0700 Subject: [PATCH 37/43] update todo --- bflibcpp/todo.md | 1 + 1 file changed, 1 insertion(+) diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index 9f18f541..9b994abf 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -37,4 +37,5 @@ - [x] release & compare functor work - [x] tree - [x] basic map + - [ ] allocator to accept byte size From 105f77741b61e2dbea0591a47f79bc412af33b06 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 27 May 2025 01:19:54 -0700 Subject: [PATCH 38/43] hashset tests --- bflibcpp/src/hashset.hpp | 4 +- bflibcpp/testbench/hashset_tests.hpp | 85 ++++++++++++++++++++++++++++ bflibcpp/testbench/set_tests.hpp | 2 +- bflibcpp/testbench/tests.cpp | 2 + bflibcpp/todo.md | 2 +- 5 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 bflibcpp/testbench/hashset_tests.hpp diff --git a/bflibcpp/src/hashset.hpp b/bflibcpp/src/hashset.hpp index 80448637..a6cec87c 100644 --- a/bflibcpp/src/hashset.hpp +++ b/bflibcpp/src/hashset.hpp @@ -26,8 +26,8 @@ class HashSet : public BasicSet { HashSet() : _set(NULL), BasicSet() { this->_set = BFHashSetCreate(); if (!this->_set) return; - BFHashSetSetCompare(this->_set, this->_BFMapCompare); - BFHashSetSetRelease(this->_set, this->_BFMapRelease); + BFHashSetSetCompare(this->_set, this->_BFSetCompare); + BFHashSetSetRelease(this->_set, this->_BFSetRelease); BFHashSetSetHashFunction(this->_set, this->_BFHashSetHashFunction); } diff --git a/bflibcpp/testbench/hashset_tests.hpp b/bflibcpp/testbench/hashset_tests.hpp new file mode 100644 index 00000000..a767e3df --- /dev/null +++ b/bflibcpp/testbench/hashset_tests.hpp @@ -0,0 +1,85 @@ +/** + * author: Brando + * date: 5/27/25 + */ + +#ifndef HASH_SET_TESTS_HPP +#define HASH_SET_TESTS_HPP + +#define ASSERT_PUBLIC_MEMBER_ACCESS + +#include "hashset.hpp" +#include "string.hpp" + +extern "C" { +#include +} + +using namespace BF; + +BFTEST_UNIT_FUNC(test_hashSetInit, 2<<10, { + HashSet set; +}) + +BFTEST_UNIT_FUNC(test_hashSetInsert, 2<<10, { + HashSet set; + + int setsize = 2<<7; + for (int i = 0; i < setsize; i++) { + String value("%d", i); + int err = set.insert(value); + BF_ASSERT(err == 0, "error inserting %d", err); + } +}) + +BFTEST_UNIT_FUNC(test_hashSetRemove, 2<<10, { + if (BFTEST_UNIT_FUNC_ITR == 0) { + BFRandInit(time(0)); + } + HashSet set; + + // insert + int setsize = 2<<7; + for (int i = 0; i < setsize; i++) { + String value("%d", i); + int err = set.insert(value); + BF_ASSERT(err == 0, "error inserting %d", err); + } + + // remove + int removeCount = 10; + while (removeCount) { + String value("%d", BFMathAbs(BFRand()) % setsize); + if (set.contains(value)) { + int err = set.remove(value); + BF_ASSERT(err == 0, "error removing for key=%s: %d", value.c_str(), err); + removeCount--; + } + } +}) + +BFTEST_UNIT_FUNC(test_hashSetWithAllocMem, 2<<10, { + if (BFTEST_UNIT_FUNC_ITR == 0) { + BFRandInit(time(0)); + } + HashSet set; + + // insert + int setsize = 2<<7; + for (int i = 0; i < setsize; i++) { + char * value = (char *) malloc(sizeof(char) * 32); + snprintf(value, 32, "%d", i); + int err = set.insert(value); + BF_ASSERT(err == 0, "error inserting %d", err); + } +}) + +BFTEST_COVERAGE_FUNC(hashset_tests, { + BFTEST_LAUNCH(test_hashSetInit); + BFTEST_LAUNCH(test_hashSetInsert); + BFTEST_LAUNCH(test_hashSetRemove); + BFTEST_LAUNCH(test_hashSetWithAllocMem); +}) + +#endif // HASH_SET_TESTS_HPP + diff --git a/bflibcpp/testbench/set_tests.hpp b/bflibcpp/testbench/set_tests.hpp index eba58b95..870b91f7 100644 --- a/bflibcpp/testbench/set_tests.hpp +++ b/bflibcpp/testbench/set_tests.hpp @@ -1,6 +1,6 @@ /** * author: Brando - * date: 11/26/24 + * date: 5/27/25 */ #ifndef SET_TESTS_HPP diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 92c6d4ec..4972bd54 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -21,6 +21,7 @@ #include "url_tests.hpp" #include "defer_tests.hpp" #include "set_tests.hpp" +#include "hashset_tests.hpp" BFTEST_SUITE_FUNC({ /* @@ -48,5 +49,6 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(defer_tests); */ BFTEST_SUITE_LAUNCH(set_tests); + BFTEST_SUITE_LAUNCH(hashset_tests); }) diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index 9b994abf..3a544362 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -33,7 +33,7 @@ - [x] Atomically figure out hash function without manually defining one for HashMap - [ ] Set - [x] implementation - - [ ] tests + - [x] tests - [x] release & compare functor work - [x] tree - [x] basic map From f1314af40a7421fa24f94b9e532bffa27bcd97d0 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 27 May 2025 01:24:47 -0700 Subject: [PATCH 39/43] update --- bflibcpp/testbench/tests.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 4972bd54..8eeb48bd 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -24,7 +24,6 @@ #include "hashset_tests.hpp" BFTEST_SUITE_FUNC({ - /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); @@ -33,21 +32,13 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(data_tests); BFTEST_SUITE_LAUNCH(array_tests); BFTEST_SUITE_LAUNCH(vector_tests); - */ BFTEST_SUITE_LAUNCH(map_tests); - /* BFTEST_SUITE_LAUNCH(object_tests); BFTEST_SUITE_LAUNCH(list_tests); - */ BFTEST_SUITE_LAUNCH(hashmap_tests); - /* BFTEST_SUITE_LAUNCH(exception_tests); - */ BFTEST_SUITE_LAUNCH(tree_tests); - /* BFTEST_SUITE_LAUNCH(url_tests); - BFTEST_SUITE_LAUNCH(defer_tests); - */ BFTEST_SUITE_LAUNCH(set_tests); BFTEST_SUITE_LAUNCH(hashset_tests); }) From 99983869713bea9326956fb004fbdd6f7d8ab8f7 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 27 May 2025 18:56:28 -0700 Subject: [PATCH 40/43] clean up --- .github/workflows/test.yml | 4 ++-- bflibcpp/src/implecompare.hpp | 16 ---------------- 2 files changed, 2 insertions(+), 18 deletions(-) delete mode 100644 bflibcpp/src/implecompare.hpp diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fd5c65b3..365bffbd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,11 +4,11 @@ on: push: branches: [ "dev" ] paths-ignore: - - '**.md' + - '*.md' pull_request: branches: [ "dev" ] paths-ignore: - - '**.md' + - '*.md' jobs: unit-tests-linux: diff --git a/bflibcpp/src/implecompare.hpp b/bflibcpp/src/implecompare.hpp deleted file mode 100644 index 162a6bde..00000000 --- a/bflibcpp/src/implecompare.hpp +++ /dev/null @@ -1,16 +0,0 @@ -/** - * author: brando - * date: 5/16/25 - */ - -#ifndef IMPL_LESSER_HPP -#define IMPL_LESSER_HPP - -#include "compare.hpp" - -namespace BF { - -} - -#endif // IMPL_LESSER_HPP - From 90f5091060056f2a4fcef04e702a09e05eb4fb52 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 27 May 2025 19:04:27 -0700 Subject: [PATCH 41/43] adding remove pointer --- bflibcpp/src/bflibcpp.hpp | 1 + bflibcpp/src/removepointer.hpp | 20 ++++++++++++++ bflibcpp/testbench/removepointer_tests.hpp | 31 ++++++++++++++++++++++ bflibcpp/testbench/tests.cpp | 4 +++ 4 files changed, 56 insertions(+) create mode 100644 bflibcpp/src/removepointer.hpp create mode 100644 bflibcpp/testbench/removepointer_tests.hpp diff --git a/bflibcpp/src/bflibcpp.hpp b/bflibcpp/src/bflibcpp.hpp index 65ab9749..fbe48a68 100644 --- a/bflibcpp/src/bflibcpp.hpp +++ b/bflibcpp/src/bflibcpp.hpp @@ -34,6 +34,7 @@ #include "sort.hpp" #include "set.hpp" #include "hashset.hpp" +#include "removepointer.hpp" #endif // BFLIBCPP_HPP diff --git a/bflibcpp/src/removepointer.hpp b/bflibcpp/src/removepointer.hpp new file mode 100644 index 00000000..b59a9f0a --- /dev/null +++ b/bflibcpp/src/removepointer.hpp @@ -0,0 +1,20 @@ +/** + * author: brando + * date: 5/27/25 + */ + +#ifndef BF_REMOVE_POINTER_HPP +#define BF_REMOVE_POINTER_HPP + +template +struct RemovePointer { + using type = T; +}; + +template +struct RemovePointer { + using type = T; +}; + +#endif // BF_REMOVE_POINTER_HPP + diff --git a/bflibcpp/testbench/removepointer_tests.hpp b/bflibcpp/testbench/removepointer_tests.hpp new file mode 100644 index 00000000..7ab91f95 --- /dev/null +++ b/bflibcpp/testbench/removepointer_tests.hpp @@ -0,0 +1,31 @@ +/** + * author: Brando + * date: 5/27/25 + */ + +#ifndef REMOVE_POINTER_TESTS_HPP +#define REMOVE_POINTER_TESTS_HPP + +#define ASSERT_PUBLIC_MEMBER_ACCESS + +#include "removepointer.hpp" + +extern "C" { +#include +} + +using namespace BF; + +BFTEST_UNIT_FUNC(test_removePointerInit, 2<<10, { + srand(time(0)); + int expected = rand(); + RemovePointer::type actual = expected; + BF_ASSERT(actual == expected); +}) + +BFTEST_COVERAGE_FUNC(removepointer_tests, { + BFTEST_LAUNCH(test_removePointerInit); +}) + +#endif // REMOVE_POINTER_TESTS_HPP + diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 8eeb48bd..8f948139 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -22,8 +22,10 @@ #include "defer_tests.hpp" #include "set_tests.hpp" #include "hashset_tests.hpp" +#include "removepointer_tests.hpp" BFTEST_SUITE_FUNC({ + /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); @@ -41,5 +43,7 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(set_tests); BFTEST_SUITE_LAUNCH(hashset_tests); + */ + BFTEST_SUITE_LAUNCH(removepointer_tests); }) From ede2d9c7767cae0d86dcf59e49f81a3ecdc72e1f Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 27 May 2025 19:09:25 -0700 Subject: [PATCH 42/43] clean up --- bflibcpp/testbench/tests.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 8f948139..ad3375a3 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -25,7 +25,6 @@ #include "removepointer_tests.hpp" BFTEST_SUITE_FUNC({ - /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); @@ -43,7 +42,6 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(set_tests); BFTEST_SUITE_LAUNCH(hashset_tests); - */ BFTEST_SUITE_LAUNCH(removepointer_tests); }) From ac3ec59873fb8859b60228703078b0a91b2ddbb2 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 27 May 2025 22:05:34 -0700 Subject: [PATCH 43/43] update todo --- bflibcpp/todo.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index 3a544362..aca6d676 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -37,5 +37,6 @@ - [x] release & compare functor work - [x] tree - [x] basic map - - [ ] allocator to accept byte size +- [ ] allocator + - [ ] add onto the create() method. Might be useful in the Array and List structures