Esempio n. 1
0
size_t borNearestLinear(bor_list_t *list, void *p,
                        bor_nearest_linear_dist_t dist_cb,
                        bor_list_t **nearest, size_t num,
                        void *data)
{
    bor_list_t *item;
    bor_real_t *dists, dist;
    size_t len;

    if (num == 0)
        return 0;

    dists = BOR_ALLOC_ARR(bor_real_t, num);
    len = 0;

    BOR_LIST_FOR_EACH(list, item){
        dist = dist_cb(p, item, data);

        if (len < num){
            dists[len]   = dist;
            nearest[len] = item;
            len++;

            bubbleUp(dists, nearest, len);
        }else if (dist < dists[len - 1]){
            dists[len - 1]   = dist;
            nearest[len - 1] = item;

            bubbleUp(dists, nearest, len);
        }
    }
/* Make two measurements on the signal of length numSamples sampled at
   sampleRate in pcm: the RMS of the highest amplitude 30ms segment
   (returned in peakRms), and the RMS of the top 50 peak absolute
   values found (returned in peakAverage).  If the signal is too
   short to make reasonable measurements, the function returns 0, else
   it returns 1. */
static int peakLevels(short* pcm, int numSamples, float sampleRate,
                      float* peakAverage, float* peakRms) {
    float rmsFrameSize = 0.03;
    float rmsFrameStep = 0.01;
    int frameStep = int(0.5 + (sampleRate * rmsFrameStep));
    int frameSize = int(0.5 + (sampleRate * rmsFrameSize));
    int numFrames = 1 + ((numSamples - frameSize) / frameStep);

    if (numFrames < 10) {
        return 0; // failure for too short signal
    }

    // Peak RMS calculation
    double maxEnergy = 0.0;
    for (int frame = 0; frame < numFrames; ++frame) {
        double energy = 0.0;
        int limit = (frame * frameStep) + frameSize;
        for (int i = frame * frameStep; i < limit; ++i) {
            double s = pcm[i];
            energy += s * s;
        }
        if (energy > maxEnergy) {
            maxEnergy = energy;
        }
    }
    *peakRms = sqrt(maxEnergy / frameSize);

    // Find the absolute highest topN peaks in the signal and compute
    // the RMS of their values.
    int topN = 50; // The number of highest peaks over which to average.
    int topM = topN - 1;
    int* maxVal = new int[topN];
    for (int i = 0; i < topN; ++i) {
        maxVal[i] = 0;
    }
    for (int i = 0; i < numSamples; ++i) {
        if (pcm[i] >= 0) {
            bubbleUp(maxVal, topM, pcm[i]);
        } else {
            bubbleUp(maxVal, topM, -pcm[i]);
        }
    }
    float sum = 0.0;
    // The RMS is taken bacause we want the values of the highest peaks
    // to dominate.
    for (int i = 0; i < topN; ++i) {
        float fval = maxVal[i];
        sum += (fval * fval);
    }
    delete [] maxVal;
    *peakAverage = sqrt(sum/topN);
    return 1; // success
}
Esempio n. 3
0
// deletes this_node from the heap, and then repairs the heap
void deleteNodeFromHeap(node* this_node)
{ 
  this_node->inHeap = closed;
  tempInd = this_node->heapIndex;  
  this_node->heapIndex = -1;
  
  // if this was the last node on the heap, then we are done
  if(tempInd == indexOfLast)
  {
    indexOfLast--;
    
    if(indexOfLast == 0)
      parentOfLast = -1;
    else
      parentOfLast = (indexOfLast-1)/2;
    
    return;
  }
  else if(tempInd == 0) // if this is the top node, then just pop it
  {
    popHeap();
    return;
  }  
  
  // put last node from heap where this node used to be
  heapNode[tempInd] = heapNode[indexOfLast];
  heapNode[tempInd]->heapIndex = tempInd;
  
  // take care of heap values at old last node position
  heapNode[indexOfLast] = NULL;
  indexOfLast--;
  
  if(indexOfLast == 0)
    parentOfLast = -1;
  else
    parentOfLast = (indexOfLast-1)/2;
  
  // need to repair heap differently depending on the key vector of heapNode[tempInd]
  if(tempInd > parentOfLast) // then can't go down any more so only try to go up
  {
    bubbleUp(tempInd);
  }
  else if(nodeLess(heapNode[tempInd], heapNode[(tempInd-1)/2])) // this node is less than its parent so try to go up
  {
      bubbleUp(tempInd);
  }
  else // this node is not less than its parent, so try to go down
  {
    bubbleDown(tempInd); 
  }
}
Esempio n. 4
0
// repairs the heap if this_node is in the wrong place in the heap
void updateHeapPositionOfNode(node* this_node)
{ 
  tempInd = this_node->heapIndex;  
  
  // need to repair heap differently depending on the key vector of heapNode[tempInd]
  if(tempInd > parentOfLast) // then can't go down any more so only try to go up
    bubbleUp(tempInd);
  else if(tempInd == 0) // can't go up any more so only try to go down
    bubbleDown(tempInd);
  else if(nodeLess(heapNode[tempInd], heapNode[(tempInd-1)/2])) // this node is less than its parent so try to go up
    bubbleUp(tempInd);  
  else // this node is not less than its parent, so try to go down
    bubbleDown(tempInd);
}
 // associate key with index i; 0 < i < NMAX
 void insert(int i, int key) {
     N++;
     index[i] = N;
     heap[N] = i;
     keys[i] = key;
     bubbleUp(N);
 }
 // delete the key associated with index i
 void deleteKey(int i)   {
     int ind = index[i];
     swap(ind, N--);
     bubbleUp(ind);
     bubbleDown(ind);
     index[i] = -1;
 }
Esempio n. 7
0
void HeapPriorityQueue::enqueue(string value) {
    //Create new node with the value as the word.
    string* newWord = new string(value);
    
    /*
     * Insert node into tree.
     */
    
    //If there's no other elements, this element becomes the root
    if(isEmpty()){
        root = newWord;
    }
    //For all other cases:
    else{
        //Find next empty spot and insert:
        elements[size() + 1] = newWord;
        
        //Swap with parent nodes as needed until tree is valid again.
        bubbleUp(elements[size() + 1], size() + 1, elements);
      
    }
    //Increment the total.
    total ++;
    
    //If necessary, grow the tree.
    if(total == length){
        growTree(elements);
    }
}
Esempio n. 8
0
void heap_insert(Heap *heap, uint32_t value, uint32_t priority) {
   // The heap will always have at least 1 extra space for an element at
   // the end when this function ends.
   
   // Create the new node and add it to the available spot in the heap.
   Node node;
   node.priority = priority;
   node.value = value;

   uint32_t pos = heap->numElements;
   heap->elements[pos] = node;
   heap->numElements++;

   // "Bubble up" the new element
   bubbleUp(heap, pos);

   // Reallocate the elements array to twice the numElements if we're full
   if (heap->numElements == heap->size) {
      heap->size *= 2;
#if DEBUG
      printf("\nincreasing heap size to %u...\n\n", heap->size);
#endif
      heap->elements = realloc(heap->elements, heap->size * sizeof(Node));
   }
}
/* helper function: bubbleUp(int index)
 *
 * lexicographically repairs the heap
 * by swapping elements with their parents
 */
void HeapPriorityQueue::bubbleUp(int index) {
    int parentIndex = index / PARENT_CHILD_MULT;
    if (parentIndex == 0) return;
    if (elems[index] < elems[parentIndex]) {
        swap(index, parentIndex);
        bubbleUp(parentIndex);
    }
}
Esempio n. 10
0
 // preserve the heap invariant, by moving a violating
 // child upward
 void binheap::bubbleUp(idx i) 
 {
   idx par_i = parent(i);
   if (i != 0 && heap[par_i] > heap[i]) {
     std::swap(heap[par_i], heap[i]);
     bubbleUp(par_i);
   }
 }
Esempio n. 11
0
void HeapPriorityQueue::enqueue(string value) {
    logicalLength++;
    if ((size()) == allocatedLength) {
		grow();
	}
    elems[size()] = value;
    bubbleUp(size());
}
Esempio n. 12
0
void bubbleSort(int *unsorted, int size) {
    bool isSorted = false;
    for(int i = size; i > 1; i--) {
        if(isSorted) 
            break;
        else
            isSorted = bubbleUp(unsorted, i);
    }
}
Esempio n. 13
0
void bubbleUp(POTNODE a, GRAPH G, POT P)
{
    if (MYDEBUG) printf("Entering bubbleUP.\n");
    if ((a > 1) &&
        (priority(a, G, P) < priority(a/2, G, P))) {
        swap(a, a/2, G, P);
        bubbleUp(a/2, G, P);
    }
    if (MYDEBUG) printf("Exiting bubbleUp.\n");
}
Esempio n. 14
0
bool CPriorityHeap<T>::insert(T key, float priority)
{
  CPriority<T> temp(key, priority);
  heap.push_back( temp );

  bubbleUp(index);
  index++;

  return true;
}
Esempio n. 15
0
/*	==================== bubbleSort ====================
	Sort list using bubble sort. Adjacent elements are 
	compared and exchanged until list is completely ordered.
	   Pre   the list must contain at least one item 
	         last contains index to last element in list
	   Post  list rearranged in sequence low to high
*/
void bubbleSort (int list [],
                 int last)
{
/*	 Local Definitions */
	int current;

/*	Statements */
	 for(current = 0; current < last; current++)
	     bubbleUp (list, current, last);

	return;
}	/* bubbleSort */
Esempio n. 16
0
 void freqtable::insert(const char* fileid, struct stat& st, classifier& cl)
 {
   long pos = find(fileid, cl);
   
   if ( pos == -1 ) {
     table.push_back(new entry(fileid, st));
     pos = table.size() - 1;
   } else {
     table.at(pos)->add(st);
   }
   
   bubbleUp(pos);
 }
Esempio n. 17
0
void heap_insert(heap_t* h, double idx, void* data)
{
	if (h->size == h->avail)
	{
		h->avail = h->avail ? 2*h->avail : 1;
		h->tree = (hnode_t*) realloc(h->tree, sizeof(hnode_t)*h->avail);
		assert(h->tree != NULL);
	}

	size_t i = h->size++;
	h->tree[i] = (hnode_t){idx,data};
	bubbleUp(h, i);
}
Esempio n. 18
0
void Heap::bubbleUp(int pos) {
	if (pos < 1)
		return;
	if (arr[pos]->priority < arr[getParentIndex(pos)]->priority) {
		Node *temp;
		temp = arr[getParentIndex(pos)];
		arr[getParentIndex(pos)] = arr[pos];
		arr[pos] = temp;
		pos = getParentIndex(pos);
		bubbleUp(pos);
	}
	return;
}
Esempio n. 19
0
void Heap::insert(Node *n) {
	if (lastPosition == -1) {
		arr[++lastPosition] = n;
		numItems++;
		return;
	}
	if (lastPosition >= size - 1) {
		expandArr();
	}
	numItems++;
	arr[++lastPosition] = n;
	bubbleUp(lastPosition);
	return;
}
Esempio n. 20
0
  //TODO: implement this function!
  virtual std::pair<typename BST<Data>::iterator,bool> insert(const Data& item)
  {
   
    std::pair <typename BST<Data>::iterator,bool> pa = BST<Data>::insert(item);

    
    if ( pa.second == false )
      return pa;
   
       BSTNode<Data>* x = pa.first.curr;

    x->info = rand();
    bubbleUp( x );
    return pa;
  }
Esempio n. 21
0
void PQueue::enQueue(int e, float w)
{
	if(qsize>=MAX_QUEUE_SIZE) {
		printf("PQueue Full...");
		exit(-1);
	}

	qsize++;
	queue[qsize] = e;
	weight[qsize] = w;
	
	// mark
	//e->pqIndex = getSize();

	bubbleUp(qsize);
}
Esempio n. 22
0
void dtNodeQueue::trickleDown(int i, dtNode* node)
{
	int child = (i*2)+1;
	while (child < m_size)
	{
		if (((child+1) < m_size) && 
			(m_heap[child]->total > m_heap[child+1]->total))
		{
			child++;
		}
		m_heap[i] = m_heap[child];
		i = child;
		child = (i*2)+1;
	}
	bubbleUp(i, node);
}
Esempio n. 23
0
// add thisNode to the heap
void addToHeap(node* thisNode)
{ 
  if(thisNode->inHeap == false || thisNode->inHeap == closed)
  {
    indexOfLast++;
    
    if(indexOfLast == 0)
      parentOfLast = -1;
    else
      parentOfLast = (indexOfLast-1)/2;

    heapNode[indexOfLast] = thisNode;
    thisNode->heapIndex = indexOfLast;
    bubbleUp(indexOfLast);     
    thisNode->inHeap = true;
  }
}
Esempio n. 24
0
void Dijkstra(GRAPH G, POT P, int *pLast)
{
    NODE u, v; /* v is the node we select to settle */
    LIST ps; /* ps runs down the list of successors of v;
              u is the successor pointed to by ps */
    if (MYDEBUG) printf("Entering Dijkstra.\n");
    initialize(G, P, pLast);
    while ((*pLast) > 1) {
        v = P[1];
        swap(1, *pLast, G, P);
        --(*pLast);
        bubbleDown(1, G, P, *pLast);
        ps = G[v].successors;
        while (ps != NULL) {
            u = ps->nodeName;
            if (G[u].distance > G[v].distance + ps->nodeLabel) {
                G[u].distance = G[v].distance + ps->nodeLabel;
                bubbleUp(G[u].toPOT, G, P);
            }
            ps = ps->next;
        }
    }
}
Esempio n. 25
0
void HeapPriorityQueue::enqueue(string value) {
    if (numAllocated == capacity) grow();
    pQueue[++numAllocated] = value;
    bubbleUp(numAllocated);
}
Esempio n. 26
0
void Heap::insert(int x)
{
  heap[size] = x;
  size++;
  bubbleUp(size-1);
}
 // change the key associated with index i to the specified value
 void changeKey(int i, int key)  {
     keys[i] = key;
     bubbleUp(index[i]);
     bubbleDown(index[i]);
 }
Esempio n. 28
0
/*Insere um elemento na heap*/
int insertHeap(Heap *h, Elem x){
	if(h->used == h->size) return 1;
	h->values[h->used] = x;
	bubbleUp(h->values,h->used++);
	return 0; 	
}
Esempio n. 29
0
//gotta use the min-heap!!
void binary_heap::push(int i) { 
	//parent is (i - 1)/2
	data.push(i);
	bubbleUp(data.size() - 1);
}
 // decrease the key associated with index i to the specified value
 void decreaseKey(int i, int key)    {
     keys[i] = key;
     bubbleUp(index[i]);
 }