1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
15 ******************************************************************************/
17 #ifndef TITANVECTOR_H_
18 #define TITANVECTOR_H_
25 // there's no point in including Error.hh and all the includes that come with it
26 // when building the profiler merge tool, just use this simple error function
29 void TTCN_error(const char *fmt, ...)
32 va_start(parameters, fmt);
33 vfprintf(stderr, fmt, parameters);
48 void copy(const Vector<T>&);
50 explicit Vector(size_t p_capacity = 4);
52 explicit Vector(const Vector<T>& other);
55 Vector<T>& operator=(const Vector<T>& rhs);
58 size_t size() const { return nof_elem; }
59 void resize(size_t new_size, T element = T());
60 size_t capacity() const { return cap; }
61 bool empty() const { return nof_elem == 0; }
62 void reserve(size_t n);
66 T& operator[](size_t idx);
67 const T& operator[](size_t idx) const;
69 const T& at(size_t idx) const;
70 T& front() { return at(0); }
71 const T& front() const { return at(0); }
72 T& back() { return at(nof_elem - 1); }
73 const T& back() const { return at(nof_elem - 1); }
74 // This could be used better with iterators
75 void erase_at(size_t idx);
78 void push_back(const T& element);
84 Vector<T>::Vector(size_t p_capacity)
85 : cap(p_capacity), nof_elem(0) {
89 TTCN_error("Internal error: new returned NULL");
94 Vector<T>::Vector(const Vector<T>& other) {
99 Vector<T>& Vector<T>::operator=(const Vector<T>& rhs) {
113 void Vector<T>::copy(const Vector<T>& other) {
117 TTCN_error("Internal error: new returned NULL");
120 for (size_t i = 0; i < other.nof_elem; ++i) {
121 data[i] = other.data[i];
124 nof_elem = other.nof_elem;
128 Vector<T>::~Vector() {
134 void Vector<T>::resize(size_t new_size, T element) {
135 if (new_size > nof_elem) {
137 while (nof_elem < new_size) {
138 data[nof_elem++] = element;
147 void Vector<T>::reserve(size_t new_size) {
148 if (new_size <= cap) {
153 T* data_tmp = new T[cap];
155 TTCN_error("Internal error: new returned NULL");
157 for (size_t i = 0; i < nof_elem; ++i) {
158 data_tmp[i] = data[i];
167 void Vector<T>::push_back(const T& element) {
168 if (nof_elem == cap) {
169 size_t new_cap = (cap == 0 ? 4 : (cap * 2));
173 data[nof_elem++] = element;
177 const T& Vector<T>::at(size_t idx) const {
178 if (idx >= nof_elem) {
179 TTCN_error("Internal error: Vector over-indexing.");
186 T& Vector<T>::at(size_t idx) {
187 if (idx >= nof_elem) {
188 TTCN_error("Internal error: Vector over-indexing.");
195 const T& Vector<T>::operator[](size_t idx) const {
200 T& Vector<T>::operator[](size_t idx) {
205 void Vector<T>::erase_at(size_t idx) {
206 if (idx >= nof_elem) {
207 TTCN_error("Internal error: Vector over-indexing.");
210 while (idx < nof_elem - 1) {
211 data[idx] = data[idx + 1];
219 void Vector<T>::shrink_to_fit() {
220 if (nof_elem == cap) {
225 T* data_tmp = new T[nof_elem];
227 TTCN_error("Internal error: new returned NULL");
229 for (size_t i = 0; i < nof_elem; ++i) {
230 data_tmp[i] = data[i];
237 void Vector<T>::clear() {
241 #endif /* TITANVECTOR_H_ */