Added functional methods (map, filter and reduce)
This commit is contained in:
101
usage.c
101
usage.c
@@ -16,6 +16,8 @@
|
||||
puts("\n"); \
|
||||
} while(0)
|
||||
|
||||
#define UNUSED(X) (void)(X)
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -30,6 +32,9 @@ static int bigint_usage(void);
|
||||
|
||||
static vector_order_t cmp_int_asc(const void *x, const void *y);
|
||||
static vector_order_t cmp_int_desc(const void *x, const void *y);
|
||||
static void square(void *element, void *env);
|
||||
static int is_even(const void *element, void *env);
|
||||
static void adder(void *accumulator, const void *element, void *env);
|
||||
|
||||
int main(void) {
|
||||
int st;
|
||||
@@ -64,6 +69,24 @@ vector_order_t cmp_int_desc(const void *x, const void *y) {
|
||||
return cmp_int_asc(y, x);
|
||||
}
|
||||
|
||||
void square(void *element, void *env) {
|
||||
UNUSED(env);
|
||||
int *value = (int*)element;
|
||||
*value = (*value) * (*value);
|
||||
}
|
||||
|
||||
int is_even(const void *element, void *env) {
|
||||
UNUSED(env);
|
||||
int value = *(int*)element;
|
||||
|
||||
return (value % 2) == 0;
|
||||
}
|
||||
|
||||
void adder(void *accumulator, const void *element, void *env) {
|
||||
UNUSED(env);
|
||||
*(int*)accumulator += *(int*)element;
|
||||
}
|
||||
|
||||
int vector_usage(void) {
|
||||
// Create a vector of 3 integers
|
||||
vector_result_t res = vector_new(3, sizeof(int));
|
||||
@@ -197,6 +220,84 @@ int vector_usage(void) {
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
vector_result_t map_clear_res = vector_clear(vector);
|
||||
if (map_clear_res.status != VECTOR_OK) {
|
||||
printf("Cannot clear vector: %s\n", map_clear_res.message);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Map vector elements
|
||||
for (size_t idx = 1; idx <= 5; idx++) {
|
||||
vector_result_t map_push_res = vector_push(vector, &idx);
|
||||
if (map_push_res.status != VECTOR_OK) {
|
||||
printf("Error while adding elements: %s\n", map_push_res.message);
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
sz = vector_size(vector);
|
||||
|
||||
// Square vector elements: [1, 2, 3, 4, 5] -> [1, 4, 9, 16, 25]
|
||||
vector_result_t map_res = vector_map(vector, square, NULL);
|
||||
if (map_res.status != VECTOR_OK) {
|
||||
printf("Error while mapping vector: %s\n", map_res.message);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Squared vector: ");
|
||||
for (size_t idx = 0; idx < sz; idx++) {
|
||||
vector_result_t map_get_res = vector_get(vector, idx);
|
||||
if (map_get_res.status != VECTOR_OK) {
|
||||
printf("Cannot retrieve vec[%zu]: %s\n", idx, map_get_res.message);
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
int *val = (int*)map_get_res.value.element;
|
||||
printf("%d ", *val);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
// Filter vector elements: [1, 4, 9, 16, 25] -> [4, 16]
|
||||
vector_result_t filter_res = vector_filter(vector, is_even, NULL);
|
||||
if (filter_res.status != VECTOR_OK) {
|
||||
printf("Error while filtering vector: %s\n", filter_res.message);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
sz = vector_size(vector);
|
||||
|
||||
printf("Filtered vector: ");
|
||||
for (size_t idx = 0; idx < sz; idx++) {
|
||||
vector_result_t map_get_res = vector_get(vector, idx);
|
||||
if (map_get_res.status != VECTOR_OK) {
|
||||
printf("Cannot retrieve vec[%zu]: %s\n", idx, map_get_res.message);
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
int *val = (int*)map_get_res.value.element;
|
||||
printf("%d ", *val);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
// Reduce vector elements: [4, 16] -> 20
|
||||
int sum = 0;
|
||||
vector_result_t reduce_res = vector_reduce(vector, &sum, adder, NULL);
|
||||
if (reduce_res.status != VECTOR_OK) {
|
||||
printf("Error while reducing vector: %s\n", reduce_res.message);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Sum of vector: %d\n\n", sum);
|
||||
|
||||
// Free vector
|
||||
vector_result_t del_res = vector_destroy(vector);
|
||||
if (del_res.status != VECTOR_OK) {
|
||||
|
||||
Reference in New Issue
Block a user