Some mild refactoring of the BinaryHeap code. style only
This commit is contained in:
parent
7825279405
commit
22c2efded9
@ -25,8 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef BINARYHEAP_H_INCLUDED
|
#ifndef BINARY_HEAP_H
|
||||||
#define BINARYHEAP_H_INCLUDED
|
#define BINARY_HEAP_H
|
||||||
|
|
||||||
//Not compatible with non contiguous node ids
|
//Not compatible with non contiguous node ids
|
||||||
|
|
||||||
@ -82,6 +82,9 @@ private:
|
|||||||
|
|
||||||
template< typename NodeID, typename Key >
|
template< typename NodeID, typename Key >
|
||||||
class UnorderedMapStorage {
|
class UnorderedMapStorage {
|
||||||
|
typedef boost::unordered_map<NodeID, Key> UnorderedMapType;
|
||||||
|
typedef typename UnorderedMapType::iterator UnorderedMapIterator;
|
||||||
|
typedef typename UnorderedMapType::const_iterator UnorderedMapConstIterator;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
UnorderedMapStorage( size_t ) {
|
UnorderedMapStorage( size_t ) {
|
||||||
@ -89,8 +92,8 @@ public:
|
|||||||
nodes.rehash(1000);
|
nodes.rehash(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
Key &operator[]( const NodeID node ) {
|
Key & operator[]( const NodeID node ) {
|
||||||
return nodes[node];
|
return nodes[node];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear() {
|
||||||
@ -101,13 +104,13 @@ private:
|
|||||||
boost::unordered_map< NodeID, Key > nodes;
|
boost::unordered_map< NodeID, Key > nodes;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename NodeID = unsigned>
|
template<
|
||||||
struct _SimpleHeapData {
|
typename NodeID,
|
||||||
NodeID parent;
|
typename Key,
|
||||||
_SimpleHeapData( NodeID p ) : parent(p) { }
|
typename Weight,
|
||||||
};
|
typename Data,
|
||||||
|
typename IndexStorage = ArrayStorage<NodeID, NodeID>
|
||||||
template < typename NodeID, typename Key, typename Weight, typename Data, typename IndexStorage = ArrayStorage<NodeID, NodeID> >
|
>
|
||||||
class BinaryHeap {
|
class BinaryHeap {
|
||||||
private:
|
private:
|
||||||
BinaryHeap( const BinaryHeap& right );
|
BinaryHeap( const BinaryHeap& right );
|
||||||
@ -117,7 +120,9 @@ public:
|
|||||||
typedef Data DataType;
|
typedef Data DataType;
|
||||||
|
|
||||||
BinaryHeap( size_t maxID )
|
BinaryHeap( size_t maxID )
|
||||||
: nodeIndex( maxID ) {
|
:
|
||||||
|
nodeIndex( maxID )
|
||||||
|
{
|
||||||
Clear();
|
Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,11 +215,13 @@ public:
|
|||||||
private:
|
private:
|
||||||
class HeapNode {
|
class HeapNode {
|
||||||
public:
|
public:
|
||||||
HeapNode() {
|
|
||||||
}
|
|
||||||
HeapNode( NodeID n, Key k, Weight w, Data d )
|
HeapNode( NodeID n, Key k, Weight w, Data d )
|
||||||
: node( n ), key( k ), weight( w ), data( d ) {
|
:
|
||||||
}
|
node(n),
|
||||||
|
key(k),
|
||||||
|
weight(w),
|
||||||
|
data(d)
|
||||||
|
{ }
|
||||||
|
|
||||||
NodeID node;
|
NodeID node;
|
||||||
Key key;
|
Key key;
|
||||||
@ -234,14 +241,17 @@ private:
|
|||||||
const Key droppingIndex = heap[key].index;
|
const Key droppingIndex = heap[key].index;
|
||||||
const Weight weight = heap[key].weight;
|
const Weight weight = heap[key].weight;
|
||||||
Key nextKey = key << 1;
|
Key nextKey = key << 1;
|
||||||
while ( nextKey < static_cast<Key>( heap.size() ) ) {
|
while( nextKey < static_cast<Key>( heap.size() ) ){
|
||||||
const Key nextKeyOther = nextKey + 1;
|
const Key nextKeyOther = nextKey + 1;
|
||||||
if ( ( nextKeyOther < static_cast<Key> ( heap.size() ) )&& ( heap[nextKey].weight > heap[nextKeyOther].weight) )
|
if (
|
||||||
|
( nextKeyOther < static_cast<Key>( heap.size() ) ) &&
|
||||||
|
( heap[nextKey].weight > heap[nextKeyOther].weight )
|
||||||
|
) {
|
||||||
nextKey = nextKeyOther;
|
nextKey = nextKeyOther;
|
||||||
|
}
|
||||||
if ( weight <= heap[nextKey].weight )
|
if ( weight <= heap[nextKey].weight ){
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
heap[key] = heap[nextKey];
|
heap[key] = heap[nextKey];
|
||||||
insertedNodes[heap[key].index].key = key;
|
insertedNodes[heap[key].index].key = key;
|
||||||
key = nextKey;
|
key = nextKey;
|
||||||
@ -277,4 +287,4 @@ private:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //#ifndef BINARYHEAP_H_INCLUDED
|
#endif //BINARY_HEAP_H
|
||||||
|
Loading…
Reference in New Issue
Block a user