Adding remove demand method to toolkit

This commit is contained in:
Michael Tryby
2019-04-24 16:30:27 -04:00
parent d927851d20
commit dc3c2ffe1a
2 changed files with 16 additions and 18 deletions

View File

@@ -2527,12 +2527,12 @@ int DLLEXPORT EN_setjuncdata(EN_Project p, int index, double elev,
}
int DLLEXPORT EN_adddemand(EN_Project p, int node_index, double demand,
char *demand_pattern, const char *category_name, int *demand_index)
char *demand_pattern, const char *category_name, int *demand_key)
{
Network *net = &p->network;
int pattern_index, error = 0;
*demand_index = -1;
*demand_key = -1;
if (error = EN_getpatternindex(p, demand_pattern, &pattern_index) != 0) return error;
@@ -2541,29 +2541,27 @@ int DLLEXPORT EN_adddemand(EN_Project p, int node_index, double demand,
if (!demand_list) {
demand_list = create_demand_list(demand/p->Ucf[FLOW], pattern_index, category_name);
if (!demand_list) return 101;
Node[node_index].D = demand_list;
}
else {
demand_data_t *demand_data = create_demand_data(demand/p->Ucf[FLOW], pattern_index, category_name);
if (!demand_data) return 101;
append_list(demand_list, &demand_data);
*demand_key = append_list(demand_list, &demand_data);
}
*demand_index = size_list(demand_list);
return 0;
}
int DLLEXPORT EN_removedemand(EN_Project p, int node_index, int demand_index) {
// Problem: Removing a demand will in most cases invalidate the index
// returned previously in EN_adddemand(). This occurs in all cases except
// when the demand at the tail of the list is removed. This is why indexing
// is a flawed strategy for random access to a list data structure.
// One possible solution is to have the user be responsible for creating a
// unique category name. Another possible solution would be for the
// application to create a unique key for each demand entry. They have
// random access based on searching for a key value in the list.
int DLLEXPORT EN_removedemand(EN_Project p, int node_index, int demand_key)
{
Network *net = &p->network;
Snode *Node = net->Node;
list_t *dlist = Node[node_index].D;
remove_node(dlist, search_list(dlist, demand_key));
return 0;
}

View File

@@ -84,15 +84,15 @@ BOOST_AUTO_TEST_CASE(test_categories_reopen, * boost::unit_test::depends_on("tes
BOOST_FIXTURE_TEST_CASE(test_adddemand, FixtureSingleNode)
{
int demand_index;
int demand_key;
error = EN_adddemand(ph, node_qhut, 100.0, "PrimaryPattern", "PrimaryDemand", &demand_index);
error = EN_adddemand(ph, node_qhut, 100.0, "PrimaryPattern", "PrimaryDemand", &demand_key);
BOOST_CHECK(error != 0);
error = EN_addpattern(ph, (char *)"PrimaryPattern");
BOOST_REQUIRE(error == 0);
error = EN_adddemand(ph, node_qhut, 100.0, "PrimaryPattern", "PrimaryDemand", &demand_index);
error = EN_adddemand(ph, node_qhut, 100.0, "PrimaryPattern", "PrimaryDemand", &demand_key);
BOOST_CHECK(error == 0);
}