<commit_before>#include "hashmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

long long get_ustime_sec(void)
{
    struct timeval tv;
    long long ust;

    gettimeofday(&tv, NULL);
    ust = ((long long)tv.tv_sec)*1000000;
    ust += tv.tv_usec;
    return ust;
}

typedef struct {
    int index;
    char word[30];
} value;

int compare_word(const void *desc, const void *src)
{
    return strcmp(desc, src);
}

int main(int argc, char **argv)
{
    hashmap *map = hashmap_new(0, compare_word);
    FILE *f = fopen("test/wordlist.txt", "r");
    
    long long start, stop;
    int i = 0;
    
    start = get_ustime_sec();
    while (!feof(f)) {
        i++;
        value *v = (value *)malloc(sizeof(value));
        fscanf(f, "%s\n", v->word);
        v->index = i;
        hashmap_put(map, v->word, v);
    }
    stop = get_ustime_sec();
    printf("cost time %lld\n", stop-start);
    char test[30] = "comically";
    value *t = hashmap_get(map, test);
    printf("%p\n", t);
    printf("%d %s\n", t->index, t->word);

    hashmap_delete(map, test);

    t = hashmap_get(map, test);
    printf("%p\n", t);
    //printf("%d %s\n", t->index, t->word);
    value *v = (value *)malloc(sizeof(value));
    v->index = 88;
    strcpy(v->word, test);
    hashmap_put(map, test, v);

    t = hashmap_get(map, test);
    printf("%p\n", t);
    printf("%d %s\n", t->index, t->word);
    return 0;
}<commit_msg>增加 hashmap demos<commit_after>#include "hashmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/time.h>

/*
 * Hashmap 使用，其中 Key 和 Value 均为指针类型，Key 定义为 const 类型，
 * 使用时需要自己管理指针指向的内容：下面将 wordlist 文件中的单词加入 Hashmap
 * 中
 */

long long get_ustime_sec(void)
{
    struct timeval tv;
    long long ust;

    gettimeofday(&tv, NULL);
    ust = ((long long)tv.tv_sec)*1000000;
    ust += tv.tv_usec;
    return ust;
}

static int value = 1;

/* 定义对 Key 比较的函数 */
int compare_word(const void *desc, const void *src)
{
    return strcmp(desc, src);
}


int main(int argc, char **argv)
{
    /* 新建一个使用默认初始大小的 Hashmap */
    hashmap *map = hashmap_new(0, compare_word);
    assert(map != NULL);

    FILE *f = fopen("wordlist.txt", "r");
    assert(f != NULL);

    char *key;
    int i = 0;
    long long start, stop;

    start = get_ustime_sec();
    while (!feof(f)) {
        i++;
        key = (char *)malloc(sizeof(char) * 30);
        assert(key != NULL);

        fscanf(f, "%s\n", key);
        hashmap_put(map, key, &value);
    }
    stop = get_ustime_sec();

    printf("put %d word in hashmap cost time %lldμs\n", i, stop-start);
    
    /* 查询 */
    char test[30] = "comically";
    int *t = hashmap_get(map, test);
    if (t) {
        printf("%s in wordlist\n", test);
    } else {
        printf("%s not in wordlist\n", test);
    }

    hashmap_delete(map, test);

    t = hashmap_get(map, test);
    if (t) {
        printf("%s in wordlist\n", test);
    } else {
        printf("%s not in wordlist\n", test);
    }

    /* 在删除 Hashmap 前，需要自己释放 Key, Value 指向的内存 */
    hashmap_free(map);
    return 0;
}
{'fraction_non_alphanumeric': 0.1725692208628461, 'fraction_numerical': 0.009980682549903412, 'mean_word_length': 2.6381733021077283, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7017008', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1253, 'n_words': 300}