//----------------------------------------------------------------------------- // // $Logfile:: /Quake 2 Engine/Sin/code/game/prioritystack.h $ // $Revision:: 2 $ // $Author:: Jimdose $ // $Date:: 6/09/98 4:26p $ // // 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:: /Quake 2 Engine/Sin/code/game/prioritystack.h $ // // 2 6/09/98 4:26p Jimdose // created file // // 1 6/09/98 4:26p Jimdose // // 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 EXPORT_FROM_DLL 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 EXPORT_FROM_DLL PriorityStack::Clear ( void ) { while( !Empty() ) { Pop(); } } template inline qboolean EXPORT_FROM_DLL PriorityStack::Empty ( void ) { if ( head == NULL ) { return true; } return false; } template inline void EXPORT_FROM_DLL 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 EXPORT_FROM_DLL 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 */