Skip to content

Commit 6bcbdfb

Browse files
Eric Wonggitster
authored andcommitted
hashmap_get_next returns "struct hashmap_entry *"
This is a step towards removing the requirement for hashmap_entry being the first field of a struct. Signed-off-by: Eric Wong <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 973d5ee commit 6bcbdfb

File tree

6 files changed

+39
-23
lines changed

6 files changed

+39
-23
lines changed

diff.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,10 @@ static void pmb_advance_or_null_multi_match(struct diff_options *o,
10351035
{
10361036
int i;
10371037
char *got_match = xcalloc(1, pmb_nr);
1038+
struct hashmap_entry *ent;
10381039

1039-
for (; match; match = hashmap_get_next(hm, &match->ent)) {
1040+
for (ent = &match->ent; ent; ent = hashmap_get_next(hm, ent)) {
1041+
match = container_of(ent, struct moved_entry, ent);
10401042
for (i = 0; i < pmb_nr; i++) {
10411043
struct moved_entry *prev = pmb[i].match;
10421044
struct moved_entry *cur = (prev && prev->next_line) ?
@@ -1135,29 +1137,30 @@ static void mark_color_as_moved(struct diff_options *o,
11351137

11361138
for (n = 0; n < o->emitted_symbols->nr; n++) {
11371139
struct hashmap *hm = NULL;
1140+
struct hashmap_entry *ent = NULL;
11381141
struct moved_entry *key;
1139-
struct moved_entry *match = NULL;
1142+
struct moved_entry *match;
11401143
struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
11411144
enum diff_symbol last_symbol = 0;
11421145

11431146
switch (l->s) {
11441147
case DIFF_SYMBOL_PLUS:
11451148
hm = del_lines;
11461149
key = prepare_entry(o, n);
1147-
match = hashmap_get(hm, &key->ent, NULL);
1150+
ent = hashmap_get(hm, &key->ent, NULL);
11481151
free(key);
11491152
break;
11501153
case DIFF_SYMBOL_MINUS:
11511154
hm = add_lines;
11521155
key = prepare_entry(o, n);
1153-
match = hashmap_get(hm, &key->ent, NULL);
1156+
ent = hashmap_get(hm, &key->ent, NULL);
11541157
free(key);
11551158
break;
11561159
default:
11571160
flipped_block = 0;
11581161
}
11591162

1160-
if (!match) {
1163+
if (!ent) {
11611164
int i;
11621165

11631166
adjust_last_block(o, n, block_length);
@@ -1169,6 +1172,7 @@ static void mark_color_as_moved(struct diff_options *o,
11691172
last_symbol = l->s;
11701173
continue;
11711174
}
1175+
match = container_of(ent, struct moved_entry, ent);
11721176

11731177
if (o->color_moved == COLOR_MOVED_PLAIN) {
11741178
last_symbol = l->s;
@@ -1189,8 +1193,9 @@ static void mark_color_as_moved(struct diff_options *o,
11891193
* The current line is the start of a new block.
11901194
* Setup the set of potential blocks.
11911195
*/
1192-
for (; match; match = hashmap_get_next(hm,
1193-
&match->ent)) {
1196+
for (; ent; ent = hashmap_get_next(hm, ent)) {
1197+
match = container_of(ent, struct moved_entry,
1198+
ent);
11941199
ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
11951200
if (o->color_moved_ws_handling &
11961201
COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) {

diffcore-rename.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,20 +274,23 @@ static int find_identical_files(struct hashmap *srcs,
274274
struct diff_options *options)
275275
{
276276
int renames = 0;
277-
277+
struct hashmap_entry *ent;
278278
struct diff_filespec *target = rename_dst[dst_index].two;
279279
struct file_similarity *p, *best = NULL;
280280
int i = 100, best_score = -1;
281281

282282
/*
283283
* Find the best source match for specified destination.
284284
*/
285-
p = hashmap_get_from_hash(srcs,
285+
ent = hashmap_get_from_hash(srcs,
286286
hash_filespec(options->repo, target),
287287
NULL);
288-
for (; p; p = hashmap_get_next(srcs, &p->entry)) {
288+
for (; ent; ent = hashmap_get_next(srcs, ent)) {
289289
int score;
290-
struct diff_filespec *source = p->filespec;
290+
struct diff_filespec *source;
291+
292+
p = container_of(ent, struct file_similarity, entry);
293+
source = p->filespec;
291294

292295
/* False hash collision? */
293296
if (!oideq(&source->oid, &target->oid))

hashmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void *hashmap_get(const struct hashmap *map, const struct hashmap_entry *key,
192192
return *find_entry_ptr(map, key, keydata);
193193
}
194194

195-
void *hashmap_get_next(const struct hashmap *map,
195+
struct hashmap_entry *hashmap_get_next(const struct hashmap *map,
196196
const struct hashmap_entry *entry)
197197
{
198198
struct hashmap_entry *e = entry->next;

hashmap.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,19 @@
5555
*
5656
* if (!strcmp("print_all_by_key", action)) {
5757
* struct long2string k, *e;
58+
* struct hashmap_entry *ent;
5859
* hashmap_entry_init(&k->ent, memhash(&key, sizeof(long)));
5960
* k.key = key;
6061
*
6162
* flags &= ~COMPARE_VALUE;
62-
* e = hashmap_get(&map, &k, NULL);
63-
* if (e) {
63+
* ent = hashmap_get(&map, &k, NULL);
64+
* if (ent) {
65+
* e = container_of(ent, struct long2string, ent);
6466
* printf("first: %ld %s\n", e->key, e->value);
65-
* while ((e = hashmap_get_next(&map, e)))
67+
* while ((ent = hashmap_get_next(&map, ent))) {
68+
* e = container_of(ent, struct long2string, ent);
6669
* printf("found more: %ld %s\n", e->key, e->value);
70+
* }
6771
* }
6872
* }
6973
*
@@ -320,7 +324,7 @@ static inline void *hashmap_get_from_hash(const struct hashmap *map,
320324
* `entry` is the hashmap_entry to start the search from, obtained via a previous
321325
* call to `hashmap_get` or `hashmap_get_next`.
322326
*/
323-
void *hashmap_get_next(const struct hashmap *map,
327+
struct hashmap_entry *hashmap_get_next(const struct hashmap *map,
324328
const struct hashmap_entry *entry);
325329

326330
/*

name-hash.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,15 +702,17 @@ void adjust_dirname_case(struct index_state *istate, char *name)
702702
struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
703703
{
704704
struct cache_entry *ce;
705+
struct hashmap_entry *ent;
705706

706707
lazy_init_name_hash(istate);
707708

708-
ce = hashmap_get_from_hash(&istate->name_hash,
709+
ent = hashmap_get_from_hash(&istate->name_hash,
709710
memihash(name, namelen), NULL);
710-
while (ce) {
711+
while (ent) {
712+
ce = container_of(ent, struct cache_entry, ent);
711713
if (same_name(ce, name, namelen, icase))
712714
return ce;
713-
ce = hashmap_get_next(&istate->name_hash, &ce->ent);
715+
ent = hashmap_get_next(&istate->name_hash, ent);
714716
}
715717
return NULL;
716718
}

t/helper/test-hashmap.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,18 @@ int cmd__hashmap(int argc, const char **argv)
194194
free(entry);
195195

196196
} else if (!strcmp("get", cmd) && p1) {
197+
struct hashmap_entry *e;
197198

198199
/* lookup entry in hashmap */
199-
entry = hashmap_get_from_hash(&map, hash, p1);
200+
e = hashmap_get_from_hash(&map, hash, p1);
200201

201202
/* print result */
202-
if (!entry)
203+
if (!e)
203204
puts("NULL");
204-
while (entry) {
205+
while (e) {
206+
entry = container_of(e, struct test_entry, ent);
205207
puts(get_value(entry));
206-
entry = hashmap_get_next(&map, &entry->ent);
208+
e = hashmap_get_next(&map, e);
207209
}
208210

209211
} else if (!strcmp("remove", cmd) && p1) {

0 commit comments

Comments
 (0)