Hashmap example

This commit is contained in:
Leonardo committed 2019-05-03 23:57:58 -03:00
1 parent 661455abe4
commit f0b763afba
4 files changed
+324 -1

No files matched your search

+1 -1
View File
@@ -1,3 +1,3 @@
# Hashmap examples
Currently none
* countries - Uses a hashmap to map the country name to its population
+216
View File
@@ -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
1 Afghanistan 34656032
2 Albania 2876101
3 Algeria 40606052
4 American Samoa 55599
5 Andorra 77281
6 Angola 28813463
7 Antigua and Barbuda 100963
8 Argentina 43847430
9 Armenia 2924816
10 Aruba 104822
11 Australia 24127159
12 Austria 8747358
13 Azerbaijan 9762274
14 Bahamas; The 391232
15 Bahrain 1425171
16 Bangladesh 162951560
17 Barbados 284996
18 Belarus 9507120
19 Belgium 11348159
20 Belize 366954
21 Benin 10872298
22 Bermuda 65331
23 Bhutan 797765
24 Bolivia 10887882
25 Bosnia and Herzegovina 3516816
26 Botswana 2250260
27 Brazil 207652865
28 British Virgin Islands 30661
29 Brunei Darussalam 423196
30 Bulgaria 7127822
31 Burkina Faso 18646433
32 Burundi 10524117
33 Cabo Verde 539560
34 Cambodia 15762370
35 Cameroon 23439189
36 Canada 36286425
37 Cayman Islands 60765
38 Central African Republic 4594621
39 Chad 14452543
40 Channel Islands 164541
41 Chile 17909754
42 China 1378665000
43 Colombia 48653419
44 Comoros 795601
45 Congo; Dem. Rep. 78736153
46 Congo; Rep. 5125821
47 Costa Rica 4857274
48 Cote d'Ivoire 23695919
49 Croatia 4170600
50 Cuba 11475982
51 Curacao 159999
52 Cyprus 1170125
53 Czech Republic 10561633
54 Denmark 5731118
55 Djibouti 942333
56 Dominica 73543
57 Dominican Republic 10648791
58 Ecuador 16385068
59 Egypt; Arab Rep. 95688681
60 El Salvador 6344722
61 Equatorial Guinea 1221490
62 Estonia 1316481
63 Ethiopia 102403196
64 Faroe Islands 49117
65 Fiji 898760
66 Finland 5495096
67 France 66896109
68 French Polynesia 280208
69 Gabon 1979786
70 Gambia; The 2038501
71 Georgia 3719300
72 Germany 82667685
73 Ghana 28206728
74 Gibraltar 34408
75 Greece 10746740
76 Greenland 56186
77 Grenada 107317
78 Guam 162896
79 Guatemala 16582469
80 Guinea 12395924
81 Guinea-Bissau 1815698
82 Guyana 773303
83 Haiti 10847334
84 Honduras 9112867
85 Hong Kong SAR; China 7346700
86 Hungary 9817958
87 Iceland 334252
88 India 1324171354
89 Indonesia 261115456
90 Iran; Islamic Rep. 80277428
91 Iraq 37202572
92 Ireland 4773095
93 Isle of Man 83737
94 Israel 8547100
95 Italy 60600590
96 Jamaica 2881355
97 Japan 126994511
98 Jordan 9455802
99 Kazakhstan 17797032
100 Kenya 48461567
101 Kiribati 114395
102 Korea; Dem. People’s Rep. 25368620
103 Korea; Rep. 51245707
104 Kosovo 1816200
105 Kuwait 4052584
106 Kyrgyz Republic 6082700
107 Lao PDR 6758353
108 Latvia 1960424
109 Lebanon 6006668
110 Lesotho 2203821
111 Liberia 4613823
112 Libya 6293253
113 Liechtenstein 37666
114 Lithuania 2872298
115 Luxembourg 582972
116 Macao SAR; China 612167
117 Macedonia; FYR 2081206
118 Madagascar 24894551
119 Malawi 18091575
120 Malaysia 31187265
121 Maldives 417492
122 Mali 17994837
123 Malta 436947
124 Marshall Islands 53066
125 Mauritania 4301018
126 Mauritius 1263473
127 Mexico 127540423
128 Micronesia; Fed. Sts. 104937
129 Moldova 3552000
130 Monaco 38499
131 Mongolia 3027398
132 Montenegro 622781
133 Morocco 35276786
134 Mozambique 28829476
135 Myanmar 52885223
136 Namibia 2479713
137 Nauru 13049
138 Nepal 28982771
139 Netherlands 17018408
140 New Caledonia 278000
141 New Zealand 4692700
142 Nicaragua 6149928
143 Niger 20672987
144 Nigeria 185989640
145 Northern Mariana Islands 55023
146 Norway 5232929
147 Oman 4424762
148 Pakistan 193203476
149 Palau 21503
150 Panama 4034119
151 Papua New Guinea 8084991
152 Paraguay 6725308
153 Peru 31773839
154 Philippines 103320222
155 Poland 37948016
156 Portugal 10324611
157 Puerto Rico 3411307
158 Qatar 2569804
159 Romania 19705301
160 Russian Federation 144342396
161 Rwanda 11917508
162 Samoa 195125
163 San Marino 33203
164 Sao Tome and Principe 199910
165 Saudi Arabia 32275687
166 Senegal 15411614
167 Serbia 7057412
168 Seychelles 94677
169 Sierra Leone 7396190
170 Singapore 5607283
171 Sint Maarten (Dutch part) 40005
172 Slovak Republic 5428704
173 Slovenia 2064845
174 Solomon Islands 599419
175 Somalia 14317996
176 South Africa 55908865
177 South Sudan 12230730
178 Spain 46443959
179 Sri Lanka 21203000
180 St. Kitts and Nevis 54821
181 St. Lucia 178015
182 St. Martin (French part) 31949
183 St. Vincent and the Grenadines 109643
184 Sudan 39578828
185 Suriname 558368
186 Swaziland 1343098
187 Sweden 9903122
188 Switzerland 8372098
189 Syrian Arab Republic 18430453
190 Tajikistan 8734951
191 Tanzania 55572201
192 Thailand 68863514
193 Timor-Leste 1268671
194 Togo 7606374
195 Tonga 107122
196 Trinidad and Tobago 1364962
197 Tunisia 11403248
198 Turkey 79512426
199 Turkmenistan 5662544
200 Turks and Caicos Islands 34900
201 Tuvalu 11097
202 Uganda 41487965
203 Ukraine 45004645
204 United Arab Emirates 9269612
205 United Kingdom 65637239
206 United States 323127513
207 Uruguay 3444006
208 Uzbekistan 31848200
209 Vanuatu 270402
210 Venezuela; RB 31568179
211 Vietnam 92701100
212 Virgin Islands (U.S.) 102951
213 West Bank and Gaza 4551566
214 Yemen; Rep. 27584213
215 Zambia 16591390
216 Zimbabwe 16150362
+90
View File
@@ -0,0 +1,90 @@
#include "hashmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <inttypes.h>
#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;
}
+17
View File
@@ -0,0 +1,17 @@
#include <stdlib.h>
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;
}