mirror of
https://github.com/JHUAPL/kami.git
synced 2026-01-08 22:38:03 -05:00
Make shared_ptrs that can be unique, unique
This commit is contained in:
@@ -85,7 +85,7 @@ namespace kami {
|
||||
*
|
||||
* @returns a `std::vector` of all the `AgentID`'s in the `Population`
|
||||
*/
|
||||
[[nodiscard]] std::shared_ptr<std::vector<AgentID>> get_agent_list() const;
|
||||
[[nodiscard]] std::unique_ptr<std::vector<AgentID>> get_agent_list() const;
|
||||
};
|
||||
} // namespace kami
|
||||
|
||||
|
||||
@@ -77,7 +77,8 @@ namespace kami {
|
||||
*
|
||||
* @returns returns vector of agents successfully stepped
|
||||
*/
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>>
|
||||
step(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) override;
|
||||
|
||||
/**
|
||||
* @brief Set the RNG
|
||||
|
||||
@@ -63,7 +63,7 @@ namespace kami {
|
||||
*
|
||||
* @returns returns vector of agents successfully stepped
|
||||
*/
|
||||
virtual std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model) = 0;
|
||||
virtual std::optional<std::unique_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model) = 0;
|
||||
|
||||
/**
|
||||
* @brief Execute a single time step.
|
||||
@@ -78,7 +78,8 @@ namespace kami {
|
||||
*
|
||||
* @returns returns vector of agents successfully stepped
|
||||
*/
|
||||
virtual std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) = 0;
|
||||
virtual std::optional<std::unique_ptr<std::vector<AgentID>>>
|
||||
step(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) = 0;
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace kami {
|
||||
*
|
||||
* @returns returns vector of agents successfully stepped
|
||||
*/
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model) override;
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model) override;
|
||||
|
||||
/**
|
||||
* @brief Execute a single time step.
|
||||
@@ -77,7 +77,8 @@ namespace kami {
|
||||
*
|
||||
* @returns returns vector of agents successfully stepped
|
||||
*/
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>>
|
||||
step(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) override;
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace kami {
|
||||
*
|
||||
* @returns returns vector of agents successfully advanced
|
||||
*/
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> advance(std::shared_ptr<Model> model);
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>> advance(std::shared_ptr<Model> model);
|
||||
|
||||
/**
|
||||
* @brief Advance a single time step.
|
||||
@@ -76,7 +76,8 @@ namespace kami {
|
||||
*
|
||||
* @returns returns vector of agents successfully advanced
|
||||
*/
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> advance(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list);
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>>
|
||||
advance(const std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list);
|
||||
|
||||
public:
|
||||
/**
|
||||
@@ -92,7 +93,8 @@ namespace kami {
|
||||
*
|
||||
* @returns returns vector of agents successfully stepped
|
||||
*/
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) override;
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>>
|
||||
step(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) override;
|
||||
};
|
||||
|
||||
} // namespace kami
|
||||
|
||||
@@ -57,9 +57,9 @@ namespace kami {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::shared_ptr<std::vector<AgentID>> Population::get_agent_list() const {
|
||||
auto key_selector = [](auto pair){ return pair.first; };
|
||||
auto agent_ids = std::make_shared<std::vector<AgentID>>(_agent_map.size());
|
||||
std::unique_ptr<std::vector<AgentID>> Population::get_agent_list() const {
|
||||
auto key_selector = [](auto pair) { return pair.first; };
|
||||
auto agent_ids = std::make_unique<std::vector<AgentID>>(_agent_map.size());
|
||||
|
||||
transform(_agent_map.begin(), _agent_map.end(), agent_ids->begin(), key_selector);
|
||||
return std::move(agent_ids);
|
||||
|
||||
@@ -40,12 +40,13 @@ namespace kami {
|
||||
this->_rng = std::move(rng);
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> RandomScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>>
|
||||
RandomScheduler::step(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) {
|
||||
if (_rng == nullptr)
|
||||
return std::nullopt;
|
||||
|
||||
shuffle(agent_list->begin(), agent_list->end(), *_rng);
|
||||
return std::move(this->SequentialScheduler::step(model, agent_list));
|
||||
return std::move(this->SequentialScheduler::step(model, std::move(agent_list)));
|
||||
}
|
||||
|
||||
std::shared_ptr<std::ranlux24> RandomScheduler::set_rng(std::shared_ptr<std::ranlux24> rng) {
|
||||
|
||||
@@ -32,27 +32,28 @@
|
||||
|
||||
namespace kami {
|
||||
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> SequentialScheduler::step(std::shared_ptr<Model> model) {
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>> SequentialScheduler::step(std::shared_ptr<Model> model) {
|
||||
auto population = model->get_population();
|
||||
|
||||
if(!population)
|
||||
if (!population)
|
||||
return std::nullopt;
|
||||
|
||||
return std::move(this->step(model, population.value()->get_agent_list()));
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> SequentialScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
auto return_agent_list = std::make_shared<std::vector<AgentID>>();
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>>
|
||||
SequentialScheduler::step(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) {
|
||||
auto return_agent_list = std::make_unique<std::vector<AgentID>>();
|
||||
auto population = model->get_population();
|
||||
|
||||
if(!population)
|
||||
if (!population)
|
||||
return std::nullopt;
|
||||
|
||||
Scheduler::_step_counter++;
|
||||
for(auto & agent_id : *agent_list) {
|
||||
for (auto &agent_id: *agent_list) {
|
||||
auto agent_opt = population.value()->get_agent_by_id(agent_id);
|
||||
|
||||
if(agent_opt) {
|
||||
if (agent_opt) {
|
||||
auto agent = agent_opt.value();
|
||||
|
||||
agent->step(model);
|
||||
|
||||
@@ -33,31 +33,35 @@
|
||||
|
||||
namespace kami {
|
||||
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> StagedScheduler::step(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
this->SequentialScheduler::step(model, agent_list);
|
||||
return std::move(this->advance(model, agent_list));
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>>
|
||||
StagedScheduler::step(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) {
|
||||
auto stepped_agent_list = this->SequentialScheduler::step(model, std::move(agent_list));
|
||||
if (!stepped_agent_list)
|
||||
return std::nullopt;
|
||||
return std::move(this->advance(model, std::move(stepped_agent_list.value())));
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> StagedScheduler::advance(std::shared_ptr<Model> model) {
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>> StagedScheduler::advance(std::shared_ptr<Model> model) {
|
||||
auto population = model->get_population();
|
||||
|
||||
if(!population)
|
||||
if (!population)
|
||||
return std::nullopt;
|
||||
|
||||
return std::move(this->advance(model, population.value()->get_agent_list()));
|
||||
}
|
||||
|
||||
std::optional<std::shared_ptr<std::vector<AgentID>>> StagedScheduler::advance(std::shared_ptr<Model> model, std::shared_ptr<std::vector<AgentID>> agent_list) {
|
||||
auto return_agent_list = std::make_shared<std::vector<AgentID>>();
|
||||
std::optional<std::unique_ptr<std::vector<AgentID>>>
|
||||
StagedScheduler::advance(std::shared_ptr<Model> model, std::unique_ptr<std::vector<AgentID>> agent_list) {
|
||||
auto return_agent_list = std::make_unique<std::vector<AgentID>>();
|
||||
auto population = model->get_population();
|
||||
|
||||
if(!population)
|
||||
if (!population)
|
||||
return std::nullopt;
|
||||
|
||||
for(auto & agent_id : *agent_list) {
|
||||
for (auto &agent_id: *agent_list) {
|
||||
auto agent_opt = population.value()->get_agent_by_id(agent_id);
|
||||
|
||||
if(agent_opt) {
|
||||
if (agent_opt) {
|
||||
auto agent = std::static_pointer_cast<StagedAgent>(agent_opt.value());
|
||||
|
||||
agent->advance(model);
|
||||
|
||||
@@ -52,7 +52,7 @@ public:
|
||||
return _sched->step(shared_from_this());
|
||||
}
|
||||
|
||||
optional<shared_ptr<vector<AgentID>>> step(shared_ptr<vector<AgentID>> agent_list) {
|
||||
optional<shared_ptr<vector<AgentID>>> step(unique_ptr<vector<AgentID>> agent_list) {
|
||||
return _sched->step(shared_from_this(), move(agent_list));
|
||||
}
|
||||
};
|
||||
@@ -104,7 +104,8 @@ TEST_F(RandomSchedulerTest, step_interface1) {
|
||||
|
||||
TEST_F(RandomSchedulerTest, step_interface2) {
|
||||
auto tval = mod->get_population().value()->get_agent_list();
|
||||
auto rval = mod->step(tval);
|
||||
auto aval = mod->get_population().value()->get_agent_list();
|
||||
auto rval = mod->step(std::move(aval));
|
||||
|
||||
EXPECT_TRUE(rval);
|
||||
EXPECT_EQ(rval.value()->size(), 10);
|
||||
|
||||
@@ -50,7 +50,7 @@ public:
|
||||
return _sched->step(shared_from_this());
|
||||
}
|
||||
|
||||
optional<shared_ptr<vector<AgentID>>> step(shared_ptr<vector<AgentID>> agent_list) {
|
||||
optional<shared_ptr<vector<AgentID>>> step(unique_ptr<vector<AgentID>> agent_list) {
|
||||
return _sched->step(shared_from_this(), move(agent_list));
|
||||
}
|
||||
};
|
||||
@@ -95,7 +95,8 @@ TEST_F(SequentialSchedulerTest, step_interface1) {
|
||||
|
||||
TEST_F(SequentialSchedulerTest, step_interface2) {
|
||||
auto tval = mod->get_population().value()->get_agent_list();
|
||||
auto rval = mod->step(tval);
|
||||
auto aval = mod->get_population().value()->get_agent_list();
|
||||
auto rval = mod->step(std::move(aval));
|
||||
|
||||
EXPECT_TRUE(rval);
|
||||
EXPECT_EQ(rval.value()->size(), 10);
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -54,7 +55,7 @@ public:
|
||||
return _sched->step(shared_from_this());
|
||||
}
|
||||
|
||||
optional<shared_ptr<vector<AgentID>>> step(shared_ptr<vector<AgentID>> agent_list) {
|
||||
optional<shared_ptr<vector<AgentID>>> step(unique_ptr<vector<AgentID>> agent_list) {
|
||||
return _sched->step(shared_from_this(), move(agent_list));
|
||||
}
|
||||
};
|
||||
@@ -99,7 +100,8 @@ TEST_F(StagedSchedulerTest, step_interface1) {
|
||||
|
||||
TEST_F(StagedSchedulerTest, step_interface2) {
|
||||
auto tval = mod->get_population().value()->get_agent_list();
|
||||
auto rval = mod->step(tval);
|
||||
auto aval = mod->get_population().value()->get_agent_list();
|
||||
auto rval = mod->step(std::move(aval));
|
||||
|
||||
EXPECT_TRUE(rval);
|
||||
EXPECT_EQ(rval.value()->size(), 10);
|
||||
|
||||
Reference in New Issue
Block a user