epyt-flow 0.14.0__py3-none-any.whl → 0.14.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- epyt_flow/EPANET/EPANET/SRC_engines/AUTHORS +40 -8
- epyt_flow/EPANET/EPANET/SRC_engines/LICENSE +3 -3
- epyt_flow/EPANET/EPANET/SRC_engines/enumstxt.h +24 -7
- epyt_flow/EPANET/EPANET/SRC_engines/epanet.c +726 -374
- epyt_flow/EPANET/EPANET/SRC_engines/epanet2.c +128 -32
- epyt_flow/EPANET/EPANET/SRC_engines/errors.dat +7 -1
- epyt_flow/EPANET/EPANET/SRC_engines/flowbalance.c +186 -0
- epyt_flow/EPANET/EPANET/SRC_engines/funcs.h +40 -14
- epyt_flow/EPANET/EPANET/SRC_engines/hash.c +177 -177
- epyt_flow/EPANET/EPANET/SRC_engines/hash.h +28 -28
- epyt_flow/EPANET/EPANET/SRC_engines/hydcoeffs.c +192 -40
- epyt_flow/EPANET/EPANET/SRC_engines/hydraul.c +101 -46
- epyt_flow/EPANET/EPANET/SRC_engines/hydsolver.c +85 -24
- epyt_flow/EPANET/EPANET/SRC_engines/hydstatus.c +29 -63
- epyt_flow/EPANET/EPANET/SRC_engines/include/epanet2.h +70 -37
- epyt_flow/EPANET/EPANET/SRC_engines/include/epanet2_2.h +408 -234
- epyt_flow/EPANET/EPANET/SRC_engines/include/epanet2_enums.h +87 -37
- epyt_flow/EPANET/EPANET/SRC_engines/inpfile.c +153 -79
- epyt_flow/EPANET/EPANET/SRC_engines/input1.c +59 -94
- epyt_flow/EPANET/EPANET/SRC_engines/input2.c +73 -202
- epyt_flow/EPANET/EPANET/SRC_engines/input3.c +446 -351
- epyt_flow/EPANET/EPANET/SRC_engines/leakage.c +527 -0
- epyt_flow/EPANET/EPANET/SRC_engines/mempool.c +8 -4
- epyt_flow/EPANET/EPANET/SRC_engines/mempool.h +23 -23
- epyt_flow/EPANET/EPANET/SRC_engines/output.c +5 -4
- epyt_flow/EPANET/EPANET/SRC_engines/project.c +407 -75
- epyt_flow/EPANET/EPANET/SRC_engines/quality.c +12 -2
- epyt_flow/EPANET/EPANET/SRC_engines/qualreact.c +70 -13
- epyt_flow/EPANET/EPANET/SRC_engines/qualroute.c +7 -5
- epyt_flow/EPANET/EPANET/SRC_engines/report.c +88 -20
- epyt_flow/EPANET/EPANET/SRC_engines/rules.c +144 -6
- epyt_flow/EPANET/EPANET/SRC_engines/smatrix.c +19 -19
- epyt_flow/EPANET/EPANET/SRC_engines/text.h +16 -5
- epyt_flow/EPANET/EPANET/SRC_engines/types.h +73 -19
- epyt_flow/EPANET/EPANET/SRC_engines/util/cstr_helper.c +59 -0
- epyt_flow/EPANET/EPANET/SRC_engines/util/cstr_helper.h +38 -0
- epyt_flow/EPANET/EPANET/SRC_engines/util/errormanager.c +92 -0
- epyt_flow/EPANET/EPANET/SRC_engines/util/errormanager.h +39 -0
- epyt_flow/EPANET/EPANET/SRC_engines/util/filemanager.c +212 -0
- epyt_flow/EPANET/EPANET/SRC_engines/util/filemanager.h +81 -0
- epyt_flow/EPANET/EPANET/SRC_engines/validate.c +408 -0
- epyt_flow/EPANET/compile_linux.sh +1 -1
- epyt_flow/EPANET/compile_macos.sh +1 -1
- epyt_flow/VERSION +1 -1
- epyt_flow/gym/scenario_control_env.py +26 -3
- epyt_flow/simulation/events/quality_events.py +6 -6
- epyt_flow/simulation/events/sensor_faults.py +24 -24
- epyt_flow/simulation/events/system_event.py +3 -3
- epyt_flow/simulation/scada/scada_data.py +1 -1
- epyt_flow/simulation/scenario_simulator.py +14 -11
- epyt_flow/topology.py +8 -1
- epyt_flow/uncertainty/model_uncertainty.py +292 -150
- {epyt_flow-0.14.0.dist-info → epyt_flow-0.14.1.dist-info}/METADATA +2 -2
- {epyt_flow-0.14.0.dist-info → epyt_flow-0.14.1.dist-info}/RECORD +57 -51
- epyt_flow/EPANET/EPANET/SRC_engines/Readme_SRC_Engines.txt +0 -18
- epyt_flow/EPANET/EPANET/SRC_engines/epanet2.def +0 -131
- epyt_flow/EPANET/EPANET/SRC_engines/main.c +0 -93
- {epyt_flow-0.14.0.dist-info → epyt_flow-0.14.1.dist-info}/WHEEL +0 -0
- {epyt_flow-0.14.0.dist-info → epyt_flow-0.14.1.dist-info}/licenses/LICENSE +0 -0
- {epyt_flow-0.14.0.dist-info → epyt_flow-0.14.1.dist-info}/top_level.txt +0 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/*
|
|
2
2
|
******************************************************************************
|
|
3
3
|
Project: OWA EPANET
|
|
4
|
-
Version: 2.
|
|
4
|
+
Version: 2.3
|
|
5
5
|
Module: funcs.h
|
|
6
6
|
Description: prototypes of external functions called by various modules
|
|
7
7
|
Authors: see AUTHORS
|
|
8
8
|
Copyright: see AUTHORS
|
|
9
9
|
License: see LICENSE
|
|
10
|
-
Last Updated:
|
|
10
|
+
Last Updated: 04/23/2025
|
|
11
11
|
******************************************************************************
|
|
12
12
|
*/
|
|
13
13
|
#ifndef FUNCS_H
|
|
@@ -19,6 +19,7 @@ void initpointers(Project *);
|
|
|
19
19
|
int allocdata(Project *);
|
|
20
20
|
void freedata(Project *);
|
|
21
21
|
|
|
22
|
+
int openproject(Project *, const char *, const char *, const char *, int);
|
|
22
23
|
int openfiles(Project *, const char *, const char *,const char *);
|
|
23
24
|
int openhydfile(Project *);
|
|
24
25
|
int openoutfile(Project *);
|
|
@@ -28,17 +29,20 @@ int buildadjlists(Network *);
|
|
|
28
29
|
void freeadjlists(Network *);
|
|
29
30
|
|
|
30
31
|
int incontrols(Project *, int, int);
|
|
32
|
+
int changevalvetype(Project *, int, int);
|
|
31
33
|
int valvecheck(Project *, int, int, int, int);
|
|
32
|
-
int
|
|
33
|
-
|
|
34
|
+
int unlinked(Project *);
|
|
35
|
+
|
|
36
|
+
int findnode(Network *, const char *);
|
|
37
|
+
int findlink(Network *, const char *);
|
|
34
38
|
int findtank(Network *, int);
|
|
35
39
|
int findvalve(Network *, int);
|
|
36
40
|
int findpump(Network *, int);
|
|
37
|
-
int findpattern(Network *, char *);
|
|
38
|
-
int findcurve(Network *, char *);
|
|
41
|
+
int findpattern(Network *, const char *);
|
|
42
|
+
int findcurve(Network *, const char *);
|
|
39
43
|
|
|
40
44
|
Pdemand finddemand(Pdemand, int);
|
|
41
|
-
int adddemand(Snode *, double, int, char *);
|
|
45
|
+
int adddemand(Snode *, double, int, const char *);
|
|
42
46
|
void freedemands(Snode *);
|
|
43
47
|
|
|
44
48
|
int addlinkvertex(Slink *, double, double);
|
|
@@ -46,11 +50,13 @@ void freelinkvertices(Slink *);
|
|
|
46
50
|
|
|
47
51
|
void adjustpatterns(Network *, int);
|
|
48
52
|
void adjustcurves(Network *, int);
|
|
49
|
-
int adjustpumpparams(Project *, int);
|
|
50
53
|
int resizecurve(Scurve *, int);
|
|
54
|
+
int setcontrol(Project *, int, int, double, int, double, Scontrol *);
|
|
51
55
|
|
|
52
56
|
int getcomment(Network *, int, int, char *);
|
|
53
57
|
int setcomment(Network *, int, int, const char *);
|
|
58
|
+
int gettag(Network *, int, int, char *);
|
|
59
|
+
int settag(Network *, int, int, const char *);
|
|
54
60
|
|
|
55
61
|
int namevalid(const char *);
|
|
56
62
|
void getTmpName(char *);
|
|
@@ -67,7 +73,7 @@ int getdata(Project *);
|
|
|
67
73
|
void setdefaults(Project *);
|
|
68
74
|
void initreport(Report *);
|
|
69
75
|
void adjustdata(Project *);
|
|
70
|
-
|
|
76
|
+
void inittanks(Project *);
|
|
71
77
|
void initunits(Project *);
|
|
72
78
|
void convertunits(Project *);
|
|
73
79
|
|
|
@@ -75,7 +81,6 @@ void convertunits(Project *);
|
|
|
75
81
|
|
|
76
82
|
int netsize(Project *);
|
|
77
83
|
int readdata(Project *);
|
|
78
|
-
int updatepumpparams(Project *, int);
|
|
79
84
|
int findmatch(char *, char *[]);
|
|
80
85
|
int match(const char *, const char *);
|
|
81
86
|
int gettokens(char *, char **, int, char *);
|
|
@@ -98,6 +103,7 @@ int controldata(Project *);
|
|
|
98
103
|
int energydata(Project *);
|
|
99
104
|
int sourcedata(Project *);
|
|
100
105
|
int emitterdata(Project *);
|
|
106
|
+
int leakagedata(Project *);
|
|
101
107
|
int qualdata(Project *);
|
|
102
108
|
int reactdata(Project *);
|
|
103
109
|
int mixingdata(Project *);
|
|
@@ -106,6 +112,7 @@ int reportdata(Project *);
|
|
|
106
112
|
int timedata(Project *);
|
|
107
113
|
int optiondata(Project *);
|
|
108
114
|
int vertexdata(Project *);
|
|
115
|
+
int tagdata(Project *);
|
|
109
116
|
|
|
110
117
|
// ------- RULES.C ------------------
|
|
111
118
|
|
|
@@ -117,28 +124,30 @@ void freerules(Project *);
|
|
|
117
124
|
int ruledata(Project *);
|
|
118
125
|
void ruleerrmsg(Project *);
|
|
119
126
|
void adjustrules(Project *, int, int);
|
|
120
|
-
void adjusttankrules(Project
|
|
127
|
+
void adjusttankrules(Project *, int);
|
|
121
128
|
Spremise *getpremise(Spremise *, int);
|
|
122
129
|
Saction *getaction(Saction *, int);
|
|
123
130
|
int writerule(Project *, FILE *, int);
|
|
124
131
|
int checkrules(Project *, long);
|
|
132
|
+
void updateruleunits(Project *pr, double dcf, double pcf, double hcf, double qcf);
|
|
125
133
|
|
|
126
134
|
// ------- REPORT.C -----------------
|
|
127
135
|
|
|
128
136
|
int clearreport(Project *);
|
|
129
|
-
int copyreport(Project *, char *);
|
|
137
|
+
int copyreport(Project *, const char *);
|
|
130
138
|
int writereport(Project *);
|
|
131
139
|
void writelogo(Project *);
|
|
132
140
|
void writesummary(Project *);
|
|
133
141
|
void writehydstat(Project *, int, double);
|
|
134
142
|
void writeheader(Project *, int,int);
|
|
135
|
-
void writeline(Project *, char *);
|
|
143
|
+
void writeline(Project *, const char *);
|
|
136
144
|
void writerelerr(Project *, int, double);
|
|
137
145
|
void writestatchange(Project *, int,char,char);
|
|
138
146
|
void writecontrolaction(Project *, int, int);
|
|
139
147
|
void writeruleaction(Project *, int, char *);
|
|
140
148
|
int writehydwarn(Project *, int,double);
|
|
141
149
|
void writehyderr(Project *, int);
|
|
150
|
+
void writeflowbalance(Project *);
|
|
142
151
|
void writemassbalance(Project *);
|
|
143
152
|
void writetime(Project *, char *);
|
|
144
153
|
char *clocktime(char *, long);
|
|
@@ -153,6 +162,7 @@ void closehyd(Project *);
|
|
|
153
162
|
void setlinkstatus(Project *, int, char, StatusType *, double *);
|
|
154
163
|
void setlinksetting(Project *, int, double, StatusType *, double *);
|
|
155
164
|
int tanktimestep(Project *, long *);
|
|
165
|
+
int controltimestep(Project *, long *);
|
|
156
166
|
void getenergy(Project *, int, double *, double *);
|
|
157
167
|
double tankvolume(Project *, int, double);
|
|
158
168
|
double tankgrade(Project *, int, double);
|
|
@@ -162,8 +172,9 @@ double tankgrade(Project *, int, double);
|
|
|
162
172
|
void resistcoeff(Project *, int);
|
|
163
173
|
void headlosscoeffs(Project *);
|
|
164
174
|
void matrixcoeffs(Project *);
|
|
165
|
-
void emitterheadloss(Project *, int, double *, double *);
|
|
175
|
+
void emitterheadloss(Project *, int, double *, double *);
|
|
166
176
|
void demandheadloss(Project *, int, double, double, double *, double *);
|
|
177
|
+
double pcvlosscoeff(Project *, int, double);
|
|
167
178
|
|
|
168
179
|
// ------- QUALITY.C --------------------
|
|
169
180
|
|
|
@@ -190,4 +201,19 @@ int savefinaloutput(Project *);
|
|
|
190
201
|
|
|
191
202
|
int saveinpfile(Project *, const char *);
|
|
192
203
|
|
|
204
|
+
// ------- LEAKAGE.C --------------------
|
|
205
|
+
|
|
206
|
+
int openleakage(Project *);
|
|
207
|
+
void closeleakage(Project *);
|
|
208
|
+
double findlinkleakage(Project *, int);
|
|
209
|
+
void leakagecoeffs(Project *);
|
|
210
|
+
double leakageflowchange(Project *, int);
|
|
211
|
+
int leakagehasconverged(Project *);
|
|
212
|
+
|
|
213
|
+
// ------- FLOWBALANCE.C-----------------
|
|
214
|
+
|
|
215
|
+
void startflowbalance(Project *);
|
|
216
|
+
void updateflowbalance(Project *, long);
|
|
217
|
+
void endflowbalance(Project *);
|
|
218
|
+
|
|
193
219
|
#endif
|
|
@@ -1,177 +1,177 @@
|
|
|
1
|
-
/*
|
|
2
|
-
******************************************************************************
|
|
3
|
-
Project: OWA EPANET
|
|
4
|
-
Version: 2.
|
|
5
|
-
Module: hash.c
|
|
6
|
-
Description: implementation of a simple hash table
|
|
7
|
-
Authors: see AUTHORS
|
|
8
|
-
Copyright: see AUTHORS
|
|
9
|
-
License: see LICENSE
|
|
10
|
-
Last Updated: 05/15/2019
|
|
11
|
-
******************************************************************************
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
#include <stdlib.h>
|
|
15
|
-
#include <string.h>
|
|
16
|
-
#include "hash.h"
|
|
17
|
-
|
|
18
|
-
#define HASHTABLEMAXSIZE 128000
|
|
19
|
-
|
|
20
|
-
// An entry in the hash table
|
|
21
|
-
typedef struct DataEntryStruct
|
|
22
|
-
{
|
|
23
|
-
char *key;
|
|
24
|
-
int data;
|
|
25
|
-
struct DataEntryStruct *next;
|
|
26
|
-
} DataEntry;
|
|
27
|
-
|
|
28
|
-
// Hash a string to an integer
|
|
29
|
-
unsigned int gethash(char *str)
|
|
30
|
-
{
|
|
31
|
-
unsigned int hash = 5381;
|
|
32
|
-
unsigned int retHash;
|
|
33
|
-
int c;
|
|
34
|
-
while ((c = *str++))
|
|
35
|
-
{
|
|
36
|
-
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
|
37
|
-
}
|
|
38
|
-
retHash = hash % HASHTABLEMAXSIZE;
|
|
39
|
-
return retHash;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Produce a duplicate string
|
|
43
|
-
char *dupstr(const char *s)
|
|
44
|
-
{
|
|
45
|
-
size_t size = strlen(s) + 1;
|
|
46
|
-
char *p = malloc(size);
|
|
47
|
-
if (p) memcpy(p, s, size);
|
|
48
|
-
return p;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Create a hash table
|
|
52
|
-
HashTable *hashtable_create()
|
|
53
|
-
{
|
|
54
|
-
int i;
|
|
55
|
-
HashTable *ht = (HashTable *) calloc(HASHTABLEMAXSIZE, sizeof(HashTable));
|
|
56
|
-
if (ht != NULL)
|
|
57
|
-
{
|
|
58
|
-
for (i = 0; i < HASHTABLEMAXSIZE; i++) ht[i] = NULL;
|
|
59
|
-
}
|
|
60
|
-
return ht;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Insert an entry into the hash table
|
|
64
|
-
int hashtable_insert(HashTable *ht, char *key, int data)
|
|
65
|
-
{
|
|
66
|
-
unsigned int i = gethash(key);
|
|
67
|
-
DataEntry *entry;
|
|
68
|
-
if ( i >= HASHTABLEMAXSIZE ) return 0;
|
|
69
|
-
entry = (DataEntry *) malloc(sizeof(DataEntry));
|
|
70
|
-
if (entry == NULL) return(0);
|
|
71
|
-
entry->key = dupstr(key);
|
|
72
|
-
entry->data = data;
|
|
73
|
-
entry->next = ht[i];
|
|
74
|
-
ht[i] = entry;
|
|
75
|
-
return 1;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Change the hash table's data entry for a particular key
|
|
79
|
-
int hashtable_update(HashTable *ht, char *key, int new_data)
|
|
80
|
-
{
|
|
81
|
-
unsigned int i = gethash(key);
|
|
82
|
-
DataEntry *entry;
|
|
83
|
-
|
|
84
|
-
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
|
85
|
-
entry = ht[i];
|
|
86
|
-
while (entry != NULL)
|
|
87
|
-
{
|
|
88
|
-
if ( strcmp(entry->key, key) == 0 )
|
|
89
|
-
{
|
|
90
|
-
entry->data = new_data;
|
|
91
|
-
return 1;
|
|
92
|
-
}
|
|
93
|
-
entry = entry->next;
|
|
94
|
-
}
|
|
95
|
-
return NOTFOUND;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Delete an entry in the hash table
|
|
99
|
-
int hashtable_delete(HashTable *ht, char *key)
|
|
100
|
-
{
|
|
101
|
-
unsigned int i = gethash(key);
|
|
102
|
-
DataEntry *entry, *preventry;
|
|
103
|
-
|
|
104
|
-
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
|
105
|
-
|
|
106
|
-
preventry = NULL;
|
|
107
|
-
entry = ht[i];
|
|
108
|
-
while (entry != NULL)
|
|
109
|
-
{
|
|
110
|
-
if (strcmp(entry->key, key) == 0)
|
|
111
|
-
{
|
|
112
|
-
if (preventry == NULL) ht[i] = entry->next;
|
|
113
|
-
else preventry->next = entry->next;
|
|
114
|
-
free(entry->key);
|
|
115
|
-
free(entry);
|
|
116
|
-
return 1;
|
|
117
|
-
}
|
|
118
|
-
preventry = entry;
|
|
119
|
-
entry = entry->next;
|
|
120
|
-
}
|
|
121
|
-
return NOTFOUND;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Find the data for a particular key
|
|
125
|
-
int hashtable_find(HashTable *ht, char *key)
|
|
126
|
-
{
|
|
127
|
-
unsigned int i = gethash(key);
|
|
128
|
-
DataEntry *entry;
|
|
129
|
-
|
|
130
|
-
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
|
131
|
-
entry = ht[i];
|
|
132
|
-
while (entry != NULL)
|
|
133
|
-
{
|
|
134
|
-
if ( strcmp(entry->key, key) == 0 )
|
|
135
|
-
{
|
|
136
|
-
return entry->data;
|
|
137
|
-
}
|
|
138
|
-
entry = entry->next;
|
|
139
|
-
}
|
|
140
|
-
return NOTFOUND;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Find a particular key in the hash table
|
|
144
|
-
char *hashtable_findkey(HashTable *ht, char *key)
|
|
145
|
-
{
|
|
146
|
-
unsigned int i = gethash(key);
|
|
147
|
-
DataEntry *entry;
|
|
148
|
-
if ( i >= HASHTABLEMAXSIZE ) return NULL;
|
|
149
|
-
entry = ht[i];
|
|
150
|
-
while (entry != NULL)
|
|
151
|
-
{
|
|
152
|
-
if ( strcmp(entry->key, key) == 0 ) return entry->key;
|
|
153
|
-
entry = entry->next;
|
|
154
|
-
}
|
|
155
|
-
return NULL;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Delete a hash table and free all of its memory
|
|
159
|
-
void hashtable_free(HashTable *ht)
|
|
160
|
-
{
|
|
161
|
-
DataEntry *entry, *nextentry;
|
|
162
|
-
int i;
|
|
163
|
-
|
|
164
|
-
for (i = 0; i < HASHTABLEMAXSIZE; i++)
|
|
165
|
-
{
|
|
166
|
-
entry = ht[i];
|
|
167
|
-
while (entry != NULL)
|
|
168
|
-
{
|
|
169
|
-
nextentry = entry->next;
|
|
170
|
-
free(entry->key);
|
|
171
|
-
free(entry);
|
|
172
|
-
entry = nextentry;
|
|
173
|
-
}
|
|
174
|
-
ht[i] = NULL;
|
|
175
|
-
}
|
|
176
|
-
free(ht);
|
|
177
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
******************************************************************************
|
|
3
|
+
Project: OWA EPANET
|
|
4
|
+
Version: 2.3
|
|
5
|
+
Module: hash.c
|
|
6
|
+
Description: implementation of a simple hash table
|
|
7
|
+
Authors: see AUTHORS
|
|
8
|
+
Copyright: see AUTHORS
|
|
9
|
+
License: see LICENSE
|
|
10
|
+
Last Updated: 05/15/2019
|
|
11
|
+
******************************************************************************
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
#include <stdlib.h>
|
|
15
|
+
#include <string.h>
|
|
16
|
+
#include "hash.h"
|
|
17
|
+
|
|
18
|
+
#define HASHTABLEMAXSIZE 128000
|
|
19
|
+
|
|
20
|
+
// An entry in the hash table
|
|
21
|
+
typedef struct DataEntryStruct
|
|
22
|
+
{
|
|
23
|
+
char *key;
|
|
24
|
+
int data;
|
|
25
|
+
struct DataEntryStruct *next;
|
|
26
|
+
} DataEntry;
|
|
27
|
+
|
|
28
|
+
// Hash a string to an integer
|
|
29
|
+
unsigned int gethash(const char *str)
|
|
30
|
+
{
|
|
31
|
+
unsigned int hash = 5381;
|
|
32
|
+
unsigned int retHash;
|
|
33
|
+
int c;
|
|
34
|
+
while ((c = *str++))
|
|
35
|
+
{
|
|
36
|
+
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
|
37
|
+
}
|
|
38
|
+
retHash = hash % HASHTABLEMAXSIZE;
|
|
39
|
+
return retHash;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Produce a duplicate string
|
|
43
|
+
char *dupstr(const char *s)
|
|
44
|
+
{
|
|
45
|
+
size_t size = strlen(s) + 1;
|
|
46
|
+
char *p = malloc(size);
|
|
47
|
+
if (p) memcpy(p, s, size);
|
|
48
|
+
return p;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Create a hash table
|
|
52
|
+
HashTable *hashtable_create()
|
|
53
|
+
{
|
|
54
|
+
int i;
|
|
55
|
+
HashTable *ht = (HashTable *) calloc(HASHTABLEMAXSIZE, sizeof(HashTable));
|
|
56
|
+
if (ht != NULL)
|
|
57
|
+
{
|
|
58
|
+
for (i = 0; i < HASHTABLEMAXSIZE; i++) ht[i] = NULL;
|
|
59
|
+
}
|
|
60
|
+
return ht;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Insert an entry into the hash table
|
|
64
|
+
int hashtable_insert(HashTable *ht, const char *key, int data)
|
|
65
|
+
{
|
|
66
|
+
unsigned int i = gethash(key);
|
|
67
|
+
DataEntry *entry;
|
|
68
|
+
if ( i >= HASHTABLEMAXSIZE ) return 0;
|
|
69
|
+
entry = (DataEntry *) malloc(sizeof(DataEntry));
|
|
70
|
+
if (entry == NULL) return(0);
|
|
71
|
+
entry->key = dupstr(key);
|
|
72
|
+
entry->data = data;
|
|
73
|
+
entry->next = ht[i];
|
|
74
|
+
ht[i] = entry;
|
|
75
|
+
return 1;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Change the hash table's data entry for a particular key
|
|
79
|
+
int hashtable_update(HashTable *ht, const char *key, int new_data)
|
|
80
|
+
{
|
|
81
|
+
unsigned int i = gethash(key);
|
|
82
|
+
DataEntry *entry;
|
|
83
|
+
|
|
84
|
+
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
|
85
|
+
entry = ht[i];
|
|
86
|
+
while (entry != NULL)
|
|
87
|
+
{
|
|
88
|
+
if ( strcmp(entry->key, key) == 0 )
|
|
89
|
+
{
|
|
90
|
+
entry->data = new_data;
|
|
91
|
+
return 1;
|
|
92
|
+
}
|
|
93
|
+
entry = entry->next;
|
|
94
|
+
}
|
|
95
|
+
return NOTFOUND;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Delete an entry in the hash table
|
|
99
|
+
int hashtable_delete(HashTable *ht, const char *key)
|
|
100
|
+
{
|
|
101
|
+
unsigned int i = gethash(key);
|
|
102
|
+
DataEntry *entry, *preventry;
|
|
103
|
+
|
|
104
|
+
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
|
105
|
+
|
|
106
|
+
preventry = NULL;
|
|
107
|
+
entry = ht[i];
|
|
108
|
+
while (entry != NULL)
|
|
109
|
+
{
|
|
110
|
+
if (strcmp(entry->key, key) == 0)
|
|
111
|
+
{
|
|
112
|
+
if (preventry == NULL) ht[i] = entry->next;
|
|
113
|
+
else preventry->next = entry->next;
|
|
114
|
+
free(entry->key);
|
|
115
|
+
free(entry);
|
|
116
|
+
return 1;
|
|
117
|
+
}
|
|
118
|
+
preventry = entry;
|
|
119
|
+
entry = entry->next;
|
|
120
|
+
}
|
|
121
|
+
return NOTFOUND;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Find the data for a particular key
|
|
125
|
+
int hashtable_find(HashTable *ht, const char *key)
|
|
126
|
+
{
|
|
127
|
+
unsigned int i = gethash(key);
|
|
128
|
+
DataEntry *entry;
|
|
129
|
+
|
|
130
|
+
if ( i >= HASHTABLEMAXSIZE ) return NOTFOUND;
|
|
131
|
+
entry = ht[i];
|
|
132
|
+
while (entry != NULL)
|
|
133
|
+
{
|
|
134
|
+
if ( strcmp(entry->key, key) == 0 )
|
|
135
|
+
{
|
|
136
|
+
return entry->data;
|
|
137
|
+
}
|
|
138
|
+
entry = entry->next;
|
|
139
|
+
}
|
|
140
|
+
return NOTFOUND;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Find a particular key in the hash table
|
|
144
|
+
char *hashtable_findkey(HashTable *ht, const char *key)
|
|
145
|
+
{
|
|
146
|
+
unsigned int i = gethash(key);
|
|
147
|
+
DataEntry *entry;
|
|
148
|
+
if ( i >= HASHTABLEMAXSIZE ) return NULL;
|
|
149
|
+
entry = ht[i];
|
|
150
|
+
while (entry != NULL)
|
|
151
|
+
{
|
|
152
|
+
if ( strcmp(entry->key, key) == 0 ) return entry->key;
|
|
153
|
+
entry = entry->next;
|
|
154
|
+
}
|
|
155
|
+
return NULL;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Delete a hash table and free all of its memory
|
|
159
|
+
void hashtable_free(HashTable *ht)
|
|
160
|
+
{
|
|
161
|
+
DataEntry *entry, *nextentry;
|
|
162
|
+
int i;
|
|
163
|
+
|
|
164
|
+
for (i = 0; i < HASHTABLEMAXSIZE; i++)
|
|
165
|
+
{
|
|
166
|
+
entry = ht[i];
|
|
167
|
+
while (entry != NULL)
|
|
168
|
+
{
|
|
169
|
+
nextentry = entry->next;
|
|
170
|
+
free(entry->key);
|
|
171
|
+
free(entry);
|
|
172
|
+
entry = nextentry;
|
|
173
|
+
}
|
|
174
|
+
ht[i] = NULL;
|
|
175
|
+
}
|
|
176
|
+
free(ht);
|
|
177
|
+
}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
/*
|
|
2
|
-
******************************************************************************
|
|
3
|
-
Project: OWA EPANET
|
|
4
|
-
Version: 2.
|
|
5
|
-
Module: hash.h
|
|
6
|
-
Description: header for a simple hash table
|
|
7
|
-
Authors: see AUTHORS
|
|
8
|
-
Copyright: see AUTHORS
|
|
9
|
-
License: see LICENSE
|
|
10
|
-
Last Updated: 11/27/2018
|
|
11
|
-
******************************************************************************
|
|
12
|
-
*/
|
|
13
|
-
#ifndef HASH_H
|
|
14
|
-
#define HASH_H
|
|
15
|
-
|
|
16
|
-
#define NOTFOUND 0
|
|
17
|
-
|
|
18
|
-
typedef struct DataEntryStruct *HashTable;
|
|
19
|
-
|
|
20
|
-
HashTable *hashtable_create(void);
|
|
21
|
-
int hashtable_insert(HashTable *, char *, int);
|
|
22
|
-
int hashtable_find(HashTable *, char *);
|
|
23
|
-
char *hashtable_findkey(HashTable *, char *);
|
|
24
|
-
void hashtable_free(HashTable *);
|
|
25
|
-
int hashtable_update(HashTable *ht, char *key, int new_data);
|
|
26
|
-
int hashtable_delete(HashTable *ht, char *key);
|
|
27
|
-
|
|
28
|
-
#endif
|
|
1
|
+
/*
|
|
2
|
+
******************************************************************************
|
|
3
|
+
Project: OWA EPANET
|
|
4
|
+
Version: 2.3
|
|
5
|
+
Module: hash.h
|
|
6
|
+
Description: header for a simple hash table
|
|
7
|
+
Authors: see AUTHORS
|
|
8
|
+
Copyright: see AUTHORS
|
|
9
|
+
License: see LICENSE
|
|
10
|
+
Last Updated: 11/27/2018
|
|
11
|
+
******************************************************************************
|
|
12
|
+
*/
|
|
13
|
+
#ifndef HASH_H
|
|
14
|
+
#define HASH_H
|
|
15
|
+
|
|
16
|
+
#define NOTFOUND 0
|
|
17
|
+
|
|
18
|
+
typedef struct DataEntryStruct *HashTable;
|
|
19
|
+
|
|
20
|
+
HashTable *hashtable_create(void);
|
|
21
|
+
int hashtable_insert(HashTable *, const char *, int);
|
|
22
|
+
int hashtable_find(HashTable *, const char *);
|
|
23
|
+
char *hashtable_findkey(HashTable *, const char *);
|
|
24
|
+
void hashtable_free(HashTable *);
|
|
25
|
+
int hashtable_update(HashTable *ht, const char *key, int new_data);
|
|
26
|
+
int hashtable_delete(HashTable *ht, const char *key);
|
|
27
|
+
|
|
28
|
+
#endif
|