Skip to content

Commit

Permalink
Add array_new_filter{,_swap}()
Browse files Browse the repository at this point in the history
  • Loading branch information
khorben committed Apr 23, 2018
1 parent 9ac1d70 commit ef1fdb2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/System/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ typedef void (*ArrayForeachSwap)(void * data, void * value);

/* functions */
Array * array_new(size_t size);
Array * array_new_filter(Array * array, ArrayFilter func, void * data);
Array * array_new_filter_swap(Array * array, ArrayFilterSwap func, void * data);
# define array_new_type(object) array_new(sizeof(type))
void array_delete(Array * array);

Expand Down
46 changes: 46 additions & 0 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,52 @@ Array * array_new(size_t size)
}


/* array_new_filter */
Array * array_new_filter(Array * array, ArrayFilter func, void * data)
{
Array * a;
size_t i;
char * value;

if((a = array_new(array->size)) == NULL)
return NULL;
for(i = 0; i < array->count; i++)
{
value = array->value + (array->size * i);
if(func(value, data) == true)
if(array_append(a, value) != 0)
{
array_delete(a);
return NULL;
}
}
return a;
}


/* array_new_filter_swap */
Array * array_new_filter_swap(Array * array, ArrayFilterSwap func, void * data)
{
Array * a;
size_t i;
char * value;

if((a = array_new(array->size)) == NULL)
return NULL;
for(i = 0; i < array->count; i++)
{
value = array->value + (array->size * i);
if(func(data, value) == true)
if(array_append(a, value) != 0)
{
array_delete(a);
return NULL;
}
}
return a;
}


/* array_delete */
void array_delete(Array * array)
{
Expand Down

0 comments on commit ef1fdb2

Please sign in to comment.