Skip to content

Commit 6edc863

Browse files
committed
readtags,refactor: use ptrArray to store tags
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 663753d commit 6edc863

File tree

1 file changed

+12
-57
lines changed

1 file changed

+12
-57
lines changed

extra-cmds/readtags-cmd.c

Lines changed: 12 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -173,68 +173,19 @@ static tagEntry *copyTag (tagEntry *o)
173173
return n;
174174
}
175175

176-
struct tagEntryHolder {
177-
tagEntry *e;
178-
};
179-
struct tagEntryArray {
180-
int count;
181-
int length;
182-
struct tagEntryHolder *a;
183-
};
184-
185-
static struct tagEntryArray *tagEntryArrayNew (void)
186-
{
187-
struct tagEntryArray * a = eMalloc (sizeof (struct tagEntryArray));
188-
189-
a->count = 0;
190-
a->length = 1024;
191-
a->a = eMalloc(a->length * sizeof (a->a[0]));
192-
193-
return a;
194-
}
195-
196-
static void tagEntryArrayPush (struct tagEntryArray *a, tagEntry *e)
197-
{
198-
if (a->count + 1 == a->length)
199-
{
200-
if (a->length * 2 < a->length) {
201-
fprintf(stderr, "too large array allocation");
202-
exit(1);
203-
}
204-
205-
struct tagEntryHolder *tmp = eRealloc (a->a, sizeof (a->a[0]) * (a->length * 2));
206-
a->a = tmp;
207-
a->length *= 2;
208-
}
209-
210-
a->a[a->count++].e = e;
211-
}
212-
213-
static void tagEntryArrayFree (struct tagEntryArray *a, int freeTags)
214-
{
215-
if (freeTags)
216-
{
217-
for (int i = 0; i < a->count; i++)
218-
freeCopiedTag (a->a[i].e);
219-
}
220-
free (a->a);
221-
free (a);
222-
}
223-
224176
static int compareTagEntry (const void *a, const void *b)
225177
{
226-
return s_compare (((struct tagEntryHolder *)a)->e, ((struct tagEntryHolder *)b)->e, Sorter);
178+
return s_compare (a, b, Sorter);
227179
}
228180

229181
static void walkTags (tagFile *const file, tagEntry *first_entry,
230182
tagResult (* nextfn) (tagFile *const, tagEntry *),
231183
void (* actionfn) (const tagEntry *, void *), void *data,
232184
struct actionSpec *actionSpec)
233185
{
234-
struct tagEntryArray *a = NULL;
235-
186+
ptrArray *a = NULL;
236187
if (Sorter)
237-
a = tagEntryArrayNew ();
188+
a = ptrArrayNew ((ptrArrayDeleteFunc)freeCopiedTag);
238189

239190
do
240191
{
@@ -265,7 +216,7 @@ static void walkTags (tagFile *const file, tagEntry *first_entry,
265216
if (a)
266217
{
267218
tagEntry *e = copyTag (shadow);
268-
tagEntryArrayPush (a, e);
219+
ptrArrayAdd (a, e);
269220
}
270221
else
271222
(* actionfn) (shadow, data);
@@ -282,10 +233,14 @@ static void walkTags (tagFile *const file, tagEntry *first_entry,
282233

283234
if (a)
284235
{
285-
qsort (a->a, a->count, sizeof (a->a[0]), compareTagEntry);
286-
for (int i = 0; i < a->count; i++)
287-
(* actionfn) (a->a[i].e, data);
288-
tagEntryArrayFree (a, 1);
236+
ptrArraySort (a, compareTagEntry);
237+
size_t count = ptrArrayCount(a);
238+
for (unsigned int i = 0; i < count; i++)
239+
{
240+
tagEntry *e = ptrArrayItem (a, i);
241+
(* actionfn) (e, data);
242+
}
243+
ptrArrayDelete (a);
289244
}
290245
}
291246
#else

0 commit comments

Comments
 (0)