Commit f25fa70f authored by Robert Schmidt's avatar Robert Schmidt

apply clang-format

parent e8824827
...@@ -27,24 +27,23 @@ SOFTWARE. ...@@ -27,24 +27,23 @@ SOFTWARE.
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
elm_arr_t find_if_arr_it(seq_arr_t* arr, void* start_it, void* end_it, void* value , bool(*f)(const void*, const void*)) elm_arr_t find_if_arr_it(seq_arr_t* arr, void* start_it, void* end_it, void* value, bool (*f)(const void*, const void*))
{ {
assert(arr != NULL); assert(arr != NULL);
while(start_it != end_it){ while (start_it != end_it) {
if(f(value,start_it)) if (f(value, start_it))
return (elm_arr_t){.found = true, .it = start_it}; return (elm_arr_t){.found = true, .it = start_it};
start_it = seq_arr_next(arr,start_it); start_it = seq_arr_next(arr, start_it);
} }
// If I trusted the average developer I should return it=start_it, but I don't. // If I trusted the average developer I should return it=start_it, but I don't.
return (elm_arr_t){.found = false, .it = NULL}; return (elm_arr_t){.found = false, .it = NULL};
} }
elm_arr_t find_if_arr(seq_arr_t* arr, void* value , bool(*f)(const void*, const void*)) elm_arr_t find_if_arr(seq_arr_t* arr, void* value, bool (*f)(const void*, const void*))
{ {
assert(arr != NULL); assert(arr != NULL);
void* start_it = seq_arr_front(arr); void* start_it = seq_arr_front(arr);
void* end_it = seq_arr_end(arr); void* end_it = seq_arr_end(arr);
return find_if_arr_it(arr, start_it, end_it, value, f); return find_if_arr_it(arr, start_it, end_it, value, f);
} }
...@@ -23,12 +23,12 @@ SOFTWARE. ...@@ -23,12 +23,12 @@ SOFTWARE.
*/ */
#ifndef FIND_ALGORITHM #ifndef FIND_ALGORITHM
#define FIND_ALGORITHM #define FIND_ALGORITHM
#include <stdbool.h> #include <stdbool.h>
#include "../ds/seq_arr.h" #include "../ds/seq_arr.h"
typedef struct{ typedef struct {
void* it; void* it;
bool found; bool found;
} elm_arr_t; } elm_arr_t;
...@@ -36,24 +36,23 @@ typedef struct{ ...@@ -36,24 +36,23 @@ typedef struct{
// Sequencial containers // Sequencial containers
/** /**
* @brief Find elements in an array if the predicate is true * @brief Find elements in an array if the predicate is true
* @param arr The sequence container * @param arr The sequence container
* @param value Pointer to the value that will be used by the predicate * @param value Pointer to the value that will be used by the predicate
* @param f Function representing the predicate * @param f Function representing the predicate
* @return Whether the predicate was fullfilled and the iterator to the element if true * @return Whether the predicate was fullfilled and the iterator to the element if true
*/ */
elm_arr_t find_if_arr(seq_arr_t* arr, void* value, bool(*f)(const void* value, const void* it)); elm_arr_t find_if_arr(seq_arr_t* arr, void* value, bool (*f)(const void* value, const void* it));
/** /**
* @brief Find elements in an array in the semi-open range [start_it, end_it) * @brief Find elements in an array in the semi-open range [start_it, end_it)
* @param arr The sequence container * @param arr The sequence container
* @param start_it Iterator to the first element * @param start_it Iterator to the first element
* @param end_it Iterator to the one past last element * @param end_it Iterator to the one past last element
* @param value Pointer to the value usied in the predicate * @param value Pointer to the value usied in the predicate
* @param f Function representing the predicate * @param f Function representing the predicate
* @return Whether the predicate was fullfilled and the iterator to the element if true * @return Whether the predicate was fullfilled and the iterator to the element if true
*/ */
elm_arr_t find_if_arr_it(seq_arr_t* arr, void* start_it, void* end_it, void* value , bool(*f)(const void* value, const void* it)); elm_arr_t find_if_arr_it(seq_arr_t* arr, void* start_it, void* end_it, void* value, bool (*f)(const void* value, const void* it));
#endif #endif
...@@ -33,10 +33,8 @@ void for_each(seq_arr_t* arr, void* value, void (*f)(void* value, void* it)) ...@@ -33,10 +33,8 @@ void for_each(seq_arr_t* arr, void* value, void (*f)(void* value, void* it))
void* it = seq_arr_front(arr); void* it = seq_arr_front(arr);
void* end = seq_arr_end(arr); void* end = seq_arr_end(arr);
while(it != end){ while (it != end) {
f(value, it); f(value, it);
it = seq_arr_next(arr,it); it = seq_arr_next(arr, it);
} }
} }
...@@ -23,16 +23,16 @@ SOFTWARE. ...@@ -23,16 +23,16 @@ SOFTWARE.
*/ */
#ifndef FOR_EACH_ALGORITHM #ifndef FOR_EACH_ALGORITHM
#define FOR_EACH_ALGORITHM #define FOR_EACH_ALGORITHM
#include "../ds/seq_arr.h" #include "../ds/seq_arr.h"
/** /**
* @brief Apply function to each element in the sequence container * @brief Apply function to each element in the sequence container
* @param arr The sequence container * @param arr The sequence container
* @param value Pointer to the value that will be used by the function applied * @param value Pointer to the value that will be used by the function applied
* @param fn_apply Function called by every element in the sequence container * @param fn_apply Function called by every element in the sequence container
*/ */
void for_each(seq_arr_t* arr, void* value, void (*fn_apply)(void* value, void* it)); void for_each(seq_arr_t* arr, void* value, void (*fn_apply)(void* value, void* it));
#endif #endif
...@@ -29,30 +29,27 @@ SOFTWARE. ...@@ -29,30 +29,27 @@ SOFTWARE.
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
static static const size_t MIN_SIZE = 8;
const size_t MIN_SIZE = 8;
static static void maybe_expand(seq_arr_t* arr)
void maybe_expand(seq_arr_t* arr)
{ {
if(arr->size + 1 == arr->cap){ if (arr->size + 1 == arr->cap) {
arr->data = realloc(arr->data, 2*arr->cap*arr->elt_size); arr->data = realloc(arr->data, 2 * arr->cap * arr->elt_size);
assert(arr->data != NULL && "realloc failed to allocate memory"); assert(arr->data != NULL && "realloc failed to allocate memory");
arr->cap *=2; arr->cap *= 2;
} }
} }
static static void maybe_shrink(seq_arr_t* arr)
void maybe_shrink(seq_arr_t* arr)
{ {
const float occ = (float)arr->size / (float)arr->cap; const float occ = (float)arr->size / (float)arr->cap;
if(arr->size > MIN_SIZE && occ < 0.25){ if (arr->size > MIN_SIZE && occ < 0.25) {
assert(arr->cap > MIN_SIZE); assert(arr->cap > MIN_SIZE);
seq_arr_t tmp = {.data = NULL, .size = arr->size, .elt_size = arr->elt_size, .cap = arr->cap/2}; seq_arr_t tmp = {.data = NULL, .size = arr->size, .elt_size = arr->elt_size, .cap = arr->cap / 2};
tmp.data = calloc(tmp.cap, tmp.cap); tmp.data = calloc(tmp.cap, tmp.cap);
assert(tmp.data != NULL && "Memory exhausted"); assert(tmp.data != NULL && "Memory exhausted");
assert(arr->size <= tmp.cap); assert(arr->size <= tmp.cap);
memcpy(tmp.data, arr->data, arr->size*arr->elt_size); memcpy(tmp.data, arr->data, arr->size * arr->elt_size);
free(arr->data); free(arr->data);
memcpy(arr, &tmp, sizeof(seq_arr_t)); memcpy(arr, &tmp, sizeof(seq_arr_t));
} }
...@@ -67,16 +64,16 @@ void seq_arr_init(seq_arr_t* arr, size_t elt_size) //__attribute__(malloc) ...@@ -67,16 +64,16 @@ void seq_arr_init(seq_arr_t* arr, size_t elt_size) //__attribute__(malloc)
assert(arr->data != NULL); assert(arr->data != NULL);
} }
void seq_arr_free(seq_arr_t* arr, void (*free_func)(void*) ) void seq_arr_free(seq_arr_t* arr, void (*free_func)(void*))
{ {
assert(arr != NULL); assert(arr != NULL);
assert(arr->data != NULL); assert(arr->data != NULL);
if(free_func != NULL) { if (free_func != NULL) {
void* start_it = seq_arr_front(arr); void* start_it = seq_arr_front(arr);
void* end_it = seq_arr_end(arr); void* end_it = seq_arr_end(arr);
while(start_it != end_it){ while (start_it != end_it) {
free_func(start_it); free_func(start_it);
start_it = seq_arr_next(arr, start_it); start_it = seq_arr_next(arr, start_it);
} }
} }
...@@ -88,12 +85,12 @@ void seq_arr_push_back(seq_arr_t* arr, void* data, size_t len) ...@@ -88,12 +85,12 @@ void seq_arr_push_back(seq_arr_t* arr, void* data, size_t len)
{ {
assert(arr != NULL); assert(arr != NULL);
assert(arr->data != NULL); assert(arr->data != NULL);
//assert(data != NULL); // assert(data != NULL);
assert(len == arr->elt_size); assert(len == arr->elt_size);
maybe_expand(arr); maybe_expand(arr);
const size_t offset = arr->size * arr->elt_size; const size_t offset = arr->size * arr->elt_size;
memcpy(&arr->data[offset], data, arr->elt_size); memcpy(&arr->data[offset], data, arr->elt_size);
arr->size += 1; arr->size += 1;
} }
...@@ -111,7 +108,7 @@ void seq_arr_erase_deep(seq_arr_t* arr, void* start_it, void (*free_func)(void* ...@@ -111,7 +108,7 @@ void seq_arr_erase_deep(seq_arr_t* arr, void* start_it, void (*free_func)(void*
// start_it must be in the range of arr->data // start_it must be in the range of arr->data
assert(arr != NULL); assert(arr != NULL);
assert(start_it != NULL); assert(start_it != NULL);
return seq_arr_erase_it(arr, start_it, seq_arr_next(arr, start_it), free_func); return seq_arr_erase_it(arr, start_it, seq_arr_next(arr, start_it), free_func);
} }
void seq_arr_erase_it(seq_arr_t* arr, void* start_it, void* end_it, void (*free_func)(void* it)) void seq_arr_erase_it(seq_arr_t* arr, void* start_it, void* end_it, void (*free_func)(void* it))
...@@ -120,25 +117,26 @@ void seq_arr_erase_it(seq_arr_t* arr, void* start_it, void* end_it, void (*free_ ...@@ -120,25 +117,26 @@ void seq_arr_erase_it(seq_arr_t* arr, void* start_it, void* end_it, void (*free_
assert(arr != NULL); assert(arr != NULL);
assert(start_it != NULL); assert(start_it != NULL);
assert(end_it != NULL); assert(end_it != NULL);
assert(end_it >= start_it); assert(end_it >= start_it);
if(start_it == end_it) return; if (start_it == end_it)
return;
if(free_func != NULL) { if (free_func != NULL) {
void* start_it = seq_arr_front(arr); void* start_it = seq_arr_front(arr);
void* end_it = seq_arr_end(arr); void* end_it = seq_arr_end(arr);
while(start_it != end_it){ while (start_it != end_it) {
free_func(start_it); free_func(start_it);
start_it = seq_arr_next(arr, start_it); start_it = seq_arr_next(arr, start_it);
} }
} }
const int num_bytes_move = seq_arr_end(arr) - end_it; const int num_bytes_move = seq_arr_end(arr) - end_it;
assert(num_bytes_move > -1); assert(num_bytes_move > -1);
memmove(start_it, end_it, num_bytes_move); memmove(start_it, end_it, num_bytes_move);
const int num_bytes_erase = end_it - start_it; const int num_bytes_erase = end_it - start_it;
const int32_t num_elm_erase = num_bytes_erase/arr->elt_size; const int32_t num_elm_erase = num_bytes_erase / arr->elt_size;
assert(num_elm_erase > 0); assert(num_elm_erase > 0);
arr->size -= num_elm_erase; arr->size -= num_elm_erase;
...@@ -170,7 +168,7 @@ void* seq_arr_end(seq_arr_t const* arr) ...@@ -170,7 +168,7 @@ void* seq_arr_end(seq_arr_t const* arr)
{ {
assert(arr != NULL); assert(arr != NULL);
assert(arr->data != NULL); assert(arr->data != NULL);
return &arr->data[arr->size*arr->elt_size]; return &arr->data[arr->size * arr->elt_size];
} }
void* seq_arr_at(seq_arr_t const* arr, uint32_t pos) void* seq_arr_at(seq_arr_t const* arr, uint32_t pos)
...@@ -178,7 +176,7 @@ void* seq_arr_at(seq_arr_t const* arr, uint32_t pos) ...@@ -178,7 +176,7 @@ void* seq_arr_at(seq_arr_t const* arr, uint32_t pos)
assert(arr != NULL); assert(arr != NULL);
assert(arr->data != NULL); assert(arr->data != NULL);
assert(pos < arr->size); assert(pos < arr->size);
return arr->data + pos*arr->elt_size; return arr->data + pos * arr->elt_size;
} }
ptrdiff_t seq_arr_dist(seq_arr_t const* arr, void const* first, void const* last) ptrdiff_t seq_arr_dist(seq_arr_t const* arr, void const* first, void const* last)
...@@ -186,8 +184,7 @@ ptrdiff_t seq_arr_dist(seq_arr_t const* arr, void const* first, void const* last ...@@ -186,8 +184,7 @@ ptrdiff_t seq_arr_dist(seq_arr_t const* arr, void const* first, void const* last
assert(arr != NULL); assert(arr != NULL);
assert(first != NULL); assert(first != NULL);
assert(last != NULL); assert(last != NULL);
const ptrdiff_t pos = (last - first)/arr->elt_size; const ptrdiff_t pos = (last - first) / arr->elt_size;
assert(pos > -1); assert(pos > -1);
return pos; return pos;
} }
...@@ -26,7 +26,7 @@ SOFTWARE. ...@@ -26,7 +26,7 @@ SOFTWARE.
#define SEQ_CONTAINER_ARRAY #define SEQ_CONTAINER_ARRAY
/* /*
* Basic sequence container with a similar API and behaviour to C++ std::vector * Basic sequence container with a similar API and behaviour to C++ std::vector
*/ */
#include <stdbool.h> #include <stdbool.h>
...@@ -34,67 +34,68 @@ SOFTWARE. ...@@ -34,67 +34,68 @@ SOFTWARE.
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
typedef struct seq_arr_s{ typedef struct seq_arr_s {
uint8_t* data; uint8_t* data;
size_t size; size_t size;
const size_t elt_size; const size_t elt_size;
size_t cap; size_t cap;
} seq_arr_t; } seq_arr_t;
/** /**
* Init a sequence container, similar to a constructor * Init a sequence container, similar to a constructor
* @brief Constructor. * @brief Constructor.
* @param arr The sequence container * @param arr The sequence container
* @param elm_sz value returned by the sizeof operator of the type that the container will hold e.g., sizeof(int). * @param elm_sz value returned by the sizeof operator of the type that the container will hold e.g., sizeof(int).
*/ */
void seq_arr_init(seq_arr_t* arr, size_t elm_sz); void seq_arr_init(seq_arr_t* arr, size_t elm_sz);
/** /**
* Free a sequence container, similar to a destructor * Free a sequence container, similar to a destructor
* @brief Destructor. * @brief Destructor.
* @param arr The sequence container * @param arr The sequence container
* @param free_func Function called for every element while destructing e.g., useful to free memory of deep objects. * @param free_func Function called for every element while destructing e.g., useful to free memory of deep objects.
*/ */
void seq_arr_free(seq_arr_t* arr, void (*free_func)(void* it)); void seq_arr_free(seq_arr_t* arr, void (*free_func)(void* it));
/** /**
* @brief Push back elements into the sequence container. Value semantic. i.e., the void* data will be shallowly copied in the array. * @brief Push back elements into the sequence container. Value semantic. i.e., the void* data will be shallowly copied in the
* @param arr The sequence container * array.
* @param data Pointer to the data to be copied * @param arr The sequence container
* @param elm_sz Size of the element to be copied e.g., sizeof(int) * @param data Pointer to the data to be copied
*/ * @param elm_sz Size of the element to be copied e.g., sizeof(int)
*/
void seq_arr_push_back(seq_arr_t* arr, void* data, size_t elm_sz); void seq_arr_push_back(seq_arr_t* arr, void* data, size_t elm_sz);
/** /**
* @brief Erase the element pointed by it * @brief Erase the element pointed by it
* @param arr The sequence container * @param arr The sequence container
* @param it Iterator to the element to erase * @param it Iterator to the element to erase
*/ */
void seq_arr_erase(seq_arr_t*, void* it); void seq_arr_erase(seq_arr_t*, void* it);
/** /**
* @brief Erase the element pointed by it and call the f function. Useful to free deep copies * @brief Erase the element pointed by it and call the f function. Useful to free deep copies
* @param arr The sequence container * @param arr The sequence container
* @param it Iterator to the element to erase * @param it Iterator to the element to erase
* @param free_func Function to call while erasing element * @param free_func Function to call while erasing element
*/ */
void seq_arr_erase_deep(seq_arr_t* arr, void* it, void (*free_func)(void* it)); void seq_arr_erase_deep(seq_arr_t* arr, void* it, void (*free_func)(void* it));
/** /**
* @brief Erase the elements in the semi-open range [first,last) * @brief Erase the elements in the semi-open range [first,last)
* @param arr The sequence container * @param arr The sequence container
* @param first Iterator to the first element to erase * @param first Iterator to the first element to erase
* @param last Iterator to the last element. Note that this element will NOT be erased * @param last Iterator to the last element. Note that this element will NOT be erased
* @param f Function that will be called by every element while erasing. Useful for deep copies. Pass NULL if shallow * @param f Function that will be called by every element while erasing. Useful for deep copies. Pass NULL if shallow
* erased required * erased required
*/ */
void seq_arr_erase_it(seq_arr_t* arr, void* first, void* last, void (*free_func)(void* it)); void seq_arr_erase_it(seq_arr_t* arr, void* first, void* last, void (*free_func)(void* it));
/** /**
* @brief Elements in the array * @brief Elements in the array
* @param arr The sequence container * @param arr The sequence container
* @return The number of elements in the sequence container * @return The number of elements in the sequence container
*/ */
size_t seq_arr_size(seq_arr_t const* arr); size_t seq_arr_size(seq_arr_t const* arr);
///// /////
...@@ -102,43 +103,42 @@ size_t seq_arr_size(seq_arr_t const* arr); ...@@ -102,43 +103,42 @@ size_t seq_arr_size(seq_arr_t const* arr);
///// /////
/** /**
* @brief First iterator * @brief First iterator
* @param arr The sequence container * @param arr The sequence container
* @return The iterator to the first element in the sequence container * @return The iterator to the first element in the sequence container
*/ */
void* seq_arr_front(seq_arr_t const* arr); void* seq_arr_front(seq_arr_t const* arr);
/** /**
* @brief Next iterator * @brief Next iterator
* @param arr The sequence container * @param arr The sequence container
* @param it Iterator to a valid element * @param it Iterator to a valid element
* @return The iterator to the next element in the sequence container * @return The iterator to the next element in the sequence container
*/ */
void* seq_arr_next(seq_arr_t const* arr, void const* it); void* seq_arr_next(seq_arr_t const* arr, void const* it);
/** /**
* @brief End iterator * @brief End iterator
* @param arr The sequence container * @param arr The sequence container
* @return The iterator to one past the last element in the sequence container * @return The iterator to one past the last element in the sequence container
*/ */
void* seq_arr_end(seq_arr_t const* arr); void* seq_arr_end(seq_arr_t const* arr);
/** /**
* @brief Returns iterator in positions pos * @brief Returns iterator in positions pos
* @param arr The sequence container * @param arr The sequence container
* @param pos The position of the element in the sequence container * @param pos The position of the element in the sequence container
* @return The iterator to the element * @return The iterator to the element
*/ */
void* seq_arr_at(seq_arr_t const* arr, uint32_t pos); void* seq_arr_at(seq_arr_t const* arr, uint32_t pos);
/** /**
* @brief Distance between iterators * @brief Distance between iterators
* @param arr The sequence container * @param arr The sequence container
* @param first Iterator to first element * @param first Iterator to first element
* @param last Iterator to last element * @param last Iterator to last element
* @return The distance among iterators * @return The distance among iterators
*/ */
ptrdiff_t seq_arr_dist(seq_arr_t const* arr, void const* first, void const* last); ptrdiff_t seq_arr_dist(seq_arr_t const* arr, void const* first, void const* last);
#endif #endif
...@@ -4,39 +4,35 @@ ...@@ -4,39 +4,35 @@
#include <assert.h> #include <assert.h>
/* /*
Example to show seq_arr_t capabilities and usage Example to show seq_arr_t capabilities and usage
To compile: gcc test_seq_array.c ../seq_arr.c ../../alg/find.c ../../alg/foreach.c To compile: gcc test_seq_array.c ../seq_arr.c ../../alg/find.c ../../alg/foreach.c
*/ */
static bool eq_int(const void* value, const void* it)
static
bool eq_int(const void* value, const void* it)
{ {
const int* v = (const int *)value; const int* v = (const int*)value;
const int* i = (const int*)it; const int* i = (const int*)it;
return *v == *i; return *v == *i;
} }
static static void sum_elm(void* value, void* it)
void sum_elm(void* value, void* it)
{ {
int* sum = (int*)value; int* sum = (int*)value;
int* elm = (int*)it; int* elm = (int*)it;
*sum += *elm; *sum += *elm;
} }
static static int compar(const void* m0, const void* m1)
int compar(const void* m0, const void* m1)
{ {
int* a = (int*) m0; int* a = (int*)m0;
int* b = (int*) m1; int* b = (int*)m1;
if(*a < *b) if (*a < *b)
return -1; return -1;
if(*a == *b) if (*a == *b)
return 0; return 0;
return 1; return 1;
} }
...@@ -46,7 +42,7 @@ int main() ...@@ -46,7 +42,7 @@ int main()
seq_arr_init(&arr, sizeof(int)); seq_arr_init(&arr, sizeof(int));
// Insert data and expand // Insert data and expand
for(int i = 0; i < 100; ++i) for (int i = 0; i < 100; ++i)
seq_arr_push_back(&arr, &i, sizeof(int)); seq_arr_push_back(&arr, &i, sizeof(int));
// Check inserted data // Check inserted data
...@@ -58,7 +54,7 @@ int main() ...@@ -58,7 +54,7 @@ int main()
int value = 50; int value = 50;
elm_arr_t elm = find_if_arr(&arr, &value, eq_int); elm_arr_t elm = find_if_arr(&arr, &value, eq_int);
assert(elm.found == true); assert(elm.found == true);
//Check // Check
assert(*(int*)elm.it == 50); assert(*(int*)elm.it == 50);
assert(seq_arr_dist(&arr, seq_arr_front(&arr), elm.it) == 50); assert(seq_arr_dist(&arr, seq_arr_front(&arr), elm.it) == 50);
...@@ -74,12 +70,12 @@ int main() ...@@ -74,12 +70,12 @@ int main()
assert(*(int*)seq_arr_at(&arr, 50) == 51); assert(*(int*)seq_arr_at(&arr, 50) == 51);
// Erase range and force shrink // Erase range and force shrink
seq_arr_erase_it(&arr, seq_arr_front(&arr), seq_arr_at(&arr, 90) ,NULL); seq_arr_erase_it(&arr, seq_arr_front(&arr), seq_arr_at(&arr, 90), NULL);
assert(seq_arr_size(&arr) == 9); assert(seq_arr_size(&arr) == 9);
assert(*(int*)seq_arr_front(&arr) == 91); assert(*(int*)seq_arr_front(&arr) == 91);
//Recall that C functions qsort and bsearch, also work // Recall that C functions qsort and bsearch, also work
int key = 95; int key = 95;
void* base = seq_arr_front(&arr); void* base = seq_arr_front(&arr);
size_t nmemb = seq_arr_size(&arr); size_t nmemb = seq_arr_size(&arr);
size_t size = sizeof(int); size_t size = sizeof(int);
...@@ -92,4 +88,3 @@ int main() ...@@ -92,4 +88,3 @@ int main()
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment