Skip to content

Commit

Permalink
[clib] Add reverse lookup table to SharedCabinet
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Mar 5, 2023
1 parent 6d24884 commit e8f2d65
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/clib/Cabinet.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define CT_CABINET_H

#include "cantera/base/ctexceptions.h"
#include <unordered_map>

namespace Cantera {

Expand Down Expand Up @@ -216,7 +217,7 @@ class Cabinet
* maintained by the appropriate SharedCabinet<M> instance. The pointer is retrieved
* from the list by the interface function, the desired method is invoked, and the
* result returned to the non-C++ calling procedure. By storing the pointers in a
* SharedCabinet, there is no need to encode them in a std::string or integer and pass
* SharedCabinet, there is no need to encode them in a string or integer and pass
* them out to the non-C++ calling routine, as some other interfacing schemes do.
*
* The SharedCabinet<M> class can be used to store pointers to arbitrary objects. In
Expand All @@ -243,7 +244,8 @@ template<class M>
class SharedCabinet
{
public:
typedef std::vector<std::shared_ptr<M>>& dataRef;
typedef vector<shared_ptr<M>>& dataRef;
typedef std::unordered_map<const M*, int>& lookupRef;

/**
* Constructor.
Expand All @@ -253,10 +255,12 @@ class SharedCabinet
/**
* Add a new object. The index of the object is returned.
*/
static int add(std::shared_ptr<M> obj) {
static int add(shared_ptr<M> obj) {
dataRef data = getData();
data.push_back(obj);
int index = data.size() - 1;
lookupRef lookup = getLookup();
lookup[obj.get()] = index;
return index;
}

Expand Down Expand Up @@ -284,6 +288,7 @@ class SharedCabinet
*/
static int reset() {
getData().clear();
getLookup().clear();
return 0;
}

Expand All @@ -293,6 +298,7 @@ class SharedCabinet
static void del(size_t n) {
dataRef data = getData();
if (n >= 0 && n < data.size()) {
getLookup().erase(data[n].get());
data[n].reset();
} else {
throw CanteraError("SharedCabinet::del",
Expand All @@ -303,7 +309,7 @@ class SharedCabinet
/**
* Return a shared pointer to object n.
*/
static std::shared_ptr<M>& at(size_t n) {
static shared_ptr<M>& at(size_t n) {
dataRef data = getData();
if (n < 0 || n >= data.size()) {
throw CanteraError("SharedCabinet::at", "Index {} out of range.", n);
Expand Down Expand Up @@ -340,15 +346,11 @@ class SharedCabinet
* object is not in the SharedCabinet.
*/
static int index(const M& obj) {
dataRef data = getData();
int count = 0;
for (const auto& item : data) {
if (item.get() == &obj) {
return count;
}
count++;
lookupRef lookup = getLookup();
if (!lookup.count(&obj)) {
return -1;
}
return -1;
return lookup.at(&obj);
}

private:
Expand All @@ -364,15 +366,32 @@ class SharedCabinet
return s_storage->m_table;
}

/**
* Static function that returns a pointer to the reverse lookup table of
* the singleton SharedCabinet<M> instance. All member functions should
* access the lookup table through this function.
*/
static lookupRef getLookup() {
if (s_storage == nullptr) {
s_storage = new SharedCabinet<M>();
}
return s_storage->m_lookup;
}

/**
* Pointer to the single instance of this class.
*/
static SharedCabinet<M>* s_storage;

/**
* Reverse lookup table for the single instance of this class.
*/
std::unordered_map<const M*, int> m_lookup;

/**
* list to hold pointers to objects.
*/
std::vector<std::shared_ptr<M>> m_table;
vector<shared_ptr<M>> m_table;
};

}
Expand Down

0 comments on commit e8f2d65

Please sign in to comment.