123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- ////////////////////////////////////////////////////////////////////////////////////////
- // RAVEN STANDARD TEMPLATE LIBRARY
- // (c) 2002 Activision
- //
- //
- // Heap
- // ------
- //
- //
- //
- //
- // TODO:
- //
- //
- // NOTES:
- //
- //
- ////////////////////////////////////////////////////////////////////////////////////////
- #if !defined(RATL_HEAP_VS_INC)
- #define RATL_HEAP_VS_INC
- ////////////////////////////////////////////////////////////////////////////////////////
- // Includes
- ////////////////////////////////////////////////////////////////////////////////////////
- #if !defined(RATL_COMMON_INC)
- #include "ratl_common.h"
- #endif
- namespace ratl
- {
- ////////////////////////////////////////////////////////////////////////////////////////
- // The Vector Class
- ////////////////////////////////////////////////////////////////////////////////////////
- template<class T>
- class heap_base : public ratl_base
- {
- public:
- typedef typename T TStorageTraits;
- typedef typename T::TValue TTValue;
- ////////////////////////////////////////////////////////////////////////////////////
- // Capacity Enum
- ////////////////////////////////////////////////////////////////////////////////////
- enum
- {
- CAPACITY = T::CAPACITY
- };
- ////////////////////////////////////////////////////////////////////////////////////
- // Data
- ////////////////////////////////////////////////////////////////////////////////////
- private:
- array_base<TStorageTraits> mData; // The Memory
- int mPush; // Address Of Next Add Location
- ////////////////////////////////////////////////////////////////////////////////////
- // Returns The Location Of Node (i)'s Parent Node (The Parent Node Of Zero Is Zero)
- ////////////////////////////////////////////////////////////////////////////////////
- static int parent(int i)
- {
- return ((i-1)/2);
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Returns The Location Of Node (i)'s Left Child (The Child Of A Leaf Is The Leaf)
- ////////////////////////////////////////////////////////////////////////////////////
- static int left(int i)
- {
- return (2*i)+1;
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Returns The Location Of Node (i)'s Right Child (The Child Of A Leaf Is The Leaf)
- ////////////////////////////////////////////////////////////////////////////////////
- static int right(int i)
- {
- return (2*i)+2;
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Returns The Location Of Largest Child Of Node (i)
- ////////////////////////////////////////////////////////////////////////////////////
- int largest_child(int i) const
- {
- if (left(i)<mPush)
- {
- if (right(i)<mPush)
- {
- return ( (mData[right(i)] < mData[left(i)]) ? (left(i)) : (right(i)) );
- }
- return left(i); // Node i only has a left child, so by default it is the biggest
- }
- return i; // Node i is a leaf, so just return it
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Swaps Two Element Locations
- ////////////////////////////////////////////////////////////////////////////////////
- void swap(int a, int b)
- {
- if (a==b)
- {
- return;
- }
- assert(a>=0 && b>=0 && a<CAPACITY && b<CAPACITY);
- mData.swap(a,b);
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Swaps The Data Up The Heap Until It Reaches A Valid Location
- ////////////////////////////////////////////////////////////////////////////////////
- void reheapify_upward(int Pos)
- {
- while (Pos && mData[parent(Pos)]<mData[Pos])
- {
- swap(parent(Pos), Pos);
- Pos = parent(Pos);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Swaps The Data Down The Heap Until It Reaches A Valid Location
- ////////////////////////////////////////////////////////////////////////////////////
- void reheapify_downward(int Pos)
- {
- int largestChild = largest_child(Pos);
- while (largestChild!=Pos && mData[Pos]<mData[largestChild])
- {
- swap(largestChild, Pos);
- Pos = largestChild;
- largestChild = largest_child(Pos);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Validate Will Run Through The Heap And Make Sure The Top Element Is Smallest
- ////////////////////////////////////////////////////////////////////////////////////
- bool valid()
- {
- for (int i=1; i<mPush; i++)
- {
- if (mData[0]<mData[i])
- {
- return false;
- }
- }
- return true;
- }
- public:
- ////////////////////////////////////////////////////////////////////////////////////
- // Constructor
- ////////////////////////////////////////////////////////////////////////////////////
- heap_base() : mPush(0)
- {
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Get The Size (The Difference Between The Push And Pop "Pointers")
- ////////////////////////////////////////////////////////////////////////////////////
- int size() const
- {
- return mPush;
- }
-
- ////////////////////////////////////////////////////////////////////////////////////
- // Check To See If The Size Is Zero
- ////////////////////////////////////////////////////////////////////////////////////
- bool empty() const
- {
- return !size();
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Check To See If The Size Is Full
- ////////////////////////////////////////////////////////////////////////////////////
- bool full() const
- {
- return size()==CAPACITY;
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Empty Out The Entire Heap
- ////////////////////////////////////////////////////////////////////////////////////
- void clear()
- {
- mPush = 0;
- mData.clear();
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Get The Data Value At The Top Of The Heap
- ////////////////////////////////////////////////////////////////////////////////////
- const TTValue & top() const
- {
- assert(mPush>0); // Don't Try To Look At This If There Is Nothing In Here
- return mData[0];
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Add A Value To The Queue
- ////////////////////////////////////////////////////////////////////////////////////
- void push(const TTValue& nValue)
- {
- assert(size()<CAPACITY);
- // Add It
- //--------
- mData.construct(mPush,nValue);
- // Fix Possible Heap Inconsistancies
- //-----------------------------------
- reheapify_upward(mPush);
-
- mPush++;
- assert(valid());
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Alloc A Value, call push_alloced to add
- ////////////////////////////////////////////////////////////////////////////////////
- TTValue& alloc()
- {
- assert(size()<CAPACITY);
- // Add It
- //--------
- mData.construct(mPush);
- return mData[mPush];
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Alloc A Raw Value for placement new, call push_alloced to add
- ////////////////////////////////////////////////////////////////////////////////////
- TRatlNew * alloc_raw()
- {
- assert(size()<CAPACITY);
- return mData.alloc_raw(mPush);
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Add A Value To The Queue, after filling an alloced slot
- ////////////////////////////////////////////////////////////////////////////////////
- void push_alloced()
- {
- assert(size()<CAPACITY);
- // Fix Possible Heap Inconsistancies
- //-----------------------------------
- reheapify_upward(mPush);
-
- mPush++;
- assert(valid());
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // Remove A Value From The Queue
- ////////////////////////////////////////////////////////////////////////////////////
- void pop()
- {
- assert(size()>0);
- mPush--;
- // Swap The Lowest Element Up To The Spot We Just "Erased"
- //---------------------------------------------------------
- swap(0, mPush);
- mData.destruct(mPush);
- // Fix Possible Heap Inconsistancies
- //-----------------------------------
- reheapify_downward(0);
- assert(valid());
- }
- };
- template<class T, int ARG_CAPACITY>
- class heap_vs : public heap_base<storage::value_semantics<T,ARG_CAPACITY> >
- {
- public:
- typedef typename storage::value_semantics<T,ARG_CAPACITY> TStorageTraits;
- typedef typename TStorageTraits::TValue TTValue;
- enum
- {
- CAPACITY = ARG_CAPACITY
- };
- heap_vs() {}
- };
- template<class T, int ARG_CAPACITY>
- class heap_os : public heap_base<storage::object_semantics<T,ARG_CAPACITY> >
- {
- public:
- typedef typename storage::object_semantics<T,ARG_CAPACITY> TStorageTraits;
- typedef typename TStorageTraits::TValue TTValue;
- enum
- {
- CAPACITY = ARG_CAPACITY
- };
- heap_os() {}
- };
- template<class T, int ARG_CAPACITY, int ARG_MAX_CLASS_SIZE>
- class heap_is : public heap_base<storage::virtual_semantics<T,ARG_CAPACITY,ARG_MAX_CLASS_SIZE> >
- {
- public:
- typedef typename storage::virtual_semantics<T,ARG_CAPACITY,ARG_MAX_CLASS_SIZE> TStorageTraits;
- typedef typename TStorageTraits::TValue TTValue;
- enum
- {
- CAPACITY = ARG_CAPACITY,
- MAX_CLASS_SIZE = ARG_MAX_CLASS_SIZE
- };
- heap_is() {}
- };
- }
- #endif
|