From: Dejan L. <dlo...@us...> - 2004-06-06 15:57:47
|
Update of /cvsroot/rtk/rtk/rtk In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27576/rtk Added Files: PriorityQueue.h Log Message: --- NEW FILE: PriorityQueue.h --- #ifndef _RTK_PRIORITYQUEUE_H_ #define _RTK_PRIORITYQUEUE_H_ 1 /** * * RTK * Fast and easy cross-platform GUI ToolKit. * * Copyright (C) 2001-200x RTK Development Team * * This library is free software; you can redistribute it and/or modify it * under the terms of the slightly modified (see the "EXCEPTION NOTICE" part * of RTK Library License) GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * and along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA . * * Also you should have received a copy of RTK Library License, if not please * write an e-mail to some of RTK authors (listed in file AUTHORS). * * Bug reports: bu...@rt... * Suggestions: rf...@rt... ***************************************************************************/ /** * $filename.$ext * $description * Authors (chronological order): * Dejan Lozanovic, null§rtk.cx * Contributors (chronological order): * $fname $lname, $email ***************************************************************************/ //--------------------------------------------------------------------------- #include "Export.h" #include "Mutex.h" #include "Slist.h" // for SLNode struct //--------------------------------------------------------------------------- namespace Rtk { // Single-List Element type #define QueueNode SLNode // Priority-List Element type typedef struct _PriorityNode { struct _PriorityNode *next; /// Pointer to next node QueueNode *element; /// Element to last QueueNode with given priority int priority; /// Priority of given node } PriorityNode; #define SORT_ASC false #define SORT_DES true /// S free data handler /// Called whenever element is removed from list. /// Parameter 'data' is pointer to data of removed element. typedef void (*FreeDataFunc)(void *data); /** * Queue or FIFO(first in, first out) class */ class RTK_API PriorityQueue { public: // --------- CONSTRUCTORS ---------------------------------- /** * Default constructor. * @param sort is order ascendencig or descendencing */ PriorityQueue(bool sort=SORT_ASC): _sort(sort),_count(0), _head(0), _tail(0), _free_func(0) { } // --------- DESTRUCTORS ----------------------------------- /** * Default destructor. */ ~PriorityQueue(); // --------- GET METHODS ----------------------------------- /** * Returns number of items in the list. */ int GetCount() const { return this->_count; } /** * Returns pointer to _head node in Queue. * @return QueueNode pointer to first node in Queue. */ QueueNode* GetFirst() const { return this->_head; } /** * Get free data function. */ FreeDataFunc GetFreeDataFunc() { return _free_func; } // --------- SET METHODS ----------------------------------- /** * Set function to free content of element. * Function is called whenever element is removed from list. */ void SetFreeDataFunc(FreeDataFunc func) { _free_func = func; } // --------- OTHER METHODS --------------------------------- /** * Clears the PriorityQueue. This will call 'Free Data Handler' */ void Clear(); /** * Insert new element to PriorityQueue, element would be * placed to the last position of elements with same priority * @param data Data to insert * @param priority Priority of given element(eg. the smalest * number with SORT_ASC will have the higest priority) */ bool Push(void *data, int priority); /** * Remove first element from PriorityQueue * This will call 'Free Data Handler' * @return Data assigned to removed element, or NULL on error */ void *Pop(); /** * Get number of items containing given data. * @param data Search for this data * @return Number of elements found, that contains given data. */ int Contains(void *data) const; /** * Returns TRUE if given node is _head node in Queue. * @param element QueueNode* Node that should be checked. * @return TRUE if given node is _head node in Queue, FALSE otherwize. */ bool IsFirst(const QueueNode* element) const { return ((element == this->_head) ? true : false); } /** * Returns TRUE if given node is _tail node in Queue. * @param element QueueNode* Node that should be checked. * @return TRUE if given node oElement is _tail node in Queue, FALSE otherwize. */ bool IsLast(const QueueNode* element) const { return ((element->next == 0) ? true : false); } protected: // Members int _count; /// Size of the list object (how many elements are stored inside) bool _sort; /// How priority queue is sorted QueueNode* _head; /// Pointer to _head element of the List. QueueNode* _tail; /// Pointer to _tail element of the List. PriorityNode _priority_head; /// FreeDataFunc _free_func; /// Free data function }; // Queue }; // Rtk namespace /** * $Id: PriorityQueue.h,v 1.1 2004/06/06 15:57:38 dlozanovic Exp $ ***************************************************************************/ #endif |