Exemplo n.º 1
0
/*
 * Helper function to push element onto heap.
 * Element is added at the end of the heap and pushed up.
 */
double * push_sum(double heap[], unsigned int last,
				double sum)
{
	heap[last]= sum;

	if( last > 0 ) pull_up(heap, last, last);
	
	return heap;
}
Exemplo n.º 2
0
/*
 * Helper function to pull element up a heap.
 */
void pull_up(double heap[], unsigned int n,  unsigned int i)
{
	int parent = (i-1)/2;
	if( i > 0 && heap[i] < heap[parent] ){
		double temp = heap[parent];
		heap[parent] = heap[i];
		heap[i] = temp;
		pull_up(heap, n, parent);
	}
}
Exemplo n.º 3
0
	void update(Node *proot, int start, int end, int value)
	{
		if (start > proot->r || end < proot->l)
			return;
		start = max(start, proot->l);
		end = min(end, proot->r);
		if (start == proot->l && end == proot->r)
		{
			//do something
			return;
		}
		push_down(proot);
		update(proot->pleft, start, end, value);
		update(proot->pright, start, end, value);
		pull_up(proot);
	}
Exemplo n.º 4
0
	int query(Node *proot, int start, int end)
	{
		int ret = 0;
		if (start > proot->r || end < proot->l)
			return 0;
		start = max(start, proot->l);
		end = min(end, proot->r);
		if (start == proot->l && end == proot->r)
		{
			//do something
			return;
		}
		push_down(proot);
		ret = max(ret, query(proot->pleft, start, end));
		ret = max(ret, query(proot->pright, start, end));
		pull_up(proot);
		return ret;
	}
Exemplo n.º 5
0
void iv_timer_unregister(struct iv_timer *_t)
{
	struct iv_state *st = iv_get_state();
	struct iv_timer_ *t = (struct iv_timer_ *)_t;
	struct iv_timer_ **m;
	struct iv_timer_ **p;

	if (t->index == -1) {
		iv_fatal("iv_timer_unregister: called with timer not "
			 "on the heap");
	}

	if (t->index > st->num_timers) {
		iv_fatal("iv_timer_unregister: timer index %d > %d",
			 t->index, st->num_timers);
	}

	p = get_node(st, t->index);
	if (*p != t) {
		iv_fatal("iv_timer_unregister: unregistered timer "
			 "index belonging to other timer");
	}

	st->numobjs--;

	m = get_node(st, st->num_timers);
	st->num_timers--;

	*p = *m;
	(*p)->index = t->index;
	*m = NULL;
	if (p != m) {
		pull_up(st, (*p)->index, p);
		push_down(st, (*p)->index, p);
	}

	t->index = -1;
}
Exemplo n.º 6
0
void iv_timer_register(struct iv_timer *_t)
{
	struct iv_state *st = iv_get_state();
	struct iv_timer_ *t = (struct iv_timer_ *)_t;
	struct iv_timer_ **p;
	int index;

	if (t->index != -1) {
		iv_fatal("iv_timer_register: called with timer still "
			 "on the heap");
	}

	st->numobjs++;

	index = ++st->num_timers;
	p = get_node(st, index);
	if (p == NULL)
		iv_fatal("iv_timer_register: timer list overflow");

	*p = t;
	t->index = index;

	pull_up(st, index, p);
}
Exemplo n.º 7
0
// Make a new pin object from a string
Pin* Pin::from_string(std::string value){
    LPC_GPIO_TypeDef* gpios[5] ={LPC_GPIO0,LPC_GPIO1,LPC_GPIO2,LPC_GPIO3,LPC_GPIO4};

    // cs is the current position in the string
    const char* cs = value.c_str();
    // cn is the position of the next char after the number we just read
    char* cn = NULL;

    // grab first integer as port. pointer to first non-digit goes in cn
    this->port_number = strtol(cs, &cn, 10);
    // if cn > cs then strtol read at least one digit
    if ((cn > cs) && (port_number <= 4)){
        // translate port index into something useful
        this->port = gpios[(unsigned int) this->port_number];
        // if the char after the first integer is a . then we should expect a pin index next
        if (*cn == '.'){
            // move pointer to first digit (hopefully) of pin index
            cs = ++cn;

            // grab pin index.
            this->pin = strtol(cs, &cn, 10);

            // if strtol read some numbers, cn will point to the first non-digit
            if ((cn > cs) && (pin < 32)){
                this->port->FIOMASK &= ~(1 << this->pin);

                // now check for modifiers:-
                // ! = invert pin
                // o = set pin to open drain
                // ^ = set pin to pull up
                // v = set pin to pull down
                // - = set pin to no pull up or down
                // @ = set pin to repeater mode
                for (;*cn;cn++) {
                    switch(*cn) {
                        case '!':
                            this->inverting = true;
                            break;
                        case 'o':
                            as_open_drain();
                            break;
                        case '^':
                            pull_up();
                            break;
                        case 'v':
                            pull_down();
                            break;
                        case '-':
                            pull_none();
                            break;
                        case '@':
                            as_repeater();
                            break;
                        default:
                            // skip any whitespace following the pin index
                            if (!is_whitespace(*cn))
                                return this;
                    }
                }
                return this;
            }
        }
    }

    // from_string failed. TODO: some sort of error
    port_number = 0;
    port = gpios[0];
    pin = 255;
    inverting = false;
    return this;
}