Debian Codegraph @

src/keyword-list.cc

raw
1 /* Keyword list.
2
3 Copyright (C) 2002-2003, 2025 Free Software Foundation, Inc.
4 Written by Bruno Haible <[email protected]>.
5
6 This file is part of GNU GPERF.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22
23 /* Specification. */
24 #include "keyword-list.h"
25
26 #include <stddef.h>
27
28 /* ------------------------ Keyword_List<KT> class ------------------------- */
29
30 /* Constructor. */
31 template <class KT>
32 Keyword_List<KT>::Keyword_List (KT *car)
33 : _cdr (NULL), _car (car)
34 {
35 }
36
37 /* ---------------------- Keyword_List<KT> functions ----------------------- */
38
39 /* Copies a linear list, sharing the list elements. */
40 template <class KT>
41 Keyword_List<KT> *
42 copy_list (Keyword_List<KT> *list)
43 {
44 Keyword_List<KT> *result;
45 Keyword_List<KT> **lastp = &result;
46 while (list != NULL)
47 {
48 Keyword_List<KT> *new_cons = new Keyword_List<KT> (list->first());
49 *lastp = new_cons;
50 lastp = &new_cons->rest();
51 list = list->rest();
52 }
53 *lastp = NULL;
54 return result;
55 }
56
57 /* Deletes a linear list, keeping the list elements in memory. */
58 template <class KT>
59 void
60 delete_list (Keyword_List<KT> *list)
61 {
62 while (list != NULL)
63 {
64 Keyword_List<KT> *rest = list->rest();
65 delete list;
66 list = rest;
67 }
68 }
69
70 /* Type of a comparison function. */
71 #if 0 /* wishful thinking */
72 template <class KT>
73 typedef bool (*Keyword_Comparison) (KT *keyword1, KT *keyword2);
74 #endif
75
76 /* Merges two sorted lists together to form one sorted list. */
77 template <class KT>
78 static Keyword_List<KT> *
79 merge (Keyword_List<KT> *list1, Keyword_List<KT> *list2,
80 bool (*less) (KT *keyword1, KT *keyword2))
81 {
82 Keyword_List<KT> *result;
83 Keyword_List<KT> **resultp = &result;
84 for (;;)
85 {
86 if (!list1)
87 {
88 *resultp = list2;
89 break;
90 }
91 if (!list2)
92 {
93 *resultp = list1;
94 break;
95 }
96 if (less (list2->first(), list1->first()))
97 {
98 *resultp = list2;
99 resultp = &list2->rest();
100 /* We would have a stable sorting if the next line would read:
101 list2 = *resultp; */
102 list2 = list1; list1 = *resultp;
103 }
104 else
105 {
106 *resultp = list1;
107 resultp = &list1->rest();
108 list1 = *resultp;
109 }
110 }
111 return result;
112 }
113
114 /* Sorts a linear list, given a comparison function.
115 Note: This uses a variant of mergesort that is *not* a stable sorting
116 algorithm. */
117 template <class KT>
118 Keyword_List<KT> *
119 mergesort_list (Keyword_List<KT> *list,
120 bool (*less) (KT *keyword1, KT *keyword2))
121 {
122 if (list == NULL || list->rest() == NULL)
123 /* List of length 0 or 1. Nothing to do. */
124 return list;
125 else
126 {
127 /* Determine a list node in the middle. */
128 Keyword_List<KT> *middle = list;
129 for (Keyword_List<KT> *temp = list->rest();;)
130 {
131 temp = temp->rest();
132 if (temp == NULL)
133 break;
134 temp = temp->rest();
135 middle = middle->rest();
136 if (temp == NULL)
137 break;
138 }
139
140 /* Cut the list into two halves.
141 If the list has n elements, the left half has ceiling(n/2) elements
142 and the right half has floor(n/2) elements. */
143 Keyword_List<KT> *right_half = middle->rest();
144 middle->rest() = NULL;
145
146 /* Sort the two halves, then merge them. */
147 return merge (mergesort_list (list, less),
148 mergesort_list (right_half, less),
149 less);
150 }
151 }
152
153
154 #ifndef __OPTIMIZE__
155
156 #define INLINE /* not inline */
157 #include "keyword-list.icc"
158 #undef INLINE
159
160 #endif /* not defined __OPTIMIZE__ */
161
162 /* ------------------------------------------------------------------------- */
163
164 /* Explicit template instantiations. Needed to avoid link-time errors.
165
166 C++ is just misdesigned: The most important aspect in building large
167 software packages is information hiding. (That's the point of having the
168 implementation of a .h file in a .cc file, isn't it? And of having
169 classes with private fields and methods, isn't it?) The fact that we
170 need the instantiation of the Keyword_List<KT> class and associate functions
171 only for KT = KeywordExt comes from the code in main.cc. It is ugly that
172 implementation details of main.cc have an influence into this file here. */
173
174 template class Keyword_List<KeywordExt>;
175 template Keyword_List<KeywordExt> *
176 copy_list<KeywordExt> (Keyword_List<KeywordExt> *list);
177 template void
178 delete_list<KeywordExt> (Keyword_List<KeywordExt> *list);
179 template Keyword_List<KeywordExt> *
180 mergesort_list<KeywordExt> (Keyword_List<KeywordExt> *list,
181 bool (*less) (KeywordExt *keyword1,
182 KeywordExt *keyword2));
183
184 /* ------------------------------------------------------------------------- */