EMA C++ Edition
EmaVector.h
Go to the documentation of this file.
1/*|-----------------------------------------------------------------------------
2 *| This source code is provided under the Apache 2.0 license
3 *| and is provided AS IS with no warranty or guarantee of fit for purpose.
4 *| See the project's LICENSE.md for details.
5 *| Copyright (C) 2019 LSEG. All rights reserved. --
6 *|-----------------------------------------------------------------------------
7 */
8
9#ifndef __refinitiv_ema_access_EmaVector_h
10#define __refinitiv_ema_access_EmaVector_h
11
12#include "EmaString.h"
14
15#include <new>
16
17namespace refinitiv {
18
19namespace ema {
20
21namespace access {
22
29template< class T > class EmaVector
30{
31public :
32
34
35
40
42
43
45 EmaVector( const EmaVector< T >& other );
47
49
50
54
56
57
59 virtual ~EmaVector();
61
63
64
66 bool empty() const;
67
71 UInt32 size() const;
72
76 UInt32 capacity() const;
77
81 const T& operator[]( UInt32 index ) const;
82
86 T& operator[]( UInt32 index );
87
93 Int64 getPositionOf( const T& value ) const;
94
97 bool operator==( const EmaVector< T >& other ) const;
99
101
102
104 void clear();
105
109 void push_back( const T& entry );
110
115 bool removePosition( UInt32 pos );
116
123 bool removeValue( const T& value );
125
126private :
127
128 UInt32 _capacity;
129 UInt32 _size;
130 T* _list;
131
132 class EmaVectorException : public OmmOutOfRangeException
133 {
134 private:
135
136 EmaVectorException(const EmaString& text)
137 {
139 }
140
141 virtual ~~EmaVectorException(){}
142
143 EmaVectorException(const EmaVectorException& other) :
145
146 EmaVectorException& operator=(const EmaVectorException& other)
147 {
148 if (this == &other) return *this;
149
151
152 return *this;
153 }
154
155 friend class EmaVector;
156 };
157};
158
159template< class T >
161 _size( 0 ),
162 _capacity( capacity ),
163 _list( 0 )
164{
165 if ( !_capacity ) return;
166
167 _list = new T[ (unsigned int)_capacity ];
168}
169
170template< class T >
172 _capacity( other._capacity ),
173 _size( 0 ),
174 _list( 0 )
175{
176 if ( ! _capacity ) return;
177
178 _size = other._size;
179
180 _list = new T[ (unsigned int)_capacity ];
181
182 for ( UInt32 pos = 0; pos < _size; ++pos )
183 {
184 _list[pos] = other._list[pos];
185 }
186}
187
188template< class T >
190{
191 if ( this == &other ) return *this;
192
193 if ( _capacity >= other._size )
194 {
195 _size = other._size;
196
197 for ( UInt32 pos = 0; pos < _size; ++pos )
198 {
199 _list[pos] = other._list[pos];
200 }
201 }
202 else
203 {
204 if ( _list ) delete [] _list;
205
206 _capacity = other._capacity;
207 _size = other._size;
208
209 _list = new T[ (unsigned int)(_capacity) ];
210
211 for ( UInt32 pos = 0; pos < _size; ++pos )
212 {
213 _list[pos] = other._list[pos];
214 }
215 }
216
217 return *this;
218}
219
220template< class T >
222{
223 if ( _size != other._size ) return false;
224
225 for ( UInt32 pos = 0; pos < _size; ++pos )
226 {
227 if ( ! ( _list[pos] == other._list[pos] ) ) return false;
228 }
229
230 return true;
231}
232
233template< class T >
235{
236 _capacity = 0;
237 _size = 0;
238 if ( _list ) delete [] _list;
239}
240
241template< class T >
243{
244 _size = 0;
245}
246
247template < class T >
248void EmaVector< T >::push_back( const T& entry )
249{
250 if ( _size < _capacity )
251 {
252 _list[_size] = entry;
253 ++_size;
254 }
255 else
256 {
257 UInt32 i = 0;
258 if ( _capacity == 0 )
259 {
260 _capacity = 5;
261 }
262 else
263 {
264 _capacity = 2 * _capacity;
265 }
266
267 T* tempList;
268
269 tempList = new T[ (unsigned int)(_capacity)];
270
271 for ( i = 0; i < _size; i++ )
272 tempList[i] = _list[i];
273
274 if ( _list ) delete [] _list;
275
276 _list = tempList;
277
278 _list[ _size ] = entry;
279 ++_size;
280 }
281}
282
283template <class T >
285{
286 return _size;
287}
288
289template <class T >
291{
292 return _capacity;
293}
294
295template < class T >
297{
298 if (position >= _size)
299 {
300 EmaVectorException exception("Passed in position is out of range.");
301 throw exception;
302 }
303
304 return _list[position];
305}
306
307template < class T >
308const T& EmaVector< T >::operator[]( UInt32 position ) const
309{
310 if (position >= _size)
311 {
312 EmaVectorException exception("Passed in position is out of range.");
313 throw exception;
314 }
315
316 return _list[position];
317}
318
319template < class T >
321{
322 Int64 position = -1;
323
324 for ( UInt32 idx = 0; idx < _size; ++idx )
325 {
326 if ( operator[]( idx ) == value )
327 {
328 position = (Int64)(idx);
329 break;
330 }
331 }
332
333 return position;
334}
335
336template < class T >
338{
339 if ( position >= _size ) return false;
340
341 for ( UInt32 i = position + 1; i < _size; ++i )
342 {
343 _list[ i - 1 ] = _list[ i ];
344 }
345
346 --_size;
347
348 return true;
349}
350
351template< class T >
352bool
354{
355 UInt32 i( 0 );
356 while ( i < _size && operator[]( i ) != value )
357 ++i;
358 if ( i == _size )
359 return false;
360 for ( ++i; i < _size; ++i )
361 _list[ i - 1 ] = _list[ i ];
362 --_size;
363 return true;
364}
365
366template < class T >
368{
369 return ( _size ) ? false : true;
370}
371
372}
373
374}
375
376}
377
378#endif // __refinitiv_ema_access_EmaVector_h
*|--------------------------------------------------------------------------—
Definition: AckMsg.h:62
unsigned int UInt32
represents 32-bit unsigned integer
Definition: Common.h:56
long long Int64
represents 64-bit signed integer
Definition: Common.h:74
The access namespace contains all interfaces and definitions specified for use with the EMA Access pa...
The ema namespace contains all interfaces and definitions specified for use with EMA.
EmaString class is a container of a null terminated Ascii character string.
Definition: EmaString.h:57
EmaVector class provides template vector implementation.
Definition: EmaVector.h:30
EmaVector< T > & operator=(const EmaVector< T > &other)
Definition: EmaVector.h:189
EmaVector(UInt32 capacity=0)
Definition: EmaVector.h:160
Int64 getPositionOf(const T &value) const
Definition: EmaVector.h:320
bool removeValue(const T &value)
Definition: EmaVector.h:353
bool operator==(const EmaVector< T > &other) const
Definition: EmaVector.h:221
void push_back(const T &entry)
Definition: EmaVector.h:248
bool removePosition(UInt32 pos)
Definition: EmaVector.h:337
const T & operator[](UInt32 index) const
Definition: EmaVector.h:308
OmmException & statusText(const EmaString &statusText)
OmmException & operator=(const OmmException &)
OmmOutOfRangeException is thrown when a passed in method argument is out of range.