csutil/fifo.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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::Container::ArrayAllocDefault,
    00035   class CapacityHandler = csArrayCapacityFixedGrow<16> >
    00036 class csFIFO
    00037 {
    00038 public:
    00039   typedef csFIFO<T, ElementHandler, MemoryAllocator> ThisType;
    00040   typedef T ValueType;
    00041   typedef ElementHandler ElementHandlerType;
    00042   typedef MemoryAllocator AllocatorType;
    00043
    00044 private:
    00045   csArray<T, ElementHandler, MemoryAllocator, CapacityHandler> a1, a2;
    00046 public:
    00051   csFIFO (size_t icapacity = 0,
    00052     const CapacityHandler& ch = CapacityHandler())
    00053     :  a1 (icapacity, ch), a2 (icapacity, ch) { }
    00054
    00058   T PopTop ()
    00059   {
    00060     CS_ASSERT ((a1.GetSize () > 0) || (a2.GetSize () > 0));
    00061     if (a2.GetSize () == 0)
    00062     {
    00063       size_t n = a1.GetSize ();
    00064       while (n-- > 0)
    00065       {
    00066         a2.Push (a1[n]);
    00067       }
    00068       a1.Empty ();
    00069     }
    00070     return a2.Pop ();
    00071   }
    00072
    00076   T& Top ()
    00077   {
    00078     CS_ASSERT ((a1.GetSize () > 0) || (a2.GetSize () > 0));
    00079
    00080     if (a2.GetSize () == 0)
    00081     {
    00082       size_t n = a1.GetSize ();
    00083       while (n-- > 0)
    00084       {
    00085         a2.Push (a1[n]);
    00086       }
    00087       a1.Empty ();
    00088     }
    00089     return a2.Top ();
    00090   }
    00091
    00095   T PopBottom ()
    00096   {
    00097     CS_ASSERT ((a1.GetSize () > 0) || (a2.GetSize () > 0));
    00098
    00099     if(a1.GetSize () > 0)
    00100     {
    00101       return a1.Pop ();
    00102     }
    00103     else
    00104     {
    00105       T tmp = a2[0];
    00106       a2.DeleteIndex (0);
    00107       return tmp;
    00108     }
    00109   }
    00110
    00114   T& Bottom ()
    00115   {
    00116     CS_ASSERT ((a1.GetSize () > 0) || (a2.GetSize () > 0));
    00117
    00118     if(a1.GetSize () > 0)
    00119     {
    00120       return a1.Top ();
    00121     }
    00122     else
    00123     {
    00124       T tmp = a2[0];
    00125       return tmp;
    00126     }
    00127   }
    00128
    00132   void Push (T const& what)
    00133   {
    00134     a1.Push (what);
    00135   }
    00136
    00138   size_t GetSize() const
    00139   {
    00140     return a1.GetSize() + a2.GetSize();
    00141   }
    00142
    00147   CS_DEPRECATED_METHOD_MSG("Use GetSize() instead.")
    00148   size_t Length() const
    00149   {
    00150     return GetSize();
    00151   }
    00152
    00157   bool Delete (T const& what)
    00158   {
    00159     return (a1.Delete (what) || a2.Delete (what));
    00160   }
    00161
    00166   bool Contains (T const& what)
    00167   {
    00168     return ((a1.Find (what) != csArrayItemNotFound)
    00169       || (a2.Find (what) != csArrayItemNotFound));
    00170   }
    00171
    00173   void DeleteAll ()
    00174   {
    00175     a1.DeleteAll();
    00176     a2.DeleteAll();
    00177   }
    00178 };
    00179
    00180 #endif // __CS_CSUTIL_FIFO_H__