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 400
#define KEYLEN 10
/* buffer is twice the key size */
#define BUFLEN (KEYLEN 1)
#define SEED Ox270beef
/* return a random printable character with high-order bit =0*/
#define ALFALEN 64;
static char *alfabet = "ABCDEFGHIJKLMNOPQRSTUVNDYZabcdefghijklmnopqrstuvwxyz"
"123456789*+:";
static int count =0;
char randchar(void){
static union {
long bits;
unsigned char bytes[4];
} u;
char c;
if (count==0)
u.bits = random();
c = alfabet[u.bytes[count]&0x3f];
count =(count +1) & 3;
return c;
}
void randstring(char *bufp, int len){
int i;
for (i=0; i>1);
/* make some random keys */
srandom(SEED);
for ( j=0; j NUMTOPUT; j++)
randstring(allkeys[j],(random()%KEYLEN)+ keypad);
for (j=0; j NUMTOPUT; j++)
strmap_put(map, allkeys[j],(void *)j);
strmap_dump(map);
printf("before resizing: capacity =%d, load factor =%f.
",
map->strmap_nbuckets, strmap_getloadfactor(map));
strmap_resize(map,1.0);
printf("after resizing w/target 1.0: capacity =%d, load factor =%f.
",
map->strmap_nbuckets, strmap_getloadfactor(map));
strmap_dump(map);
/* now remove half of what's there */
limit = NUMTOPUT >>1;
for (j=0; j ```
void *pp = strmap_remove(map, allkeys[j]);
if (((int)pp)!= j)
printf("Error: strmap_remove(%s) returned %p (expected %x).
",
allkeys[j],pp,j);
}
printf("after removing %d keys, load factor =%f.
",limit,
strmap_get loadfactor(map));
strmap_resize(map,0.75);
printf("after resizing (target LF =0.75), load factor =%f, #buckets=%d.
",
strmap_getloadfactor(map),strmap_getnbuckets(map));
strmap_dump(map);
printf("End of test1.c.
");
return 0;
}
``````
#include // for printf, ONLY IN strmap_dump
#include // for strcmp
#include // for malloc/calloc
#define MAX_BUCKETS 10000
#define MIN_BUCKETS 10
#define LFSLOP 0.125
typedef struct strmap_e {
char *sme_key;
void *sme_value;
struct strmap_e *sme_next;
} smel_t;
typedef struct sm {
smel_t **strmap_buckets; // array of pointers to elements
unsigned int strmap_size; /// # elements in the table
unsigned int strmap_nbuckets; // size of the array
} strmap_t;
/* Create a new hashtab, initialized to empty. */
strmap_t *strmap_create(int numbuckets);
/* if the load factor is more than LFSLOP away from the target,
* allocate a new array to bring the load factor within 1/8 of
* the given target (i.e., between 7/8* target and 9/8*target),
* then re-hash 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 strmap_buckets and strmap_nbuckets.)
* If the load factor is already within the target range,
* the map is left unchanged.
*/
void strmap_resize(strmap_t *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 *strmap_put(strmap_t *m, char *key, void *value);
/* return the value associated with the given key, or null if none */
void *strmap_get(strmap_t *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 *strmap_remove(strmap_t *m, char *key);
/* return the # of elements in the hashtab */
int strmap_getsize(strmap_t *m);
/* return the # of buckets in the hashtab */
int strmap_getnbuckets(strmap_t *m);
/* print out the contents of each bucket */
void strmap_dump(strmap_t *m);
/* return the current load factor of the map */
double strmap_getloadfactor(strmap_t *m);
```
` ` ` / * initial test driver for strmap

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!