Question: ` ` ` / * initial test driver for strmap implementation * / #include #include strmapbis.h / * includes string.h and stdlib.h * /
initial test driver for strmap implementation
#include
#include "strmapbis.h includes string.h and stdlib.h
#define NUMTOPUT
#define KEYLEN
buffer is twice the key size
#define BUFLEN KEYLEN
#define SEED Oxbeef
return a random printable character with highorder bit
#define ALFALEN ;
static char alfabet "ABCDEFGHIJKLMNOPQRSTUVNDYZabcdefghijklmnopqrstuvwxyz"
:;
static int count ;
char randcharvoid
static union
long bits;
unsigned char bytes;
u;
char c;
if count
ubits random;
c alfabetubytescount&xf;
count count & ;
return c;
void randstringchar bufp int len
int i;
for i; i;
make some random keys
srandomSEED;
for j; j NUMTOPUT; j
randstringallkeysjrandomKEYLEN keypad;
for j; j NUMTOPUT; j
strmapputmap allkeysjvoid j;
strmapdumpmap;
printfbefore resizing: capacity d load factor f
mapstrmapnbuckets, strmapgetloadfactormap;
strmapresizemap;
printfafter resizing wtarget : capacity d load factor f
mapstrmapnbuckets, strmapgetloadfactormap;
strmapdumpmap;
now remove half of what's there
limit NUMTOPUT ;
for j; j
void pp strmapremovemap allkeysj;
if intpp j
printfError: strmapremoves returned p expected x
allkeysjppj;
printfafter removing d keys, load factor f
limit
strmapget loadfactormap;
strmapresizemap;
printfafter resizing target LF load factor f #bucketsd
strmapgetloadfactormapstrmapgetnbucketsmap;
strmapdumpmap;
printfEnd of testc
;
return ;
#include for printf, ONLY IN strmapdump
#include for strcmp
#include for malloccalloc
#define MAXBUCKETS
#define MINBUCKETS
#define LFSLOP
typedef struct strmape
char smekey;
void smevalue;
struct strmape smenext;
smelt;
typedef struct sm
smelt strmapbuckets; array of pointers to elements
unsigned int strmapsize; # elements in the table
unsigned int strmapnbuckets; size of the array
strmapt;
Create a new hashtab, initialized to empty.
strmapt strmapcreateint numbuckets;
if the load factor is more than LFSLOP away from the target,
allocate a new array to bring the load factor within of
the given target ie between target and target
then rehash everything into that array, free the old bucket
array, and make the map point to the new bucket array. The only
fields that change in the map are strmapbuckets and strmapnbuckets.
If the load factor is already within the target range,
the map is left unchanged.
void strmapresizestrmapt m double target;
Insert an element with the given key and value.
Return the previous value associated with that key, or null if none.
void strmapputstrmapt m char key void value;
return the value associated with the given key, or null if none
void strmapgetstrmapt m char key;
remove the element with the given key and return its value.
Return null if the hashtab contains no element with the given key
void strmapremovestrmapt m char key;
return the # of elements in the hashtab
int strmapgetsizestrmapt m;
return the # of buckets in the hashtab
int strmapgetnbucketsstrmapt m;
print out the contents of each bucket
void strmapdumpstrmapt m;
return the current load factor of the map
double strmapgetloadfactorstrmapt m;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
