base-list.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2002-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_base_list_h)
00024 #define octave_base_list_h 1
00025 
00026 #include <list>
00027 
00028 template <typename elt_type>
00029 class
00030 octave_base_list
00031 {
00032 public:
00033 
00034   typedef typename std::list<elt_type>::iterator iterator;
00035   typedef typename std::list<elt_type>::const_iterator const_iterator;
00036 
00037   bool empty (void) const { return lst.empty (); }
00038 
00039   size_t size (void) const { return lst.size (); }
00040   size_t length (void) const { return size (); }
00041 
00042   iterator erase (iterator pos) { return lst.erase (pos); }
00043 
00044   template <class P>
00045   void remove_if (P pred)
00046   {
00047     // We would like to simply call
00048     //
00049     //   lst.remove_if (pred);
00050     //
00051     // but the Sun Studio compiler chokes on that.
00052     //
00053     // FIXME -- this kluge should be removed at some point.
00054 
00055     iterator b = lst.begin ();
00056     iterator e = lst.end ();
00057     while (b != e)
00058       {
00059         iterator n = b;
00060         n++;
00061         if (pred (*b))
00062           lst.erase (b);
00063         b = n;
00064       }
00065   }
00066 
00067   void clear (void) { lst.clear (); }
00068 
00069   iterator begin (void) { return iterator (lst.begin ()); }
00070   const_iterator begin (void) const { return const_iterator (lst.begin ()); }
00071 
00072   iterator end (void) { return iterator (lst.end ()); }
00073   const_iterator end (void) const { return const_iterator (lst.end ()); }
00074 
00075   elt_type& front (void) { return lst.front (); }
00076   elt_type& back (void) { return lst.back (); }
00077 
00078   const elt_type& front (void) const { return lst.front (); }
00079   const elt_type& back (void) const { return lst.back (); }
00080 
00081   void push_front (const elt_type& s) { lst.push_front (s); }
00082   void push_back (const elt_type& s) { lst.push_back (s); }
00083 
00084   void pop_front (void) { lst.pop_front (); }
00085   void pop_back (void) { lst.pop_back (); }
00086 
00087   // For backward compatibility.
00088   void append (const elt_type& s) { lst.push_back (s); }
00089 
00090 protected:
00091 
00092   octave_base_list (void) : lst () { }
00093 
00094   octave_base_list (const std::list<elt_type>& l) : lst (l) { }
00095 
00096   octave_base_list (const octave_base_list& bl) : lst (bl.lst) { }
00097 
00098   octave_base_list& operator = (const octave_base_list& bl)
00099     {
00100       if (this != &bl)
00101         {
00102           lst = bl.lst;
00103         }
00104       return *this;
00105     }
00106 
00107   ~octave_base_list (void) { }
00108 
00109 private:
00110 
00111   std::list<elt_type> lst;
00112 };
00113 
00114 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines