默认值

数组和集合 / 字典

//C 没有字典:这是一个小型哈希表,
//键为字符串,值为 int
constexpr int BUCKETS = 16;

typedef struct Entry {
    char *key;
    int value;
    struct Entry *next;
} Entry;

typedef struct {
    Entry *buckets[BUCKETS];
    size_t count;
} Dict;

unsigned dictHash(const char *key) {
    unsigned hash = 5381;
    for (; *key != '\0'; key++) {
        hash = hash * 33 + (unsigned char)*key;
    }
    return hash % BUCKETS;
}

//指向该值的指针,没有这个键时为 nullptr
int *dictGet(Dict *d, const char *key) {
    Entry *e = d->buckets[dictHash(key)];
    while (e != nullptr && strcmp(e->key, key) != 0) {
        e = e->next;
    }
    return e != nullptr ? &e->value : nullptr;
}

//添加键值对,或替换该键的值
void dictPut(Dict *d, const char *key, int value) {
    int *old = dictGet(d, key);
    if (old != nullptr) {
        *old = value;
        return;
    }
    unsigned bucket = dictHash(key);
    Entry *e = malloc(sizeof *e);
    *e = (Entry){strdup(key), value, d->buckets[bucket]};
    d->buckets[bucket] = e;
    d->count++;
}

//按桶的顺序输出各个键值对
void dictPrint(const char *name, const Dict *d) {
    printf("%s is {", name);
    const char *separator = "";
    for (int i = 0; i < BUCKETS; i++) {
        for (Entry *e = d->buckets[i]; e != nullptr; e = e->next) {
            printf("%s\"%s\": %d", separator, e->key, e->value);
            separator = ", ";
        }
    }
    printf("}\n");
}

//释放每个键值对,并让字典保持为空
void dictFree(Dict *d) {
    for (int i = 0; i < BUCKETS; i++) {
        for (Entry *e = d->buckets[i], *next; e != nullptr; e = next) {
            next = e->next;
            free(e->key);
            free(e);
        }
    }
    *d = (Dict){};
}

Dict dic = {};
dictPut(&dic, "one", 1);
dictPut(&dic, "two", 2);

//没有这个键:改用默认值
int *found = dictGet(&dic, "three");
int value3 = found != nullptr ? *found : 0;
//value3 is 0

//或把默认值放入字典
if (dictGet(&dic, "three") == nullptr) {
    dictPut(&dic, "three", 0);
}
//dic is {"one": 1, "three": 0, "two": 2}

printf("value3 is %d\n", value3);
dictPrint("dic", &dic);
dictFree(&dic);