Skip to content

Commit 2176792

Browse files
pks-tgitster
authored andcommitted
reftable/refname: refactor binary search over refnames
It is comparatively hard to understand how exactly the binary search over refnames works given that the function and variable names are not exactly easy to grasp. Rename them to make this more obvious. This should not result in any change in behaviour. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e8b8082 commit 2176792

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

reftable/refname.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
#include "refname.h"
1313
#include "reftable-iterator.h"
1414

15-
struct find_arg {
16-
char **names;
17-
const char *want;
15+
struct refname_needle_lesseq_args {
16+
char **haystack;
17+
const char *needle;
1818
};
1919

20-
static int find_name(size_t k, void *arg)
20+
static int refname_needle_lesseq(size_t k, void *_args)
2121
{
22-
struct find_arg *f_arg = arg;
23-
return strcmp(f_arg->names[k], f_arg->want) >= 0;
22+
struct refname_needle_lesseq_args *args = _args;
23+
return strcmp(args->needle, args->haystack[k]) <= 0;
2424
}
2525

2626
static int modification_has_ref(struct modification *mod, const char *name)
@@ -29,21 +29,21 @@ static int modification_has_ref(struct modification *mod, const char *name)
2929
int err = 0;
3030

3131
if (mod->add_len > 0) {
32-
struct find_arg arg = {
33-
.names = mod->add,
34-
.want = name,
32+
struct refname_needle_lesseq_args args = {
33+
.haystack = mod->add,
34+
.needle = name,
3535
};
36-
size_t idx = binsearch(mod->add_len, find_name, &arg);
36+
size_t idx = binsearch(mod->add_len, refname_needle_lesseq, &args);
3737
if (idx < mod->add_len && !strcmp(mod->add[idx], name))
3838
return 0;
3939
}
4040

4141
if (mod->del_len > 0) {
42-
struct find_arg arg = {
43-
.names = mod->del,
44-
.want = name,
42+
struct refname_needle_lesseq_args args = {
43+
.haystack = mod->del,
44+
.needle = name,
4545
};
46-
size_t idx = binsearch(mod->del_len, find_name, &arg);
46+
size_t idx = binsearch(mod->del_len, refname_needle_lesseq, &args);
4747
if (idx < mod->del_len && !strcmp(mod->del[idx], name))
4848
return 1;
4949
}
@@ -71,11 +71,11 @@ static int modification_has_ref_with_prefix(struct modification *mod,
7171
int err = 0;
7272

7373
if (mod->add_len > 0) {
74-
struct find_arg arg = {
75-
.names = mod->add,
76-
.want = prefix,
74+
struct refname_needle_lesseq_args args = {
75+
.haystack = mod->add,
76+
.needle = prefix,
7777
};
78-
size_t idx = binsearch(mod->add_len, find_name, &arg);
78+
size_t idx = binsearch(mod->add_len, refname_needle_lesseq, &args);
7979
if (idx < mod->add_len &&
8080
!strncmp(prefix, mod->add[idx], strlen(prefix)))
8181
goto done;
@@ -90,11 +90,11 @@ static int modification_has_ref_with_prefix(struct modification *mod,
9090
goto done;
9191

9292
if (mod->del_len > 0) {
93-
struct find_arg arg = {
94-
.names = mod->del,
95-
.want = ref.refname,
93+
struct refname_needle_lesseq_args args = {
94+
.haystack = mod->del,
95+
.needle = ref.refname,
9696
};
97-
size_t idx = binsearch(mod->del_len, find_name, &arg);
97+
size_t idx = binsearch(mod->del_len, refname_needle_lesseq, &args);
9898
if (idx < mod->del_len &&
9999
!strcmp(ref.refname, mod->del[idx]))
100100
continue;

0 commit comments

Comments
 (0)