fix8  version 1.4.0
Open Source C++ FIX Framework
FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare > Class Template Reference

#include <traits.hpp>

Public Types

using iterator = FieldTrait *
 
using const_iterator = const FieldTrait *
 
using result = std::pair< iterator, bool >
 

Public Member Functions

 presorted_set (const_iterator arr_start, const size_t sz, const FieldTrait_Hash_Array *ftha)
 
 presorted_set (const_iterator arr_start, const size_t sz, const size_t reserve=FIX8_RESERVE_PERCENT)
 
 presorted_set (const presorted_set &from)
 
 presorted_set (const size_t sz=0, const size_t reserve=FIX8_RESERVE_PERCENT)
 
 ~presorted_set ()
 dtor More...
 
iterator find (const FieldTrait what, bool &answer)
 
iterator find (const unsigned short key, bool &answer)
 
const_iterator find (const unsigned short key) const
 
const_iterator find (const FieldTrait what) const
 
iterator find (const unsigned short key)
 
iterator find (const FieldTrait what)
 
const_iterator at (const size_t idx) const
 
result insert (const_iterator what)
 
void insert (const_iterator what_begin, const_iterator what_end)
 
void clear ()
 
size_t size () const
 
bool empty () const
 
size_t rsize () const
 
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 

Static Public Member Functions

static size_t calc_reserve (size_t sz, size_t res)
 
static size_t distance (const_iterator what_begin, const_iterator what_end)
 

Private Types

using internal_result = std::pair< iterator, iterator >
 
using const_internal_result = std::pair< const_iterator, const_iterator >
 

Private Attributes

size_t _reserve
 
size_t _sz
 
size_t _rsz
 
FieldTrait_arr
 
const FieldTrait_Hash_Array_ftha
 

Detailed Description

template<>
class FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >

Specialisation of Presorted set using hash array lookup Search complexity is O(1), ctor complexity approaches O(1), no insert

Definition at line 218 of file traits.hpp.

Member Typedef Documentation

Definition at line 244 of file traits.hpp.

Definition at line 222 of file traits.hpp.

using FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::internal_result = std::pair<iterator, iterator>
private

Definition at line 243 of file traits.hpp.

Definition at line 221 of file traits.hpp.

using FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::result = std::pair<iterator, bool>

Definition at line 223 of file traits.hpp.

Constructor & Destructor Documentation

FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::presorted_set ( const_iterator  arr_start,
const size_t  sz,
const FieldTrait_Hash_Array ftha 
)
inline

ctor - initialise from static sorted set. Used by Message encoder/decoder.

Parameters
arr_startpointer to start of static array to copy elements from
sznumber of elements in set to copy
fthapointer to field hash array

Definition at line 251 of file traits.hpp.

FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::presorted_set ( const_iterator  arr_start,
const size_t  sz,
const size_t  reserve = FIX8_RESERVE_PERCENT 
)
inline

ctor - initialise an empty set; defer memory allocation;

Parameters
arr_startpointer to start of static array to copy elements from
sznumber of elements to initially allocate
reservepercentage of sz to keep in reserve

Definition at line 259 of file traits.hpp.

FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::presorted_set ( const presorted_set< unsigned short, FieldTrait, FieldTrait::Compare > &  from)
inline

copy ctor

Parameters
frompresorted_set object to copy

Definition at line 265 of file traits.hpp.

References FIX8::presorted_set< K, T, Comp >::_arr.

FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::presorted_set ( const size_t  sz = 0,
const size_t  reserve = FIX8_RESERVE_PERCENT 
)
inlineexplicit

ctor - initialise an empty set; defer memory allocation;

Parameters
sznumber of elements to initially allocate
reservepercentage of sz to keep in reserve

Definition at line 272 of file traits.hpp.

Member Function Documentation

const_iterator FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::at ( const size_t  idx) const
inline

Get the element at index location

Parameters
idxof the pair to retrieve
Returns
const_iterator to element or end() if not found

Definition at line 344 of file traits.hpp.

References FIX8::presorted_set< K, T, Comp >::end().

Referenced by FIX8::ConsoleMenu::SelectField().

const_iterator FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::begin ( ) const
inline

Get a const pointer to the first element

Returns
the first element

Definition at line 426 of file traits.hpp.

References FIX8::presorted_set< K, T, Comp >::_arr.

static size_t FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::calc_reserve ( size_t  sz,
size_t  res 
)
inlinestatic

Calculate the amount of space to reserve in set

Parameters
sznumber of elements currently in set; if 0 retun reserve elements as size to reserve
respercentage of sz to keep in reserve
Returns
number of elements to additionally reserve (at least 1)

Definition at line 229 of file traits.hpp.

230  {
231  if (!sz) // special case - reserve means number to reserve, not %
232  return res;
233  const size_t val(sz * res / 100);
234  return val ? val : 1;
235  }
void FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::clear ( )
inline

Clear the set (does not delete)

Definition at line 402 of file traits.hpp.

static size_t FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::distance ( const_iterator  what_begin,
const_iterator  what_end 
)
inlinestatic

Find the distance between two iterators

Parameters
what_beginstart iterator
what_endend iterator (must be >= to what_begin
Returns
distance in elements

Definition at line 388 of file traits.hpp.

389  { return what_end - what_begin; }
bool FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::empty ( ) const
inline

Check if the set is empty

Returns
true if empty

Definition at line 410 of file traits.hpp.

const_iterator FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::end ( ) const
inline

Get a const pointer to the last element + 1

Returns
the last element + 1

Definition at line 430 of file traits.hpp.

References FIX8::presorted_set< K, T, Comp >::_sz.

iterator FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::find ( const FieldTrait  what,
bool &  answer 
)
inline

Find an element with the given value

Parameters
whatelement to find
answertrue if element is found
Returns
pointer to found element or pointer to location where element would be inserted

Definition at line 282 of file traits.hpp.

References FIX8::FieldTrait_Hash_Array::_arr, FIX8::FieldTrait::_fnum, and FIX8::FieldTrait_Hash_Array::_sz.

Referenced by FIX8::FieldTraits::clear(), FIX8::FieldTraits::get(), FIX8::FieldTraits::getComp(), FIX8::FieldTraits::getPos(), FIX8::FieldTraits::getval(), FIX8::FieldTraits::has(), and FIX8::FieldTraits::set().

283  {
284  if (_ftha)
285  return (answer = what._fnum < _ftha->_sz && (_arr + _ftha->_arr[what._fnum])->_fnum == what._fnum) ? _arr + _ftha->_arr[what._fnum] : 0;
286  const internal_result res(std::equal_range (_arr, _arr + _sz, what, FieldTrait::Compare()));
287  answer = res.first != res.second;
288  return res.first;
289  }
unsigned short * _arr
Definition: traits.hpp:201
iterator FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::find ( const unsigned short  key,
bool &  answer 
)
inline

Find an element with the given key

Parameters
keyto find
answertrue if element is found
Returns
pointer to found element or pointer to location where element would be inserted

Definition at line 295 of file traits.hpp.

References find().

Referenced by find().

295 { return find(FieldTrait(key), answer); }
iterator find(const FieldTrait what, bool &answer)
Definition: traits.hpp:282
const_iterator FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::find ( const unsigned short  key) const
inline

Find an element with the given key (const version)

Parameters
keyto find
Returns
pointer to found element or end()

Definition at line 300 of file traits.hpp.

References FIX8::FieldTrait_Hash_Array::_arr, FIX8::FieldTrait_Hash_Array::_sz, and FIX8::presorted_set< K, T, Comp >::end().

301  {
302  if (_ftha)
303  return key < _ftha->_sz && (_arr + _ftha->_arr[key])->_fnum == key ? _arr + _ftha->_arr[key] : end();
304  const FieldTrait what(key);
305  const const_internal_result res(std::equal_range (_arr, _arr + _sz, what, FieldTrait::Compare()));
306  return res.first != res.second ? res.first : end();
307  }
unsigned short * _arr
Definition: traits.hpp:201
std::pair< const_iterator, const_iterator > const_internal_result
Definition: traits.hpp:244
const_iterator FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::find ( const FieldTrait  what) const
inline

Find an element with the given value (const version)

Parameters
whatelement to find
Returns
pointer to found element or end()

Definition at line 312 of file traits.hpp.

References FIX8::FieldTrait_Hash_Array::_arr, FIX8::FieldTrait::_fnum, FIX8::FieldTrait_Hash_Array::_sz, and FIX8::presorted_set< K, T, Comp >::end().

313  {
314  if (_ftha)
315  return what._fnum < _ftha->_sz && (_arr + _ftha->_arr[what._fnum])->_fnum == what._fnum ? _arr + _ftha->_arr[what._fnum] : end();
316  const const_internal_result res(std::equal_range (_arr, _arr + _sz, what, FieldTrait::Compare()));
317  return res.first != res.second ? res.first : end();
318  }
unsigned short * _arr
Definition: traits.hpp:201
std::pair< const_iterator, const_iterator > const_internal_result
Definition: traits.hpp:244
iterator FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::find ( const unsigned short  key)
inline

Find an element with the given key

Parameters
keyto find
Returns
pointer to found element or end()

Definition at line 323 of file traits.hpp.

References FIX8::FieldTrait_Hash_Array::_arr, FIX8::FieldTrait_Hash_Array::_sz, and FIX8::presorted_set< K, T, Comp >::end().

324  {
325  if (_ftha)
326  return key < _ftha->_sz && (_arr + _ftha->_arr[key])->_fnum == key ? _arr + _ftha->_arr[key] : end();
327  const FieldTrait what(key);
328  internal_result res(std::equal_range (_arr, _arr + _sz, what, FieldTrait::Compare()));
329  return res.first != res.second ? res.first : end();
330  }
unsigned short * _arr
Definition: traits.hpp:201
iterator FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::find ( const FieldTrait  what)
inline

Find an element with the given value

Parameters
whatvalue to find
Returns
pointer to found element or end()

Definition at line 335 of file traits.hpp.

References FIX8::presorted_set< K, T, Comp >::end().

336  {
337  internal_result res(std::equal_range (_arr, _arr + _sz, what, FieldTrait::Compare()));
338  return res.first != res.second ? res.first : end();
339  }
result FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::insert ( const_iterator  what)
inline

Insert an element into the set

Parameters
whatpointer to element to insert
Returns
result with iterator to insert location and true or end() and false

Definition at line 349 of file traits.hpp.

References FIX8::presorted_set< K, T, Comp >::_arr, FIX8::presorted_set< K, T, Comp >::_rsz, FIX8::presorted_set< K, T, Comp >::_sz, FIX8::presorted_set< K, T, Comp >::calc_reserve(), FIX8::presorted_set< K, T, Comp >::end(), and FIX8::presorted_set< K, T, Comp >::find().

Referenced by FIX8::FieldTraits::add().

350  {
351  if (!_sz)
352  {
353  _arr = new FieldTrait[_rsz];
354  memcpy(_arr, what, sizeof(FieldTrait));
355  ++_sz;
356  return std::make_pair(_arr, true);
357  }
358 
359  bool answer;
360  iterator where(find(*what, answer));
361  if (answer) // sorry already here
362  return std::make_pair(end(), false);
363 
364  if (_sz < _rsz) // we have space
365  {
366  memmove(where + 1, where, (end() - where) * sizeof(FieldTrait));
367  memcpy(where, what, sizeof(FieldTrait));
368  }
369  else // we have to make space
370  {
371  iterator new_arr(new FieldTrait[_rsz = _sz + calc_reserve(_sz, _reserve)]);
372  const size_t wptr(where - _arr);
373  if (wptr > 0)
374  memcpy(new_arr, _arr, sizeof(FieldTrait) * wptr);
375  memcpy(new_arr + wptr, what, sizeof(FieldTrait));
376  memcpy(new_arr + wptr + 1, where, (end() - where) * sizeof(FieldTrait));
377  delete[] _arr;
378  _arr = new_arr;
379  }
380  ++_sz;
381  return std::make_pair(where, true);
382  }
iterator find(const FieldTrait what, bool &answer)
Definition: traits.hpp:282
void FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::insert ( const_iterator  what_begin,
const_iterator  what_end 
)
inline

Insert a range of elements into the set

Parameters
what_beginpointer to 1st element to insert
what_endpointer to nth element + 1 to insert

Definition at line 394 of file traits.hpp.

References FIX8::presorted_set< K, T, Comp >::insert().

395  {
396  for (const_iterator ptr(what_begin); ptr < what_end; ++ptr)
397  if (!insert(ptr).second)
398  break;
399  }
size_t FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::rsize ( ) const
inline

Obtain the number of elements that can be inserted before reallocating

Returns
reserved + sz

Definition at line 414 of file traits.hpp.

References FIX8::presorted_set< K, T, Comp >::_rsz.

size_t FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::size ( ) const
inline

Obtain the number of elements in the set

Returns
the number of elements

Definition at line 406 of file traits.hpp.

References FIX8::presorted_set< K, T, Comp >::_sz.

Referenced by generate_group_bodies(), generate_group_traits(), FIX8::ConsoleMenu::SelectField(), and FIX8::FieldTraits::size().

Member Data Documentation

FieldTrait* FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::_arr
private

Definition at line 240 of file traits.hpp.

const FieldTrait_Hash_Array* FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::_ftha
private

Definition at line 241 of file traits.hpp.

size_t FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::_reserve
private

Definition at line 238 of file traits.hpp.

size_t FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::_rsz
private

Definition at line 239 of file traits.hpp.

size_t FIX8::presorted_set< unsigned short, FieldTrait, FieldTrait::Compare >::_sz
private

Definition at line 239 of file traits.hpp.


The documentation for this class was generated from the following file: