/usr/include/dune/common/reservedvector.hh is in libdune-common-dev 2.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef RESERVEDVECTOR_HH
#define RESERVEDVECTOR_HH
/** \file
* \brief An stl-compliant random-access container which stores everything on the stack
*/
#include <iostream>
#include <dune/common/genericiterator.hh>
#ifdef CHECK_RESERVEDVECTOR
#define CHECKSIZE(X) assert(X)
#else
#define CHECKSIZE(X) {}
#endif
namespace Dune
{
/**
\brief A Vector class with statically reserved memory.
ReservedVector is something between Dune::array and std::vector.
You have vector which can be extended and shrunk using methods like
push_back and pop_back, but reserved memory is predefined.
This implies that the vector can not grow bigger than the predefined
maximum size.
\tparam T The data type ReservedVector stores.
\tparam n The maximum number of objects the ReservedVector can store.
*/
template<class T, int n>
class ReservedVector
{
public:
/** @{ Typedefs */
//! The type of object, T, stored in the vector.
typedef T value_type;
//! Pointer to T.
typedef T* pointer;
//! Reference to T
typedef T& reference;
//! Const reference to T
typedef const T& const_reference;
//! An unsigned integral type.
typedef size_t size_type;
//! A signed integral type.
typedef std::ptrdiff_t difference_type;
//! Iterator used to iterate through a vector.
typedef Dune::GenericIterator<ReservedVector, value_type> iterator;
//! Const iterator used to iterate through a vector.
typedef Dune::GenericIterator<const ReservedVector, const value_type> const_iterator;
/** @} */
/** @{ Constructors */
//! Constructor
ReservedVector() : sz(0) {}
/** @} */
/** @{ Data access operations */
//! Erases all elements.
void clear()
{
sz = 0;
}
//! Specifies a new size for the vector.
void resize(size_t s)
{
CHECKSIZE(s<=n);
sz = s;
}
//! Appends an element to the end of a vector, up to the maximum size n, O(1) time.
void push_back(const T& t)
{
CHECKSIZE(sz<n);
data[sz++] = t;
}
//! Erases the last element of the vector, O(1) time.
void pop_back()
{
if (! empty()) sz--;
}
//! Returns a iterator pointing to the beginning of the vector.
iterator begin(){
return iterator(*this, 0);
}
//! Returns a const_iterator pointing to the beginning of the vector.
const_iterator begin() const {
return const_iterator(*this, 0);
}
//! Returns an iterator pointing to the end of the vector.
iterator end(){
return iterator(*this, sz);
}
//! Returns a const_iterator pointing to the end of the vector.
const_iterator end() const {
return const_iterator(*this, sz);
}
//! Returns reference to the i'th element.
reference operator[] (size_type i)
{
CHECKSIZE(sz>i);
return data[i];
}
//! Returns a const reference to the i'th element.
const_reference operator[] (size_type i) const
{
CHECKSIZE(sz>i);
return data[i];
}
//! Returns reference to first element of vector.
reference front()
{
CHECKSIZE(sz>0);
return data[0];
}
//! Returns const reference to first element of vector.
const_reference front() const
{
CHECKSIZE(sz>0);
return data[0];
}
//! Returns reference to last element of vector.
reference back()
{
CHECKSIZE(sz>0);
return data[sz-1];
}
//! Returns const reference to last element of vector.
const_reference back() const
{
CHECKSIZE(sz>0);
return data[sz-1];
}
/** @} */
/** @{ Informative Methods */
//! Returns number of elements in the vector.
size_type size () const
{
return sz;
}
//! Returns true if vector has no elements.
bool empty() const
{
return sz==0;
}
//! Returns current capacity (allocated memory) of the vector.
static size_type capacity()
{
return n;
}
//! Returns the maximum length of the vector.
static size_type max_size()
{
return n;
}
/** @} */
//! Send ReservedVector to an output stream
friend std::ostream& operator<< (std::ostream& s, const ReservedVector& v)
{
for (size_t i=0; i<v.size(); i++)
s << v[i] << " ";
return s;
}
private:
T data[n];
size_type sz;
};
}
#undef CHECKSIZE
#endif // RESERVEDVECTOR_HH
|