Skip to content

Commit

Permalink
API for hashlist_ntl().
Browse files Browse the repository at this point in the history
Signed-off-by: Rule Timothy (VM/EMT3) <Timothy.Rule@de.bosch.com>
  • Loading branch information
Dharshana Ravi authored and timrulebosch committed Aug 20, 2024
1 parent 65bfcea commit 3859080
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
21 changes: 21 additions & 0 deletions dse/clib/collections/hashlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define DSE_CLIB_COLLECTIONS_HASHLIST_H_

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
Expand Down Expand Up @@ -52,5 +53,25 @@ static __inline__ void* hashlist_at(HashList* h, uint32_t index)
return hashmap_get(&h->hash_map, key);
}

static __inline__ void* hashlist_ntl(HashList *h, size_t object_size, bool destroy)
{
size_t count = hashlist_length(h);
void *object_array = calloc(count + 1, object_size);

// Copy elements from the HashList to the object array.
for (uint32_t i = 0; i < count; i++) {
void *source_hashlist = hashlist_at(h, i);
if (source_hashlist != NULL) {
memcpy((void *)object_array + i * object_size, &source_hashlist, object_size);
}
}

// Optionally destroy the original HashList.
if (destroy) {
hashlist_destroy(h);
}

return object_array;
}

#endif // DSE_CLIB_COLLECTIONS_HASHLIST_H_
3 changes: 2 additions & 1 deletion tests/collections/__test__.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void test_hash_destroy_ext_mallocd_1(void** state);
void test_hash_destroy_ext_with_callback(void** state);
void test_set(void** state);
void test_hashlist(void** state);

void test_hashlist_ntl(void** state);

int main()
{
Expand All @@ -22,6 +22,7 @@ int main()
cmocka_unit_test(test_hash_destroy_ext_with_callback),
cmocka_unit_test(test_set),
cmocka_unit_test(test_hashlist),
cmocka_unit_test(test_hashlist_ntl),
};

return cmocka_run_group_tests(tests, NULL, NULL);
Expand Down
27 changes: 27 additions & 0 deletions tests/collections/test_hashlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,30 @@ void test_hashlist(void** state)
hashlist_destroy(&h);
assert_int_equal(hashlist_length(&h), 0);
}

void test_hashlist_ntl(void** state)
{
UNUSED(state);

HashList h;
hashlist_init(&h, 2);
hashlist_append(&h, (void*)"foo");
hashlist_append(&h, (void*)"bar");


// Call hashlist_ntl to convert to NTL.
char** converted_array = (char**)hashlist_ntl(&h, sizeof(char*), false);
assert_int_equal(hashlist_length(&h), 2);
assert_string_equal(converted_array[0], "foo");
assert_string_equal(converted_array[1], "bar");
assert_null(converted_array[2]);
free(converted_array);

// Call hashlist_ntl to convert to NTL AND DESTROY.
converted_array = (char**)hashlist_ntl(&h, sizeof(char*), true);
assert_int_equal(hashlist_length(&h), 0);
assert_string_equal(converted_array[0], "foo");
assert_string_equal(converted_array[1], "bar");
assert_null(converted_array[2]);
free(converted_array);
}

0 comments on commit 3859080

Please sign in to comment.