staticstack.h

Go to the documentation of this file.
00001 /** @file rutz/staticstack.h STL-style class for push/pop stacks with
00002     a fixed maximum size */
00003 
00004 ///////////////////////////////////////////////////////////////////////
00005 //
00006 // Copyright (c) 1999-2004 California Institute of Technology
00007 // Copyright (c) 2004-2007 University of Southern California
00008 // Rob Peters <rjpeters at usc dot edu>
00009 //
00010 // created: Wed Oct 13 16:34:11 2004
00011 // commit: $Id: staticstack.h 8249 2007-04-12 06:03:40Z rjpeters $
00012 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/staticstack.h $
00013 //
00014 // --------------------------------------------------------------------
00015 //
00016 // This file is part of GroovX.
00017 //   [http://ilab.usc.edu/rjpeters/groovx/]
00018 //
00019 // GroovX is free software; you can redistribute it and/or modify it
00020 // under the terms of the GNU General Public License as published by
00021 // the Free Software Foundation; either version 2 of the License, or
00022 // (at your option) any later version.
00023 //
00024 // GroovX is distributed in the hope that it will be useful, but
00025 // WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License
00030 // along with GroovX; if not, write to the Free Software Foundation,
00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00032 //
00033 ///////////////////////////////////////////////////////////////////////
00034 
00035 #ifndef GROOVX_RUTZ_STATICSTACK_H_UTC20050626084019_DEFINED
00036 #define GROOVX_RUTZ_STATICSTACK_H_UTC20050626084019_DEFINED
00037 
00038 #include "rutz/abort.h"
00039 
00040 namespace rutz
00041 {
00042   /// STL-style class for fixed-size stacks whose size is known at compile time.
00043   /** Because this class doesn't rely on dynamically-allocated memory,
00044       it can be both highly efficient and exception-safe. */
00045   template <typename T, unsigned int N>
00046   class static_stack
00047   {
00048   public:
00049     static_stack() throw() : vec(), sz(0) {}
00050 
00051     static_stack(const static_stack& other) throw() :
00052       vec(), sz(0)
00053     {
00054       *this = other;
00055     }
00056 
00057     static_stack& operator=(const static_stack& other) throw()
00058     {
00059       sz = other.sz;
00060 
00061       for (unsigned int i = 0; i < sz; ++i)
00062         {
00063           vec[i] = other.vec[i];
00064         }
00065 
00066       return *this;
00067     }
00068 
00069     unsigned int size() const throw() { return sz; }
00070 
00071     static unsigned int capacity() throw() { return N; }
00072 
00073     bool push(T p) throw()
00074     {
00075       if (sz >= N)
00076         return false;
00077 
00078       vec[sz++] = p;
00079       return true;
00080     }
00081 
00082     void pop() throw()
00083     {
00084       if (sz == 0)
00085         GVX_ABORT("underflow in static_stack::pop");
00086 
00087       --sz;
00088     }
00089 
00090     T top() const throw()
00091     {
00092       return (sz > 0) ? vec[sz-1] : 0;
00093     }
00094 
00095     T at(unsigned int i) const throw()
00096     {
00097       return (i < sz) ? vec[i] : 0;
00098     }
00099 
00100     T operator[](unsigned int i) const throw() { return at(i); }
00101 
00102     typedef       T*       iterator;
00103     typedef const T* const_iterator;
00104 
00105     iterator begin() throw() { return &vec[0]; }
00106     iterator end()   throw() { return &vec[0] + sz; }
00107 
00108     const_iterator begin() const throw() { return &vec[0]; }
00109     const_iterator end()   const throw() { return &vec[0] + sz; }
00110 
00111   private:
00112     T vec[N];
00113     unsigned int sz;
00114   };
00115 }
00116 
00117 static const char __attribute__((used)) vcid_groovx_rutz_staticstack_h_utc20050626084019[] = "$Id: staticstack.h 8249 2007-04-12 06:03:40Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/staticstack.h $";
00118 #endif // !GROOVX_RUTZ_STATICSTACK_H_UTC20050626084019_DEFINED
Generated on Sun May 8 08:42:13 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3