14 #ifndef SST_CORE_SPARSEVECTORMAP_H 15 #define SST_CORE_SPARSEVECTORMAP_H 17 #include "sst/core/serialization/serializable.h" 18 #include "sst/core/sst_types.h" 45 template <
typename keyT,
typename classT = keyT>
59 std::vector<classT> data;
60 int binary_search_insert(keyT
id)
const 65 int size = data.size();
68 if (
size == 0 )
return 0;
69 if (
id > data[
size - 1].key() )
return size;
70 if (
id < data[0].key() )
return 0;
76 while ( bottom <= top ) {
77 middle = bottom + (top - bottom) / 2;
78 if (
id == data[middle].key() )
return -1;
79 if (
id < data[middle].key() ) {
83 if (
id > data[middle - 1].key() )
106 int binary_search_find(keyT
id)
const 109 int top = data.size() - 1;
112 if ( data.size() == 0 )
return -1;
113 while ( bottom <= top ) {
114 middle = bottom + (top - bottom) / 2;
115 if (
id == data[middle].key() )
117 else if (
id < data[middle].key() )
159 std::sort(data.begin(), data.end(),
160 [](
const classT& lhs,
const classT& rhs) ->
bool {
return lhs.key() < rhs.key(); });
163 using iterator =
typename std::vector<classT>::iterator;
164 using const_iterator =
typename std::vector<classT>::const_iterator;
178 int index = binary_search_insert(val.key());
179 if ( index == -1 )
return data[binary_search_find(val.key())];
180 iterator it = data.begin();
182 data.insert(it, val);
191 iterator
begin() {
return data.begin(); }
198 iterator
end() {
return data.end(); }
205 const_iterator
begin()
const {
return data.begin(); }
212 const_iterator
end()
const {
return data.end(); }
223 if ( binary_search_find(
id) == -1 )
return false;
240 int index = binary_search_find(
id);
242 throw std::out_of_range(
"SparseVectorMap: trying to access element that does not exist");
261 int index = binary_search_find(
id);
263 throw std::out_of_range(
"SparseVectorMap: trying to access element that does not exist");
278 size_t size() {
return data.size(); }
291 runtime_error(what_arg)
313 template <
typename keyT,
typename classT>
327 std::vector<classT*> data;
328 int binary_search_insert(keyT
id)
const 333 int size = data.size();
336 if ( size == 0 )
return 0;
337 if (
id > data[size - 1]->key() )
return size;
338 if (
id < data[0]->key() )
return 0;
344 while ( bottom <= top ) {
345 middle = bottom + (top - bottom) / 2;
346 if (
id == data[middle]->key() )
return -1;
347 if (
id < data[middle]->key() ) {
351 if (
id > data[middle - 1]->key() )
374 int binary_search_find(keyT
id)
const 377 int top = data.size() - 1;
380 if ( data.size() == 0 )
return -1;
381 while ( bottom <= top ) {
382 middle = bottom + (top - bottom) / 2;
383 if (
id == data[middle]->key() )
385 else if (
id < data[middle]->key() )
427 std::sort(data.begin(), data.end(),
428 [](
const classT* lhs,
const classT* rhs) ->
bool {
return lhs->key() < rhs->key(); });
431 using iterator =
typename std::vector<classT*>::iterator;
432 using const_iterator =
typename std::vector<classT*>::const_iterator;
446 int index = binary_search_insert(val->key());
447 if ( index == -1 )
return data[binary_search_find(val->key())];
448 iterator it = data.begin();
450 data.insert(it, val);
459 iterator
begin() {
return data.begin(); }
466 iterator
end() {
return data.end(); }
473 const_iterator
begin()
const {
return data.begin(); }
480 const_iterator
end()
const {
return data.end(); }
491 if ( binary_search_find(
id) == -1 )
return false;
508 int index = binary_search_find(
id);
510 throw std::out_of_range(
"SparseVectorMap: trying to access element that does not exist");
529 int index = binary_search_find(
id);
531 throw std::out_of_range(
"SparseVectorMap: trying to access element that does not exist");
546 size_t size() {
return data.size(); }
564 template <
typename filterT>
568 for (
size_t i = 0; i < data.size(); ++i ) {
569 auto key = data[i]->key();
570 data[write_ptr] = filt(data[i]);
571 if ( data[write_ptr] !=
nullptr ) {
573 if ( UNLIKELY(data[write_ptr]->key() != key) ) {
576 "ERROR: Filter object passed to SparseVectorMap::filter returned an object that had a " 577 "different value of key() than what was passed into the filter. The filter object must return " 578 "either an object with the same value of key(), or nullptr if the object should be removed.");
588 data.resize(write_ptr);
589 data.shrink_to_fit();
599 template <
typename keyT>
613 std::vector<keyT> data;
614 int binary_search_insert(keyT
id)
const 619 int size = data.size();
622 if (
size == 0 )
return 0;
623 if (
id < data[0] )
return 0;
624 if (
id > data[
size - 1] )
return size;
630 while ( bottom <= top ) {
631 middle = bottom + (top - bottom) / 2;
632 if (
id == data[middle] )
return -1;
633 if (
id < data[middle] ) {
637 if (
id > data[middle - 1] )
660 int binary_search_find(keyT
id)
const 663 int top = data.size() - 1;
666 if ( data.size() == 0 )
return -1;
667 while ( bottom <= top ) {
668 middle = bottom + (top - bottom) / 2;
669 if (
id == data[middle] )
671 else if (
id < data[middle] )
713 std::sort(data.begin(), data.end, [](
const keyT& lhs,
const keyT& rhs) ->
bool {
return lhs < rhs; });
716 using iterator =
typename std::vector<keyT>::iterator;
717 using const_iterator =
typename std::vector<keyT>::const_iterator;
731 int index = binary_search_insert(val);
732 if ( index == -1 )
return data[binary_search_find(val)];
733 iterator it = data.begin();
735 data.insert(it, val);
744 iterator
begin() {
return data.begin(); }
751 iterator
end() {
return data.end(); }
758 const_iterator
begin()
const {
return data.begin(); }
765 const_iterator
end()
const {
return data.end(); }
776 if ( binary_search_find(
id) == -1 )
return false;
793 int index = binary_search_find(
id);
795 throw std::out_of_range(
"SparseVectorMap: trying to access element that does not exist");
814 int index = binary_search_find(
id);
816 throw std::out_of_range(
"SparseVectorMap: trying to access element that does not exist");
831 size_t size() {
return data.size(); }
838 template <
typename keyT,
typename classT>
844 SST_SER(v.data, options);
849 #endif // SST_CORE_SPARSEVECTORMAP_H void sort()
Force a sort of the data.
Definition: sparseVectorMap.h:157
iterator end()
Returns the end iterator to the underlying vector.
Definition: sparseVectorMap.h:751
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
iterator end()
Returns the end iterator to the underlying vector.
Definition: sparseVectorMap.h:466
classT & operator[](keyT id)
Operator returns a reference to data with the specified id.
Definition: sparseVectorMap.h:238
const_iterator begin() const
Returns the const begin iterator to the underlying vector.
Definition: sparseVectorMap.h:473
bool contains(keyT id)
Checks if the provided id is found in the SparseVectorMap.
Definition: sparseVectorMap.h:774
classT & insert(const classT &val)
Insert new value into SparseVectorMap.
Definition: sparseVectorMap.h:176
void sort()
Force a sort of the data.
Definition: sparseVectorMap.h:425
SparseVectorMap(std::vector< classT *> &new_data, bool sorted=false)
Constructor that allows you to pass an already filled in array with data.
Definition: sparseVectorMap.h:414
classT * operator[](keyT id)
Operator returns a reference to data with the specified id.
Definition: sparseVectorMap.h:506
A Configuration Graph A graph representing Components and Links.
Definition: configGraph.h:585
Base serialize class.
Definition: serialize.h:113
iterator end()
Returns the end iterator to the underlying vector.
Definition: sparseVectorMap.h:198
iterator begin()
Returns the begin iterator to the underlying vector.
Definition: sparseVectorMap.h:191
const_iterator end() const
Returns the const end iterator to the underlying vector.
Definition: sparseVectorMap.h:212
size_t size()
Returns the number of items in the SparseVectorMap.
Definition: sparseVectorMap.h:546
size_t size()
Returns the number of items in the SparseVectorMap.
Definition: sparseVectorMap.h:278
const classT * operator[](keyT id) const
Operator returns a const reference to data with the specified id.
Definition: sparseVectorMap.h:527
keyT & operator[](keyT id)
Operator returns a reference to data with the specified id.
Definition: sparseVectorMap.h:791
void filter(filterT &filt)
Function to filter the contents of the SparseVectorMap.
Definition: sparseVectorMap.h:565
SparseVectorMap(std::vector< keyT > &new_data, bool sorted=false)
Constructor that allows you to pass an already filled in array with data.
Definition: sparseVectorMap.h:700
iterator begin()
Returns the begin iterator to the underlying vector.
Definition: sparseVectorMap.h:459
const_iterator begin() const
Returns the const begin iterator to the underlying vector.
Definition: sparseVectorMap.h:758
const classT & operator[](keyT id) const
Operator returns a const reference to data with the specified id.
Definition: sparseVectorMap.h:259
SparseVectorMap(std::vector< classT > &new_data, bool sorted=false)
Constructor that allows you to pass an already filled in array with data.
Definition: sparseVectorMap.h:146
iterator begin()
Returns the begin iterator to the underlying vector.
Definition: sparseVectorMap.h:744
const keyT & operator[](keyT id) const
Operator returns a const reference to data with the specified id.
Definition: sparseVectorMap.h:812
keyT & insert(const keyT &val)
Insert new value into SparseVectorMap.
Definition: sparseVectorMap.h:729
size_t size()
Returns the number of items in the SparseVectorMap.
Definition: sparseVectorMap.h:831
const_iterator end() const
Returns the const end iterator to the underlying vector.
Definition: sparseVectorMap.h:480
SparseVectorMap()
Default constructor for SparseVectorMap.
Definition: sparseVectorMap.h:131
const_iterator begin() const
Returns the const begin iterator to the underlying vector.
Definition: sparseVectorMap.h:205
SparseVectorMap()
Default constructor for SparseVectorMap.
Definition: sparseVectorMap.h:685
bool contains(keyT id) const
Checks if the provided id is found in the SparseVectorMap.
Definition: sparseVectorMap.h:221
Class that stores data in a vector, but can access the data similar to a map.
Definition: sparseVectorMap.h:46
bool contains(keyT id) const
Checks if the provided id is found in the SparseVectorMap.
Definition: sparseVectorMap.h:489
void clear()
Clears the contents of the SparseVectorMap.
Definition: sparseVectorMap.h:824
Exception that is thrown by SparseVectorMap::filter() if the filtering object returns an object that ...
Definition: sparseVectorMap.h:287
SparseVectorMap()
Default constructor for SparseVectorMap.
Definition: sparseVectorMap.h:399
void clear()
Clears the contents of the SparseVectorMap.
Definition: sparseVectorMap.h:271
classT * insert(classT *val)
Insert new value into SparseVectorMap.
Definition: sparseVectorMap.h:444
const_iterator end() const
Returns the const end iterator to the underlying vector.
Definition: sparseVectorMap.h:765
void sort()
Force a sort of the data.
Definition: sparseVectorMap.h:711
void clear()
Clears the contents of the SparseVectorMap.
Definition: sparseVectorMap.h:539