@@ -12,11 +12,6 @@ struct test_entry
12
12
char key [FLEX_ARRAY ];
13
13
};
14
14
15
- static const char * get_value (const struct test_entry * e )
16
- {
17
- return e -> key + strlen (e -> key ) + 1 ;
18
- }
19
-
20
15
static int test_entry_cmp (const void * cmp_data ,
21
16
const struct hashmap_entry * eptr ,
22
17
const struct hashmap_entry * entry_or_key ,
@@ -141,30 +136,16 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
141
136
/*
142
137
* Read stdin line by line and print result of commands to stdout:
143
138
*
144
- * hash key -> strhash(key) memhash(key) strihash(key) memihash(key)
145
- * put key value -> NULL / old value
146
- * get key -> NULL / value
147
- * remove key -> NULL / old value
148
- * iterate -> key1 value1\nkey2 value2\n...
149
- * size -> tablesize numentries
150
- *
151
139
* perfhashmap method rounds -> test hashmap.[ch] performance
152
140
*/
153
141
int cmd__hashmap (int argc , const char * * argv )
154
142
{
155
143
struct string_list parts = STRING_LIST_INIT_NODUP ;
156
144
struct strbuf line = STRBUF_INIT ;
157
- int icase ;
158
- struct hashmap map = HASHMAP_INIT (test_entry_cmp , & icase );
159
-
160
- /* init hash map */
161
- icase = argc > 1 && !strcmp ("ignorecase" , argv [1 ]);
162
145
163
146
/* process commands from stdin */
164
147
while (strbuf_getline (& line , stdin ) != EOF ) {
165
148
char * cmd , * p1 , * p2 ;
166
- unsigned int hash = 0 ;
167
- struct test_entry * entry ;
168
149
169
150
/* break line into command and up to two parameters */
170
151
string_list_setlen (& parts , 0 );
@@ -180,84 +161,8 @@ int cmd__hashmap(int argc, const char **argv)
180
161
cmd = parts .items [0 ].string ;
181
162
p1 = parts .nr >= 1 ? parts .items [1 ].string : NULL ;
182
163
p2 = parts .nr >= 2 ? parts .items [2 ].string : NULL ;
183
- if (p1 )
184
- hash = icase ? strihash (p1 ) : strhash (p1 );
185
-
186
- if (!strcmp ("add" , cmd ) && p1 && p2 ) {
187
-
188
- /* create entry with key = p1, value = p2 */
189
- entry = alloc_test_entry (hash , p1 , p2 );
190
-
191
- /* add to hashmap */
192
- hashmap_add (& map , & entry -> ent );
193
-
194
- } else if (!strcmp ("put" , cmd ) && p1 && p2 ) {
195
-
196
- /* create entry with key = p1, value = p2 */
197
- entry = alloc_test_entry (hash , p1 , p2 );
198
-
199
- /* add / replace entry */
200
- entry = hashmap_put_entry (& map , entry , ent );
201
-
202
- /* print and free replaced entry, if any */
203
- puts (entry ? get_value (entry ) : "NULL" );
204
- free (entry );
205
-
206
- } else if (!strcmp ("get" , cmd ) && p1 ) {
207
- /* lookup entry in hashmap */
208
- entry = hashmap_get_entry_from_hash (& map , hash , p1 ,
209
- struct test_entry , ent );
210
-
211
- /* print result */
212
- if (!entry )
213
- puts ("NULL" );
214
- hashmap_for_each_entry_from (& map , entry , ent )
215
- puts (get_value (entry ));
216
-
217
- } else if (!strcmp ("remove" , cmd ) && p1 ) {
218
-
219
- /* setup static key */
220
- struct hashmap_entry key ;
221
- struct hashmap_entry * rm ;
222
- hashmap_entry_init (& key , hash );
223
-
224
- /* remove entry from hashmap */
225
- rm = hashmap_remove (& map , & key , p1 );
226
- entry = rm ? container_of (rm , struct test_entry , ent )
227
- : NULL ;
228
-
229
- /* print result and free entry*/
230
- puts (entry ? get_value (entry ) : "NULL" );
231
- free (entry );
232
-
233
- } else if (!strcmp ("iterate" , cmd )) {
234
- struct hashmap_iter iter ;
235
-
236
- hashmap_for_each_entry (& map , & iter , entry ,
237
- ent /* member name */ )
238
- printf ("%s %s\n" , entry -> key , get_value (entry ));
239
-
240
- } else if (!strcmp ("size" , cmd )) {
241
-
242
- /* print table sizes */
243
- printf ("%u %u\n" , map .tablesize ,
244
- hashmap_get_size (& map ));
245
-
246
- } else if (!strcmp ("intern" , cmd ) && p1 ) {
247
-
248
- /* test that strintern works */
249
- const char * i1 = strintern (p1 );
250
- const char * i2 = strintern (p1 );
251
- if (strcmp (i1 , p1 ))
252
- printf ("strintern(%s) returns %s\n" , p1 , i1 );
253
- else if (i1 == p1 )
254
- printf ("strintern(%s) returns input pointer\n" , p1 );
255
- else if (i1 != i2 )
256
- printf ("strintern(%s) != strintern(%s)" , i1 , i2 );
257
- else
258
- printf ("%s\n" , i1 );
259
164
260
- } else if (!strcmp ("perfhashmap" , cmd ) && p1 && p2 ) {
165
+ if (!strcmp ("perfhashmap" , cmd ) && p1 && p2 ) {
261
166
262
167
perf_hashmap (atoi (p1 ), atoi (p2 ));
263
168
@@ -270,6 +175,5 @@ int cmd__hashmap(int argc, const char **argv)
270
175
271
176
string_list_clear (& parts , 0 );
272
177
strbuf_release (& line );
273
- hashmap_clear_and_free (& map , struct test_entry , ent );
274
178
return 0 ;
275
179
}
0 commit comments