BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
CollectionRegistry.cpp
Go to the documentation of this file.
2#include "Keywords.h"
3#include <strutil.h>
4#include <cassert>
5
6using namespace BPMNOS;
7
9 // register empty collection with index 0
10 (*this)(std::vector<double>());
11}
12
13const std::vector<double>& CollectionRegistry::operator[](size_t i) const {
14 assert( i < registeredCollections.size() );
15 std::shared_lock read_lock(registryMutex);
16 return registeredCollections[i];
17}
18
19size_t CollectionRegistry::operator()(const std::vector<double>& collection) {
20 std::shared_lock read_lock(registryMutex);
21 if ( auto it = index.find(collection);
22 it != index.end()
23 ) {
24 return it->second;
25 }
26 read_lock.unlock();
27
28 std::unique_lock write_lock(registryMutex);
29 auto [it, inserted] = index.try_emplace(collection, index.size());
30
31 if ( !inserted ) {
32 assert( index.size() == registeredCollections.size() );
33 return it->second;
34 }
35
36 registeredCollections.push_back(collection);
37
38 assert( index.size() == registeredCollections.size() );
39 return registeredCollections.size()-1;
40}
41
43 std::shared_lock read_lock(registryMutex);
44 return registeredCollections.size();
45}
46
48 std::unique_lock write_lock(registryMutex);
49 index.clear();
50 registeredCollections.clear();
51
52 index.emplace(std::vector<double>(),0);
53 registeredCollections.push_back(std::vector<double>());
54
55 assert( index.size() == registeredCollections.size() );
56}
57
58// Create global registry
CollectionRegistry collectionRegistry
iterator find(const Key &key)
Definition vector_map.h:138
iterator end() noexcept
Definition vector_map.h:83
std::pair< iterator, bool > emplace(const Key &key, const Value &value)
Definition vector_map.h:113
void clear() noexcept
Definition vector_map.h:105
size_type size() const noexcept
Definition vector_map.h:100
std::pair< iterator, bool > try_emplace(const Key &key, const Value &value)
Definition vector_map.h:117
Utility class for representing collections by numeric values.
const std::vector< double > & operator[](size_t i) const
Operator providing access to a registered collections by index.
size_t operator()(const std::vector< double > &collection)
Operator to register a collection by its values and return its index.
CollectionRegistry()
Constructor adds empty collection at index 0.