BWAPI
Public Member Functions | Private Member Functions | Private Attributes
Heap< Key, Val > Class Template Reference

#include <Heap.h>

List of all members.

Public Member Functions

 Heap (bool isMinHeap=false)
void push (const std::pair< Key, Val > &val)
void pop ()
const std::pair< Key, Val > & top () const
bool empty () const
bool set (const Key &key, const Val &val)
const Val & get (const Key &key) const
bool contains (const Key &key) const
int size () const
void clear ()
bool erase (const Key &key)

Private Member Functions

int percolate_up (int index)
int percolate_down (int index)

Private Attributes

std::vector< std::pair< Key,
Val > > 
mData
std::map< Key, int > mMapping
bool mIsMinHeap

Detailed Description

template<class Key, class Val>
class Heap< Key, Val >

Definition at line 6 of file Heap.h.


Constructor & Destructor Documentation

template<class Key, class Val>
Heap< Key, Val >::Heap ( bool  isMinHeap = false) [inline]

Definition at line 9 of file Heap.h.

: mIsMinHeap(isMinHeap) {}

Member Function Documentation

template<class Key, class Val>
void Heap< Key, Val >::clear ( ) [inline]

Definition at line 91 of file Heap.h.

References Heap< Key, Val >::mData, and Heap< Key, Val >::mMapping.

Referenced by Heap< Key, Val >::erase().

        {
                mData.clear();
                mMapping.clear();
        }

Here is the caller graph for this function:

template<class Key, class Val>
bool Heap< Key, Val >::contains ( const Key &  key) const [inline]

Definition at line 80 of file Heap.h.

References Heap< Key, Val >::mMapping.

        {
                std::map<Key, int>::const_iterator it = mMapping.find(key);
                return (it != mMapping.end());
        }
template<class Key, class Val>
bool Heap< Key, Val >::empty ( ) const [inline]
template<class Key, class Val>
bool Heap< Key, Val >::erase ( const Key &  key) [inline]

Definition at line 97 of file Heap.h.

References Heap< Key, Val >::clear(), Heap< Key, Val >::mData, Heap< Key, Val >::mMapping, and Heap< Key, Val >::percolate_down().

Referenced by TerrainAnaysisClass::createRegions().

        {
                std::map<Key, int>::iterator it = mMapping.find(key);
                if(it == mMapping.end())
                        return false;

                if(mData.size() == 1)
                        clear();
                else
                {
                        int index = it->second;
                        mData[index] = mData.back();
                        mData.pop_back();
                        mMapping.erase(it);
                        percolate_down(index);
                }

                return true;
        }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class Key, class Val>
const Val& Heap< Key, Val >::get ( const Key &  key) const [inline]

Definition at line 73 of file Heap.h.

References Heap< Key, Val >::mData, and Heap< Key, Val >::mMapping.

        {
                std::map<Key, int>::const_iterator it = mMapping.find(key);
                int index = it->second;
                return mData[index].second;
        }
template<class Key, class Val>
int Heap< Key, Val >::percolate_down ( int  index) [inline, private]

Definition at line 142 of file Heap.h.

References Heap< Key, Val >::mData, Heap< Key, Val >::mIsMinHeap, and Heap< Key, Val >::mMapping.

Referenced by Heap< Key, Val >::erase(), Heap< Key, Val >::pop(), and Heap< Key, Val >::set().

        {
                if(index < 0 || index >= (int)mData.size())
                        return -1;

                unsigned int lchild = index * 2 + 1;
                unsigned int rchild = index * 2 + 2;
                unsigned int mchild;
                int m = mIsMinHeap ? -1 : 1;

                while((mData.size() > lchild && m * mData[index].second < m * mData[lchild].second) || (mData.size() > rchild && m * mData[index].second < m * mData[rchild].second))
                {
                        mchild = lchild;
                        if(mData.size() > rchild && m * mData[rchild].second > m * mData[lchild].second)
                                mchild = rchild;

                        std::swap(mData[mchild], mData[index]);
                        mMapping.find(mData[index].first)->second = index;
                        index = mchild;
                        lchild = index * 2 + 1;
                        rchild = index * 2 + 2;
                }

                mMapping.find(mData[index].first)->second = index;

                return index;
        }

Here is the caller graph for this function:

template<class Key, class Val>
int Heap< Key, Val >::percolate_up ( int  index) [inline, private]

Definition at line 122 of file Heap.h.

References Heap< Key, Val >::mData, Heap< Key, Val >::mIsMinHeap, and Heap< Key, Val >::mMapping.

Referenced by Heap< Key, Val >::push(), and Heap< Key, Val >::set().

        {
                if(index < 0 || index >= (int)mData.size())
                        return -1;

                unsigned int parent = (index - 1) / 2;
                int m = mIsMinHeap ? -1 : 1;

                while(index > 0 && m * mData[parent].second < m * mData[index].second)
                {
                        std::swap(mData[parent], mData[index]);
                        mMapping.find(mData[index].first)->second = index;
                        index = parent;
                        parent = (index - 1) / 2;
                }
                mMapping.find(mData[index].first)->second = index;

                return index;
        }

Here is the caller graph for this function:

template<class Key, class Val>
void Heap< Key, Val >::pop ( ) [inline]

Definition at line 21 of file Heap.h.

References Heap< Key, Val >::mData, Heap< Key, Val >::mMapping, and Heap< Key, Val >::percolate_down().

Referenced by TerrainAnaysisClass::calculateWalkTileClearance(), PathFinderClass::CreateRegionPath(), TerrainAnaysisClass::createRegions(), PathFinderClass::CreateTilePath(), PathFinderClass::CreateWalkPath(), and MapHelper::walkSearch().

        {
                if(mData.empty())
                        return;

                mMapping.erase(mData.front().first);
                mData.front() = mData.back();
                mData.pop_back();

                if(mData.empty())
                        return;

                std::map<Key, int>::iterator it = mMapping.find(mData.front().first);
                if(it != mMapping.end())
                {
                        it->second = 0;
                        percolate_down(0);
                }
        }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class Key, class Val>
void Heap< Key, Val >::push ( const std::pair< Key, Val > &  val) [inline]

Definition at line 11 of file Heap.h.

References Heap< Key, Val >::mData, Heap< Key, Val >::mMapping, and Heap< Key, Val >::percolate_up().

Referenced by PathFinderClass::CreateRegionPath(), PathFinderClass::CreateTilePath(), PathFinderClass::CreateWalkPath(), Heap< Key, Val >::set(), and MapHelper::walkSearch().

        {
                int index = mData.size();
                if(mMapping.insert(std::make_pair(val.first, index)).second)
                {
                        mData.push_back(val);
                        percolate_up(index);
                }
        }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class Key, class Val>
bool Heap< Key, Val >::set ( const Key &  key,
const Val &  val 
) [inline]

Definition at line 51 of file Heap.h.

References Heap< Key, Val >::mData, Heap< Key, Val >::mMapping, Heap< Key, Val >::percolate_down(), Heap< Key, Val >::percolate_up(), and Heap< Key, Val >::push().

Referenced by TerrainAnaysisClass::calculateWalkTileClearance(), PathFinderClass::CreateRegionPath(), TerrainAnaysisClass::createRegions(), PathFinderClass::CreateTilePath(), PathFinderClass::CreateWalkPath(), and MapHelper::walkSearch().

        {
                std::map<Key, int>::iterator it = mMapping.find(key);
                if(it == mMapping.end())
                {
                        push(std::make_pair(key, val));
                        return true;
                }

                int index = it->second;
                mData[index].second = val;
                index = percolate_up(index);

                if(index >= 0 && index < (int)mData.size())
                {
                        percolate_down(index);
                        return true;
                }

                return false;
        }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class Key, class Val>
int Heap< Key, Val >::size ( ) const [inline]

Definition at line 86 of file Heap.h.

References Heap< Key, Val >::mData.

        {
                return mData.size();
        }
template<class Key, class Val>
const std::pair<Key, Val>& Heap< Key, Val >::top ( ) const [inline]

Member Data Documentation

template<class Key, class Val>
std::vector<std::pair<Key, Val> > Heap< Key, Val >::mData [private]
template<class Key, class Val>
bool Heap< Key, Val >::mIsMinHeap [private]

Definition at line 120 of file Heap.h.

Referenced by Heap< Key, Val >::percolate_down(), and Heap< Key, Val >::percolate_up().

template<class Key, class Val>
std::map<Key, int> Heap< Key, Val >::mMapping [private]

The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines