1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 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
7 ///////////////////////////////////////////////////////////////////////////////
10 #define TITANVECTOR_H_
17 // there's no point in including Error.hh and all the includes that come with it
18 // when building the profiler merge tool, just use this simple error function
21 void TTCN_error(const char *fmt, ...)
24 va_start(parameters, fmt);
25 vfprintf(stderr, fmt, parameters);
40 void copy(const Vector<T>&);
42 explicit Vector(size_t p_capacity = 4);
44 explicit Vector(const Vector<T>& other);
47 Vector<T>& operator=(const Vector<T>& rhs);
50 size_t size() const { return nof_elem; }
51 void resize(size_t new_size, T element = T());
52 size_t capacity() const { return cap; }
53 bool empty() const { return nof_elem == 0; }
54 void reserve(size_t n);
58 T& operator[](size_t idx);
59 const T& operator[](size_t idx) const;
61 const T& at(size_t idx) const;
62 T& front() { return at(0); }
63 const T& front() const { return at(0); }
64 T& back() { return at(nof_elem - 1); }
65 const T& back() const { return at(nof_elem - 1); }
66 // This could be used better with iterators
67 void erase_at(size_t idx);
70 void push_back(const T& element);
76 Vector<T>::Vector(size_t p_capacity)
77 : cap(p_capacity), nof_elem(0) {
81 TTCN_error("Internal error: new returned NULL");
86 Vector<T>::Vector(const Vector<T>& other) {
91 Vector<T>& Vector<T>::operator=(const Vector<T>& rhs) {
105 void Vector<T>::copy(const Vector<T>& other) {
109 TTCN_error("Internal error: new returned NULL");
112 for (size_t i = 0; i < other.nof_elem; ++i) {
113 data[i] = other.data[i];
116 nof_elem = other.nof_elem;
120 Vector<T>::~Vector() {
126 void Vector<T>::resize(size_t new_size, T element) {
127 if (new_size > nof_elem) {
129 while (nof_elem < new_size) {
130 data[nof_elem++] = element;
139 void Vector<T>::reserve(size_t new_size) {
140 if (new_size <= cap) {
145 T* data_tmp = new T[cap];
147 TTCN_error("Internal error: new returned NULL");
149 for (size_t i = 0; i < nof_elem; ++i) {
150 data_tmp[i] = data[i];
159 void Vector<T>::push_back(const T& element) {
160 if (nof_elem == cap) {
161 size_t new_cap = (cap == 0 ? 4 : (cap * 2));
165 data[nof_elem++] = element;
169 const T& Vector<T>::at(size_t idx) const {
170 if (idx >= nof_elem) {
171 TTCN_error("Internal error: Vector over-indexing.");
178 T& Vector<T>::at(size_t idx) {
179 if (idx >= nof_elem) {
180 TTCN_error("Internal error: Vector over-indexing.");
187 const T& Vector<T>::operator[](size_t idx) const {
192 T& Vector<T>::operator[](size_t idx) {
197 void Vector<T>::erase_at(size_t idx) {
198 if (idx >= nof_elem) {
199 TTCN_error("Internal error: Vector over-indexing.");
202 while (idx < nof_elem - 1) {
203 data[idx] = data[idx + 1];
211 void Vector<T>::shrink_to_fit() {
212 if (nof_elem == cap) {
217 T* data_tmp = new T[nof_elem];
219 TTCN_error("Internal error: new returned NULL");
221 for (size_t i = 0; i < nof_elem; ++i) {
222 data_tmp[i] = data[i];
229 void Vector<T>::clear() {
233 #endif /* TITANVECTOR_H_ */