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 
25 namespace m2m
26 {
27 
28 template <typename ObjectTemplate>
29 
30 class Vector
31 {
32  public:
33  explicit Vector( int init_size = MIN_CAPACITY)
34  : _size(0),
35  _capacity((init_size >= MIN_CAPACITY) ? init_size : MIN_CAPACITY) {
36  _object_template = new ObjectTemplate[ _capacity ];
37  }
38 
39  Vector(const Vector & rhs ): _object_template(0) {
40  operator=(rhs);
41  }
42 
43  ~Vector() {
44  delete [] _object_template;
45  }
46 
47  const Vector & operator=(const Vector & rhs) {
48  if(this != &rhs) {
49  delete[] _object_template;
50  _size = rhs.size();
51  _capacity = rhs._capacity;
52 
53  _object_template = new ObjectTemplate[capacity()];
54  for(int k = 0; k < size(); k++) {
55  _object_template[k] = rhs._object_template[k];
56  }
57  }
58  return *this;
59  }
60 
61  void resize(int new_size) {
62  if(new_size > _capacity) {
63  reserve(new_size * 2 + 1);
64  }
65  _size = new_size;
66  }
67 
68  void reserve(int new_capacity) {
69  if(new_capacity < _size) {
70  return;
71  }
72  ObjectTemplate *old_array = _object_template;
73 
74  _object_template = new ObjectTemplate[new_capacity];
75  for(int k = 0; k < _size; k++) {
76  _object_template[k] = old_array[k];
77  }
78  _capacity = new_capacity;
79  delete [] old_array;
80  }
81 
82  ObjectTemplate & operator[](int idx) {
83  return _object_template[idx];
84  }
85 
86  const ObjectTemplate& operator[](int idx) const {
87  return _object_template[idx];
88  }
89 
90  bool empty() const{
91  return size() == 0;
92  }
93 
94  int size() const {
95  return _size;
96  }
97 
98  int capacity() const {
99  return _capacity;
100  }
101 
102  void push_back(const ObjectTemplate& x) {
103  if(_size == _capacity) {
104  reserve(2 * _capacity + 1);
105  }
106  _object_template[_size] = x;
107  _size++;
108  }
109 
110  void pop_back() {
111  _size--;
112  }
113 
114  void clear() {
115  _size = 0;
116  }
117 
118  const ObjectTemplate& back() const {
119  return _object_template[_size - 1];
120  }
121 
122  typedef ObjectTemplate* iterator;
123  typedef const ObjectTemplate* const_iterator;
124 
125  iterator begin() {
126  return &_object_template[0];
127  }
128 
129  const_iterator begin() const {
130  return &_object_template[0];
131  }
132 
133  iterator end() {
134  return &_object_template[_size];
135  }
136 
137  const_iterator end() const {
138  return &_object_template[_size];
139  }
140 
141  void erase(int position) {
142  if(position < _size) {
143  for(int k = position; k + 1 < _size; k++) {
144  _object_template[k] = _object_template[k + 1];
145  }
146  _size--;
147  }
148  }
149 
150  enum {
151  MIN_CAPACITY = 1
152  };
153 
154  private:
155  int _size;
156  int _capacity;
157  ObjectTemplate* _object_template;
158 };
159 
160 } // namespace
161 
162 #endif // M2M_VECTOR_H
Definition: m2mvector.h:30
Namespace defined as replace for components defined under std namespace.
Definition: m2mstring.h:22