00001 /*MT* 00002 00003 MediaTomb - http://www.mediatomb.cc/ 00004 00005 base_queue.h - this file is part of MediaTomb. 00006 00007 Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>, 00008 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc> 00009 00010 Copyright (C) 2006-2010 Gena Batyan <bgeradz@mediatomb.cc>, 00011 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>, 00012 Leonhard Wimmer <leo@mediatomb.cc> 00013 00014 MediaTomb is free software; you can redistribute it and/or modify 00015 it under the terms of the GNU General Public License version 2 00016 as published by the Free Software Foundation. 00017 00018 MediaTomb is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 GNU General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 version 2 along with MediaTomb; if not, write to the Free Software 00025 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00026 00027 $Id: base_queue.h 2081 2010-03-23 20:18:00Z lww $ 00028 */ 00029 00031 00032 #ifndef __ZMMF_BASE_QUEUE_H__ 00033 #define __ZMMF_BASE_QUEUE_H__ 00034 00035 #include "zmm/zmm.h" 00036 00037 namespace zmm 00038 { 00040 template <typename T> 00041 class BaseQueue : public Object 00042 { 00043 public: 00044 BaseQueue(int initialCapacity, T emptyType) : Object() 00045 { 00046 capacity = initialCapacity; 00047 this->emptyType = emptyType; 00048 queueEnd = 0; 00049 queueBegin = 0; 00050 overlap = false; 00051 data = (T *)MALLOC(capacity * sizeof(T)); 00052 } 00053 00054 ~BaseQueue() 00055 { 00056 FREE(this->data); 00057 } 00058 00059 inline int getCapacity() 00060 { 00061 return capacity; 00062 } 00063 00064 int size() 00065 { 00066 if (overlap) return capacity; 00067 if (queueBegin <= queueEnd) 00068 return queueEnd - queueBegin; 00069 return capacity - queueBegin + queueEnd; 00070 } 00071 00072 inline bool isEmpty() 00073 { 00074 return (! overlap && queueEnd == queueBegin); 00075 } 00076 00077 void resize(int requiredSize) 00078 { 00079 if(requiredSize > capacity) 00080 { 00081 int count = size(); 00082 int oldCapacity = capacity; 00083 capacity = count + (count / 2); 00084 if(requiredSize > capacity) 00085 capacity = requiredSize; 00086 data = (T *)REALLOC(data, capacity * sizeof(T)); 00087 log_debug("resizing %d -> %d\n", oldCapacity, capacity); 00088 if ((overlap && (queueEnd != 0)) || queueBegin > queueEnd) 00089 { 00090 int moveAmount = oldCapacity - queueBegin; 00091 int newQueueBegin = capacity - moveAmount; 00092 memmove(data + newQueueBegin, data + queueBegin, moveAmount * sizeof(T)); 00093 queueBegin = newQueueBegin; 00094 } 00095 else if (queueEnd == 0) 00096 queueEnd = oldCapacity; 00097 overlap = false; 00098 } 00099 } 00100 00101 inline void enqueue(T element) 00102 { 00103 if (overlap) 00104 resize(size() + 1); 00105 data[queueEnd++] = element; 00106 if (queueEnd == capacity) 00107 queueEnd = 0; 00108 if (queueEnd == queueBegin) 00109 overlap = true; 00110 } 00111 00112 inline T dequeue() 00113 { 00114 if (isEmpty()) 00115 return emptyType; 00116 T ret = data[queueBegin++]; 00117 overlap = false; 00118 if (queueBegin == capacity) 00119 queueBegin = 0; 00120 return ret; 00121 } 00122 00123 T get(int index) 00124 { 00125 return data[ (queueBegin + index) % capacity ]; 00126 } 00127 00128 protected: 00129 T *data; 00130 T emptyType; 00131 int capacity; 00132 int queueBegin; 00133 int queueEnd; 00134 bool overlap; 00135 }; 00136 } 00137 00138 #endif // __ZMMF_BASE_QUEUE_H__
1.6.1