EMA C++ Edition
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 Refinitiv. All rights reserved. --
6  *|-----------------------------------------------------------------------------
7  */
8 
9 #ifndef __thomsonreuters_ema_access_EmaVector_h
10 #define __thomsonreuters_ema_access_EmaVector_h
11 
12 #include "EmaString.h"
13 #include "OmmOutOfRangeException.h"
14 
15 #include <new>
16 
17 namespace thomsonreuters {
18 
19 namespace ema {
20 
21 namespace access {
22 
29 template< class T > class EmaVector
30 {
31 public :
32 
34 
35 
38  EmaVector( UInt32 capacity = 0 );
40 
42 
43 
45  EmaVector( const EmaVector< T >& other );
47 
49 
50 
52  EmaVector< T >& operator=( const EmaVector< T >& other );
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 
126 private :
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) :
144  OmmOutOfRangeException(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 
159 template< 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 
170 template< 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 
188 template< 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 
220 template< 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 
233 template< class T >
235 {
236  _capacity = 0;
237  _size = 0;
238  if ( _list ) delete [] _list;
239 }
240 
241 template< class T >
243 {
244  _size = 0;
245 }
246 
247 template < class T >
248 void 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 
283 template <class T >
285 {
286  return _size;
287 }
288 
289 template <class T >
291 {
292  return _capacity;
293 }
294 
295 template < 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 
307 template < class T >
308 const 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 
319 template < class T >
320 Int64 EmaVector< T >::getPositionOf( const T& value ) const
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 
336 template < 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 
351 template< class T >
352 bool
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 
366 template < class T >
368 {
369  return ( _size ) ? false : true;
370 }
371 
372 }
373 
374 }
375 
376 }
377 
378 #endif // __thomsonreuters_ema_access_EmaVector_h
long long Int64
represents 64-bit signed integer
Definition: Common.h:74
Int64 getPositionOf(const T &value) const
Definition: EmaVector.h:320
EmaVector class provides template vector implementation.
Definition: EmaVector.h:29
unsigned int UInt32
represents 32-bit unsigned integer
Definition: Common.h:56
EmaString class is a container of a null terminated Ascii character string.
Definition: EmaString.h:56
OmmException & statusText(const EmaString &statusText)
OmmException & operator=(const OmmException &)
OmmOutOfRangeException is thrown when a passed in method argument is out of range.
bool operator==(const EmaVector< T > &other) const
Definition: EmaVector.h:221
EmaVector< T > & operator=(const EmaVector< T > &other)
Definition: EmaVector.h:189
bool removeValue(const T &value)
Definition: EmaVector.h:353
const T & operator[](UInt32 index) const
Definition: EmaVector.h:308