Beispiel #1
0
inline void balance(std::vector<ext_task>& tasks, std::vector<std::vector<ext_task> >& sched) {
    int size = sched.size();
    int n = tasks.size();

    std::sort(tasks.begin(), tasks.end());

    typedef boost::heap::fibonacci_heap<heap_node> heap_type;
    std::vector<heap_type::handle_type> Sh(size);
    heap_type S;

    int lim = std::min(size, n);

    // initialize heap
    for (int i = 0; i < lim; ++i) {
        sched[i].push_back(tasks.back());
        Sh[i] = S.push(heap_node(i, tasks.back().t.cost));
        tasks.pop_back();
    }

    // update heap (and schedule)
    for (int i = 0; i < n - lim; ++i) {
        heap_node nd = S.top();
        sched[nd.pos].push_back(tasks.back());
        nd.cost += tasks.back().t.cost;
        S.update(Sh[nd.pos], nd);
        tasks.pop_back();
    }
} // balance
Beispiel #2
0
void matcher::heap::push(int u, int v) {
	int pos = heap_pos[u][v];
	if (pos <= len && nodes[pos].u == u && nodes[pos].v == v)
		return;
	nodes[++len] = heap_node(u, v);
	heap_pos[u][v] = len;
	heap_up(len);
}
Beispiel #3
0
matcher::heap::heap(int n, int m, class matcher *o) {
	owner = o;
	len = 0;
	memset(heap_pos, 0, sizeof(heap_pos));
	for (int i=1; i<=n; i++)
		for (int j=1; j<=m; j++){
			heap_pos[i][j] = ++len;
			nodes[len] = heap_node(i, j);
		}
	for (int i=len/2; i>0; i--)
		heap_down(i);
}
Beispiel #4
0
matcher::heap::heap(map <int, double> *weights, int n, class matcher *o) {
	owner = o;
	len = 0;
	for (int i=1; i<=n; i++){
		for (std::map<int, double> ::iterator it=weights[i].begin();
			it != weights[i].end(); it++) {
			heap_pos[i][it->first] = ++len;
			nodes[len] = heap_node(i, it->first);
		}
	}
	for (int i=len/2; i>0; i--)
		heap_down(i);
}
// juergen: added 26 July, 2002
size_t CIndexedPriorityQueue::insertStochReaction(const size_t index, const C_FLOAT64 key)
{
  size_t pos;

  // check if index is valid
  if (index >= mIndexPointer.size()) return - 1;

  // first the node is inserted at the end of the heap
  mIndexPointer[index] = mHeap.size();
  PQNode heap_node(index, key);
  mHeap.push_back(heap_node);
  // bubble the node up the tree to the right position !
  pos = mIndexPointer[index];

  while ((pos > 0) && (mHeap[parent(pos)].mKey > key))
    {
      swapNodes(pos, parent(pos));
      pos = parent(pos);
    }

  return 0;
}
size_t CIndexedPriorityQueue::pushPair(const size_t index, const C_FLOAT64 key)
{
  // Add an element to the priority queue. This merely pushes an item onto
  // the back of the vector corresponding to the heap, and pushes the index
  // onto the back of the vector corresponding to the index structure.
  // Implicit is the assumption that this is done in contiguous ascending order
  // for the index; i.e. in index order 0,1,2,3...
  // N.B. The structures are not yet ordered as an indexed priority queue; this must
  // be done using the buildHeap() method

  // First check that the index corresponds to the heap size before insertion
  if (static_cast<unsigned int>(index) != mHeap.size())
    {
      CCopasiMessage(CCopasiMessage::ERROR, "Error inserting pair into priority queue");
      return - 1;
    }

  PQNode heap_node(index, key);
  mHeap.push_back(heap_node);
  // at first, position == index
  size_t position = index; // for clarity
  mIndexPointer.push_back(position);
  return 0;
}