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, 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; Network *net = &p->network;
int pattern_index, error = 0; int pattern_index, error = 0;
*demand_index = -1; *demand_key = -1;
if (error = EN_getpatternindex(p, demand_pattern, &pattern_index) != 0) return error; if (error = EN_getpatternindex(p, demand_pattern, &pattern_index) != 0) return error;
@@ -2548,22 +2548,20 @@ int DLLEXPORT EN_adddemand(EN_Project p, int node_index, double demand,
demand_data_t *demand_data = create_demand_data(demand/p->Ucf[FLOW], pattern_index, category_name); demand_data_t *demand_data = create_demand_data(demand/p->Ucf[FLOW], pattern_index, category_name);
if (!demand_data) return 101; 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; return 0;
} }
int DLLEXPORT EN_removedemand(EN_Project p, int node_index, int demand_index) { int DLLEXPORT EN_removedemand(EN_Project p, int node_index, int demand_key)
// Problem: Removing a demand will in most cases invalidate the index {
// returned previously in EN_adddemand(). This occurs in all cases except Network *net = &p->network;
// when the demand at the tail of the list is removed. This is why indexing Snode *Node = net->Node;
// 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 list_t *dlist = Node[node_index].D;
// unique category name. Another possible solution would be for the
// application to create a unique key for each demand entry. They have remove_node(dlist, search_list(dlist, demand_key));
// random access based on searching for a key value in the list.
return 0; 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) 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); BOOST_CHECK(error != 0);
error = EN_addpattern(ph, (char *)"PrimaryPattern"); error = EN_addpattern(ph, (char *)"PrimaryPattern");
BOOST_REQUIRE(error == 0); 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); BOOST_CHECK(error == 0);
} }