From f0b763afbaec8f1a2efbc0b275797871452dc348 Mon Sep 17 00:00:00 2001 From: Leonardo Date: Fri, 3 May 2019 23:57:58 -0300 Subject: [PATCH] Hashmap example --- examples/hashmap/README.md | 2 +- examples/hashmap/countries/data.csv | 216 ++++++++++++++++++++++++++++ examples/hashmap/countries/main.c | 90 ++++++++++++ examples/hashmap/countries/util.c | 17 +++ 4 files changed, 324 insertions(+), 1 deletion(-) create mode 100644 examples/hashmap/countries/data.csv create mode 100644 examples/hashmap/countries/main.c create mode 100644 examples/hashmap/countries/util.c diff --git a/examples/hashmap/README.md b/examples/hashmap/README.md index ac07aca..0010373 100644 --- a/examples/hashmap/README.md +++ b/examples/hashmap/README.md @@ -1,3 +1,3 @@ # Hashmap examples -Currently none +* countries - Uses a hashmap to map the country name to its population diff --git a/examples/hashmap/countries/data.csv b/examples/hashmap/countries/data.csv new file mode 100644 index 0000000..5146c12 --- /dev/null +++ b/examples/hashmap/countries/data.csv @@ -0,0 +1,216 @@ +Afghanistan,34656032 +Albania,2876101 +Algeria,40606052 +American Samoa,55599 +Andorra,77281 +Angola,28813463 +Antigua and Barbuda,100963 +Argentina,43847430 +Armenia,2924816 +Aruba,104822 +Australia,24127159 +Austria,8747358 +Azerbaijan,9762274 +Bahamas; The,391232 +Bahrain,1425171 +Bangladesh,162951560 +Barbados,284996 +Belarus,9507120 +Belgium,11348159 +Belize,366954 +Benin,10872298 +Bermuda,65331 +Bhutan,797765 +Bolivia,10887882 +Bosnia and Herzegovina,3516816 +Botswana,2250260 +Brazil,207652865 +British Virgin Islands,30661 +Brunei Darussalam,423196 +Bulgaria,7127822 +Burkina Faso,18646433 +Burundi,10524117 +Cabo Verde,539560 +Cambodia,15762370 +Cameroon,23439189 +Canada,36286425 +Cayman Islands,60765 +Central African Republic,4594621 +Chad,14452543 +Channel Islands,164541 +Chile,17909754 +China,1378665000 +Colombia,48653419 +Comoros,795601 +Congo; Dem. Rep.,78736153 +Congo; Rep.,5125821 +Costa Rica,4857274 +Cote d'Ivoire,23695919 +Croatia,4170600 +Cuba,11475982 +Curacao,159999 +Cyprus,1170125 +Czech Republic,10561633 +Denmark,5731118 +Djibouti,942333 +Dominica,73543 +Dominican Republic,10648791 +Ecuador,16385068 +Egypt; Arab Rep.,95688681 +El Salvador,6344722 +Equatorial Guinea,1221490 +Estonia,1316481 +Ethiopia,102403196 +Faroe Islands,49117 +Fiji,898760 +Finland,5495096 +France,66896109 +French Polynesia,280208 +Gabon,1979786 +Gambia; The,2038501 +Georgia,3719300 +Germany,82667685 +Ghana,28206728 +Gibraltar,34408 +Greece,10746740 +Greenland,56186 +Grenada,107317 +Guam,162896 +Guatemala,16582469 +Guinea,12395924 +Guinea-Bissau,1815698 +Guyana,773303 +Haiti,10847334 +Honduras,9112867 +Hong Kong SAR; China,7346700 +Hungary,9817958 +Iceland,334252 +India,1324171354 +Indonesia,261115456 +Iran; Islamic Rep.,80277428 +Iraq,37202572 +Ireland,4773095 +Isle of Man,83737 +Israel,8547100 +Italy,60600590 +Jamaica,2881355 +Japan,126994511 +Jordan,9455802 +Kazakhstan,17797032 +Kenya,48461567 +Kiribati,114395 +Korea; Dem. People’s Rep.,25368620 +Korea; Rep.,51245707 +Kosovo,1816200 +Kuwait,4052584 +Kyrgyz Republic,6082700 +Lao PDR,6758353 +Latvia,1960424 +Lebanon,6006668 +Lesotho,2203821 +Liberia,4613823 +Libya,6293253 +Liechtenstein,37666 +Lithuania,2872298 +Luxembourg,582972 +Macao SAR; China,612167 +Macedonia; FYR,2081206 +Madagascar,24894551 +Malawi,18091575 +Malaysia,31187265 +Maldives,417492 +Mali,17994837 +Malta,436947 +Marshall Islands,53066 +Mauritania,4301018 +Mauritius,1263473 +Mexico,127540423 +Micronesia; Fed. Sts.,104937 +Moldova,3552000 +Monaco,38499 +Mongolia,3027398 +Montenegro,622781 +Morocco,35276786 +Mozambique,28829476 +Myanmar,52885223 +Namibia,2479713 +Nauru,13049 +Nepal,28982771 +Netherlands,17018408 +New Caledonia,278000 +New Zealand,4692700 +Nicaragua,6149928 +Niger,20672987 +Nigeria,185989640 +Northern Mariana Islands,55023 +Norway,5232929 +Oman,4424762 +Pakistan,193203476 +Palau,21503 +Panama,4034119 +Papua New Guinea,8084991 +Paraguay,6725308 +Peru,31773839 +Philippines,103320222 +Poland,37948016 +Portugal,10324611 +Puerto Rico,3411307 +Qatar,2569804 +Romania,19705301 +Russian Federation,144342396 +Rwanda,11917508 +Samoa,195125 +San Marino,33203 +Sao Tome and Principe,199910 +Saudi Arabia,32275687 +Senegal,15411614 +Serbia,7057412 +Seychelles,94677 +Sierra Leone,7396190 +Singapore,5607283 +Sint Maarten (Dutch part),40005 +Slovak Republic,5428704 +Slovenia,2064845 +Solomon Islands,599419 +Somalia,14317996 +South Africa,55908865 +South Sudan,12230730 +Spain,46443959 +Sri Lanka,21203000 +St. Kitts and Nevis,54821 +St. Lucia,178015 +St. Martin (French part),31949 +St. Vincent and the Grenadines,109643 +Sudan,39578828 +Suriname,558368 +Swaziland,1343098 +Sweden,9903122 +Switzerland,8372098 +Syrian Arab Republic,18430453 +Tajikistan,8734951 +Tanzania,55572201 +Thailand,68863514 +Timor-Leste,1268671 +Togo,7606374 +Tonga,107122 +Trinidad and Tobago,1364962 +Tunisia,11403248 +Turkey,79512426 +Turkmenistan,5662544 +Turks and Caicos Islands,34900 +Tuvalu,11097 +Uganda,41487965 +Ukraine,45004645 +United Arab Emirates,9269612 +United Kingdom,65637239 +United States,323127513 +Uruguay,3444006 +Uzbekistan,31848200 +Vanuatu,270402 +Venezuela; RB,31568179 +Vietnam,92701100 +Virgin Islands (U.S.),102951 +West Bank and Gaza,4551566 +Yemen; Rep.,27584213 +Zambia,16591390 +Zimbabwe,16150362 diff --git a/examples/hashmap/countries/main.c b/examples/hashmap/countries/main.c new file mode 100644 index 0000000..3d4e7ef --- /dev/null +++ b/examples/hashmap/countries/main.c @@ -0,0 +1,90 @@ +#include "hashmap.h" +#include +#include +#include +#include +#include +#include "util.c" + +HASHMAP_GENERATE(hm, hmap, , char *, size_t) + +hmap *load(const char *fname); + +int main(int argc, char const *argv[]) +{ + hmap *map = load("data.csv"); + + if (!map) + return 1; + + printf("+--------------------------------------------------+\n"); + printf("| Search Country Population by Country Name |\n"); + printf("| |\n"); + printf("| - Type 'exit' to exit |\n"); + printf("| - Type country name to search its population |\n"); + printf("+--------------------------------------------------+\n\n"); + + // If you wish to list all items + // char *key; + // size_t value, index; + + // hmap_iter iter; + // for (hm_iter_new(&iter, map); !hm_iter_end(&iter);) + // { + // hm_iter_next(&iter, &key, &value, &index); + // printf("%30s : %" PRIuMAX "\n", key, value); + + // free(key); + // } + + char sbuffer[1000]; + + while (1) + { + printf("Query > "); + fflush(stdin); + scanf("%[^\n]s", sbuffer); + + if (strcmp(sbuffer, "exit") == 0) + break; + + size_t *result = hm_get_ref(map, sbuffer); + + if (result == NULL) + printf("Result > %s Not Found\n", sbuffer); + else + printf("Result > %" PRIuMAX "\n", *result); + } + + hm_free(map); + + return 0; +} + +hmap *load(const char *fname) +{ + FILE *fptr = fopen(fname, "r"); + + if (!fptr) + return NULL; + + hmap *map = hm_new(10000, 0.7, chrcmp, chrhash); + + char buff[400]; + size_t value; + + while (fscanf(fptr, "%[^,],%" PRIuMAX "\n", buff, &value) != EOF) + { + char *name = strdup(buff); + + if (!hm_insert(map, name, value)) + { + printf("\n\n\nERROR\n"); + printf("%s : %" PRIuMAX "", name, value); + + exit(2); + } + } + + return map; +} \ No newline at end of file diff --git a/examples/hashmap/countries/util.c b/examples/hashmap/countries/util.c new file mode 100644 index 0000000..e20c824 --- /dev/null +++ b/examples/hashmap/countries/util.c @@ -0,0 +1,17 @@ +#include + +int chrcmp(char *a, char *b) +{ + return strcmp(a, b); +} + +size_t chrhash(char *str) +{ + size_t hash = 5381; + int c; + + while ((c = *str++)) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + + return hash; +}