예제 #1
0
void
EdgeHeap::
del_(int pos)
{
  if(print) cerr << "del_ " << pos << endl;
  assert(unusedPos_);
  if(pos == (unusedPos_ - 1) )
    {
      unusedPos_--;
      array[unusedPos_] = NULL;
      return;
    }
  /* move the final edge in heap to empty position */
  array[pos] = array[unusedPos_ - 1];
  if(!array[pos])
    {
      error("Never get here");
      return;
    }
  array[pos]->heapPos() = pos;
  array[unusedPos_ -1] = NULL;
  unusedPos_--;
  if(upheap(pos)) return;
  downHeap(pos);
}
예제 #2
0
bool
ValHeap::
upheap(int pos)
{
  if(print) cerr << "in Upheap " << pos << " " << array.size() << endl;
  if(pos == 0) return false;
  Val* atp = array[pos];
  assert(atp);
  double merit = atp->fom();
  int   parPos = parent(pos);
  Val* par = array[parPos];
  double pmerit = par->fom();
  if(print) cerr << "merits " << merit << " " << pmerit<< endl;
  if(merit > pmerit)
    {
      array[parPos] = atp;
      array[pos] = par;
      if(print) cerr << "Put " << pos << " in " << parPos << endl;
      upheap(parPos);
      return true;
    }
  else if(print)
    {
      cerr << "upheap of " << merit << "stopped by "
	<< parPos << " " << pmerit << endl;
    }
  return false;
}
예제 #3
0
void
AnsTreeHeap::
insert(AnsTreePair* atp)
{
  if(!(unusedPos_ < AHeapSize))
    {
      cerr << "UP = " << unusedPos_ << endl;
      assert(unusedPos_ < AHeapSize);
    }
  if(print)
    cerr << "heap insertion of atp at " << unusedPos_ << endl;
  array[unusedPos_] = atp;
  upheap(unusedPos_);
  if(unusedPos_ == nth)
    {
      assert(front());
      //cerr << "Bef pop " << front()->first << endl;
      unusedPos_++;
      assert(array[1]);
      //cerr << "BP2 " << array[1]->first << endl;
      AnsTreePair* atp2 = pop();
      delete atp2;
      assert(front());
    }
  else unusedPos_++;
}
예제 #4
0
/* move an element suitably so it is in a correct place */
static inline void adjustheap (ANHE *heap, int N, int k)
{
  if (k > HEAP0 && heap[k].at <= heap[HPARENT(k)].at )
    upheap (heap, k);
  else
    downheap (heap, N, k);
}
예제 #5
0
// insert item into list.
PriorityItem* PriorityQueue::insert(unsigned long priority, void* data) {
    // pick location to add at.
    PriorityItem* location = head == 0x0 ? head : findinsertionposition(head);

    // create item to base off of.
    PriorityItem* newItem = new PriorityItem();
    newItem->priority = priority;
    newItem->data = data;

    // is the head null?
    if(head == 0x0) {
        head = newItem;
    } else if(location->left == 0x0) { // if not, is the location's left null?
        location->left = newItem;
        newItem->parent = location;
    } else { // if not, overwrite the right.
        location->right = newItem;
        newItem->parent = location;
    }

    // increment size
    size++;

    // quickly sort bottom up.
    upheap(newItem);
    return newItem;
}
예제 #6
0
bool
EdgeHeap::
upheap(int pos)
{
  if(print) cerr << "in Upheap " << pos << endl;
  if(pos == 0) return false;
  assert(pos < HeapSize);
  Edge* edge = array[pos];
  assert(edge->heapPos() == pos);
  double merit = edge->merit();
  int   parPos = parent(pos);
  assert(parPos < HeapSize);
  Edge* par = array[parPos];
  assert(par->heapPos() == parPos);
  if(merit > par->merit())
    {
      assert(parPos < HeapSize);
      array[parPos] = edge;
      edge->heapPos() = parPos;
      assert(pos < HeapSize);
      array[pos] = par;
      par->heapPos() = pos;
      if(print) cerr << "Put " << *edge << " in " << parPos << endl;
      upheap(parPos);
      return true;
    }
  else if(print)
    {
      cerr << "upheap of " << merit << "stopped by "
	<< *par << " " << par->merit() << endl;
    }
  return false;
}
예제 #7
0
bool
AnsTreeHeap::
upheap(int pos)
{
  if(print) cerr << "in Upheap " << pos << endl;
  if(pos == 0) return false;
  AnsTreePair* atp = array[pos];
  double merit = atp->first;
  int   parPos = parent(pos);
  AnsTreePair* par = array[parPos];
  double pmerit = par->first;
  if(merit < pmerit)
    {
      array[parPos] = atp;
      array[pos] = par;
      if(print) cerr << "Put " << pos << " in " << parPos << endl;
      upheap(parPos);
      return true;
    }
  else if(print)
    {
      cerr << "upheap of " << merit << "stopped by "
	<< parPos << " " << pmerit << endl;
    }
  return false;
}
예제 #8
0
// sort through list to make sure that it is sorted. (Bottom up)
void PriorityQueue::upheap(PriorityItem* current) {
    // go up this current node's tree section.
    if(current == 0x0) return;

    // check the left side
    if(current->left != 0x0) {
        if(current->left->priority < current->priority) {
            dataswap(current, current->left);
        }
    }

    // check the right side
    if(current->right != 0x0) {
        if(current->right->priority < current->priority) {
            dataswap(current, current->right);
        }
    }

    // make sure that the parent is set and upheap this branch.
    if(current->parent != 0x0) {
        if(current->parent->priority > current->priority) {
            dataswap(current, current->parent);
        }

        upheap(current->parent);
    }
}
예제 #9
0
            bool insert(item_type* pItem)
            { 
                if ( !pItem )
                {
                    return false;
                }

                if ( m_items.size() > m_size)
                {
                    m_items[m_size] = pItem;
                }
                else
                {
                    try
                    {
                        m_items.push_back(pItem);
                    }
                    catch (std::bad_alloc&)
                    {
                        return false;
                    }
                }
                            
                pItem->m_position = m_size;
                m_size++;

                upheap(pItem->m_position);
                return true;
            };
예제 #10
0
int abstractHeap::insert(void *t)
{
 if (numels == maxels) return -1;

 heap[++numels] = t;
 if (positions != NULL) positions[(j_voidint)t] = numels;
 return upheap(numels);
}
예제 #11
0
/* rebuild the heap: this function is used only once and executed rarely */
static inline void reheap (ANHE *heap, int N)
{
  int i;

  /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
  /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */
  for (i = 0; i < N; ++i)
    upheap (heap, i + HEAP0);
}
예제 #12
0
파일: MxHeap.cpp 프로젝트: ptitSeb/libgsalt
void MxHeap::insert(MxHeapable *t, float v)
{
    t->heap_key(v);

    add(t);
    unsigned int i = last_id();
    t->set_heap_pos(i);

    upheap(i);
}
예제 #13
0
파일: heapsort.cpp 프로젝트: jazzboysc/RTGI
void  heapUpheap( void * v ){

  int i = heapIndex( v );
  if( !i ){
    fprintf(stderr, "There is no such element in heap \n");
    return;
  }
  upheap(i);

}
예제 #14
0
/**
 * Liefert als Resultat das Heap-Element a[k], entfernt a[k]
 * aus dem Heap und stellt die Heap-Eigenschaft wieder her.
 */
static int removeh(int *a, int k) {
	int v = a[k];
	a[k] = a[--N];
	if ((k > 0) && (a[k] < a[k / 2]))
		upheap(a, k);
	else
		downheap(a, k);

	return v;
}
예제 #15
0
void insert(int elem) {
  //ヒープに登録できる余裕があるか確認
  if (n >= MAX_HEAP) {
    fprintf(stderr, "これ以上登録できません\n");
  }

  //要素をとりあえず最後尾に入れる
  a[++n] = elem;

  upheap(n);
}
예제 #16
0
파일: MxHeap.cpp 프로젝트: ptitSeb/libgsalt
void MxHeap::update(MxHeapable *t, float v)
{
    SanityCheck( t->is_in_heap() );
    t->heap_key(v);

    unsigned int i = t->get_heap_pos();

    if( i>0 && v>ref(parent(i))->heap_key() )
	upheap(i);
    else
	downheap(i);
}
예제 #17
0
파일: heap.C 프로젝트: nullarm/MCF
//==============================================================
// Insert
//==============================================================
void heap::insert(int i)
{
    if (N >= maxsize)
        std::cout << "error, N >= maxsize, cannot insert another event" << std::endl;
    else
    {
        N++;
        a[N] = i;
        index[i] = N;
        upheap(N);
    }
}
예제 #18
0
파일: heap.cpp 프로젝트: mboghiu/try
void Heap::upheap(int node)
{
    if (GetParentIndex(node) < 0)
        return;

    if (SatisfiesHeapProperty(GetParentIndex(node)))
        return;

    std::swap(m_heap.at(node), m_heap.at(GetParentIndex(node)));

    upheap(GetParentIndex(node));
}
예제 #19
0
void
EdgeHeap::
insert(Edge* edge)
{
  if(print)
    cerr << "heap insertion of " << *edge << " at " << unusedPos_ << endl;
  assert(unusedPos_ < HeapSize);
  array[unusedPos_] = edge;
  edge->heapPos() = unusedPos_;
  upheap(unusedPos_);
  unusedPos_++;
  assert(unusedPos_ < HeapSize);
}
예제 #20
0
파일: heap.c 프로젝트: srsbsns/libsrsbsns
void
heap_insert(heap_t h, void *elem)
{
	if (!elem)
		return;

	if (h->next >= h->treesz)
		heap_resize(h, h->treesz*2);
	
	h->tree[h->next] = elem;
	h->count++;
	upheap(h, h->next++);
}
예제 #21
0
void
ValHeap::
push(Val* atp)
{
  assert(atp);
  if(print)
    cerr << "heap insertion of atp at " << unusedPos_ << endl;
  assert(array.size() >= unusedPos_);
  if(array.size() ==  unusedPos_) array.push_back(atp);
  else array[unusedPos_] = atp;
  upheap(unusedPos_);
  unusedPos_++;
}
예제 #22
0
파일: heap.c 프로젝트: ahltorp/afssync
static void
remove_this (Heap *h, unsigned n)
{
    assert (n < h->sz);

    --h->sz;
    h->data[n] = h->data[h->sz];
    h->data[h->sz].data = NULL;
    h->data[h->sz].ptr  = NULL;
    if (n != h->sz) {
	downheap (h, n);
	upheap (h, n);
    }
}
예제 #23
0
void gw_prioqueue_insert(gw_prioqueue_t *queue, void *item)
{
    gw_assert(queue != NULL);
    gw_assert(item != NULL);
    
    queue_lock(queue);
    make_bigger(queue, 1);
    queue->tab[queue->len] = gw_malloc(sizeof(**queue->tab));
    queue->tab[queue->len]->item = item;
    queue->tab[queue->len]->seq = queue->seq++;
    upheap(queue, queue->len);
    queue->len++;
    pthread_cond_signal(&queue->nonempty);
    queue_unlock(queue);
}
예제 #24
0
파일: MxHeap.cpp 프로젝트: ptitSeb/libgsalt
MxHeapable *MxHeap::remove(MxHeapable *t)
{
    if( !t->is_in_heap() ) return NULL;

    int i = t->get_heap_pos();
    swap(i, length()-1);
    drop();
    t->not_in_heap();

    if( ref(i)->heap_key() < t->heap_key() )
	downheap(i);
    else
	upheap(i);

    return t;
}
예제 #25
0
/* Changes the key (and optional data) of the element ai, re-sorting
 * the heap if necessary. Returns 0 upon success. */
int airHeapUpdate(airHeap *h, unsigned int ai, double newKey,
		  const void *newData) {
  double oldkey;
  if (h==NULL || h->key_a->len<=ai)
    return 1;
  oldkey = h->key[ai];
  /* replace key and data */
  h->key[ai] = newKey;
  if (h->data_a!=NULL && newData!=NULL) {
    memcpy((char*)h->data_a->data+ai*h->data_a->unit,
	   newData, h->data_a->unit);
  }
  if (oldkey<newKey) downheap(h, h->invidx[ai]);
  else upheap(h, h->invidx[ai]);
  return 0;
}
예제 #26
0
/* Inserts a key into the heap. data is copied over (deep copy) when the
 * heap was initialized to hold additional data. Otherwise, it is ignored.
 *
 * Returns the new number of elements in h.
 * Upon error, this can be the same as the old length. */
unsigned int airHeapInsert(airHeap *h, double key, const void *data) {
  unsigned int oldlen;
  if (h==NULL) return 0;
  oldlen = h->key_a->len;
  /* make space for the new element */
  if (heapLenIncr(h, 1))
    return oldlen;
  h->key[oldlen]=key;
  if (h->data_a!=NULL && data!=NULL) {
    memcpy((char*)h->data_a->data+oldlen*h->data_a->unit, data,
	   h->data_a->unit);
  }
  h->idx[oldlen]=oldlen;
  h->invidx[oldlen]=oldlen;
  upheap(h,oldlen); /* restore the heap property */
  return oldlen+1;
}
예제 #27
0
bool heap_encolar(heap_t *heap, void *elem){
	if (heap==NULL) return false;
	//Si no hay tamaño, lo redimensiono.
	if(heap->tamanio < heap->cantidad+2){
		void** nuevos_datos=realloc( (heap->datos) , (heap->tamanio)*2 *sizeof(void*));
			if(!nuevos_datos) return false;
		heap->tamanio=(heap->tamanio)*2;
		heap->datos=nuevos_datos;
		int i;
		for (i=heap->cantidad;i< heap->tamanio;i++) {
			heap->datos[i]=NULL;
		}
	}
	heap->datos[heap->cantidad]=elem;
	heap->cantidad++;
	upheap(heap);
	return true;
	}
예제 #28
0
int abstractHeap::upheap(int k)
{
 if (k < 2) return k;
 
 void *t = heap[k];
 int fk = (k%2)?((k-1)/2):(k/2);
 void *f = heap[fk];

 if (compare(t, f) <= 0)
 {
  heap[k] = f;
  heap[fk] = t;
  if (positions != NULL)
  {
   positions[(j_voidint)f] = k;
   positions[(j_voidint)t] = fk;
  }
  return upheap(fk);
 }
 return k;
}
예제 #29
0
 void update(item_type* pItem, weight_type newweight)
 {
     if (!pItem)
     {
         return;
     }
     pos_type i = pItem->m_position;
     if (i>=m_size || i == NOT_IN_HEAP)
     {
         return;
     }
     weight_type oldweight = pItem->m_weight;
     pItem->m_weight = newweight;
     if (newweight<oldweight)
     {
         downheap(i);
     }
     else
     {
         upheap(i);
     }
 }
예제 #30
0
파일: heap.c 프로젝트: ahltorp/afssync
int
heap_insert (Heap *h, const void *data, heap_ptr *ptr)
{
    assert (data != NULL);

    if (h->sz == h->max_sz) {
	unsigned new_sz = h->max_sz * 2;
	heap_element *tmp;

	tmp = realloc (h->data, new_sz * sizeof(*h->data));
	if (tmp == NULL)
	    return -1;
	h->max_sz = new_sz;
	h->data   = tmp;
    }
    if (ptr == NULL)
	ptr = &dummy;

    h->data[h->sz].data = data;
    h->data[h->sz].ptr  = ptr;
    upheap (h, h->sz);
    ++h->sz;
    return 0;
}