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"
43template <
typename keyT,
typename classT = keyT>
57 std::vector<classT> data;
58 int binary_search_insert(keyT
id)
const
63 int size = data.size();
66 if (
size == 0 )
return 0;
67 if (
id > data[
size - 1].key() )
return size;
68 if (
id < data[0].key() )
return 0;
74 while ( bottom <= top ) {
75 middle = bottom + (top - bottom) / 2;
76 if (
id == data[middle].key() )
return -1;
77 if (
id < data[middle].key() ) {
81 if (
id > data[middle - 1].key() )
104 int binary_search_find(keyT
id)
const
107 int top = data.size() - 1;
110 if ( data.size() == 0 )
return -1;
111 while ( bottom <= top ) {
112 middle = bottom + (top - bottom) / 2;
113 if (
id == data[middle].key() )
115 else if (
id < data[middle].key() )
123 friend class ConfigGraph;
148 std::sort(data.begin(), data.end,
149 [](
const classT& lhs,
const classT& rhs) ->
bool { return lhs.key() < rhs.key(); });
153 using iterator =
typename std::vector<classT>::iterator;
154 using const_iterator =
typename std::vector<classT>::const_iterator;
168 int index = binary_search_insert(val.key());
169 if ( index == -1 )
return data[binary_search_find(val.key())];
170 iterator it = data.begin();
172 data.insert(it, val);
181 iterator
begin() {
return data.begin(); }
188 iterator
end() {
return data.end(); }
195 const_iterator
begin()
const {
return data.begin(); }
202 const_iterator
end()
const {
return data.end(); }
213 if ( binary_search_find(
id) == -1 )
return false;
229 int index = binary_search_find(
id);
249 int index = binary_search_find(
id);
266 size_t size() {
return data.size(); }
275class bad_filtered_key_error :
public std::runtime_error
278 explicit bad_filtered_key_error(
const std::string& what_arg) :
279 runtime_error(what_arg)
301template <
typename keyT,
typename classT>
315 std::vector<classT*> data;
316 int binary_search_insert(keyT
id)
const
321 int size = data.size();
324 if (
size == 0 )
return 0;
325 if (
id > data[
size - 1]->key() )
return size;
326 if (
id < data[0]->key() )
return 0;
332 while ( bottom <= top ) {
333 middle = bottom + (top - bottom) / 2;
334 if (
id == data[middle]->key() )
return -1;
335 if (
id < data[middle]->key() ) {
339 if (
id > data[middle - 1]->key() )
362 int binary_search_find(keyT
id)
const
365 int top = data.size() - 1;
368 if ( data.size() == 0 )
return -1;
369 while ( bottom <= top ) {
370 middle = bottom + (top - bottom) / 2;
371 if (
id == data[middle]->key() )
373 else if (
id < data[middle]->key() )
381 friend class ConfigGraph;
406 std::sort(data.begin(), data.end,
407 [](
const classT* lhs,
const classT* rhs) ->
bool { return lhs->key() < rhs->key(); });
411 using iterator =
typename std::vector<classT*>::iterator;
412 using const_iterator =
typename std::vector<classT*>::const_iterator;
426 int index = binary_search_insert(val->key());
427 if ( index == -1 )
return data[binary_search_find(val->key())];
428 iterator it = data.begin();
430 data.insert(it, val);
439 iterator
begin() {
return data.begin(); }
446 iterator
end() {
return data.end(); }
453 const_iterator
begin()
const {
return data.begin(); }
460 const_iterator
end()
const {
return data.end(); }
471 if ( binary_search_find(
id) == -1 )
return false;
487 int index = binary_search_find(
id);
507 int index = binary_search_find(
id);
524 size_t size() {
return data.size(); }
542 template <
typename filterT>
546 for (
size_t i = 0; i < data.size(); ++i ) {
547 auto key = data[i]->key();
548 data[write_ptr] = filt(data[i]);
549 if ( data[write_ptr] !=
nullptr ) {
551 if ( UNLIKELY(data[write_ptr]->key() != key) ) {
554 "ERROR: Filter object passed to SparseVectorMap::filter returned an object that had a "
555 "different value of key() than what was passed into the filter. The filter object must return "
556 "either an object with the same value of key(), or nullptr if the object should be removed.");
566 data.resize(write_ptr);
567 data.shrink_to_fit();
577template <
typename keyT>
591 std::vector<keyT> data;
592 int binary_search_insert(keyT
id)
const
597 int size = data.size();
600 if (
size == 0 )
return 0;
601 if (
id < data[0] )
return 0;
602 if (
id > data[
size - 1] )
return size;
608 while ( bottom <= top ) {
609 middle = bottom + (top - bottom) / 2;
610 if (
id == data[middle] )
return -1;
611 if (
id < data[middle] ) {
615 if (
id > data[middle - 1] )
638 int binary_search_find(keyT
id)
const
641 int top = data.size() - 1;
644 if ( data.size() == 0 )
return -1;
645 while ( bottom <= top ) {
646 middle = bottom + (top - bottom) / 2;
647 if (
id == data[middle] )
649 else if (
id < data[middle] )
657 friend class ConfigGraph;
682 std::sort(data.begin(), data.end, [](
const keyT& lhs,
const keyT& rhs) ->
bool { return lhs < rhs; });
686 using iterator =
typename std::vector<keyT>::iterator;
687 using const_iterator =
typename std::vector<keyT>::const_iterator;
701 int index = binary_search_insert(val);
702 if ( index == -1 )
return data[binary_search_find(val)];
703 iterator it = data.begin();
705 data.insert(it, val);
714 iterator
begin() {
return data.begin(); }
721 iterator
end() {
return data.end(); }
728 const_iterator
begin()
const {
return data.begin(); }
735 const_iterator
end()
const {
return data.end(); }
746 if ( binary_search_find(
id) == -1 )
return false;
762 int index = binary_search_find(
id);
782 int index = binary_search_find(
id);
799 size_t size() {
return data.size(); }
804namespace SST::Core::Serialization {
806template <
typename keyT,
typename classT>
812 SST_SER(v.data, options);
Base serialize class.
Definition serialize.h:110
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:45
size_t size()
Returns the number of items in the SparseVectorMap.
Definition sparseVectorMap.h:524
const classT * operator[](keyT id) const
Operator returns a const reference to data with the specified id.
Definition sparseVectorMap.h:505
iterator end()
Returns the end iterator to the underlying vector.
Definition sparseVectorMap.h:446
iterator begin()
Returns the begin iterator to the underlying vector.
Definition sparseVectorMap.h:439
void filter(filterT &filt)
Function to filter the contents of the SparseVectorMap.
Definition sparseVectorMap.h:543
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:402
bool contains(keyT id) const
Checks if the provided id is found in the SparseVectorMap.
Definition sparseVectorMap.h:469
void clear()
Clears the contents of the SparseVectorMap.
Definition sparseVectorMap.h:517
const_iterator begin() const
Returns the const begin iterator to the underlying vector.
Definition sparseVectorMap.h:453
classT * insert(classT *val)
Insert new value into SparseVectorMap.
Definition sparseVectorMap.h:424
SparseVectorMap()
Default constructor for SparseVectorMap.
Definition sparseVectorMap.h:387
const_iterator end() const
Returns the const end iterator to the underlying vector.
Definition sparseVectorMap.h:460
classT * operator[](keyT id)
Operator returns a reference to data with the specified id.
Definition sparseVectorMap.h:485
const_iterator begin() const
Returns the const begin iterator to the underlying vector.
Definition sparseVectorMap.h:728
size_t size()
Returns the number of items in the SparseVectorMap.
Definition sparseVectorMap.h:799
keyT & operator[](keyT id)
Operator returns a reference to data with the specified id.
Definition sparseVectorMap.h:760
void clear()
Clears the contents of the SparseVectorMap.
Definition sparseVectorMap.h:792
const_iterator end() const
Returns the const end iterator to the underlying vector.
Definition sparseVectorMap.h:735
iterator end()
Returns the end iterator to the underlying vector.
Definition sparseVectorMap.h:721
keyT & insert(const keyT &val)
Insert new value into SparseVectorMap.
Definition sparseVectorMap.h:699
const keyT & operator[](keyT id) const
Operator returns a const reference to data with the specified id.
Definition sparseVectorMap.h:780
iterator begin()
Returns the begin iterator to the underlying vector.
Definition sparseVectorMap.h:714
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:678
SparseVectorMap()
Default constructor for SparseVectorMap.
Definition sparseVectorMap.h:663
bool contains(keyT id)
Checks if the provided id is found in the SparseVectorMap.
Definition sparseVectorMap.h:744
Class that stores data in a vector, but can access the data similar to a map.
Definition sparseVectorMap.h:45
const_iterator begin() const
Returns the const begin iterator to the underlying vector.
Definition sparseVectorMap.h:195
const_iterator end() const
Returns the const end iterator to the underlying vector.
Definition sparseVectorMap.h:202
classT & operator[](keyT id)
Operator returns a reference to data with the specified id.
Definition sparseVectorMap.h:227
SparseVectorMap()
Default constructor for SparseVectorMap.
Definition sparseVectorMap.h:129
iterator end()
Returns the end iterator to the underlying vector.
Definition sparseVectorMap.h:188
iterator begin()
Returns the begin iterator to the underlying vector.
Definition sparseVectorMap.h:181
bool contains(keyT id) const
Checks if the provided id is found in the SparseVectorMap.
Definition sparseVectorMap.h:211
void clear()
Clears the contents of the SparseVectorMap.
Definition sparseVectorMap.h:259
classT & insert(const classT &val)
Insert new value into SparseVectorMap.
Definition sparseVectorMap.h:166
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:144
size_t size()
Returns the number of items in the SparseVectorMap.
Definition sparseVectorMap.h:266
const classT & operator[](keyT id) const
Operator returns a const reference to data with the specified id.
Definition sparseVectorMap.h:247
Exception that is thrown by SparseVectorMap::filter() if the filtering object returns an object that ...
Definition sparseVectorMap.h:276