Skip to content
Snippets Groups Projects
Commit 32fc7414 authored by abivarma.kandiah's avatar abivarma.kandiah
Browse files

Add Vector filter

parent 56b778db
No related branches found
No related tags found
No related merge requests found
...@@ -31,5 +31,6 @@ void vector_empty(vector vec); ...@@ -31,5 +31,6 @@ void vector_empty(vector vec);
void vector_free(vector vec); void vector_free(vector vec);
void vector_print(vector vec, void (*print)(type)); void vector_print(vector vec, void (*print)(type));
vector vector_map(vector vec, type (*f)(type)); vector vector_map(vector vec, type (*f)(type));
vector vector_filter(vector vec, bool (*f)(type));
#endif #endif
\ No newline at end of file
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
void print_type(type val); void print_type(type val);
int times_two(type a); int times_two(type a);
bool lower_than_five(type a);
int main() int main()
{ {
...@@ -63,12 +64,17 @@ int main() ...@@ -63,12 +64,17 @@ int main()
//Test Vector map //Test Vector map
vector test_2 = vector_map(test, &times_two); vector test_2 = vector_map(test, &times_two);
vector_print(test_2, &print_type); vector_print(test_2, &print_type);
printf("\n");
//Test Vector filter //Test Vector filter
vector test_3 = vector_filter(test_2, lower_than_five);
vector_print(test_3, &print_type);
printf("\n");
//Test Vector Free //Test Vector Free
vector_free(test); vector_free(test);
vector_free(test_2); vector_free(test_2);
vector_free(test_3);
return 0; return 0;
} }
...@@ -81,4 +87,9 @@ void print_type(type val) ...@@ -81,4 +87,9 @@ void print_type(type val)
int times_two(type a) int times_two(type a)
{ {
return 2 * a; return 2 * a;
}
bool lower_than_five(type a)
{
return (a < 5);
} }
\ No newline at end of file
...@@ -147,3 +147,15 @@ vector vector_map(vector vec, type (*f)(type)) ...@@ -147,3 +147,15 @@ vector vector_map(vector vec, type (*f)(type))
return new_vec; return new_vec;
} }
vector vector_filter(vector vec, bool (*f)(type))
{
vector new_vec = vector_create();
for (int i = 0; i < vec->length; i++)
{
if(f(vec->content[i]))
{
vector_push(new_vec, vec->content[i]);
}
}
return new_vec;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment