csutil/fifo.h
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2005 by Jorrit Tyberghein 00003 (C) 2005 by Frank Richter 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00020 #ifndef __CS_CSUTIL_FIFO_H__ 00021 #define __CS_CSUTIL_FIFO_H__ 00022 00027 #include "csutil/array.h" 00028 00033 template <class T, class ElementHandler = csArrayElementHandler<T>, 00034 class MemoryAllocator = CS::Memory::AllocatorMalloc> 00035 class csFIFO 00036 { 00037 public: 00038 typedef csFIFO<T, ElementHandler, MemoryAllocator> ThisType; 00039 typedef T ValueType; 00040 typedef ElementHandler ElementHandlerType; 00041 typedef MemoryAllocator AllocatorType; 00042 00043 private: 00044 csArray<T, ElementHandler, MemoryAllocator> a1, a2; 00045 public: 00050 csFIFO (size_t icapacity = 0, size_t ithreshold = 0) 00051 : a1 (icapacity, ithreshold), a2 (icapacity, ithreshold) { } 00052 00056 T PopTop () 00057 { 00058 CS_ASSERT ((a1.GetSize () > 0) || (a2.GetSize () > 0)); 00059 if (a2.GetSize () == 0) 00060 { 00061 size_t n = a1.GetSize (); 00062 while (n-- > 0) 00063 { 00064 a2.Push (a1[n]); 00065 } 00066 a1.Empty (); 00067 } 00068 return a2.Pop (); 00069 } 00070 00074 T& Top () 00075 { 00076 CS_ASSERT ((a1.GetSize () > 0) || (a2.GetSize () > 0)); 00077 00078 if (a2.GetSize () == 0) 00079 { 00080 size_t n = a1.GetSize (); 00081 while (n-- > 0) 00082 { 00083 a2.Push (a1[n]); 00084 } 00085 a1.Empty (); 00086 } 00087 return a2.Top (); 00088 } 00089 00093 void Push (T const& what) 00094 { 00095 a1.Push (what); 00096 } 00097 00099 size_t GetSize() 00100 { 00101 return a1.GetSize() + a2.GetSize(); 00102 } 00103 00108 CS_DEPRECATED_METHOD_MSG("Use GetSize() instead.") 00109 size_t Length() 00110 { 00111 return GetSize(); 00112 } 00113 00118 bool Delete (T const& what) 00119 { 00120 return (a1.Delete (what) || a2.Delete (what)); 00121 } 00122 00127 bool Contains (T const& what) 00128 { 00129 return ((a1.Find (what) != csArrayItemNotFound) 00130 || (a2.Find (what) != csArrayItemNotFound)); 00131 } 00132 00134 void DeleteAll () 00135 { 00136 a1.DeleteAll(); 00137 a2.DeleteAll(); 00138 } 00139 }; 00140 00141 #endif // __CS_CSUTIL_FIFO_H__
Generated for Crystal Space 1.4.1 by doxygen 1.7.1