pod_vector.h
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2006-2007, Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 
00021 #ifndef CLASP_POD_VECTOR_H_INCLUDED
00022 #define CLASP_POD_VECTOR_H_INCLUDED
00023 #include <clasp/util/pod_vector.h>
00024 #include <vector>
00025 
00026 namespace Clasp {
00027 
00028 #ifdef _DEBUG
00029         template <class Type>
00030         struct PodVector {
00031                 typedef std::vector<Type> type;
00032                 static void destruct(type& t) {t.clear();}
00033         };
00034 #else
00035         template <class Type>
00036         struct PodVector {
00037                 typedef bk_lib::pod_vector<Type> type;
00038                 static void destruct(type& t) {
00039                         for (typename type::size_type i = 0, end = t.size(); i != end; ++i) {
00040                                 t[i].~Type();
00041                         }
00042                         t.clear();
00043                 }
00044         };
00045 #endif
00046 
00047 template <class T>
00048 inline void releaseVec(T& t) {
00049         T().swap(t);
00050 }
00051 
00052 template <class T>
00053 inline void shrinkVecTo(T& t, typename T::size_type j) {
00054         t.erase(t.begin()+j, t.end());
00055 }
00056 
00057 template <class T>
00058 inline void growVecTo(T& vec, typename T::size_type j, const typename T::value_type& val = typename T::value_type()) {
00059         if (vec.size() < j) {
00060                 if (vec.capacity() < j) { vec.reserve(j + j / 2); }
00061                 vec.resize(j, val);
00062         }
00063 }
00064 
00065 template <class T>
00066 void moveDown(T& t, typename T::size_type from, typename T::size_type to) {
00067         for (typename T::size_type end = t.size(); from != end;) {
00068                 t[to++] = t[from++];
00069         }
00070         shrinkVecTo(t, to);
00071 }
00072 
00073 template <class T>
00074 struct PodQueue {
00075         typedef typename PodVector<T>::type  vec_type;
00076         typedef typename vec_type::size_type size_type;
00077         PodQueue() : qFront(0) {}
00078         bool      empty() const   { return qFront == vec.size(); }
00079         size_type size()  const   { return vec.size() - qFront; }
00080         const T&  front() const   { return vec[qFront]; }
00081         const T&  back()  const   { return vec.back(); }
00082         T&        front()         { return vec[qFront]; }
00083         T&        back()          { return vec.back(); }
00084         void      push(const T& x){ vec.push_back(x);  }
00085         void      pop()           { ++qFront; }
00086         T         pop_ret()       { return vec[qFront++]; }
00087         void      clear()         { vec.clear(); qFront = 0; }
00088         void      rewind()        { qFront = 0; }
00089         vec_type  vec;    // the underlying vector holding the items
00090         size_type qFront; // front position
00091 };
00092 
00093 }
00094 
00095 #endif


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:39