OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
hash.c File Reference

Hash tables used to resolve legacy model identifiers. More...

#include <stdlib.h>
#include <string.h>
#include "hash.h"
Include dependency graph for hash.c:

Macros

#define UCHAR(x)
 

Functions

int samestr (const char *s1, const char *s2)
 Compare two nonempty ASCII identifiers without case sensitivity.
 
unsigned int hash (const char *str)
 Map an ASCII identifier to a case-insensitive Fletcher-checksum bucket.
 
HTtable * HTcreate ()
 Allocate an empty case-insensitive identifier table.
 
int HTinsert (HTtable *ht, char *key, int data)
 Prepend a key/data entry to a hash table without copying its key.
 
int HTfind (HTtable *ht, const char *key)
 Look up the most recently inserted case-insensitive key match.
 
char * HTfindKey (HTtable *ht, const char *key)
 Retrieve the original stored spelling of a case-insensitive key.
 
void HTfree (HTtable *ht)
 Free all hash entries and the table bucket array.
 

Detailed Description

Hash tables used to resolve legacy model identifiers.

Author
Caleb Buahin caleb.nosp@m..bua.nosp@m.hin@g.nosp@m.mail.nosp@m..com (OpenSWMM maintenance and extensions)
License
Apache-2.0 for OpenSWMM contributions; original EPA SWMM provenance is retained.

Macro Definition Documentation

◆ UCHAR

#define UCHAR ( x)
Value:
(((x) >= 'a' && (x) <= 'z') ? ((x) & ~32) : (x))

Function Documentation

◆ hash()

unsigned int hash ( const char * str)

Map an ASCII identifier to a case-insensitive Fletcher-checksum bucket.

Parameters
[in]strNon-null borrowed null-terminated identifier; empty is permitted.
Returns
Bucket index in [0, HTMAXSIZE - 1]. Input storage is unchanged.
Here is the caller graph for this function:

◆ HTcreate()

HTtable * HTcreate ( void )

Allocate an empty case-insensitive identifier table.

Returns
Owned zero-initialized HTMAXSIZE bucket array, or NULL on allocation failure.
Note
Release the table and its entries with HTfree(); key strings remain externally owned.

◆ HTfind()

int HTfind ( HTtable * ht,
const char * key )

Look up the most recently inserted case-insensitive key match.

Parameters
[in]htNon-null borrowed table created by HTcreate().
[in]keyNon-null borrowed nonempty null-terminated identifier.
Returns
Stored integer value, or NOTFOUND if no entry matches.
Note
No storage is modified or retained; returned values can coincide with NOTFOUND if the caller inserted that sentinel as data.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HTfindKey()

char * HTfindKey ( HTtable * ht,
const char * key )

Retrieve the original stored spelling of a case-insensitive key.

Parameters
[in]htNon-null borrowed table created by HTcreate().
[in]keyNon-null borrowed nonempty null-terminated lookup identifier.
Returns
Borrowed original key pointer, or NULL if no entry matches.
Note
The pointer's lifetime follows the external key owner; the table does not copy, free or grant mutation rights to the stored identifier.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HTfree()

void HTfree ( HTtable * ht)

Free all hash entries and the table bucket array.

Parameters
[in]htNon-null exclusively owned table created by HTcreate().
Postcondition
Table and entry pointers are invalid; borrowed key strings are not freed. The caller must discard or reset its own ht pointer after this call.

◆ HTinsert()

int HTinsert ( HTtable * ht,
char * key,
int data )

Prepend a key/data entry to a hash table without copying its key.

Parameters
[in,out]htNon-null table created by HTcreate().
[in]keyBorrowed nonempty null-terminated identifier; it must remain alive and unchanged for the entry's lifetime.
[in]dataInteger value associated with the identifier.
Returns
One on insertion, zero on bucket/allocation failure.
Note
Duplicate keys are allowed and newest entries are found first. The table owns the entry allocation, but never frees key storage.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ samestr()

int samestr ( const char * s1,
const char * s2 )

Compare two nonempty ASCII identifiers without case sensitivity.

Parameters
[in]s1Non-null borrowed null-terminated first string.
[in]s2Non-null borrowed null-terminated second string.
Returns
One for equal strings after ASCII case folding, zero otherwise.
Precondition
Both identifiers are nonempty; the loop reads one character beyond a matching pair before its termination check. No storage is retained.
Here is the caller graph for this function: