1 /*************************************************************************** 2 3 copyright : (C) 2006 by mean 4 email : fixounet@free.fr 5 ***************************************************************************/ 6 7 /*************************************************************************** 8 * * 9 * This program is free software; you can redistribute it and/or modify * 10 * it under the terms of the GNU General Public License as published by * 11 * the Free Software Foundation; either version 2 of the License, or * 12 * (at your option) any later version. * 13 * * 14 ***************************************************************************/ 15 #ifndef ADM_PTRQUEUE_H 16 #define ADM_PTRQUEUE_H 17 18 template <class T> 19 class ADM_ptrQueue 20 { 21 22 typedef struct queueElem 23 { 24 queueElem *next; 25 void *data; 26 }queueElem; 27 28 protected: 29 queueElem *head; 30 queueElem *tail; 31 public: ADM_ptrQueue()32 ADM_ptrQueue() 33 { 34 head=NULL; 35 tail=NULL; 36 } ~ADM_ptrQueue()37 ~ADM_ptrQueue() 38 { 39 if(head) 40 { 41 ADM_warning(">>>>>>> Warning queue is not empty <<<<<<<\n"); 42 } 43 } isEmpty(void)44 bool isEmpty(void) 45 { 46 if(head) return 0; 47 ADM_assert(!tail); 48 return 1; 49 } clear()50 bool clear() 51 { 52 queueElem *elem=head; 53 while(elem) 54 { 55 queueElem *c=elem; 56 elem=elem->next; 57 delete c; 58 } 59 head=tail=NULL; 60 return true; 61 } push(T * data)62 bool push(T *data) 63 { 64 queueElem *elem=new queueElem; 65 66 elem->next=NULL; 67 elem->data=data; 68 if(!head) 69 { 70 head=tail=elem; 71 return 1; 72 } 73 ADM_assert(tail); 74 tail->next=elem; 75 tail=elem; 76 return 1; 77 } pushBack(T * data)78 bool pushBack(T *data) 79 { 80 queueElem *elem=new queueElem; 81 elem->next=NULL; 82 elem->data=data; 83 84 if(tail) 85 { 86 tail->next=elem; 87 tail=elem; 88 }else 89 { 90 head=tail=elem; 91 } 92 return true; 93 } pop()94 T *pop() 95 { 96 T* r=NULL; 97 ADM_assert(head); 98 if(isEmpty()) return NULL; 99 r=(T*)head->data; 100 queueElem *tmp=head; 101 head=head->next; 102 if(!head) 103 { 104 head=tail=NULL; 105 } 106 delete tmp; 107 return r; 108 } popBack()109 T *popBack() 110 { 111 queueElem *h; 112 T *r; 113 if(isEmpty()) return NULL; 114 ADM_assert(head); 115 ADM_assert(tail); 116 117 r=(T *)tail->data; 118 119 if(head==tail) // only one element.. 120 { 121 delete tail; 122 head=tail=NULL; 123 return r; 124 } 125 126 h=head; 127 while(h->next!=tail) 128 { 129 h=h->next; 130 ADM_assert(h); 131 } 132 // h is now the one before the last 133 h->next=NULL; 134 delete tail; 135 tail=h; 136 return r; 137 } 138 139 }; 140 141 142 143 #endif 144