//----------------------------------------------------------------------------- // // $Logfile:: /fakk2_code/fakk2_new/fgame/prioritystack.h $ // $Revision:: 1 $ // $Author:: Jimdose $ // $Date:: 9/10/99 10:54a $ // // Copyright (C) 1997 by Ritual Entertainment, Inc. // All rights reserved. // // This source is may not be distributed and/or modified without // expressly written permission by Ritual Entertainment, Inc. // // $Log:: /fakk2_code/fakk2_new/fgame/prioritystack.h $ // // 1 9/10/99 10:54a Jimdose // // 1 9/08/99 3:16p Aldie // // DESCRIPTION: // Stack based object that pushes and pops objects in a priority based manner. // #ifndef __PRIORITYSTACK_H__ #define __PRIORITYSTACK_H__ #include "g_local.h" #include "class.h" template class PriorityStackNode { public: int priority; Type data; PriorityStackNode *next; PriorityStackNode( Type d, int p ); }; template inline PriorityStackNode::PriorityStackNode( Type d, int p ) : data( d ) { priority = p; next = NULL; } template class PriorityStack { private: PriorityStackNode *head; public: PriorityStack(); ~PriorityStack(); void Clear( void ); qboolean Empty( void ); void Push( Type data, int priority ); Type Pop( void ); }; template inline PriorityStack::PriorityStack() { head = NULL; } template inline PriorityStack::~PriorityStack() { Clear(); } template inline void PriorityStack::Clear ( void ) { while( !Empty() ) { Pop(); } } template inline qboolean PriorityStack::Empty ( void ) { if ( head == NULL ) { return true; } return false; } template inline void PriorityStack::Push ( Type data, int priority ) { PriorityStackNode *tmp; PriorityStackNode *next; PriorityStackNode *prev; tmp = new PriorityStackNode( data, priority ); if ( !tmp ) { assert( NULL ); gi.error( "PriorityStack::Push : Out of memory" ); } if ( !head || ( priority >= head->priority ) ) { tmp->next = head; head = tmp; } else { for( prev = head, next = head->next; next; prev = next, next = next->next ) { if ( priority >= next->priority ) { break; } } tmp->next = prev->next; prev->next = tmp; } } template inline Type PriorityStack::Pop ( void ) { Type ret; PriorityStackNode *node; if ( !head ) { return NULL; } node = head; ret = node->data; head = node->next; delete node; return ret; } #endif /* prioritystack.h */