Esempio n. 1
0
node_t*
bt_maximum(node_t *root)
{
	if(root == NULL)
		return NULL;

	node_t *aux = root;
	while(RIGHT_CHILD(aux) != NULL)
		aux = RIGHT_CHILD(aux);
	return aux;
}
Esempio n. 2
0
void downward(heap_t *h)
{
    int i = 0;
    int t;
    while (LEFT_CHILD(i) < h->curr_max) {
        if (RIGHT_CHILD(i) < h->curr_max && h->head[RIGHT_CHILD(i)] < h->head[LEFT_CHILD(i)]) {
            t = RIGHT_CHILD(i);
        } else {
            t = LEFT_CHILD(i);
        }
        if (h->head[i] < h->head[t]) break;
        swap(h->head, i, t);
        i = t;
    }
}
Esempio n. 3
0
/* rec way */
void min_heapify(HeapT* h, size_t index) {
    assert(h != NULL);

    HeapImplT* heap = (HeapImplT*) h;
    size_t left_index = LEFT_CHILD(index);
    size_t right_index = RIGHT_CHILD(index);
    size_t lowest_index = index;

    if (heap->size > left_index &&
        heap->data[left_index] < heap->data[lowest_index]
    ) {
        lowest_index = left_index;
    }

    if (heap->size > right_index &&
        heap->data[right_index] < heap->data[lowest_index]
    ) {
        lowest_index = right_index;
    }

    if (lowest_index != index) {
        swap(&heap->data[lowest_index], &heap->data[index]);
        min_heapify(h, lowest_index);
    }
}
Esempio n. 4
0
NODEPTR_TYPE *burm_kids(NODEPTR_TYPE p, int eruleno, NODEPTR_TYPE kids[]) {
	burm_assert(p, PANIC("NULL tree in burm_kids\n"));
	burm_assert(kids, PANIC("NULL kids in burm_kids\n"));
	switch (eruleno) {
	case 10: /* disp: ADDI(reg,con) */
	case 6: /* reg: ADDI(reg,rc) */
	case 4: /* stmt: ASGNI(disp,reg) */
		kids[0] = LEFT_CHILD(p);
		kids[1] = RIGHT_CHILD(p);
		break;
	case 13: /* rc: reg */
	case 12: /* rc: con */
	case 9: /* reg: disp */
	case 5: /* stmt: reg */
		kids[0] = p;
		break;
	case 7: /* reg: CVCI(INDIRC(disp)) */
		kids[0] = LEFT_CHILD(LEFT_CHILD(p));
		break;
	case 15: /* con: I0I */
	case 14: /* con: CNSTI */
	case 11: /* disp: ADDRLP */
	case 8: /* reg: I0I */
		break;
	default:
		burm_assert(0, PANIC("Bad external rule number %d in burm_kids\n", eruleno));
	}
	return kids;
}
Esempio n. 5
0
void max_heapify(HeapT* h, size_t index) {
    assert(h != NULL);

    HeapImplT* heap = (HeapImplT*) h;
    size_t largest_index, left_index, right_index;

    /* loop way */
    while (1) {
        left_index = LEFT_CHILD(index);
        right_index = RIGHT_CHILD(index);
        largest_index = index;

        if (heap->size > left_index &&
            heap->data[left_index] > heap->data[largest_index]
        ) {
            largest_index = left_index;
        }

        if (heap->size > right_index &&
            heap->data[right_index] > heap->data[largest_index]
        ) {
            largest_index = right_index;
        }

        if (largest_index == index) {
            break;
        }

        swap(&heap->data[largest_index], &heap->data[index]);
        index = largest_index;
    }
}
Esempio n. 6
0
int
bt_size(node_t* root)
{
	if(root == NULL)
		return 0;
	else
		return ( bt_size(LEFT_CHILD(root)) + 1 + bt_size(RIGHT_CHILD(root)) );
}
Esempio n. 7
0
/* =============================================================================
 * TMheapify
 * =============================================================================
 */
TM_SAFE
void
TMheapify (  heap_t* heapPtr, long startIndex)
{
    void** elements = (void**)TM_SHARED_READ_P(heapPtr->elements);
    //long (*compare)(const void*, const void*) TM_IFUNC_DECL = heapPtr->compare;
    long (*compare)(const void*, const void*) TM_SAFE = heapPtr->compare;

    long size = (long)TM_SHARED_READ(heapPtr->size);
    long index = startIndex;

    while (1) {
        long leftIndex = LEFT_CHILD(index);
        long rightIndex = RIGHT_CHILD(index);
        long maxIndex = -1;

        if (leftIndex <= size)
        {
            long ret;
            void *e1, *e2;

            e1 = (void*)TM_SHARED_READ_P(elements[leftIndex]);
            e2 = (void*)TM_SHARED_READ_P(elements[index]);
            TM_IFUNC_CALL2(ret, compare, e1, e2);

            if (ret > 0)
                maxIndex = leftIndex;
            else
                maxIndex = index;
        } else {
            maxIndex = index;
        }

        if (rightIndex <= size)
        {
            long ret;
            void *e1, *e2;

            e1 = (void*)TM_SHARED_READ_P(elements[rightIndex]);
            e2 = (void*)TM_SHARED_READ_P(elements[maxIndex]);
            TM_IFUNC_CALL2(ret, compare, e1, e2);

            if (ret > 0)
                maxIndex = rightIndex;
        }

        if (maxIndex == index) {
            break;
        } else {
            void* tmpPtr = (void*)TM_SHARED_READ_P(elements[index]);
            TM_SHARED_WRITE_P(elements[index],
                              (void*)TM_SHARED_READ_P(elements[maxIndex]));
            TM_SHARED_WRITE_P(elements[maxIndex], tmpPtr);
            index = maxIndex;
        }
    }
}
Esempio n. 8
0
code_ptr *create_code(int type, code_ptr* left_child, code_ptr* right_child) {
	code_ptr *c = (code_ptr *)malloc(sizeof(code_ptr));

	OP_LABEL(c) = type;
	LEFT_CHILD(c) = left_child;
	RIGHT_CHILD(c) = right_child;

	return c;
}
Esempio n. 9
0
void
bt_dispose(node_t *root, void (*free_data)(void*))
{
	if(root == NULL)
		return;
	bt_dispose(LEFT_CHILD(root), free_data);
	bt_dispose(RIGHT_CHILD(root), free_data);
	free_node(root, free_data);
}
Esempio n. 10
0
NODEPTR_TYPE burm_child(NODEPTR_TYPE p, int index) {
	burm_assert(p, PANIC("NULL tree in burm_child\n"));
	switch (index) {
	case 0:	return LEFT_CHILD(p);
	case 1:	return RIGHT_CHILD(p);
	}
	burm_assert(0, PANIC("Bad index %d in burm_child\n", index));
	return 0;
}
Esempio n. 11
0
void
bt_postorden_walk(node_t *root, void(*function)(void*))
{
	if(root == NULL)
		return;
	bt_postorden_walk(LEFT_CHILD(root), function);
	bt_postorden_walk(RIGHT_CHILD(root), function);
	function(DATA(root));
}
Esempio n. 12
0
struct code* create_code (int type, struct code *left_child, struct code *right_child)
{
	struct code *c = malloc (sizeof (struct code));

	OP_LABEL (c)    = type;
	LEFT_CHILD (c)  = left_child;
	RIGHT_CHILD (c) = right_child;

	return c;
}
Esempio n. 13
0
node_t*
bt_recursive_search(node_t* root, void *data, int (*cmp_function)(void*,void*))
{
	if(root == NULL  || (cmp_function(data, DATA(root)) ) == 0)
		return root;

	if( (cmp_function(data, DATA(root)) ) < 0 )
		return bt_recursive_search(LEFT_CHILD(root), data, cmp_function );
	else
		return bt_recursive_search(RIGHT_CHILD(root), data, cmp_function );
}
Esempio n. 14
0
static void burm_label1(NODEPTR_TYPE p) {
	burm_assert(p, PANIC("NULL tree in burm_label\n"));
	switch (burm_arity[OP_LABEL(p)]) {
	case 0:
		STATE_LABEL(p) = burm_state(OP_LABEL(p), 0, 0);
		break;
	case 1:
		burm_label1(LEFT_CHILD(p));
		STATE_LABEL(p) = burm_state(OP_LABEL(p),
			STATE_LABEL(LEFT_CHILD(p)), 0);
		break;
	case 2:
		burm_label1(LEFT_CHILD(p));
		burm_label1(RIGHT_CHILD(p));
		STATE_LABEL(p) = burm_state(OP_LABEL(p),
			STATE_LABEL(LEFT_CHILD(p)),
			STATE_LABEL(RIGHT_CHILD(p)));
		break;
	}
}
Esempio n. 15
0
node_t*
bt_iterative_search(node_t* root, void* data, int (*cmp_function)(void*,void*))
{
	node_t *aux = root;
	while( aux != NULL && (cmp_function(data, DATA(aux)) ) != 0){
		if( (cmp_function(data, DATA(aux))) < 0 )
			aux = LEFT_CHILD(aux);
		else
			aux = RIGHT_CHILD(aux);
	}
	return aux;	
}
Esempio n. 16
0
File: eheap.c Progetto: MicBosi/GTS
static void sift_down (GtsEHeap * heap, guint i)
{
  GtsEHeapPair * left_child, * right_child, * child, * parent;
  guint lc, rc, c;
  gpointer * pdata = heap->elts->pdata;
  guint len = heap->elts->len;
  gdouble key;

  lc = LEFT_CHILD (i);
  rc = RIGHT_CHILD (i);
  left_child = lc <= len ? pdata[lc - 1] : NULL;
  right_child = rc <= len ? pdata[rc - 1] : NULL;

  parent = pdata[i - 1];
  key = parent->key;
  while (left_child != NULL) {
    if (right_child == NULL || left_child->key  < right_child->key) {
      child = left_child;
      c = lc;
    }
    else {
      child = right_child;
      c = rc;
    }
    if (key > child->key) {
      pdata[i - 1] = child;
      child->pos = i;
      pdata[c - 1] = parent;
      parent->pos = c;
      i = c;
      lc = LEFT_CHILD (i);
      rc = RIGHT_CHILD (i);
      left_child = lc <= len ? pdata[lc - 1] : NULL;
      right_child = rc <= len ? pdata[rc - 1] : NULL;      
    }
    else
      left_child = NULL;
  }
}
Esempio n. 17
0
//----------------------------------------------------------------------------------------------------//
//  @func - prio_pdeq
//! @desc
//!   Dequeue the highest priority process from the Queue.
//! @param
//!   - queue is the queue of items.
//!   - item is buffer where queue element is returned.
//!     If queue is empty then 255 is returned.
//!   - Key for removing from the Queue (priority)
//! @return
//!   - Queue element is returned in item.
//!   - 255 (-1) is assigned to item if Error.
//! @note
//!   - Uses queue array structure as a binary heap for implementing priority queue.
//!     0 is highest priority
//!   - The priority key is not used currently. Instead the value is obtained from the ptable directly.
//----------------------------------------------------------------------------------------------------//
void prio_pdeq (queuep queue, pid_t *item, unsigned short key)
{
    pid_t *citems = (pid_t*) (queue->items);
    unsigned char tmp, cur;
    pid_t slct, ret;

    // Queue EMPTY
    if (queue->item_count == 0) {
        *item = 255;
        return;
    }

    ret = citems[0];
    // Remove head (highest prio)
    citems[0] = citems[queue->qend - 1];                                // Reshape heap (Take lowest prio. Place it at head. Propagate it down the heap)

    cur = 0;
    while (LEFT_CHILD (cur) <= (queue->qend - 1)) {                     // While cur has children in the heap
        if ((RIGHT_CHILD (cur) > (queue->qend - 1)) ||
                (PROC_PRIO (citems[LEFT_CHILD (cur)]) <= PROC_PRIO (citems[RIGHT_CHILD (cur)])))
            slct = LEFT_CHILD (cur);
        else
            slct = RIGHT_CHILD (cur);

        if (PROC_PRIO (citems[slct]) <= PROC_PRIO (citems[cur])) {
            tmp = citems[cur];                                          // Swap
            citems[cur] = citems[slct];
            citems[slct] = tmp;
        }
        else
            break;
        cur = slct;
    }

    queue->qend--;
    queue->item_count--;
    *item = ret;
}
Esempio n. 18
0
node_t*
bt_root_delete(node_t **root, void *data,
        int (*cmp_function)(void*, void*), void (*free_data)(void*))
{
	int x = 0;
	node_t *tmp = *root;
	if ( LEFT_CHILD(*root) == NULL ){
		root = RIGHT_CHILD(*root);
		//SET_RIGTH_CHILD(*root,
		free_node(tmp, free_data);	
	}
	else if( RIGHT_CHILD(*root) == NULL){
		root = RIGHT_CHILD(*root);
		free_node(tmp, free_data);
	}
	else{
		tmp = LEFT_CHILD(*root);
		while(RIGHT_CHILD(tmp) != NULL)
			tmp = RIGHT_CHILD

	}
		
}
Esempio n. 19
0
File: heap.c Progetto: ClavinSBU/gts
static void sift_down (GtsHeap * heap, guint i)
{
  gpointer left_child, right_child, child, parent;
  guint lc, rc, c;
  gpointer * pdata = heap->elts->pdata;
  guint len = heap->elts->len;
  GCompareFunc func = heap->func;

  lc = LEFT_CHILD (i);
  rc = RIGHT_CHILD (i);
  left_child = lc <= len ? pdata[lc - 1] : NULL;
  right_child = rc <= len ? pdata[rc - 1] : NULL;

  parent = pdata[i - 1];
  while (left_child != NULL) {
    if (right_child == NULL ||
	(*func) (left_child, right_child) < 0) {
      child = left_child;
      c = lc;
    }
    else {
      child = right_child;
      c = rc;
    }
    if ((*func) (parent, child) > 0) {
      pdata[i - 1] = child;
      pdata[c - 1] = parent;
      i = c;
      lc = LEFT_CHILD (i);
      rc = RIGHT_CHILD (i);
      left_child = lc <= len ? pdata[lc - 1] : NULL;
      right_child = rc <= len ? pdata[rc - 1] : NULL;      
    }
    else
      left_child = NULL;
  }
}
Esempio n. 20
0
void
bt_insert_recursive(node_t **root, node_t *new_node, int (*cmp_function)(void*,void*))
{
	if(*root == NULL){
		*root = new_node;
#ifdef DEBUG
		fprintf(stdout,"Data structure[%p] insert node[%p]\n", *root, new_node);
#endif
	}
	else if( (cmp_function(DATA(new_node), DATA(*root)) ) < 0 )
		bt_insert_recursive(&(LEFT_CHILD(*root)), new_node, cmp_function);
	else if( (cmp_function(DATA(new_node), DATA(*root)) ) > 0 )
		bt_insert_recursive(&(RIGHT_CHILD(*root)), new_node, cmp_function);
	else
		return;
}
Esempio n. 21
0
/* =============================================================================
 * TMheapify
 * =============================================================================
 */
static void
TMheapify (TM_ARGDECL  heap_t* heapPtr, long startIndex)
{
    void** elements = (void**)TM_SHARED_READ_P(heapPtr->elements);
    long (*compare)(TM_ARGDECL const void*, const void*) = heapPtr->compare->compare_tm;

    long size = (long)TM_SHARED_READ_L(heapPtr->size);
    long index = startIndex;

    while (1) {

        long leftIndex = LEFT_CHILD(index);
        long rightIndex = RIGHT_CHILD(index);
        long maxIndex = -1;

        if ((leftIndex <= size) &&
            (compare(TM_ARG
         (void*)TM_SHARED_READ_P(elements[leftIndex]),
                     (void*)TM_SHARED_READ_P(elements[index])) > 0))
        {
            maxIndex = leftIndex;
        } else {
      maxIndex = index;
        }

        if ((rightIndex <= size) &&
            (compare(TM_ARG
         (void*)TM_SHARED_READ_P(elements[rightIndex]),
                     (void*)TM_SHARED_READ_P(elements[maxIndex])) > 0))
        {
            maxIndex = rightIndex;
        }

        if (maxIndex == index) {
            break;
        } else {
            void* tmpPtr = (void*)TM_SHARED_READ_P(elements[index]);
            TM_SHARED_WRITE_P(elements[index],
                              (void*)TM_SHARED_READ_P(elements[maxIndex]));
            TM_SHARED_WRITE_P(elements[maxIndex], tmpPtr);
            index = maxIndex;
        }
    }
}
Esempio n. 22
0
void heapify(int32_t *begin,int32_t *end,int32_t *current)
{
	int32_t *lchild = LEFT_CHILD(begin,current);
	int32_t *rchild = RIGHT_CHILD(begin,current);
	int32_t *ptr = current;
	if(lchild < end && *ptr < *lchild)
	{
		ptr = lchild;
	}
	if(rchild < end && *ptr < *rchild)
	{
		ptr = rchild;
	}
	if(ptr != current)
	{
		swap(ptr,current);
		heapify(begin,end,ptr);
	}
}
Esempio n. 23
0
// XXX: BUG, infinite loop
node_t*
bt_delete_node(node_t **root, void *data,
		int (*cmp_function)(void*, void*), void (*free_data)(void*))
{
	if(*root == NULL)
		return NULL;

	node_t* cursor;
	int x = 0;

	x = cmp_function(data, DATA(*root));
	if( x < 0)
		return bt_delete_node( &LEFT_CHILD(*root), cmp_function, free_data);
	if( x > 0)
		return bt_delete_node( &RIGHT_CHILD(*root), cmp_function, free_data);
		
	else{


	}
	
}
Esempio n. 24
0
void heap_pop(struct heap *h) {
    if (h->cur_size > 0) {
        char *tmp = h->data[0];
        h->data[0] = h->data[h->cur_size - 1];
        h->data[h->cur_size-1] = tmp;
        --h->cur_size;
    }
    // fix heap invariant
    if (h->cur_size > 0) {
        int id = 0;
        int lchild, rchild, min_ind;
        char *tmp;
        while (true) {
            lchild = LEFT_CHILD(id);
            rchild = RIGHT_CHILD(id);
            if (rchild >= h->cur_size) {
                if (lchild >= h->cur_size)
                    break;
                else
                    min_ind = lchild;
            } else {
                if (-strcmp(h->data[lchild], h->data[rchild]) <= 0)
                    min_ind = lchild;
                else
                    min_ind = rchild;
            }

            if (-strcmp(h->data[id], h->data[min_ind]) > 0) {
                tmp = h->data[min_ind];
                h->data[min_ind] = h->data[id];
                h->data[id] = tmp;
                id = min_ind;
            } else {
                break;
            }

        }
    }
}
Esempio n. 25
0
static void
heapify (heap_t* heapPtr, long startIndex)
{
    void** elements = heapPtr->elements;
    long (*compare)(const void*, const void*) = heapPtr->compare;

    long size = heapPtr->size;
    long index = startIndex;

    while (1) {

        long leftIndex = LEFT_CHILD(index);
        long rightIndex = RIGHT_CHILD(index);
        long maxIndex = -1;

        if ((leftIndex <= size) &&
            (compare(elements[leftIndex], elements[index]) > 0))
        {
            maxIndex = leftIndex;
        } else {
            maxIndex = index;
        }

        if ((rightIndex <= size) &&
            (compare(elements[rightIndex], elements[maxIndex]) > 0))
        {
            maxIndex = rightIndex;
        }

        if (maxIndex == index) {
            break;
        } else {
            void* tmpPtr = elements[index];
            elements[index] = elements[maxIndex];
            elements[maxIndex] = tmpPtr;
            index = maxIndex;
        }
    }
}
Esempio n. 26
0
void code_print (struct code *c)
{
	printf ("# - CODE INFO\n");
	printf ("#  op: %i\n#  val: %li\n#  name: %s\n#  reg: %s\n#  LC: %p\n#  RC: %p\n", c->op, c->val, c->name, c->reg, LEFT_CHILD(c), RIGHT_CHILD(c));
}
Esempio n. 27
0
/*
 * Given a heap and an index, sift_down checks to see if the value
 * at that index needs to be "sifted downward" in the heap, to
 * preserve the heap properties.  Specifically, a value needs to
 * be moved down in the heap if it is greater than either of its
 * children's values.  (This is the "order" property.)  In order
 * to preserve the "shape" property of heaps, the value is swapped
 * with the *smaller* of its two child values.
 *
 * If a value only has a left child, then only the left child is
 * examined for the swap.
 *
 * If a value has both left and right children, it is possible
 * that one child may be larger than the value, while the other is
 * smaller than the value.  Since we swap with the smallest child
 * value, we preserve the heap properties even in that situation.
 */
void sift_down(float_heap *pHeap, int index)
{
  assert(pHeap != NULL);
  assert(index < pHeap->num_values);

  int left_child = LEFT_CHILD(index);
  int right_child = RIGHT_CHILD(index);

  if (left_child >= pHeap->num_values)
  {
    /* If the left child's index is past the end of the heap
     * then this value has no children.  We're done.
     */
    return;
  }

  if (right_child >= pHeap->num_values)
  {
    /* Only have a left child. */

    if (pHeap->values[left_child] < pHeap->values[index])
    {
      /* Left child value is smaller.  Swap this value and the
       * left child value.
       */
      swap_values(pHeap, index, left_child);

      /* Don't need to call sift_down again, because if this
       * node only has a left child, we are at the bottom of
       * the heap.
       */
    }
  }
  else
  {
    /* This value has a left and right child. */

    float left_val = pHeap->values[left_child];
    float right_val = pHeap->values[right_child];
    int swap_child;

    if (left_val < pHeap->values[index] ||
        right_val < pHeap->values[index])
    {
      /* Need to swap this node with one of its children.  Pick
       * the smaller of the two children, since this is a min-heap
       * and that will preserve the heap properties.
       */
      if (left_val < right_val)
        swap_child = left_child;
      else
        swap_child = right_child;

      /* Do the swap, then call sift_down again, in case we aren't
       * at the bottom of the heap yet.
       */
      swap_values(pHeap, index, swap_child);
      sift_down(pHeap, swap_child);
    }
  }
}