words.c
#include <stdio.h>
#include <stdlib.h>
#include <llib/map.h>
int main(int argc, char **argv)
{
char word[100];
const char *file = argv[1] ? argv[1] : "../readme.md";
FILE *in = fopen(file,"r");
Map *m = map_new_str_ptr();
int k = 0;
while (fscanf(in,"%99s",word) == 1) {
map_puti(m,word, map_geti(m,word) + 1);
++k;
}
fclose(in);
MapKeyValue *pkv = map_to_array(m);
int sz = array_len(pkv);
printf("unique words %d out of %d\n",sz,k);
array_sort_struct_ptr (pkv,true,MapKeyValue,value);
FOR(i,10) {
printf("%s\t%d\n",(char*)pkv[i].key,(intptr_t)pkv[i].value);
}
dispose(m,pkv);
printf("remaining %d\n",obj_kount());
return 0;
}