Skip to content

Commit

Permalink
Issue #1 add initial capacity in vector create function
Browse files Browse the repository at this point in the history
  • Loading branch information
epatrizio committed Feb 15, 2022
1 parent ab964b1 commit 276217f
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ graph resize_graph(graph g, size_t (*hash_fct)(const char *str))

graph graph_create(size_t initial_size)
{
graph g = graph_bucket_vector_create();
graph g = graph_bucket_vector_create(0);

graph_bucket_vector_resize(g, (initial_size>0) ? initial_size : GRAPH_INITIAL_SIZE);

for (unsigned int i=0 ; i<graph_bucket_vector_size(g) ; i++)
graph_bucket_vector_set(g, i, graph_elt_vector_create());
graph_bucket_vector_set(g, i, graph_elt_vector_create(0));

return g;
}
Expand Down
4 changes: 2 additions & 2 deletions src/hash_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ hash_map resize_hash_map(hash_map hm, size_t (*hash_fct)(const char *str))

hash_map hash_map_create(size_t initial_size)
{
hash_map hm = hm_bucket_vector_create();
hash_map hm = hm_bucket_vector_create(0);

hm_bucket_vector_resize(hm, (initial_size>0) ? initial_size : HASH_MAP_INITIAL_SIZE);

for (unsigned int i=0 ; i<hm_bucket_vector_size(hm) ; i++)
hm_bucket_vector_set(hm, i, hm_elt_vector_create());
hm_bucket_vector_set(hm, i, hm_elt_vector_create(0));

return hm;
}
Expand Down
4 changes: 2 additions & 2 deletions src/hash_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ hash_set resize_hash_set(hash_set hs, size_t (*hash_fct)(const char *str))

hash_set hash_set_create(size_t initial_size)
{
hash_set hs = hs_bucket_vector_create();
hash_set hs = hs_bucket_vector_create(0);

hs_bucket_vector_resize(hs, (initial_size>0) ? initial_size : HASH_SET_INITIAL_SIZE);

for (unsigned int i=0 ; i<hs_bucket_vector_size(hs) ; i++)
hs_bucket_vector_set(hs, i, hs_elt_vector_create());
hs_bucket_vector_set(hs, i, hs_elt_vector_create(0));

return hs;
}
Expand Down
2 changes: 1 addition & 1 deletion src/priority_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#define priority_queue_create_fct(T) \
T##_priority_queue T##_priority_queue_create() { \
return T##_vector_create(); \
return T##_vector_create(0); \
}

#define priority_queue_destroy_fct(T) \
Expand Down
6 changes: 3 additions & 3 deletions src/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include "vector.h"
#include "stack.h"

#define stack_create_fct(T) \
T##_stack T##_stack_create() { \
return T##_vector_create(); \
#define stack_create_fct(T) \
T##_stack T##_stack_create() { \
return T##_vector_create(0); \
}

#define stack_destroy_fct(T) \
Expand Down
17 changes: 9 additions & 8 deletions src/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
typedef struct T##_resizable_array *T##_vector;

#define vector_create_sign(T) \
T##_vector T##_vector_create(); \
T##_vector T##_vector_create(size_t); \
void T##_vector_destroy(T##_vector); \
size_t T##_vector_size(T##_vector); \
bool T##_vector_is_empty(T##_vector); \
Expand All @@ -29,13 +29,14 @@
vector_struct(T) \
vector_create_sign(T)

#define vector_create_fct(T) \
T##_vector T##_vector_create() { \
T##_vector v = malloc(sizeof(T##_vector)); \
v->elts = malloc(MIN_CAPACITY * sizeof(T)); \
v->capacity = MIN_CAPACITY; \
v->size = 0; \
return v; \
#define vector_create_fct(T) \
T##_vector T##_vector_create(size_t capacity) { \
size_t cap = (capacity > 0) ? capacity : MIN_CAPACITY; \
T##_vector v = malloc(sizeof(T##_vector)); \
v->elts = malloc(cap * sizeof(T)); \
v->capacity = cap; \
v->size = 0; \
return v; \
}

#define vector_destroy_fct(T) \
Expand Down
1 change: 1 addition & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ int main(int argc, char *argv[])
{
TestSuite *ts_vector = createTestSuite("VECTOR");
addTestFunction(ts_vector, "char_vector_empty", test_char_vector_empty);
addTestFunction(ts_vector, "short_vector_initial_capacity", test_short_vector_initial_capacity);
addTestFunction(ts_vector, "double_vector_resize", test_double_vector_resize);
addTestFunction(ts_vector, "int_vector", test_int_vector);
runTestSuite(ts_vector, argc, argv);
Expand Down
19 changes: 15 additions & 4 deletions tests/tests_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

void test_char_vector_empty(const void *function_node)
{
char_vector v = char_vector_create();
char_vector v = char_vector_create(0);

assert_true(char_vector_is_empty(v), function_node);
assert_equals_int(MIN_CAPACITY, v->capacity, function_node);
Expand All @@ -15,9 +15,20 @@ void test_char_vector_empty(const void *function_node)
char_vector_destroy(v);
}

void test_short_vector_initial_capacity(const void *function_node)
{
short_vector v = short_vector_create(42);

assert_true(short_vector_is_empty(v), function_node);
assert_equals_int(42, v->capacity, function_node);
assert_equals_int(0, short_vector_size(v), function_node);

short_vector_destroy(v);
}

void test_double_vector_resize(const void *function_node)
{
double_vector v = double_vector_create();
double_vector v = double_vector_create(0);

assert_true(double_vector_is_empty(v), function_node);
assert_equals_int(MIN_CAPACITY, v->capacity, function_node);
Expand Down Expand Up @@ -46,7 +57,7 @@ void test_double_vector_resize(const void *function_node)

void test_int_vector(const void *function_node)
{
int_vector v = int_vector_create();
int_vector v = int_vector_create(0);
int_vector_add(v, 0, 0);
int_vector_add(v, 1, 10);

Expand Down Expand Up @@ -98,7 +109,7 @@ vector_init_fct(custom_struct)

void test_custom_struct_vector(const void *function_node)
{
custom_struct_vector v = custom_struct_vector_create();
custom_struct_vector v = custom_struct_vector_create(0);

assert_true(custom_struct_vector_is_empty(v), function_node);
assert_equals_int(MIN_CAPACITY, v->capacity, function_node);
Expand Down
1 change: 1 addition & 0 deletions tests/tests_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef struct _custom_struct {
vector_init(custom_struct)

void test_char_vector_empty(const void*);
void test_short_vector_initial_capacity(const void*);
void test_double_vector_resize(const void*);
void test_int_vector(const void*);
void test_custom_struct_vector(const void*);
Expand Down

0 comments on commit 276217f

Please sign in to comment.