SimOne C++ API Reference 3.7.0
SimOne 仿真平台C++接口文档
Loading...
Searching...
No Matches
SimVector.h
Go to the documentation of this file.
1// ==========================================================================
2// Copyright (C) 2018 - 2021 Beijing 51WORLD Digital Twin Technology Co., Ltd.
3// , and/or its licensors. All rights reserved.
4//
5// The coded instructions, statements, computer programs, and/or related
6// material (collectively the "Data") in these files contain unpublished
7// information proprietary to Beijing 51WORLD Digital Twin Technology Co., Ltd.
8// ("51WORLD") and/or its licensors, which is protected by the People's
9// Republic of China and/or other countries copyright law and by
10// international treaties.
11//
12// The Data may not be disclosed or distributed to third parties or be
13// copied or duplicated, in whole or in part, without the prior written
14// consent of 51WORLD.
15//
16// The copyright notices in the Software and this entire statement,
17// including the above license grant, this restriction and the following
18// disclaimer, must be included in all copies of the Software, in whole
19// or in part, and all derivative works of the Software, unless such copies
20// or derivative works are solely in the form of machine-executable object
21// code generated by a source language processor.
22
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
24// 51WORLD DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
25// WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
26// NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
27// OR ARISING FROM A COURSE OF DEALING, USAGE, OR TRADE PRACTICE. IN NO
28// EVENT WILL 51WORLD AND/OR ITS LICENSORS BE LIABLE FOR ANY LOST
29// REVENUES, DATA, OR PROFITS, OR SPECIAL, DIRECT, INDIRECT, OR
30// CONSEQUENTIAL DAMAGES, EVEN IF 51WORLD AND/OR ITS LICENSORS HAS
31// BEEN ADVISED OF THE POSSIBILITY OR PROBABILITY OF SUCH DAMAGES.
32// ==========================================================================
33#pragma once
34
35#include "ssdexport.h"
36
37namespace SSD {
38
39 typedef unsigned long long sizeT;
40 template <typename T>
41 class SSDEXPORT SimVector
42 {
45 T *arr = nullptr; //pointer to the first element
46 public:
47 using value_type = T;
48 using reference = T & ;
49 using const_reference = const T &;
50 using iterator = T * ;
51 using const_iterator = const T *;
52 protected:
54 public:
55 //constructor
56 SimVector() : m_size(0U), m_capacity(0U), arr(nullptr) {}
57 explicit SimVector(sizeT n) : m_size(n), m_capacity(n), arr(n ? new T[n] : nullptr) {}
58 //SimVector(const SimVector& v) = delete; //this function is useless and dangerous for now
60 {
61 m_size = v.m_size;
63 arr = new T[m_size];
64 for (int i = 0; i < m_size; i++)
65 {
66 arr[i] = v.arr[i];
67 }
68 }
69 SimVector(SimVector&& v) : m_size(v.m_size), m_capacity(v.m_capacity), arr(v.arr) { v.arr = nullptr; }
70 //destructor
72 if (arr != nullptr)
73 {
74 delete[]arr;
75 }
76 arr = nullptr;
77 }
78 //operator=
80
81 //iterators
82 iterator begin() { return arr; }
83 const_iterator begin() const { return arr; }
84 iterator end() { return arr + m_size; }
85 const_iterator end() const { return arr + m_size; }
86 const_iterator cbegin() const { return arr; }
87 const_iterator cend() const { return arr + m_size; }
88
89 //capacity
90 sizeT size() const { return m_size; }
91 void resize(sizeT n);
92 sizeT capacity() const { return m_capacity; }
93 bool empty() const { return !m_size; }
94 void reserve(sizeT n);
95
96 //access to the element
97 reference operator[] (sizeT n) { return arr[n]; }
98 const_reference operator[] (sizeT n) const { return arr[n]; }
99 reference front() { return arr[0U]; }
100 const_reference front() const { return arr[0U]; }
101 reference back() { return arr[m_size - 1U]; }
102 const_reference back() const { return arr[m_size - 1U]; }
103
104 //modyfying
105 void push_back(const value_type &t);
107 void pop_back() { arr[--m_size].~T(); }
109 void clear();
110 };
111
112 template<typename T>
114 {
115 if (m_size >= m_capacity) {
116 if (m_capacity > 3)
117 reserve(m_capacity / 2U * 3U);
118 else //m_capacity == 0/1/2
119 reserve(m_capacity + 1U);
120 }
121 }
122
123 template<typename T>
125 {
126 if (this != &v) {
127 //~SimVector();
128 delete[]arr;
129 m_size = v.m_size;
131 arr = new T[m_capacity];
132 for (sizeT i = 0U; i < m_size; ++i)
133 arr[i] = v.arr[i];
134 }
135 return *this;
136 }
137
138 template<typename T>
140 {
141 if (n < m_size) {
142 while (n < m_size)
143 pop_back();
144 return;
145 }
146 if (n > m_capacity)
147 reserve(n);
148 m_size = n;
149
150 /*while (n > m_size) //initialize?
151 {
152 arr[m_size++] = {};
153 }*/
154
155 }
156
157 template<typename T>
159 {
160 if (n > m_capacity) {
161 T *newbuff = new T[n];
162 for (sizeT i = 0U; i < m_size; ++i)
163 {
164 //newbuff[i] = std::move(arr[i]);
165 newbuff[i] = arr[i];
166 }
167 delete[]arr;
168 arr = newbuff;
169 m_capacity = n;
170 }
171 }
172
173 template<typename T>
175 {
177 arr[m_size++] = t;
178 }
179
180 template<typename T>
182 {
184 //arr[m_size++] = std::move(val);
185 arr[m_size++] = val;
186 }
187
188 template<typename T>
190 {
191 position->~T();
192 for (auto i = const_cast<iterator>(position); i < end() - 1; ++i) //first option
193 {
194 //(*i) = std::move(*(i + 1));
195 (*i) = *(i + 1);
196 }
197 /*for (sizeT i = position - begin(); i < m_size; ++i) //second option
198 arr[i] = std::move(arr[i + 1]);*/
199 --m_size;
200
201 return const_cast<iterator>(position);
202 }
203
204 template<typename T>
206 {
207 for (auto &i : (*this))
208 i.~T();
209 m_size = 0U;
210 }
211}
SimVector(const SimVector &v)
Definition SimVector.h:59
const_iterator end() const
Definition SimVector.h:85
void reserve(sizeT n)
Definition SimVector.h:158
sizeT m_capacity
Definition SimVector.h:44
void resize(sizeT n)
Definition SimVector.h:139
sizeT capacity() const
Definition SimVector.h:92
const_reference front() const
Definition SimVector.h:100
iterator erase(const_iterator position)
Definition SimVector.h:189
reference back()
Definition SimVector.h:101
bool empty() const
Definition SimVector.h:93
SimPoint3D value_type
Definition SimVector.h:47
SimVector(sizeT n)
Definition SimVector.h:57
SimVector(SimVector &&v)
Definition SimVector.h:69
void push_back(const value_type &t)
Definition SimVector.h:174
sizeT size() const
Definition SimVector.h:90
sizeT m_size
Definition SimVector.h:43
iterator begin()
Definition SimVector.h:82
SimPoint3D * iterator
Definition SimVector.h:50
const SimPoint3D & const_reference
Definition SimVector.h:49
const_iterator cbegin() const
Definition SimVector.h:86
const_iterator cend() const
Definition SimVector.h:87
~SimVector()
Definition SimVector.h:71
SimPoint3D * arr
Definition SimVector.h:45
SimVector()
Definition SimVector.h:56
const SimPoint3D * const_iterator
Definition SimVector.h:51
const_reference back() const
Definition SimVector.h:102
iterator end()
Definition SimVector.h:84
void push_back(value_type &&val)
Definition SimVector.h:181
void clear()
Definition SimVector.h:205
void enough_capacity()
Definition SimVector.h:113
SimVector & operator=(const SimVector &v)
Definition SimVector.h:124
void pop_back()
Definition SimVector.h:107
const_iterator begin() const
Definition SimVector.h:83
SimPoint3D & reference
Definition SimVector.h:48
reference front()
Definition SimVector.h:99
Definition SimPoint2D.h:36
unsigned long long sizeT
Definition SimVector.h:39