Skip to content

Commit

Permalink
simplifying code
Browse files Browse the repository at this point in the history
  • Loading branch information
jpffitch committed Jan 24, 2018
1 parent 91b2315 commit feae628
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 109 deletions.
178 changes: 83 additions & 95 deletions Engine/cs_par_base.c
Expand Up @@ -27,6 +27,7 @@
#include "csoundCore.h"

#include "cs_par_base.h"
static int csp_set_exists(struct set_t *set, void *data);

int csp_thread_index_get(CSOUND *csound)
{
Expand Down Expand Up @@ -172,19 +173,18 @@ void csp_barrier_dealloc(CSOUND *csound, void **barrier)
*/

/* static prototypes */
static int set_element_delloc(CSOUND *csound,
static void set_element_delloc(CSOUND *csound,
struct set_element_t **set_element);
static int set_element_alloc(CSOUND *csound,
static void set_element_alloc(CSOUND *csound,
struct set_element_t **set_element,
char *data);
static int set_is_set(CSOUND *csound, struct set_t *set);
static int set_is_set(struct set_t *set);
#if 0
static int
set_element_is_set_element(CSOUND *csound,
struct set_element_t *set_element);
#endif

int csp_set_alloc(CSOUND *csound, struct set_t **set,
void csp_set_alloc(CSOUND *csound, struct set_t **set,
set_element_data_eq *ele_eq_func,
set_element_data_print *ele_print_func)
{
Expand All @@ -202,16 +202,16 @@ int csp_set_alloc(CSOUND *csound, struct set_t **set,
p->ele_print_func = ele_print_func;
p->cache = NULL;
//printf("csp_set_alloc: %p\n", p);
return CSOUND_SUCCESS;
return;
}

int csp_set_dealloc(CSOUND *csound, struct set_t **set)
void csp_set_dealloc(CSOUND *csound, struct set_t **set)
{
struct set_element_t *ele;
struct set_t *p = *set;
if (UNLIKELY(set == NULL || *set == NULL))
csound->Die(csound, Str("Invalid NULL Parameter set"));
if (UNLIKELY(!set_is_set(csound, *set)))
if (UNLIKELY(!set_is_set(*set)))
csound->Die(csound, Str("Invalid Parameter set not a set"));

if (p->cache != NULL) csound->Free(csound, p->cache);
Expand All @@ -226,10 +226,10 @@ int csp_set_dealloc(CSOUND *csound, struct set_t **set)
csound->Free(csound, p);
*set = NULL;

return CSOUND_SUCCESS;
return;
}

static int set_element_alloc(CSOUND *csound,
static void set_element_alloc(CSOUND *csound,
struct set_element_t **set_element,
char *data)
{
Expand All @@ -244,23 +244,22 @@ static int set_element_alloc(CSOUND *csound,
memcpy((*set_element)->hdr, SET_ELEMENT_HDR, HDR_LEN);
(*set_element)->data = cs_strdup(csound, data);

return CSOUND_SUCCESS;
return;
}

static int set_element_delloc(CSOUND *csound,
static void set_element_delloc(CSOUND *csound,
struct set_element_t **set_element)
{
if (UNLIKELY(set_element == NULL || *set_element == NULL))
csound->Die(csound, Str("Invalid NULL Parameter set_element"));
csound->Free(csound, *set_element);
*set_element = NULL;

return CSOUND_SUCCESS;
return;
}

static int set_is_set(CSOUND *csound, struct set_t *set)
static int set_is_set(struct set_t *set)
{
IGN(csound);
char buf[4];
if (set == NULL) return 0;
memcpy(buf, (char *)set, HDR_LEN);
Expand All @@ -281,11 +280,11 @@ static int
}
#endif

int csp_set_alloc_string(CSOUND *csound, struct set_t **set)
void csp_set_alloc_string(CSOUND *csound, struct set_t **set)
{
return csp_set_alloc(csound, set,
csp_set_element_string_eq,
csp_set_element_string_print);
csp_set_alloc(csound, set,
csp_set_element_string_eq,
csp_set_element_string_print);
}

int csp_set_element_string_eq(struct set_element_t *ele1,
Expand Down Expand Up @@ -314,7 +313,7 @@ void csp_set_element_ptr_print(CSOUND *csound,
csound->Message(csound, "%p", ele->data);
}

static int set_update_cache(CSOUND *csound, struct set_t *set)
static void set_update_cache(CSOUND *csound, struct set_t *set)
{
if (set->cache != NULL) {
csound->Free(csound, set->cache);
Expand All @@ -333,41 +332,30 @@ static int set_update_cache(CSOUND *csound, struct set_t *set)
ele = ele->next;
}
}
return CSOUND_SUCCESS;
return;
}

/*
* if out_set_element is not NULL and the element corresponding to
* data is not found it will not be changed
*/
static int set_element_get(CSOUND *csound, struct set_t *set,
static void set_element_get(struct set_t *set,
char *data,
struct set_element_t **out_set_element)
{
IGN(csound);
#ifdef SET_DEBUG
if (UNLIKELY(set == NULL))
csound->Die(csound, Str("Invalid NULL Parameter set"));
if (UNLIKELY(data == NULL))
csound->Die(csound, Str("Invalid NULL Parameter data"));
if (UNLIKELY(out_set_element == NULL))
csound->Die(csound, Str("Invalid NULL Parameter out_set_element"));
#endif
{
struct set_element_t *ele = set->head;
struct set_element_t data_ele = { SET_ELEMENT_HDR, data, 0 };
while (ele != NULL) {
if (set->ele_eq_func(ele, &data_ele)) {
*out_set_element = ele;
break;
}
ele = ele->next;
struct set_element_t *ele = set->head;
struct set_element_t data_ele = { SET_ELEMENT_HDR, data, 0 };
while (ele != NULL) {
if (set->ele_eq_func(ele, &data_ele)) {
*out_set_element = ele;
break;
}
ele = ele->next;
}
return CSOUND_SUCCESS;
return;
}

int csp_set_add(CSOUND *csound, struct set_t *set, void *data)
void csp_set_add(CSOUND *csound, struct set_t *set, void *data)
{
struct set_element_t *ele = NULL;
#ifdef SET_DEBUG
Expand All @@ -377,8 +365,8 @@ int csp_set_add(CSOUND *csound, struct set_t *set, void *data)
csound->Die(csound, "Invalid NULL Parameter data");
#endif

if (csp_set_exists(csound, set, data)) {
return CSOUND_SUCCESS;
if (csp_set_exists(set, data)) {
return;
}

set_element_alloc(csound, &ele, data);
Expand All @@ -394,10 +382,10 @@ int csp_set_add(CSOUND *csound, struct set_t *set, void *data)

set_update_cache(csound, set);

return CSOUND_SUCCESS;
return;
}

int csp_set_remove(CSOUND *csound, struct set_t *set, void *data)
void csp_set_remove(CSOUND *csound, struct set_t *set, void *data)
{
#ifdef SET_DEBUG
if (UNLIKELY(set == NULL))
Expand Down Expand Up @@ -430,30 +418,30 @@ int csp_set_remove(CSOUND *csound, struct set_t *set, void *data)
}
set_update_cache(csound, set);

return CSOUND_SUCCESS;
return;
}

int csp_set_exists(CSOUND *csound, struct set_t *set, void *data)
static int csp_set_exists(struct set_t *set, void *data)
{
struct set_element_t *ele = NULL;
#ifdef SET_DEBUG
if (UNLIKELY(set == NULL))
csound->Die(csound, "Invalid NULL Parameter set");
if (UNLIKELY(data == NULL))
csound->Die(csound, "Invalid NULL Parameter data");
#endif
set_element_get(csound, set, data, &ele);
/* #ifdef SET_DEBUG */
/* if (UNLIKELY(set == NULL)) */
/* csound->Die(csound, "Invalid NULL Parameter set"); */
/* if (UNLIKELY(data == NULL)) */
/* csound->Die(csound, "Invalid NULL Parameter data"); */
/* #endif */
set_element_get(set, data, &ele);

return (ele == NULL ? 0 : 1);
}

int csp_set_print(CSOUND *csound, struct set_t *set)
void csp_set_print(CSOUND *csound, struct set_t *set)
{
struct set_element_t *ele;
#ifdef SET_DEBUG
if (UNLIKELY(set == NULL))
csound->Die(csound, "Invalid NULL Parameter set");
if (UNLIKELY(!set_is_set(csound, set)))
if (UNLIKELY(!set_is_set(set)))
csound->Die(csound, "Invalid Parameter set not a set");
#endif

Expand All @@ -467,15 +455,15 @@ int csp_set_print(CSOUND *csound, struct set_t *set)
}
csound->Message(csound, " }\n");

return CSOUND_SUCCESS;
return;
}

inline int csp_set_count(struct set_t *set)
{
#ifdef SET_DEBUG
if (UNLIKELY(set == NULL))
csound->Die(csound, "Invalid NULL Parameter set");
if (UNLIKELY(!set_is_set(csound, set)))
if (UNLIKELY(!set_is_set(set)))
csound->Die(csound, "Invalid Parameter set not a set");
#endif

Expand All @@ -484,40 +472,40 @@ inline int csp_set_count(struct set_t *set)

/* 0 indexed */
// FIXME inlining breaks linkage for MSVC
/*inline*/ int csp_set_get_num(struct set_t *set, int num, void **data)
/*inline*/ void csp_set_get_num(struct set_t *set, int num, void **data)
{
#ifdef SET_DEBUG
if (UNLIKELY(set == NULL))
csound->Die(csound, "Invalid NULL Parameter set");
if (UNLIKELY(!set_is_set(csound, set)))
csound->Die(csound, "Invalid Parameter set not a set");
if (UNLIKELY(um >= set->count))
csound->Die(csound, "Invalid Parameter num is out of bounds");
if (UNLIKELY(data == NULL))
csound->Die(csound, "Invalid NULL Parameter data");
#endif
/* #ifdef SET_DEBUG */
/* if (UNLIKELY(set == NULL)) */
/* csound->Die(csound, "Invalid NULL Parameter set"); */
/* if (UNLIKELY(!set_is_set(set))) */
/* csound->Die(csound, "Invalid Parameter set not a set"); */
/* if (UNLIKELY(um >= set->count)) */
/* csound->Die(csound, "Invalid Parameter num is out of bounds"); */
/* if (UNLIKELY(data == NULL)) */
/* csound->Die(csound, "Invalid NULL Parameter data"); */
/* #endif */

*data = set->cache[num]->data;
/*
if (set->cache != NULL) {

}
else {
int ctr = 0;
struct set_element_t *ele = set->head;
while (ctr < num && ele != NULL) {
ctr++;
ele = ele->next;
}
if (ctr == num && ele != NULL) {
*data = ele->data;
}
}
*/
return CSOUND_SUCCESS;
/* if (set->cache != NULL) { */

/* } */
/* else { */
/* int ctr = 0; */
/* struct set_element_t *ele = set->head; */
/* while (ctr < num && ele != NULL) { */
/* ctr++; */
/* ele = ele->next; */
/* } */
/* if (ctr == num && ele != NULL) { */
/* *data = ele->data; */
/* } */
/* } */

return;
}

int csp_set_union(CSOUND *csound, struct set_t *first,
void csp_set_union(CSOUND *csound, struct set_t *first,
struct set_t *second, struct set_t **result)
{
int ctr = 0;
Expand All @@ -526,11 +514,11 @@ int csp_set_union(CSOUND *csound, struct set_t *first,
#ifdef SET_DEBUG
if (UNLIKELY(first == NULL))
csound->Die(csound, "Invalid NULL Parameter first");
if (UNLIKELY(!set_is_set(csound, first)))
if (UNLIKELY(!set_is_set(first)))
csound->Die(csound, "Invalid Parameter set not a first");
if (UNLIKELY(second == NULL))
csound->Die(csound, "Invalid NULL Parameter second");
if (UNLIKELY(!set_is_set(csound, second)))
if (UNLIKELY(!set_is_set(second)))
csound->Die(csound, "Invalid Parameter set not a second");
if (UNLIKELY(result == NULL))
csound->Die(csound, "Invalid NULL Parameter result");
Expand Down Expand Up @@ -559,22 +547,22 @@ int csp_set_union(CSOUND *csound, struct set_t *first,
csp_set_add(csound, *result, data);
ctr++;
}
return CSOUND_SUCCESS;
return;
}

int csp_set_intersection(CSOUND *csound, struct set_t *first,
void csp_set_intersection(CSOUND *csound, struct set_t *first,
struct set_t *second, struct set_t **result)
{
int ctr = 0;
int first_len;
#ifdef SET_DEBUG
if (UNLIKELY(first == NULL))
csound->Die(csound, "Invalid NULL Parameter first");
if (UNLIKELY(!set_is_set(csound, first)))
if (UNLIKELY(!set_is_set(first)))
csound->Die(csound, "Invalid Parameter set not a first");
if (UNLIKELY(second == NULL))
csound->Die(csound, "Invalid NULL Parameter second");
if (UNLIKELY(!set_is_set(csound, second)))
if (UNLIKELY(!set_is_set(second)))
csound->Die(csound, "Invalid Parameter set not a second");
if (UNLIKELY(result == NULL))
csound->Die(csound, "Invalid NULL Parameter result");
Expand All @@ -591,11 +579,11 @@ int csp_set_intersection(CSOUND *csound, struct set_t *first,
while (ctr < first_len) {
void *data = NULL;
csp_set_get_num(first, ctr, &data);
if (csp_set_exists(csound, second, data)) {
if (csp_set_exists(second, data)) {
csp_set_add(csound, *result, data);
}
ctr++;
}

return CSOUND_SUCCESS;
return;
}

0 comments on commit feae628

Please sign in to comment.