void deq_get_tail(node **deqhead, char *value){ node *temp = *deqhead, *temp_prev = *deqhead ; // check is queue is empty if( deq_is_empty( temp ) ) return ; if( deq_size(deqhead) == 2 ) { temp = temp->link; free(temp->contents); free(temp); return; } while( temp->link != NULL ) { temp_prev = temp ; temp = temp->link ; } //fprintf(stderr, "val: %s, size: %d \n", temp->contents, deq_size(deqhead)); value = strcpy( value, temp->contents ) ; /*if( deq_size( deqhead ) == 1 ) { free( temp->contents ) ; free( temp ) ; *deqhead = NULL ; return; }*/ free( temp->contents ) ; free( temp ) ; temp_prev->link = NULL ; }
static void free_prices(map_t *contracts) { map_iter_t *iter = map_iter_create(); for (map_begin(iter, contracts); map_iter_valid(iter, contracts); map_iter_next(iter)) { void *contract = (void *)map_iter_key(iter); deq_t *prices = map_iter_value(iter); int i, size = deq_size(prices); FREE(contract); for (i = 0; i < size; ++i) { float *price = deq_at(prices, i); FREE(price); } deq_destroy(prices); } map_iter_destroy(iter); }
static int ema_exec(void *data, void *data2) { RAII_VAR(struct msg *, msg, (struct msg *)data, msg_decr); Quote *quote = (Quote *)msg->data; float *price; map_iter_t *iter = map_iter_create(); deq_t *prices; int size; NOT_USED(data2); /* FIXME */ if (fabs(quote->thyquote.m_dZXJ) <= 0.000001) { xcb_log(XCB_LOG_WARNING, "Invalid quote: '%d,%d,%s,%.2f'", quote->thyquote.m_nTime, quote->m_nMSec, quote->thyquote.m_cHYDM, quote->thyquote.m_dZXJ); goto end; } if ((price = ALLOC(sizeof (float))) == NULL) { xcb_log(XCB_LOG_WARNING, "Error allocating memory for price"); goto end; } *price = quote->thyquote.m_dZXJ; pthread_mutex_lock(&conlock); map_find(iter, contracts, quote->thyquote.m_cHYDM); if (!map_iter_valid(iter, contracts)) { const char *contract; if ((contract = mem_strdup(quote->thyquote.m_cHYDM)) == NULL) { xcb_log(XCB_LOG_WARNING, "Error allocating memory for contract"); pthread_mutex_unlock(&conlock); FREE(price); goto end; } prices = deq_create(); map_insert(contracts, contract, prices); } else prices = map_iter_value(iter); deq_push_back(prices, price); if ((size = deq_size(prices)) == n) { int i; float ema = *((float *)deq_at(prices, 0)); time_t t = (time_t)quote->thyquote.m_nTime; struct tm lt; char datestr[64], res[256]; float *front; for (i = 1; i < size; ++i) ema = (2.0 / (n + 1)) * *((float *)deq_at(prices, i)) + (1 - (2.0 / (n + 1))) * ema; strftime(datestr, sizeof datestr, "%F %T", localtime_r(&t, <)); snprintf(res, sizeof res, "EMA,%s.%03d,%s|%.2f", datestr, quote->m_nMSec, quote->thyquote.m_cHYDM, ema); out2rmp(res); front = deq_front(prices); FREE(front); deq_pop_front(prices); } pthread_mutex_unlock(&conlock); map_iter_destroy(iter); end: return 0; }
bool deq_is_empty(node *deqhead){ return deq_size( deqhead ) == 0 ; }
// The astute student will notice that some tests may end with orphaned allocated // memory references that we did not deallocate (a put without a get for example). // For the sake of practicum sanity, we will let that go here. In reality we would // would want to make sure that we were cleaning that up properly during testing, or // perhaps write a deq_delete() function. // These variables are visible to all tests node* dequeue; /* the dequeue head */ char get_value[MAX_TEST_LENGTH]; /* string content copied here for get functions */ char put_value1[] = "ONE"; /* string content test values for put functions */ char put_value2[] = "TWO"; char put_value3[] = "THREE"; START_TEST("size of zero for an empty dequeue") dequeue = NULL; /* dequeue initally empty */ ASSERT_EQUALS( 0, deq_size( dequeue )); END_TEST() START_TEST("check for empty dequeue") dequeue = NULL; /* dequeue initally empty */ ASSERT(deq_is_empty( dequeue )); END_TEST() START_TEST("put one entry at head of empty dequeue") dequeue = NULL; /* dequeue initally empty */ deq_put_head( &dequeue, put_value1); ASSERT_EQUALS( 1, deq_size( dequeue )); END_TEST() START_TEST("check for non-empty dequeue") dequeue = NULL; /* dequeue initally empty */