Modified re-factored version of hash.c
This commit is contained in:
@@ -850,7 +850,6 @@ int DLLEXPORT EN_close(EN_ProjectHandle ph)
|
|||||||
out_file_t *out;
|
out_file_t *out;
|
||||||
|
|
||||||
EN_Project *p = (EN_Project*)ph;
|
EN_Project *p = (EN_Project*)ph;
|
||||||
|
|
||||||
if (p->Openflag) {
|
if (p->Openflag) {
|
||||||
writetime(p, FMT105);
|
writetime(p, FMT105);
|
||||||
}
|
}
|
||||||
|
|||||||
37
src/hash.c
37
src/hash.c
@@ -1,11 +1,11 @@
|
|||||||
/*-----------------------------------------------------------------------------
|
/*-----------------------------------------------------------------------------
|
||||||
** hash.c
|
** hash.c
|
||||||
**
|
**
|
||||||
** Implementation of a simple Hash Table that uses a string pointer
|
** Implementation of a simple Hash Table that uses a string as a key
|
||||||
** as a key and an associated integer as data.
|
** and an associated integer as data.
|
||||||
**
|
**
|
||||||
** Written by L. Rossman
|
** Written by L. Rossman
|
||||||
** Last Updated on 10/17/18
|
** Last Updated on 10/19/18
|
||||||
**
|
**
|
||||||
** Interface Functions:
|
** Interface Functions:
|
||||||
** hashtable_create - creates a hash table
|
** hashtable_create - creates a hash table
|
||||||
@@ -47,6 +47,14 @@ unsigned int gethash(char *str)
|
|||||||
return retHash;
|
return retHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *dupstr(const char *s)
|
||||||
|
{
|
||||||
|
size_t size = strlen(s) + 1;
|
||||||
|
char *p = malloc(size);
|
||||||
|
if (p) memcpy(p, s, size);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
HashTable *hashtable_create()
|
HashTable *hashtable_create()
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@@ -65,7 +73,7 @@ int hashtable_insert(HashTable *ht, char *key, int data)
|
|||||||
if ( i >= HASHTABLEMAXSIZE ) return 0;
|
if ( i >= HASHTABLEMAXSIZE ) return 0;
|
||||||
entry = (DataEntry *) malloc(sizeof(DataEntry));
|
entry = (DataEntry *) malloc(sizeof(DataEntry));
|
||||||
if (entry == NULL) return(0);
|
if (entry == NULL) return(0);
|
||||||
entry->key = key;
|
entry->key = dupstr(key);
|
||||||
entry->data = data;
|
entry->data = data;
|
||||||
entry->next = ht[i];
|
entry->next = ht[i];
|
||||||
ht[i] = entry;
|
ht[i] = entry;
|
||||||
@@ -76,6 +84,7 @@ int hashtable_update(HashTable *ht, char *key, int new_data)
|
|||||||
{
|
{
|
||||||
unsigned int i = gethash(key);
|
unsigned int i = gethash(key);
|
||||||
DataEntry *entry;
|
DataEntry *entry;
|
||||||
|
|
||||||
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
||||||
entry = ht[i];
|
entry = ht[i];
|
||||||
while (entry != NULL)
|
while (entry != NULL)
|
||||||
@@ -97,23 +106,15 @@ int hashtable_delete(HashTable *ht, char *key)
|
|||||||
|
|
||||||
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
||||||
|
|
||||||
// Check if first entry in bucket i to be deleted
|
preventry = NULL;
|
||||||
entry = ht[i];
|
entry = ht[i];
|
||||||
if (strcmp(entry->key, key) == 0)
|
|
||||||
{
|
|
||||||
ht[i] = entry->next;
|
|
||||||
free(entry);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check remaining entries in bucket i
|
|
||||||
preventry = ht[i];
|
|
||||||
entry = ht[i]->next;
|
|
||||||
while (entry != NULL)
|
while (entry != NULL)
|
||||||
{
|
{
|
||||||
if (strcmp(entry->key, key) == 0)
|
if (strcmp(entry->key, key) == 0)
|
||||||
{
|
{
|
||||||
preventry->next = entry->next;
|
if (preventry == NULL) ht[i] = entry->next;
|
||||||
|
else preventry->next = entry->next;
|
||||||
|
free(entry->key);
|
||||||
free(entry);
|
free(entry);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -127,6 +128,7 @@ int hashtable_find(HashTable *ht, char *key)
|
|||||||
{
|
{
|
||||||
unsigned int i = gethash(key);
|
unsigned int i = gethash(key);
|
||||||
DataEntry *entry;
|
DataEntry *entry;
|
||||||
|
|
||||||
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
||||||
entry = ht[i];
|
entry = ht[i];
|
||||||
while (entry != NULL)
|
while (entry != NULL)
|
||||||
@@ -158,15 +160,18 @@ void hashtable_free(HashTable *ht)
|
|||||||
{
|
{
|
||||||
DataEntry *entry, *nextentry;
|
DataEntry *entry, *nextentry;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < HASHTABLEMAXSIZE; i++)
|
for (i = 0; i < HASHTABLEMAXSIZE; i++)
|
||||||
{
|
{
|
||||||
entry = ht[i];
|
entry = ht[i];
|
||||||
while (entry != NULL)
|
while (entry != NULL)
|
||||||
{
|
{
|
||||||
nextentry = entry->next;
|
nextentry = entry->next;
|
||||||
|
free(entry->key);
|
||||||
free(entry);
|
free(entry);
|
||||||
entry = nextentry;
|
entry = nextentry;
|
||||||
}
|
}
|
||||||
|
ht[i] = NULL;
|
||||||
}
|
}
|
||||||
free(ht);
|
free(ht);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// Test of ENsetid EPANET API Function
|
// Test of ENsetid EPANET API Function
|
||||||
|
#define _CRT_SECURE_NO_DEPRECATE
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This is a test for the API functions that change a node or link ID name.
|
This is a test for the API functions that change a node or link ID name.
|
||||||
@@ -11,9 +12,9 @@ A node and link name are changed, the network is saved, reopened and the new nam
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "epanet2.h"
|
#include "epanet2.h"
|
||||||
|
|
||||||
#define DATA_PATH_INP "net1.inp"
|
#define DATA_PATH_INP "./net1.inp"
|
||||||
#define DATA_PATH_RPT "test.rpt"
|
#define DATA_PATH_RPT "./test.rpt"
|
||||||
#define DATA_PATH_OUT ""
|
#define DATA_PATH_OUT "./test.out"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -25,56 +26,62 @@ BOOST_AUTO_TEST_CASE(test_setid)
|
|||||||
int index;
|
int index;
|
||||||
string newid;
|
string newid;
|
||||||
|
|
||||||
|
EN_ProjectHandle ph = NULL;
|
||||||
|
EN_createproject(&ph);
|
||||||
|
|
||||||
std::string path_inp = std::string(DATA_PATH_INP);
|
std::string path_inp = std::string(DATA_PATH_INP);
|
||||||
std::string path_rpt = std::string(DATA_PATH_RPT);
|
std::string path_rpt = std::string(DATA_PATH_RPT);
|
||||||
std::string path_out = std::string(DATA_PATH_OUT);
|
std::string path_out = std::string(DATA_PATH_OUT);
|
||||||
|
|
||||||
error = ENopen(path_inp.c_str(), path_rpt.c_str(), "");
|
error = EN_open(ph, path_inp.c_str(), path_rpt.c_str(), "");
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
|
|
||||||
// Test of illegal node name change
|
// Test of illegal node name change
|
||||||
newid = "Illegal; node name";
|
newid = "Illegal; node name";
|
||||||
error = ENsetnodeid(3, (char *)newid.c_str());
|
error = EN_setnodeid(ph, 3, (char *)newid.c_str());
|
||||||
BOOST_REQUIRE(error > 0);
|
BOOST_REQUIRE(error > 0);
|
||||||
|
|
||||||
// Test of legal node name change
|
// Test of legal node name change
|
||||||
newid = "Node3";
|
newid = "Node3";
|
||||||
error = ENsetnodeid(3, (char *)newid.c_str());
|
error = EN_setnodeid(ph, 3, (char *)newid.c_str());
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
|
|
||||||
// Test of illegal link name change
|
// Test of illegal link name change
|
||||||
newid = "Illegal; link name";
|
newid = "Illegal; link name";
|
||||||
error = ENsetlinkid(3, (char *)newid.c_str());
|
error = EN_setlinkid(ph, 3, (char *)newid.c_str());
|
||||||
BOOST_REQUIRE(error > 0);
|
BOOST_REQUIRE(error > 0);
|
||||||
|
|
||||||
// Test of legal link name change
|
// Test of legal link name change
|
||||||
newid = "Link3";
|
newid = "Link3";
|
||||||
error = ENsetlinkid(3, (char *)newid.c_str());
|
error = EN_setlinkid(ph, 3, (char *)newid.c_str());
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
|
|
||||||
// Save the project
|
// Save the project
|
||||||
error = ENsaveinpfile("net1_setid.inp");
|
error = EN_saveinpfile(ph, "net1_setid.inp");
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
|
|
||||||
error = ENclose();
|
error = EN_close(ph);
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
|
EN_deleteproject(&ph);
|
||||||
|
|
||||||
// Re-open the saved project
|
// Re-open the saved project
|
||||||
error = ENopen("net1_setid.inp", path_rpt.c_str(), "");
|
EN_createproject(&ph);
|
||||||
|
error = EN_open(ph, "net1_setid.inp", path_rpt.c_str(), "");
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
|
|
||||||
// Check that 3rd node has its new name
|
// Check that 3rd node has its new name
|
||||||
error = ENgetnodeindex("Node3", &index);
|
error = EN_getnodeindex(ph, "Node3", &index);
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
BOOST_REQUIRE(index == 3);
|
BOOST_REQUIRE(index == 3);
|
||||||
|
|
||||||
// Check that 3rd link has its new name
|
// Check that 3rd link has its new name
|
||||||
error = ENgetlinkindex("Link3", &index);
|
error = EN_getlinkindex(ph, "Link3", &index);
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
BOOST_REQUIRE(index == 3);
|
BOOST_REQUIRE(index == 3);
|
||||||
|
|
||||||
error = ENclose();
|
error = EN_close(ph);
|
||||||
BOOST_REQUIRE(error == 0);
|
BOOST_REQUIRE(error == 0);
|
||||||
|
EN_deleteproject(&ph);
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|||||||
Reference in New Issue
Block a user