Mistake on this page? Email us
m2mvector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016 ARM Limited. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  * Licensed under the Apache License, Version 2.0 (the License); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef M2M_VECTOR_H
18 #define M2M_VECTOR_H
19 
22 namespace m2m
23 {
24 
25 template <typename ObjectTemplate>
26 
29 class Vector
30 {
31  public:
32  explicit Vector( int init_size = MIN_CAPACITY)
33  : _size(0),
34  _capacity((init_size >= MIN_CAPACITY) ? init_size : MIN_CAPACITY) {
35  _object_template = new ObjectTemplate[ _capacity ];
36  }
37 
38  Vector(const Vector & rhs ): _object_template(0) {
39  operator=(rhs);
40  }
41 
42  ~Vector() {
43  delete [] _object_template;
44  }
45 
46  const Vector & operator=(const Vector & rhs) {
47  if(this != &rhs) {
48  delete[] _object_template;
49  _size = rhs.size();
50  _capacity = rhs._capacity;
51 
52  _object_template = new ObjectTemplate[capacity()];
53  for(int k = 0; k < size(); k++) {
54  _object_template[k] = rhs._object_template[k];
55  }
56  }
57  return *this;
58  }
59 
60  void resize(int new_size) {
61  if(new_size > _capacity) {
62  reserve(new_size * 2 + 1);
63  }
64  _size = new_size;
65  }
66 
67  void reserve(int new_capacity) {
68  if(new_capacity < _size) {
69  return;
70  }
71  ObjectTemplate *old_array = _object_template;
72 
73  _object_template = new ObjectTemplate[new_capacity];
74  for(int k = 0; k < _size; k++) {
75  _object_template[k] = old_array[k];
76  }
77  _capacity = new_capacity;
78  delete [] old_array;
79  }
80 
81  ObjectTemplate & operator[](int idx) {
82  return _object_template[idx];
83  }
84 
85  const ObjectTemplate& operator[](int idx) const {
86  return _object_template[idx];
87  }
88 
89  bool empty() const{
90  return size() == 0;
91  }
92 
93  int size() const {
94  return _size;
95  }
96 
97  int capacity() const {
98  return _capacity;
99  }
100 
101  void push_back(const ObjectTemplate& x) {
102  if(_size == _capacity) {
103  reserve(2 * _capacity + 1);
104  }
105  _object_template[_size] = x;
106  _size++;
107  }
108 
109  void pop_back() {
110  _size--;
111  }
112 
113  void clear() {
114  _size = 0;
115  }
116 
117  const ObjectTemplate& back() const {
118  return _object_template[_size - 1];
119  }
120 
121  typedef ObjectTemplate* iterator;
122  typedef const ObjectTemplate* const_iterator;
123 
124  iterator begin() {
125  return &_object_template[0];
126  }
127 
128  const_iterator begin() const {
129  return &_object_template[0];
130  }
131 
132  iterator end() {
133  return &_object_template[_size];
134  }
135 
136  const_iterator end() const {
137  return &_object_template[_size];
138  }
139 
140  void erase(int position) {
141  if(position < _size) {
142  for(int k = position; k + 1 < _size; k++) {
143  _object_template[k] = _object_template[k + 1];
144  }
145  _size--;
146  }
147  }
148 
149  enum {
150  MIN_CAPACITY = 1
151  };
152 
153  private:
154  int _size;
155  int _capacity;
156  ObjectTemplate* _object_template;
157 };
158 
159 } // namespace
160 
161 #endif // M2M_VECTOR_H
A simple C++ Vector class, used as replacement for std::vector.
Definition: m2mvector.h:29
Namespace defined as replace for components defined under std namespace.
Definition: m2mstring.h:24