Skip to content

Commit

Permalink
Adding '*_contains_value' to Array, Deque, List and SList
Browse files Browse the repository at this point in the history
  • Loading branch information
srdja committed Apr 11, 2016
1 parent 5d02880 commit 67210e5
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 10 deletions.
17 changes: 16 additions & 1 deletion src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,26 @@ enum cc_stat array_trim_capacity(Array *ar)
* @return the number of occurrences of the element
*/
size_t array_contains(Array *ar, void *element)
{
return array_contains_value(ar, element, cc_common_cmp_ptr);
}

/**
* Returns the number of occurrences of the value pointed to by <code>e</code>
* within the specified Array.
*
* @param[in] ar Array that is being searched
* @param[in] element the element that is being searched for
* @param[in] cmp Comparator function which returns 0 if the values passed to it are equal
*
* @return the number of occurrences of the value
*/
size_t array_contains_value(Array *ar, void *e, int (*cmp) (const void*, const void*))
{
size_t o = 0;
size_t i;
for (i = 0; i < ar->size; i++) {
if (element == ar->buffer[i])
if (cmp(e, ar->buffer[i]) == 0)
o++;
}
return o;
Expand Down
1 change: 1 addition & 0 deletions src/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void array_reverse (Array *ar);
enum cc_stat array_trim_capacity (Array *ar);

size_t array_contains (Array *ar, void *element);
size_t array_contains_value (Array *ar, void *element, int (*cmp) (const void*, const void*));
size_t array_size (Array *ar);
size_t array_capacity (Array *ar);

Expand Down
21 changes: 18 additions & 3 deletions src/deque.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,21 +699,36 @@ void deque_reverse(Deque *deque)
}

/**
* Returns the number of occurrences of the element within the specified deque.
* Returns the number of occurrences of the element within the specified Deque.
*
* @param[in] deque the deque that is being searched
* @param[in] deque Deque that is being searched
* @param[in] element the element that is being searched for
*
* @return the number of occurrences of the element
*/
size_t deque_contains(Deque *deque, void *element)
{
return deque_contains_value(deque, element, cc_common_cmp_ptr);
}

/**
* Returns the number of occurrences of the value poined to by <code>element</code>
* within the deque.
*
* @param[in] deque Deque that is being searched
* @param[in] element the element that is being searched for
* @param[in] cmp Comparator function which returns 0 if the values passed to it are equal
*
* @return the number of occurrences of the element
*/
size_t deque_contains_value(Deque *deque, void *element, int (*cmp) (const void*, const void*))
{
size_t i;
size_t o = 0;

for (i = 0; i < deque->size; i++) {
size_t p = (deque->first + i) & (deque->capacity - 1);
if (deque->buffer[p] == element)
if (cmp(deque->buffer[p], element) == 0)
o++;
}
return o;
Expand Down
1 change: 1 addition & 0 deletions src/deque.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ void deque_reverse (Deque *deque);
enum cc_stat deque_trim_capacity (Deque *deque);

size_t deque_contains (Deque *deque, void *element);
size_t deque_contains_value (Deque *deque, void *element, int (*cmp)(const void*, const void*));
size_t deque_size (Deque *deque);
size_t deque_capacity (Deque *deque);

Expand Down
23 changes: 19 additions & 4 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -949,17 +949,32 @@ enum cc_stat list_to_array(List *list, void ***out)
* element within the list.
*
* @param[in] list List on which the search is performed
* @param[in] element element being looked for
* @param[in] element element being seached for
*
* @return number of found matches
*/
size_t list_contains(List *list, void *element)
{
Node *node = list->head;
size_t e_count = 0;
return list_contains_value(list, element, cc_common_cmp_ptr);
}

/**
* Returns the number occurrences of the value pointed to by <code>element</code>
* within the List.
*
* @param[in] list List on which the search is performed
* @param[in] element element being searched for
* @param[in] cmp Comparator function which returns 0 if the values passed to it are equal
*
* @return number of found matches
*/
size_t list_contains_value(List *list, void *element, int (*cmp) (const void*, const void*))
{
Node *node = list->head;
size_t e_count = 0;

while (node) {
if (node->data == element)
if (cmp(node->data, element) == 0)
e_count++;
node = node->next;
}
Expand Down
1 change: 1 addition & 0 deletions src/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ enum cc_stat list_copy_deep (List *list, void *(*cp) (void*), List **out)
enum cc_stat list_replace_at (List *list, void *element, size_t index, void **out);

size_t list_contains (List *list, void *element);
size_t list_contains_value (List *list, void *element, int (*cmp) (const void*, const void*));
enum cc_stat list_index_of (List *list, void *element, size_t *index);
enum cc_stat list_to_array (List *list, void ***out);

Expand Down
18 changes: 16 additions & 2 deletions src/slist.c
Original file line number Diff line number Diff line change
Expand Up @@ -903,12 +903,26 @@ enum cc_stat slist_copy_deep(SList *list, void *(*cp) (void*), SList **out)
*/
size_t slist_contains(SList *list, void *element)
{
SNode *node = list->head;
return slist_contains_value(list, element, cc_common_cmp_ptr);
}

/**
* Returns the number of occurrences of the value pointed to by <code>element</code>
* within the specified SList.
*
* @param[in] list SList on which the search is performed
* @param[in] element element being searched for
* @param[in] cmp Comparator function which returns 0 if the values passed to it are equal
*
* @return number of occurrences of the value
*/
size_t slist_contains_value(SList *list, void *element, int (*cmp) (const void*, const void*))
{
SNode *node = list->head;
size_t e_count = 0;

while (node) {
if (node->data == element)
if (cmp(node->data, element) == 0)
e_count++;
node = node->next;
}
Expand Down
1 change: 1 addition & 0 deletions src/slist.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ enum cc_stat slist_copy_deep (SList *list, void *(*cp) (void*), SList **o
enum cc_stat slist_replace_at (SList *list, void *element, size_t index, void **out);

size_t slist_contains (SList *list, void *element);
size_t slist_contains_value (SList *list, void *element, int (*cmp) (const void*, const void*));
enum cc_stat slist_index_of (SList *list, void *element, size_t *index);
enum cc_stat slist_to_array (SList *list, void ***out);

Expand Down

0 comments on commit 67210e5

Please sign in to comment.