bool PriorityQueue<Data, Compare, Alloc>::private_move_down_node(Data* ptr) { bool ret = false; int child = ptr->qIdx * 2 + 1; child += (child+1 < used_ && comp_(*dataQue_[child+1], *dataQue_[child])); while ( child < used_ && comp_(*dataQue_[child], *ptr) ) { dataQue_[child]->qIdx = ptr->qIdx; std::swap(dataQue_[child], dataQue_[ptr->qIdx]); ptr->qIdx = child; child = child * 2 + 1; child += (child+1 < used_ && comp_(*dataQue_[child+1], *dataQue_[child])); ret = true; } return ret; }
static void ia(int *l) { int i; int r; i = 0; r = r_line(l); comp_(l, r, i); }
//! remove the highest data inline T * pop() throw() { assert(size_>0); //------------------------------------------------------------------ // promote the last element by replacing the top element //------------------------------------------------------------------ T *ans = data_[0]; data_[0] = data_[ --size_ ]; //------------------------------------------------------------------ // reorganize the heap from the top //------------------------------------------------------------------ size_t ipos = 0; while( true ) { const size_t lpos = at_left( ipos ); const size_t rpos = at_right( ipos ); size_t mpos = ipos; //-------------------------------------------------------------- //-- test left //-------------------------------------------------------------- if( lpos < size_ && comp_( *data_[ipos], *data_[lpos] ) < 0 ) mpos = lpos; //-------------------------------------------------------------- //-- test right //-------------------------------------------------------------- if( rpos < size_ && comp_( *data_[mpos], *data_[rpos] ) < 0 ) mpos = rpos; //-------------------------------------------------------------- //-- are we done ? //-------------------------------------------------------------- if( mpos == ipos ) break; //-------------------------------------------------------------- //-- update heap //-------------------------------------------------------------- cswap<slot_t>( data_[ipos], data_[mpos] ); ipos = mpos; } return ans; }
void Push(const T & value) { if (heap_.Size() < capacity_) { heap_.Push(value); } else if (comp_(value, heap_.Top())) { heap_.Pop(); heap_.Push(value); } }
void push(const T& v) { if (data_.size() < max_size_) { data_.push_back(v); if (data_.size() == max_size_) { std::make_heap(data_.begin(), data_.end(), comp_); } } else { if (max_size_ > 0 && comp_(v, data_.front())) { std::pop_heap(data_.begin(), data_.end(), comp_); data_.back() = v; std::push_heap(data_.begin(), data_.end(), comp_); } } }
bool PriorityQueue<Data, Compare, Alloc>::private_move_up_node(Data* ptr) { bool ret = false; int parent = (ptr->qIdx - 1) / 2; while ( parent >= 0 && comp_(*ptr, *dataQue_[parent]) ) { dataQue_[parent]->qIdx = ptr->qIdx; std::swap(dataQue_[parent], dataQue_[ptr->qIdx]); ptr->qIdx = parent; parent = (ptr->qIdx - 1) / 2; ret = true; } return ret; }
//! insert the address, no memory check inline void __push( T *pObj ) throw() { assert( size_ < maxi_ ); //------------------------------------------------------------------ //-- put it at the end //------------------------------------------------------------------ size_t ipos = size_; size_t ppos = parent( ipos ); data_[ size_++ ] = (mutable_type *)pObj; //------------------------------------------------------------------ //-- organize heap //------------------------------------------------------------------ while( ipos > 0 && comp_( *data_[ppos], *data_[ipos] ) < 0 ) { cswap<slot_t>( data_[ppos], data_[ipos] ); ipos = ppos; ppos = parent( ipos ); } }
bool operator() (const T & left, const T & right) const { return comp_(right, left); }