1234567891011121314151617181920212223242526272829303132333435363738394041 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "StringIntMap.h"
- void simInit(StringIntMap* sim) {
- sim->capacity = 16;
- sim->entries = 0;
- sim->stringData = malloc(sizeof(const char*) * sim->capacity);
- sim->intData = malloc(sizeof(int) * sim->capacity);
- }
- void simDelete(StringIntMap* sim) {
- free(sim->stringData);
- free(sim->intData);
- }
- bool simSearch(StringIntMap* sim, const char* s, int* mapping) {
- for(int i = 0; i < sim->entries; i++) {
- if(strcmp(s, sim->stringData[i]) == 0) {
- *mapping = sim->intData[i];
- return true;
- }
- }
- return false;
- }
- bool simAdd(StringIntMap* sim, const char* s, int* mapping) {
- if(simSearch(sim, s, mapping)) {
- return false;
- } else if(sim->entries >= sim->capacity) {
- sim->capacity *= 2;
- sim->stringData = realloc(sim->stringData, sizeof(const char*) * sim->capacity);
- sim->intData = realloc(sim->intData, sizeof(int) * sim->capacity);
- }
- int index = sim->entries++;
- sim->stringData[index] = s;
- sim->intData[index] = *mapping;
- return true;
- }
|