/usr/include/titan/Vector.hh is in eclipse-titan 6.3.1-1build1.
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | /******************************************************************************
* Copyright (c) 2000-2017 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Balasko, Jeno
* Baranyi, Botond
* Beres, Szabolcs
* Kovacs, Ferenc
* Raduly, Csaba
*
******************************************************************************/
#ifndef TITANVECTOR_H_
#define TITANVECTOR_H_
#include "Types.h"
#include <stddef.h>
#ifndef PROF_MERGE
#include "Error.hh"
#else
// there's no point in including Error.hh and all the includes that come with it
// when building the profiler merge tool, just use this simple error function
#include <stdio.h>
#include <stdarg.h>
void TTCN_error(const char *fmt, ...)
{
va_list parameters;
va_start(parameters, fmt);
vfprintf(stderr, fmt, parameters);
va_end(parameters);
putc('\n', stderr);
fflush(stderr);
}
#endif
// Not invented here
template<typename T>
class Vector {
private:
size_t cap;
size_t nof_elem;
T* data;
void copy(const Vector<T>&);
public:
explicit Vector(size_t p_capacity = 4);
explicit Vector(const Vector<T>& other);
~Vector();
Vector<T>& operator=(const Vector<T>& rhs);
// Capacity
size_t size() const { return nof_elem; }
void resize(size_t new_size, T element = T());
size_t capacity() const { return cap; }
boolean empty() const { return nof_elem == 0; }
void reserve(size_t n);
void shrink_to_fit();
T* data_ptr() const { return data; }
// Element access
T& operator[](size_t idx);
const T& operator[](size_t idx) const;
T& at(size_t idx);
const T& at(size_t idx) const;
T& front() { return at(0); }
const T& front() const { return at(0); }
T& back() { return at(nof_elem - 1); }
const T& back() const { return at(nof_elem - 1); }
// This could be used better with iterators
void erase_at(size_t idx);
// Modifiers
void push_back(const T& element);
void pop_back();
void clear();
};
template<typename T>
Vector<T>::Vector(size_t p_capacity)
: cap(p_capacity), nof_elem(0) {
data = new T[cap];
if (!data) {
TTCN_error("Internal error: new returned NULL");
}
}
template<typename T>
Vector<T>::Vector(const Vector<T>& other) {
copy(other);
}
template<typename T>
Vector<T>& Vector<T>::operator=(const Vector<T>& rhs) {
if (this == &rhs) {
return *this;
}
clear();
delete[] data;
copy(rhs);
return *this;
}
template<typename T>
void Vector<T>::copy(const Vector<T>& other) {
cap = other.cap;
data = new T[cap];
if (!data) {
TTCN_error("Internal error: new returned NULL");
}
for (size_t i = 0; i < other.nof_elem; ++i) {
data[i] = other.data[i];
}
nof_elem = other.nof_elem;
}
template<typename T>
Vector<T>::~Vector() {
clear();
delete[] data;
}
template<typename T>
void Vector<T>::resize(size_t new_size, T element) {
if (new_size > nof_elem) {
reserve(new_size);
while (nof_elem < new_size) {
data[nof_elem++] = element;
}
return;
}
nof_elem = new_size;
}
template<typename T>
void Vector<T>::reserve(size_t new_size) {
if (new_size <= cap) {
return;
}
cap = new_size;
T* data_tmp = new T[cap];
if (!data_tmp) {
TTCN_error("Internal error: new returned NULL");
}
for (size_t i = 0; i < nof_elem; ++i) {
data_tmp[i] = data[i];
}
delete[] data;
data = data_tmp;
}
// Modifiers
template<typename T>
void Vector<T>::push_back(const T& element) {
if (nof_elem == cap) {
size_t new_cap = (cap == 0 ? 4 : (cap * 2));
reserve(new_cap);
}
data[nof_elem++] = element;
}
template<typename T>
const T& Vector<T>::at(size_t idx) const {
if (idx >= nof_elem) {
TTCN_error("Internal error: Vector over-indexing.");
}
return data[idx];
}
template<typename T>
T& Vector<T>::at(size_t idx) {
if (idx >= nof_elem) {
TTCN_error("Internal error: Vector over-indexing.");
}
return data[idx];
}
template<typename T>
const T& Vector<T>::operator[](size_t idx) const {
return at(idx);
}
template<typename T>
T& Vector<T>::operator[](size_t idx) {
return at(idx);
}
template<typename T>
void Vector<T>::erase_at(size_t idx) {
if (idx >= nof_elem) {
TTCN_error("Internal error: Vector over-indexing.");
}
while (idx < nof_elem - 1) {
data[idx] = data[idx + 1];
++idx;
}
--nof_elem;
}
template<typename T>
void Vector<T>::shrink_to_fit() {
if (nof_elem == cap) {
return;
}
cap = nof_elem;
T* data_tmp = new T[nof_elem];
if (!data_tmp) {
TTCN_error("Internal error: new returned NULL");
}
for (size_t i = 0; i < nof_elem; ++i) {
data_tmp[i] = data[i];
}
delete[] data;
data = data_tmp;
}
template<typename T>
void Vector<T>::clear() {
nof_elem = 0;
}
#endif /* TITANVECTOR_H_ */
|