1
0
mirror of https://git.savannah.gnu.org/git/gperf.git synced 2025-12-02 13:09:22 +00:00

Remove all occurrences of reinterpret_cast<>.

* src/keyword-list.h (Keyword_List): Add template parameter list <KT>.
(KeywordExt_List): Define as a typedef, not as a subclass.
(copy_list, delete_list, mergesort_list): Templatize accordingly.
* src/keyword-list.icc (Keyword_List): Add template parameter list <KT>.
(KeywordExt_List): Remove method definitions.
* src/keyword-list.cc (Keyword_List): Add template parameter list <KT>.
(copy_list, mergesort_list): Remove definitions on subclass.
Add explicit template instantiation.
* src/keyword.h (Keyword_Factory): Add template parameter list <KT>.
* src/keyword.cc (Keyword_Factory): Likewise.
Add explicit template instantiation.
* src/input.h (Input): Add template parameter list <KT>.
* src/input.cc (Input): Likewise. Add explicit template instantiation.
* src/main.cc (KeywordExt_Factory): Define as a typedef, not as a
subclass.
(main): Update.
This commit is contained in:
Bruno Haible
2025-04-05 14:18:27 +02:00
parent 67c697622f
commit a041291c80
9 changed files with 1060 additions and 1019 deletions

View File

@@ -1,6 +1,6 @@
/* Keyword list.
Copyright (C) 2002-2003 Free Software Foundation, Inc.
Copyright (C) 2002-2003, 2025 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>.
This file is part of GNU GPERF.
@@ -23,145 +23,130 @@
#include <stddef.h>
/* -------------------------- Keyword_List class --------------------------- */
/* ------------------------ Keyword_List<KT> class ------------------------- */
/* Constructor. */
Keyword_List::Keyword_List (Keyword *car)
: _cdr (NULL), _car (car)
{
}
template <class KT>
Keyword_List<KT>::Keyword_List (KT *car)
: _cdr (NULL), _car (car)
{
}
/* ------------------------- KeywordExt_List class ------------------------- */
/* Constructor. */
KeywordExt_List::KeywordExt_List (KeywordExt *car)
: Keyword_List (car)
{
}
/* ------------------------ Keyword_List functions ------------------------- */
/* ---------------------- Keyword_List<KT> functions ----------------------- */
/* Copies a linear list, sharing the list elements. */
Keyword_List *
copy_list (Keyword_List *list)
{
Keyword_List *result;
Keyword_List **lastp = &result;
while (list != NULL)
{
Keyword_List *new_cons = new Keyword_List (list->first());
*lastp = new_cons;
lastp = &new_cons->rest();
list = list->rest();
}
*lastp = NULL;
return result;
}
/* Copies a linear list, sharing the list elements. */
KeywordExt_List *
copy_list (KeywordExt_List *list)
{
return static_cast<KeywordExt_List *> (copy_list (static_cast<Keyword_List *> (list)));
}
template <class KT>
Keyword_List<KT> *
copy_list (Keyword_List<KT> *list)
{
Keyword_List<KT> *result;
Keyword_List<KT> **lastp = &result;
while (list != NULL)
{
Keyword_List<KT> *new_cons = new Keyword_List<KT> (list->first());
*lastp = new_cons;
lastp = &new_cons->rest();
list = list->rest();
}
*lastp = NULL;
return result;
}
/* Deletes a linear list, keeping the list elements in memory. */
void
delete_list (Keyword_List *list)
{
while (list != NULL)
{
Keyword_List *rest = list->rest();
delete list;
list = rest;
}
}
template <class KT>
void
delete_list (Keyword_List<KT> *list)
{
while (list != NULL)
{
Keyword_List<KT> *rest = list->rest();
delete list;
list = rest;
}
}
/* Type of a comparison function. */
typedef bool (*Keyword_Comparison) (Keyword *keyword1, Keyword *keyword2);
#if 0 /* wishful thinking */
template <class KT>
typedef bool (*Keyword_Comparison) (KT *keyword1, KT *keyword2);
#endif
/* Merges two sorted lists together to form one sorted list. */
static Keyword_List *
merge (Keyword_List *list1, Keyword_List *list2, Keyword_Comparison less)
{
Keyword_List *result;
Keyword_List **resultp = &result;
for (;;)
{
if (!list1)
{
*resultp = list2;
break;
}
if (!list2)
{
*resultp = list1;
break;
}
if (less (list2->first(), list1->first()))
{
*resultp = list2;
resultp = &list2->rest();
/* We would have a stable sorting if the next line would read:
list2 = *resultp; */
list2 = list1; list1 = *resultp;
}
else
{
*resultp = list1;
resultp = &list1->rest();
list1 = *resultp;
}
}
return result;
}
template <class KT>
static Keyword_List<KT> *
merge (Keyword_List<KT> *list1, Keyword_List<KT> *list2,
bool (*less) (KT *keyword1, KT *keyword2))
{
Keyword_List<KT> *result;
Keyword_List<KT> **resultp = &result;
for (;;)
{
if (!list1)
{
*resultp = list2;
break;
}
if (!list2)
{
*resultp = list1;
break;
}
if (less (list2->first(), list1->first()))
{
*resultp = list2;
resultp = &list2->rest();
/* We would have a stable sorting if the next line would read:
list2 = *resultp; */
list2 = list1; list1 = *resultp;
}
else
{
*resultp = list1;
resultp = &list1->rest();
list1 = *resultp;
}
}
return result;
}
/* Sorts a linear list, given a comparison function.
Note: This uses a variant of mergesort that is *not* a stable sorting
algorithm. */
Keyword_List *
mergesort_list (Keyword_List *list, Keyword_Comparison less)
{
if (list == NULL || list->rest() == NULL)
/* List of length 0 or 1. Nothing to do. */
return list;
else
{
/* Determine a list node in the middle. */
Keyword_List *middle = list;
for (Keyword_List *temp = list->rest();;)
{
temp = temp->rest();
if (temp == NULL)
break;
temp = temp->rest();
middle = middle->rest();
if (temp == NULL)
break;
}
template <class KT>
Keyword_List<KT> *
mergesort_list (Keyword_List<KT> *list,
bool (*less) (KT *keyword1, KT *keyword2))
{
if (list == NULL || list->rest() == NULL)
/* List of length 0 or 1. Nothing to do. */
return list;
else
{
/* Determine a list node in the middle. */
Keyword_List<KT> *middle = list;
for (Keyword_List<KT> *temp = list->rest();;)
{
temp = temp->rest();
if (temp == NULL)
break;
temp = temp->rest();
middle = middle->rest();
if (temp == NULL)
break;
}
/* Cut the list into two halves.
If the list has n elements, the left half has ceiling(n/2) elements
and the right half has floor(n/2) elements. */
Keyword_List *right_half = middle->rest();
middle->rest() = NULL;
/* Cut the list into two halves.
If the list has n elements, the left half has ceiling(n/2) elements
and the right half has floor(n/2) elements. */
Keyword_List<KT> *right_half = middle->rest();
middle->rest() = NULL;
/* Sort the two halves, then merge them. */
return merge (mergesort_list (list, less),
mergesort_list (right_half, less),
less);
}
}
KeywordExt_List *
mergesort_list (KeywordExt_List *list,
bool (*less) (KeywordExt *keyword1, KeywordExt *keyword2))
{
return
static_cast<KeywordExt_List *>
(mergesort_list (static_cast<Keyword_List *> (list),
reinterpret_cast<Keyword_Comparison> (less)));
}
/* Sort the two halves, then merge them. */
return merge (mergesort_list (list, less),
mergesort_list (right_half, less),
less);
}
}
#ifndef __OPTIMIZE__
@@ -171,3 +156,27 @@ mergesort_list (KeywordExt_List *list,
#undef INLINE
#endif /* not defined __OPTIMIZE__ */
/* ------------------------------------------------------------------------- */
/* Explicit template instantiations. Needed to avoid link-time errors.
C++ is just misdesigned: The most important aspect in building large
software packages is information hiding. (That's the point of having the
implementation of a .h file in a .cc file, isn't it? And of having
classes with private fields and methods, isn't it?) The fact that we
need the instantiation of the Keyword_List<KT> class and associate functions
only for KT = KeywordExt comes from the code in main.cc. It is ugly that
implementation details of main.cc have an influence into this file here. */
template class Keyword_List<KeywordExt>;
template Keyword_List<KeywordExt> *
copy_list<KeywordExt> (Keyword_List<KeywordExt> *list);
template void
delete_list<KeywordExt> (Keyword_List<KeywordExt> *list);
template Keyword_List<KeywordExt> *
mergesort_list<KeywordExt> (Keyword_List<KeywordExt> *list,
bool (*less) (KeywordExt *keyword1,
KeywordExt *keyword2));
/* ------------------------------------------------------------------------- */