clingo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
pod_vector.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2007, Benjamin Kaufmann
3 //
4 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/
5 //
6 // Clasp is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // Clasp is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Clasp; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 //
20 
21 #ifndef CLASP_POD_VECTOR_H_INCLUDED
22 #define CLASP_POD_VECTOR_H_INCLUDED
23 #include <clasp/util/pod_vector.h>
24 #include <vector>
25 
26 namespace Clasp {
27 
28 #ifdef _DEBUG
29  template <class Type>
30  struct PodVector {
31  typedef std::vector<Type> type;
32  static void destruct(type& t) {t.clear();}
33  };
34 #else
35  template <class Type>
36  struct PodVector {
38  static void destruct(type& t) {
39  for (typename type::size_type i = 0, end = t.size(); i != end; ++i) {
40  t[i].~Type();
41  }
42  t.clear();
43  }
44  };
45 #endif
46 
47 template <class T>
48 inline void releaseVec(T& t) {
49  T().swap(t);
50 }
51 
52 template <class T>
53 inline void shrinkVecTo(T& t, typename T::size_type j) {
54  t.erase(t.begin()+j, t.end());
55 }
56 
57 template <class T>
58 inline void growVecTo(T& vec, typename T::size_type j, const typename T::value_type& val = typename T::value_type()) {
59  if (vec.size() < j) {
60  if (vec.capacity() < j) { vec.reserve(j + j / 2); }
61  vec.resize(j, val);
62  }
63 }
64 
65 template <class T>
66 void moveDown(T& t, typename T::size_type from, typename T::size_type to) {
67  for (typename T::size_type end = t.size(); from != end;) {
68  t[to++] = t[from++];
69  }
70  shrinkVecTo(t, to);
71 }
72 
73 template <class T>
74 struct PodQueue {
75  typedef typename PodVector<T>::type vec_type;
76  typedef typename vec_type::size_type size_type;
77  PodQueue() : qFront(0) {}
78  bool empty() const { return qFront == vec.size(); }
79  size_type size() const { return vec.size() - qFront; }
80  const T& front() const { return vec[qFront]; }
81  const T& back() const { return vec.back(); }
82  T& front() { return vec[qFront]; }
83  T& back() { return vec.back(); }
84  void push(const T& x){ vec.push_back(x); }
85  void pop() { ++qFront; }
86  T pop_ret() { return vec[qFront++]; }
87  void clear() { vec.clear(); qFront = 0; }
88  void rewind() { qFront = 0; }
89  vec_type vec; // the underlying vector holding the items
90  size_type qFront; // front position
91 };
92 
93 }
94 
95 #endif
const T & back() const
Definition: pod_vector.h:81
void pop()
Definition: pod_vector.h:85
void push(const T &x)
Definition: pod_vector.h:84
vec_type vec
Definition: pod_vector.h:89
bk_lib::pod_vector< Type > type
Definition: pod_vector.h:37
void moveDown(T &t, typename T::size_type from, typename T::size_type to)
Definition: pod_vector.h:66
std::unique_ptr< ValTerm > val(Value v)
Definition: term_helper.hh:31
void growVecTo(T &vec, typename T::size_type j, const typename T::value_type &val=typename T::value_type())
Definition: pod_vector.h:58
T pop_ret()
Definition: pod_vector.h:86
T & front()
Definition: pod_vector.h:82
void rewind()
Definition: pod_vector.h:88
T & back()
Definition: pod_vector.h:83
void shrinkVecTo(T &t, typename T::size_type j)
Definition: pod_vector.h:53
static void destruct(type &t)
Definition: pod_vector.h:38
bool empty() const
Definition: pod_vector.h:78
vec_type::size_type size_type
Definition: pod_vector.h:76
int x
Definition: utility.cc:65
size_type size() const
Definition: pod_vector.h:79
size_type qFront
Definition: pod_vector.h:90
Definition: pod_vector.h:36
Definition: pod_vector.h:74
PodVector< T >::type vec_type
Definition: pod_vector.h:75
void clear()
Definition: pod_vector.h:87
A std::vector-replacement for POD-Types.
Definition: pod_vector.h:115
const T & front() const
Definition: pod_vector.h:80
void releaseVec(T &t)
Definition: pod_vector.h:48
PodQueue()
Definition: pod_vector.h:77
int end
Definition: literals.cc:62