Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
m2mvector.h
00001 /* 00002 * Copyright (c) 2015-2016 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef M2M_VECTOR_H 00018 #define M2M_VECTOR_H 00019 00020 #include <stdio.h> 00021 00022 namespace m2m 00023 { 00024 00025 template <typename ObjectTemplate> 00026 00027 class Vector 00028 { 00029 public: 00030 explicit Vector( int init_size = MIN_CAPACITY) 00031 : _size(0), 00032 _capacity((init_size >= MIN_CAPACITY) ? init_size : MIN_CAPACITY) { 00033 _object_template = new ObjectTemplate[ _capacity ]; 00034 } 00035 00036 Vector(const Vector & rhs ): _object_template(0) { 00037 operator=(rhs); 00038 } 00039 00040 ~Vector() { 00041 delete [] _object_template; 00042 } 00043 00044 const Vector & operator=(const Vector & rhs) { 00045 if(this != &rhs) { 00046 delete[] _object_template; 00047 _size = rhs.size(); 00048 _capacity = rhs._capacity; 00049 00050 _object_template = new ObjectTemplate[capacity()]; 00051 for(int k = 0; k < size(); k++) { 00052 _object_template[k] = rhs._object_template[k]; 00053 } 00054 } 00055 return *this; 00056 } 00057 00058 void resize(int new_size) { 00059 if(new_size > _capacity) { 00060 reserve(new_size * 2 + 1); 00061 } 00062 _size = new_size; 00063 } 00064 00065 void reserve(int new_capacity) { 00066 if(new_capacity < _size) { 00067 return; 00068 } 00069 ObjectTemplate *old_array = _object_template; 00070 00071 _object_template = new ObjectTemplate[new_capacity]; 00072 for(int k = 0; k < _size; k++) { 00073 _object_template[k] = old_array[k]; 00074 } 00075 _capacity = new_capacity; 00076 delete [] old_array; 00077 } 00078 00079 ObjectTemplate & operator[](int idx) { 00080 return _object_template[idx]; 00081 } 00082 00083 const ObjectTemplate& operator[](int idx) const { 00084 return _object_template[idx]; 00085 } 00086 00087 bool empty() const{ 00088 return size() == 0; 00089 } 00090 00091 int size() const { 00092 return _size; 00093 } 00094 00095 int capacity() const { 00096 return _capacity; 00097 } 00098 00099 void push_back(const ObjectTemplate& x) { 00100 if(_size == _capacity) { 00101 reserve(2 * _capacity + 1); 00102 } 00103 _object_template[_size] = x; 00104 _size++; 00105 } 00106 00107 void pop_back() { 00108 _size--; 00109 } 00110 00111 void clear() { 00112 _size = 0; 00113 } 00114 00115 const ObjectTemplate& back() const { 00116 return _object_template[_size - 1]; 00117 } 00118 00119 typedef ObjectTemplate* iterator; 00120 typedef const ObjectTemplate* const_iterator; 00121 00122 iterator begin() { 00123 return &_object_template[0]; 00124 } 00125 00126 const_iterator begin() const { 00127 return &_object_template[0]; 00128 } 00129 00130 iterator end() { 00131 return &_object_template[_size]; 00132 } 00133 00134 const_iterator end() const { 00135 return &_object_template[_size]; 00136 } 00137 00138 void erase(int position) { 00139 if(position < _size) { 00140 _object_template[position] = 0; 00141 for(int k = position; k + 1 < _size; k++) { 00142 _object_template[k] = _object_template[k + 1]; 00143 } 00144 _size--; 00145 } 00146 } 00147 00148 enum { 00149 MIN_CAPACITY = 1 00150 }; 00151 00152 private: 00153 int _size; 00154 int _capacity; 00155 ObjectTemplate* _object_template; 00156 }; 00157 00158 } // namespace 00159 00160 #endif // M2M_VECTOR_H
Generated on Tue Jul 12 2022 13:05:14 by
1.7.2