Пример #1
0
void Heapify(PQ h, int i) { 
  int l, r, largest; 
  l = LEFT(i); 
  r = RIGHT(i); 
  if (l < h->heapsize && greater(h->array[l], h->array[i])) 
    largest = l; 
  else  
    largest = i; 
  if (r<h->heapsize && greater(h->array[r], h->array[largest])) 
    largest = r; 
  if (largest != i) { 
    swap(h, i, largest); 
    Heapify(h, largest); 
  } 
  return; 
} 
Пример #2
0
void max_heapify(int *array, int heap_size, int i)
{
	int largest = i;
	int l = LEFT(i);
	int r = RIGHT(i);
	if( l < heap_size && array[l] > array[largest])
		largest = l;
	if( r < heap_size && array[r] > array[largest])
		largest = r;
	if(largest != i) {
		int temp = array[i];
		array[i] = array[largest];
		array[largest] = temp;
		max_heapify(array, heap_size, largest);
	}
}
Пример #3
0
/*
 * Regain the heap properties starting at position i
 * @param  heap The heap to regain heap properties
 * @param  i    The position to start heapifying
 */
static void
_ecore_sheap_heapify(Ecore_Sheap *heap, int i)
{
   int extreme;
   int left = LEFT(i);
   int right = RIGHT(i);

   if (heap->order == ECORE_SORT_MIN)
     {
	if (left <= heap->size && heap->compare(heap->data[left - 1],
						heap->data[i - 1]) < 0)
	  extreme = left;
	else
	  extreme = i;

	if (right <= heap->size && heap->compare(heap->data[right - 1],
						 heap->data[extreme - 1]) < 0)
	  extreme = right;
     }
   else
     {
	if (left <= heap->size && heap->compare(heap->data[left - 1],
						heap->data[i - 1]) > 0)
	  extreme = left;
	else
	  extreme = i;

	if (right <= heap->size && heap->compare(heap->data[right - 1],
						 heap->data[extreme - 1]) > 0)
	  extreme = right;
     }

   /*
    * If the data needs to be swapped down the heap, recurse on
    * heapifying it's new placement.
    */
   if (extreme != i)
     {
	void *temp;

	temp = heap->data[extreme - 1];
	heap->data[extreme - 1] = heap->data[i - 1];
	heap->data[i - 1] = temp;

	_ecore_sheap_heapify(heap, extreme);
     }
}
void ternary_sift_down(void *base,
                       size_t start,
                       size_t size,
                       size_t max_index,
                       compare_fun3 compare,
                       void *arg) {
  size_t root = start;
  while (TRUE) {
    size_t left_child = LEFT(root);
    if (left_child > max_index) {
      break;
    }
    void *root_ptr = POINTER(base, root, size);
    size_t swap = root;
    void *swap_ptr = root_ptr;
    void *left_child_ptr = POINTER(base, left_child, size);
    if (compare(swap_ptr, left_child_ptr, arg) < 0) {
      swap = left_child;
      swap_ptr = left_child_ptr;
    }
    size_t middle_child = MIDDLE(root);
    size_t right_child = RIGHT(root);
    if (middle_child <= max_index) {
      void *middle_child_ptr = POINTER(base, middle_child, size);
      if (compare(swap_ptr, middle_child_ptr, arg) < 0) {
        swap = middle_child;
        swap_ptr = middle_child_ptr;
      }
      if (right_child <= max_index) {
        void *right_child_ptr = POINTER(base, right_child, size);
        if (compare(swap_ptr, right_child_ptr, arg) < 0) {
          swap = right_child;
          swap_ptr = right_child_ptr;
        }
      }
    }

    if (swap != root) {
      swap_elements(root_ptr, swap_ptr, size);
      root = swap;
      root_ptr = swap_ptr;
    } else {
      return;
    }
  }
}
Пример #5
0
int bstree_delete_index_balanced( bstree_t *tree_in, long idx_in )
{
	int ret;
	long temp;
	if( bstree_delete_index( tree_in, idx_in, &temp ) < 0 )
		return -1;
	if( COLOR(temp) == BLACK )
	{
		if( LEFT(temp) != -1 )
			temp = LEFT(temp);
		else
			temp = RIGHT(temp);
		if( bstree_balance_delete( tree_in, temp ) < 0 )
			return -1;
	}
	return 0;
}
Пример #6
0
void minheapify(Heap heap, int i)
{
    int l = LEFT(i);
    int r = RIGHT(i);
    int minimal = i;
    if(l < heap.length && heap.a[l]->key < heap.a[i]->key) 
        minimal = l;
    if(r < heap.length && heap.a[r]->key < heap.a[minimal]->key)
        minimal = r;
    if(minimal != i)
    {
        HeapNode *temp = heap.a[i];
        heap.a[i] = heap.a[minimal];
        heap.a[minimal] = temp;
        minheapify(heap, minimal);
    }
}
Пример #7
0
void BeatDetector::detect(uint8_t* stream) {
    /* calculate local average energy */
    double average_energy = 0;

    for(uint32_t i = 0; i<samples; i++)
    {
        double L = (double)LEFT((int16_t *) stream, i);
        double R = (double)RIGHT((int16_t *) stream, i);
        average_energy += L*L + R*R;
    }

    /* if local average is greater than S x global average then it's a beat! */
    beat = ((average_energy * history_len) > (sensitivity * H->total)) * average_energy;

    /* update history buffer */
    H->record(average_energy);
}
Пример #8
0
/**
 * @brief Check whether 'target' has just triggered any new reaction fire
 * @param[in] target The entity triggering fire
 */
static void G_ReactionFireTargetsUpdateAll (const edict_t *target)
{
	edict_t *shooter = NULL;

	/* check all possible shooters */
	while ((shooter = G_EdictsGetNextLivingActor(shooter))) {
		/* check whether reaction fire is possible (friend/foe, LoS */
		if (G_ReactionFireIsPossible(shooter, target)) {
			const int TUs = G_ReactionFireGetTUsForItem(shooter, target, RIGHT(shooter));
			if (TUs < 0)
				continue;	/* no suitable weapon */
			G_ReactionFireTargetsAdd(shooter, target, TUs);
		} else {
			G_ReactionFireTargetsRemove(shooter, target);
		}
	}
}
Пример #9
0
void max_heapify(int A[], int i, int size)
{
    int l = LEFT(i);
    int r = RIGHT(i);
    int largest = i;
    if (l < size && A[l] > A[i])
        largest = l;

    if (r < size && A[r] > A[largest])
        largest = r;

    if (largest != i)
    {
        my_swap(&A[i], &A[largest]);
        max_heapify(A, largest, size);
    }
}
Пример #10
0
int os_key_rank(const Node *T, int key)
{
	Node *p = const_cast<Node*>(T);
	int r = 0;
	while(NIL != p) {
		if (key == KEY(p)) {
			return r + SIZE(LEFT(p)) + 1;
		} else if (key < KEY(p)){
			p = LEFT(p);
		} else {
			r += SIZE(LEFT(p)) + 1;
			p = RIGHT(p);
		}
	}

	return 0;
}
Пример #11
0
void min_heapify(MinQueue *q, int i)
{
    int l, r, smallest;
    l = LEFT(i);
    r = RIGHT(i);
    if( l <= q->size && (d[q->heap[l]] < d[q->heap[i]]))
        smallest = l;
    else
        smallest = i;
    if( r <= q->size && (d[q->heap[r]] < d[q->heap[smallest]]))
        smallest = r;
    if(smallest != i)
    {
        exchange(&(q->heap[i]), &(q->heap[smallest]));
        min_heapify(q, smallest);
    }
}
Пример #12
0
/**
 * @brief Updates the character cvars for the given character.
 *
 * The models and stats that are displayed in the menu are stored in cvars.
 * These cvars are updated here when you select another character.
 *
 * @param[in] chr Pointer to character_t (may not be null)
 * @param[in] cvarPrefix
 * @sa CL_UGVCvars
 * @sa CL_ActorSelect
 */
static void CL_ActorCvars (const character_t * chr, const char* cvarPrefix)
{
	invList_t *weapon;
	assert(chr);

	/* visible equipment */
	weapon = RIGHT(chr);
	if (weapon)
		Cvar_Set(va("%s%s", cvarPrefix, "rweapon"), weapon->item.t->model);
	else
		Cvar_Set(va("%s%s", cvarPrefix, "rweapon"), "");
	weapon = LEFT(chr);
	if (weapon)
		Cvar_Set(va("%s%s", cvarPrefix, "lweapon"), weapon->item.t->model);
	else
		Cvar_Set(va("%s%s", cvarPrefix, "lweapon"), "");
}
Пример #13
0
void min_heap(int i)
{
	int min;
	int tmp;
	int r = RIGHT(i);
	int l = LEFT(i);
	if(l <= heap[0] && d[heap[l]] < d[heap[i]]) min = l;
	else min = i;
	if(r <= heap[0] && d[heap[r]] < d[heap[min]]) min = r;
	if(min != i)
	{
		tmp = heap[i]; heap[i] = heap[min]; heap[min] = tmp;
		min_heap(min);
		pos[heap[i]] = i;
		pos[heap[min]] = min;
	}
}
Пример #14
0
/*
* Big O: O(logn)
* Helper function. Helps the remove function maintain the heap structure
* Moves the element downt hrough the heap to the proper location
*/
void siftDown(int index)
{
	int leftChildIndex = LEFT(index); //gets the index of the left child of the element you are trying to fix
	int rightChildIndex = RIGHT(index); // get the index of the right child  of the element you are tyring to fix
	int minIndex; //holds the index of the child with the lower index
	struct tree* tmp; //holds the value of an element for switching
	
	if (rightChildIndex >= heapSize) //if the right child does not exist
	{
		if (leftChildIndex >= heapSize) //if the left child does not exist
			return; //returns nothing. Base case. Node has no children
		else
			minIndex = leftChildIndex; //the min value is leftChild because right child does not exist
	}
	
	//Favors shifting left because if the leftChild is less than or equal to rightChild, it will return leftChild index
	//Will favor swapping left
	else//Both children exist. FAVORS SHIFTING LEFT
	{
		if (getData(heap[leftChildIndex]) <= getData(heap[rightChildIndex])) //If the leftChild is less than or equal to rightChild
			minIndex = leftChildIndex;//the min value is leftChild
		else//if the rightChild is less than the leftChild
			minIndex = rightChildIndex;//the min value is rightChild
	}
	/*
	//FAVORS SHIFTING RIGHT
	//Favors shifting right because if the rightChild is less than or equal to leftChild, it will return rightChild index.
	//The node will then swap with the rightChild index
	else //Both children exist. FAVORS SHIFTING RIGHT
	{
		if (getData(heap[rightChildIndex]) <= getData(heap[leftChildIndex])) //If the rightChild is less than or equal to leftChild
			minIndex = rightChildIndex;//the min value is rightChild
		else//if the leftChild is less than the rightChild
			minIndex = leftChildIndex;//the min value is leftChild
	}
	*/
	if (getData(heap[index]) > getData(heap[minIndex])) //if the data at index is greater than the the minValue of the children
	{
		//switches the parent with the min value
		tmp = heap[minIndex];//holds the minvalue
		heap[minIndex] = heap[index];//parent to minvalue
		heap[index] = tmp;//minvalue to parent
		siftDown(minIndex);//check to make sure the parent is the proper position
	}
}
Пример #15
0
void maxHeapify(Array &a, int index)
{
    int l = LEFT(index);
    int r = RIGHT(index);
    int largest = 0;
    if (l <= a.length && a[l] > a[index]) {
       largest = l; 
    }else{
        largest = index;
    }
    if (r <= a.length && a[r] > a[largest]) {
       largest = r; 
    }
    if (largest != index) {
       exchange(a[index], a[largest]); 
       maxHeapify(a, largest);
    }
}
Пример #16
0
/*
   PROGRAM:  Philosopher
   A program that simulates a philosopher participating in a dining philosophers'
   symposium. Each philosopher will join the symposium, alternating between
   thinking, going hungry and eating a bite, for a number of bites. After he
   has eaten the last bite, he leaves.
*/
int Philosopher(int argl, void* args)
{
    int i,j;
    int bites;			/* Number of bites (mpoykies) */

    assert(argl == sizeof(int[2]));
    i = ((int*)args)[0];
    bites = ((int*)args)[1];

    Mutex_Lock(&mx);		/* Philosopher arrives in thinking state */
    state[i] = THINKING;
    print_state("     %d has arrived\n",i);
    Mutex_Unlock(&mx);

    for(j=0; j<bites; j++) {

        think(i);			/* THINK */

        Mutex_Lock(&mx);		/* GO HUNGRY */
        state[i] = HUNGRY;
        trytoeat(i);		/* This may not succeed */
        while(state[i]==HUNGRY) {
            print_state("     %d waits hungry\n",i);
            Cond_Wait(&mx, &(hungry[i])); /* If hungry we sleep. trytoeat(i) will wake us. */
        }
        assert(state[i]==EATING);
        Mutex_Unlock(&mx);

        eat(i);			/* EAT */

        Mutex_Lock(&mx);
        state[i] = THINKING;	/* We are done eating, think again */
        print_state("     %d is thinking\n",i);
        trytoeat(LEFT(i));		/* Check if our left and right can eat NOW. */
        trytoeat(RIGHT(i));
        Mutex_Unlock(&mx);
    }
    Mutex_Lock(&mx);
    state[i] = NOTHERE;		/* We are done (eaten all the bites) */
    print_state("     %d is leaving\n",i);
    Mutex_Unlock(&mx);

    return i;
}
Пример #17
0
void MaxHeapify(int *array, int i, int length) {
  int l = LEFT(i);
  int r = RIGHT(i);
  int largest = i, temp;

  if(l<length && array[l]>array[largest])
    largest = l;

  if(r<length && array[r]>array[largest])
    largest = r;

  if(largest != i) {
    temp = array[i];
    array[i]=array[largest];
    array[largest] = temp;
    MaxHeapify(array, largest, length);
  }
  return;
}
void MAX_HEAPIFY (heap *A, int i)
{
    int l = LEFT(i);
    int r = RIGHT(i);
    int largest;
    if ((l< A->heap_size) && (A->data[l]>A->data[i]))
        largest = l;
    else
        largest = i;
    if ((r< A->heap_size) && (A->data[r]>A->data[largest]))
        largest = r;
    if (largest != i)
    {
        int tmp = A->data[i];
        A->data[i] = A->data[largest];
        A->data[largest] = tmp;
        MAX_HEAPIFY(A, largest);
    }
}
Пример #19
0
/**
 * Calculate the combined advisory minimum size of all components
 * 
 * @return  Advisory minimum size for the container
 */
static size2_t minimum_size(__this__)
{ 
  itk_component** children = CONTAINER(this)->children;
  long i, n = CONTAINER(this)->children_count;
  size2_t rc;
  rc.width = rc.height = 0;
  rc.defined = true;
  for (i = 0; i < n; i++)
    if ((*(children + i))->visible)
      {
	if (rc.width < (*(children + i))->minimum_size.width)
	  rc.width = (*(children + i))->minimum_size.width;
	if (rc.height < (*(children + i))->minimum_size.height)
	  rc.height = (*(children + i))->minimum_size.height;
      }
  rc.width += LEFT(this) + RIGHT(this);
  rc.height += TOP(this) + BOTTOM(this);
  return rc;
}
Пример #20
0
/**
 * @brief Check all entities to see whether target has caused reaction fire to resolve.
 * @param[in] target The entity that might be resolving reaction fire
 * @returns whether any entity fired (or would fire) upon target
 * @sa G_ReactionFireOnMovement
 * @sa G_ReactionFirePostShot
 */
static bool G_ReactionFireCheckExecution (const edict_t *target)
{
	edict_t *shooter = NULL;
	bool fired = false;

	/* check all possible shooters */
	while ((shooter = G_EdictsGetNextLivingActor(shooter))) {
		const int tus = G_ReactionFireGetTUsForItem(shooter, target, RIGHT(shooter));
		if (tus > 1) {	/* indicates an RF weapon is there */
			if (G_ReactionFireTargetsExpired(shooter, target, 0)) {
				if (G_ReactionFireTryToShoot(shooter, target)) {
					G_ReactionFireTargetsAdvance(shooter, target, tus);
					fired |= true;
				}
			}
		}
	}
	return fired;
}
Пример #21
0
void max_heapify(int *array,int i,int heap_size)
{
    int l = LEFT(i);
    int r = RIGHT(i);
    int largest = i;

    if(l <= heap_size && array[l] > array[i])
        largest = l;
    else if (l <= heap_size && array[r] > array[i])
        largest = r;
    if(largest != i)
    {
        int tmp;
        tmp = array[largest];
        array[largest] = array[i];
        array[i] = tmp;
        max_heapify(array,largest,heap_size);
    }
}
Пример #22
0
void HEAPIFY(int heap[100], int i, int heap_size)
{
int l,r,largest;
l = LEFT(i);
r = RIGHT(i);

if(l <= heap_size && heap[l] > heap[i])
	largest = l;
else
	largest = i;
if(r <= heap_size && heap[r] > heap[largest])
	largest = r;
if(largest != i)
	{
	swap(&heap[i],&heap[largest]);
	HEAPIFY(heap,largest,heap_size);
	}

}
Пример #23
0
/**
 * Constructor
 * 
 * @param  container  The container which uses the layout manager
 * @param  left       The size of the left margin
 * @param  top        The size of the top margin
 * @param  right      The size of the right margin
 * @param  bottom     The size of the bottom margin
 */
itk_layout_manager* itk_new_margin_layout(itk_component* container, dimension_t left, dimension_t top, dimension_t right, dimension_t bottom)
{
  itk_layout_manager* rc = malloc(sizeof(itk_layout_manager));
  dimension_t* margins = malloc(4 * sizeof(dimension_t));
  rc->data = malloc(2 * sizeof(void*));
  rc->prepare = prepare;
  rc->done = done;
  rc->locate = locate;
  rc->minimum_size = minimum_size;
  rc->preferred_size = preferred_size;
  rc->maximum_size = maximum_size;
  rc->free = free_margin_layout;
  CONTAINER_(rc) = container;
  MARGINS_(rc) = margins;
  LEFT(rc) = left;
  TOP(rc) = top;
  RIGHT(rc) = right;
  BOTTOM(rc) = bottom;
  return rc;
}
// 维护一个最大堆
void
Max_Heapify(int i) {

    int largest, l, r;
    while (1) {
        l = LEFT(i+1);
        r = RIGHT(i+1);
        if (l < heap_size && A[l] > A[i])
            largest = l;
        else
            largest = i;
        if (r < heap_size && A[r] > A[largest])
            largest = r;
        if (largest != i) {
            A[largest]^=A[i]^=A[largest]^=A[i];
            i = largest;
        }
        else break;
    }
}
Пример #25
0
void minheap_heapify(int *v, int p, int len)
{
    int l = LEFT(p);
    int r = RIGHT(p);
    int *min_child;

    if (l > len) return;

    if (r > len) {
        min_child = &v[l];
    } else {
        min_child = v[l] < v[r] ? &v[l] : &v[r];
    }

    if (v[p] > *min_child) {
        swap(&v[p], min_child);
        if (min_child == &v[l]) minheap_heapify(v, l, len);
        else minheap_heapify(v, r, len);
    }
}
Пример #26
0
void max_heapify(int *array,int i)	/* O(lgn),Obtain a max-heap */
{
    int largest;
    int l = LEFT(i);
    int r = RIGHT(i);

    if(l <= heap_len && array[l-1] > array[i-1])
        largest = l;
    else
        largest = i;

    if(r <= heap_len && array[r-1] > array[largest-1])
        largest = r;

    if(largest != i)
    {
        swap(&array[i-1],&array[largest-1]);
        max_heapify(array,largest);
    }
}
Пример #27
0
//This method tests whether a philosopher is able to eat. 
void test(int id) {
    //printf("philosopher %d being tested\n", id);
    if (phils[id].pState == HUNGRY && phils[LEFT(id)].pState != EATING && phils[RIGHT(id)].pState != EATING) {
         phils[id].pState = EATING;
         sem_post(&phil_sem[id]);
    }
    int i;
    for (i=0; i < TOTAL_PHILOSOPHERS; i++) {
         printf("Philosopher[%d] is %s, ", i, (phils[i].pState==THINKING)?"THINKING" : (phils[i].pState==EATING)?"EATING":"HUNGRY" );
         if (phils[i].pState == EATING) {
             phils[i].eatCount = phils[i].eatCount + 1;
         } else {
             phils[i].otherCount = phils[i].otherCount + 1;
         }
    }
    for (i=0; i < TOTAL_PHILOSOPHERS; i++) {
        assert(phils[i].pState != EATING || phils[LEFT(i)].pState != EATING);
    }
    printf("\n");
 }
Пример #28
0
void CKEventThread::Heapify( UINT32 now, UINT n )
{

	UINT lnode = LEFT(n);
	UINT rnode = RIGHT(n);
	UINT low = n;

	UINT32 dn, dl, dr;

	dn = m_ppTimers[n]->GetTimeout() - now;
	if( dn > 0x7FFFFFFF ) dn = 0;

	if( lnode <= m_nTimers )
	{
		dl = m_ppTimers[lnode]->GetTimeout() - now;
		if( dl > 0x7FFFFFFF ) dl = 0;
		if( dl < dn )
		{
			low = lnode;
			dn = dl;
		}
	}

	if( rnode <= m_nTimers )
	{
		dr = m_ppTimers[rnode]->GetTimeout() - now;
		if( dr > 0x7FFFFFFF ) dr = 0;
		if( dr < dn )
		{
			low = rnode;
			dn = dr;
		}
	}

	if( low != n )
	{
		CKTimer* tmp;
		SWAP( tmp, m_ppTimers[n], m_ppTimers[low] );
		Heapify( now, low );
	}
}
Пример #29
0
static void
heap_rdump(heap_t h, size_t node, int depth, heap_dump_fn df)
{
	if (node >= h->treesz || node >= h->next || !h->tree[node])
		return;

	heap_rdump(h, LEFT(node), depth+1, df);
	for (int i = 0; i < depth; i++)
		fputs("  ", stderr);
	fputs("``", stderr);
	df(h->tree[node]);
	fprintf(stderr, "'' [%zu; %12p (p:%12p: ``",
	    node, h->tree[node], h->tree[PARENT(node)]);
	if (node)
		df(h->tree[PARENT(node)]);
	else
		fprintf(stderr, "(root)");
	fprintf(stderr, "'')]");
	fputs("\n", stderr);
	heap_rdump(h, RIGHT(node), depth+1, df);
}
Пример #30
0
static void _heapify_(Vertex verts[], uint32_t len, uint32_t i)
{
    assert(/*len > 0 && */len < MAX_HEAP_SIZE && i > 0);

    if (len == 0) return;
    int32_t l = LEFT(i), r = RIGHT(i);
    uint32_t minimum = i;

    if (l <= len && verts[l].dist < verts[i].dist)
        minimum = l;
    if (r <= len && verts[r].dist < verts[minimum].dist)
        minimum = r;

    if (minimum != i) {
        Vertex temp = verts[i];
        verts[i] = verts[minimum];
        verts[minimum] = temp;

        _heapify_(verts, len, minimum);
    }
}