Пример #1
0
int eval(linkedlist *expr, double *val) {

    int i;
    stack_t stk; /* operator stack */

    stack_init(&stk);

    for(i = 0; i < linkedlist_size(expr); i++) {
        token_t *t = linkedlist_get(expr, i);
        token_t *n2, *n1, *res;

        /* if number, push to stack: */
        if (t->type == TOKEN_NUMBER) {
            stack_push(&stk, t);
            continue;
        }

        /* all operators in the postfix expression are binary. */
        if (stack_size(&stk) < 2) {
            while(!stack_isEmpty(&stk))
                free(stack_pop(&stk));
            /* free the remaining tokens in expr: */
            while(i < linkedlist_size(expr))
                free(linkedlist_get(expr, i++));
            return -1; /* error. */
        }
        n2 = stack_pop(&stk);
        n1 = stack_pop(&stk);

        res = malloc(sizeof(token_t));
        res->type = TOKEN_NUMBER;
        res->op   = 0;
        res->num  = do_op(n1->num, n2->num, t->op);
        stack_push(&stk, res);

        free(n2);
        free(n1);
        free(t);
    }

    if (stack_size(&stk) != 1) {
        while(!stack_isEmpty(&stk)) {
            free(stack_pop(&stk));
        }
        return -1; /* error. */
    }

    *val = ((token_t *) stack_peek(&stk))->num;
    free(stack_pop(&stk));
    return 0;

}
Пример #2
0
static void work(_lazyworker_t* worker)
{
	_unit* unit = NULL;
    static struct timespec time_wait;

    for(;worker->threadStatus == eThreadStatus_RUNNING;)
    {
        pthread_mutex_lock(&worker->mutex);
        for(;;)
        {
            int que_size = linkedlist_size(worker->sth_todo);
            if(que_size > 0)
            {
                break;
            }
            pthread_cond_wait(&worker->cond, &worker->mutex);
            if(worker->threadStatus == eThreadStatus_CLOSED)
            {
                pthread_mutex_unlock(&worker->mutex);
                pthread_exit((void*) NULL);
            }
        }

        fprintf(stderr, "who wakes me up?");
        struct timeval current;
        gettimeofday(&current, NULL);
        unit = (_unit*)linkedlist_get(worker->sth_todo, 0);
        if(current.tv_sec - unit->t.tv_sec < 1)
        {
            // Be lazy
            time_wait.tv_sec =  unit->t.tv_sec + worker->time_wait.tv_sec;
            time_wait.tv_nsec = unit->t.tv_usec * 1000 + worker->time_wait.tv_nsec;
            fprintf(stderr, "I am too lazy to work, I'll be back in 1 minute. OK?");
            pthread_cond_timedwait(&worker->cond, &worker->mutex, &time_wait);
        }
        else
        {
            unit = (_unit*)linkedlist_remove_at(worker->sth_todo, 0);
            free(unit);
            if(worker->fp != NULL)
            {
            	fprintf(stderr, "OK, now I am ready to work, what's on your mind?\n");
            	worker->fp(worker->args);
            }
        }
        pthread_mutex_unlock(&worker->mutex);
    } // for
}
Пример #3
0
int
LinkedlistExercise(int verbose, struct cfg *cfg, char *args[])
{
    int rate, i, n = 0, idx;
	char *str;
    struct linkedlist *l = linkedlist_new(EXERCISE_MED_COUNT, NULL);

	cfg = NULL; args[0] = NULL;

	if (l == NULL) {
		AMSG("");
		return -1;
	}

	rate = EXERCISE_R0;
	for (i = 0; i < EXERCISE_MED_COUNT; i++) {
		if (i == EXERCISE_MED_P1) {
			rate = EXERCISE_R1;
		} else if (i == EXERCISE_MED_P2) {
			rate = EXERCISE_R2;
		} else if (i == EXERCISE_MED_P3) {
			rate = EXERCISE_R3;
		}

		if (rand() % 10 < rate) {
			idx = 0;
	        str = malloc(8);
	        sprintf(str, "%07d", n++);
			if (rand() % 5) {
				idx = linkedlist_size(l);
				if (idx) {
					idx = rand() % idx;
				}
			}
	        if (linkedlist_insert(l, idx, str) == -1) {
				PMNO(errno);
				return -1;
			}
			tcase_printf(verbose, "INSERT: %s size now %d\n", str, linkedlist_size(l));
		} else {
			if (linkedlist_is_empty(l)) {
				tcase_printf(verbose, "EMPTY\n");
			} else {
				idx = rand() % linkedlist_size(l);
		        str = linkedlist_get(l, idx);
				if (linkedlist_remove_data(l, str) == NULL) {
					PMNO(errno);
					return -1;
				}
				if ((idx % 10) == 0) {
					unsigned int count = 0;
					iter_t iter;

					linkedlist_iterate(l, &iter);
					while (linkedlist_next(l, &iter)) {
						count++;
					}
					if (count != linkedlist_size(l)) {
						PMSG("count=%u,linkedlist_size=%u\n", count, linkedlist_size(l));
						return -1;
					}
				}
				if (str) {
	    	    	tcase_printf(verbose, "REMOVE: %s %d\n", str, linkedlist_size(l));
			       	free(str);
				} else {
					PMSG("remove failure");
					return -1;
				}
			}
		}
    }
    linkedlist_del(l, allocator_free, NULL);

    return 0;
}