Debian Codegraph @

dwsys/Collection_extensions.cpp

raw symbols SCIP
1 /* Collection_extensions.cpp
2 *
3 * Copyright (C) 1994-2019,2022 David Weenink, 2018 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 djmw 20020812 GPL header
21 djmw 20040420 Fraction in OrderedOfString_difference should be double.
22 djmw 20050511 Skip printing unique labels in OrderedOfString
23 djmw 20061214
24 djmw 20061214 Changed info to Melder_writeLine<x> format.
25 djmw 20110304 Thing_new
26 */
27
28 #include "Collection_extensions.h"cxx . . $ `<file>/dwsys/Collection_extensions.h`/
29 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
30
31 void Collection_Permutation_permuteItems_inplacecxx . . $ Collection_Permutation_permuteItems_inplace(b0d7f5232735fd18). (Collectioncxx . . $ Collection# me, Permutationcxx . . $ Permutation# him) {
32 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! size == hiscxx . . $ `melder/melder.h:47:9`! numberOfElements,
33 U"The number of elements must be equal.");
34 try {
35 autoCollectioncxx . . $ autoCollection# thee = Collection_createcxx . . $ Collection_create(859008c51157a0cd). ();
36 thycxx . . $ `melder/melder.h:45:9`! classInfocxx . . $ structThing#classInfo. = mycxx . . $ `melder/melder.h:44:9`! classInfocxx . . $ structThing#classInfo.;
37 thycxx . . $ `melder/melder.h:45:9`! _initializeOwnershipcxx . . $ CollectionOf#_initializeOwnership(b45ddcbc8423892c). (false);
38 thycxx . . $ `melder/melder.h:45:9`! _growcxx . . $ CollectionOf#_grow(1624ee8275b10bda). (mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.);
39 for (integercxx . . $ integer# pos = 1; pos <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; pos ++) {
40 const integercxx . . $ integer# oldPos = Permutation_getValueAtIndexcxx . . $ Permutation_getValueAtIndex(165de1b32307c4ff). (him, pos);
41 thycxx . . $ `melder/melder.h:45:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).pos] = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).oldPos];
42 }
43 for (integercxx . . $ integer# pos = 1; pos <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; pos ++) {
44 mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).pos] = thycxx . . $ `melder/melder.h:45:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).pos];
45 }
46 } catch (MelderErrorcxx . . $ MelderError#) {
47 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not permuted in-place.");
48 }
49 }
50
51 autoCollectioncxx . . $ autoCollection# Collection_Permutation_permuteItemscxx . . $ Collection_Permutation_permuteItems(81cbd16516e29bd3). (Collectioncxx . . $ Collection# me, Permutationcxx . . $ Permutation# him) {
52 try {
53 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! size == hiscxx . . $ `melder/melder.h:47:9`! numberOfElements,
54 U"The number of elements must be equal.");
55
56 autoCollectioncxx . . $ autoCollection# thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me);
57 Collection_Permutation_permuteItems_inplacecxx . . $ Collection_Permutation_permuteItems_inplace(b0d7f5232735fd18). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), him);
58 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
59 } catch (MelderErrorcxx . . $ MelderError#) {
60 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not permuted.");
61 }
62 }
63
64 void Collection_permuteItems_inplacecxx . . $ Collection_permuteItems_inplace(3082ebad2d2ba03e). (Collectioncxx . . $ Collection# me) {
65 try {
66 autoPermutationcxx . . $ autoPermutation# p = Permutation_createcxx . . $ Permutation_create(50161d5495f26400). (mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size., false);
67 Collection_Permutation_permuteItems_inplacecxx . . $ Collection_Permutation_permuteItems_inplace(b0d7f5232735fd18). (me, p.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
68 } catch (MelderErrorcxx . . $ MelderError#) {
69 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": items not permuted inplace.");
70 }
71 }
72
73 autoCollectioncxx . . $ autoCollection# Collection_permuteItemscxx . . $ Collection_permuteItems(aa26bbe147e98615). (Collectioncxx . . $ Collection# me) {
74 try {
75 autoPermutationcxx . . $ autoPermutation# p = Permutation_createcxx . . $ Permutation_create(50161d5495f26400). (mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size., false);
76 autoCollectioncxx . . $ autoCollection# thee = Collection_Permutation_permuteItemscxx . . $ Collection_Permutation_permuteItems(81cbd16516e29bd3). (me, p.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
77 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
78 } catch (MelderErrorcxx . . $ MelderError#) {
79 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": items not permuted.");
80 }
81 }
82
83 /****************** class OrderedOfString ******************/
84
85 int OrderedOfString_appendcxx . . $ OrderedOfString_append(ade3ccabe4a97830). (StringListcxx . . $ StringList# me, conststring32cxx . . $ conststring32# append) {
86 try {
87 if (! append)
88 return 1; // BUG: lege string appenden??
89 autoSimpleStringcxx . . $ autoSimpleString# item = SimpleString_createcxx . . $ SimpleString_create(d4d4ea089a1063c6). (append);
90 mycxx . . $ `melder/melder.h:44:9`! addItem_movecxx . . $ CollectionOf#addItem_move(bcc0cff787be24fe). (itemcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).());
91 return 1;
92 } catch (MelderErrorcxx . . $ MelderError#) {
93 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": text not appended.");
94 }
95 }
96
97 autoStringListcxx . . $ autoStringList# OrderedOfString_joinItemscxx . . $ OrderedOfString_joinItems(6f6c1fc1c2908ae0). (StringListcxx . . $ StringList# me, StringListcxx . . $ StringList# thee) {
98 try {
99 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! size == thycxx . . $ `melder/melder.h:45:9`! size,
100 U"sizes should be equal.");
101
102 autoStringListcxx . . $ autoStringList# him = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); // FIXME: this copies *all* the data from me, and only the strings from thee
103
104 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; i ++) {
105 const SimpleStringcxx . . $ SimpleString# hisCategory = hiscxx . . $ `melder/melder.h:47:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i], thyCategory = thycxx . . $ `melder/melder.h:45:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i];
106 const integercxx . . $ integer# hisLength = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (hisCategory -> stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).());
107 const integercxx . . $ integer# thyLength = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (thyCategory -> stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).());
108 hisCategory -> stringcxx . . $ structSimpleString#string.. resizecxx . . $ _autostring#resize(1624ee8275b10bda). (hisLength + thyLength);
109 str32cpycxx . . $ str32cpy(6ab934322573b179). (& hisCategory -> stringcxx . . $ structSimpleString#string. [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).hisLength], thyCategory -> stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).());
110 }
111 return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
112 } catch (MelderErrorcxx . . $ MelderError#) {
113 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Items not joined.");
114 }
115 }
116
117 autoStringSetcxx . . $ autoStringSet# StringList_to_StringSetcxx . . $ StringList_to_StringSet(7909a6d728cc3eb9). (StringListcxx . . $ StringList# me) {
118 try {
119 autoStringSetcxx . . $ autoStringSet# you = StringSet_createcxx . . $ StringSet_create(3c0f0334e2cf88fa). ();
120 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; i ++) {
121 autoSimpleStringcxx . . $ autoSimpleString# item = SimpleString_createcxx . . $ SimpleString_create(d4d4ea089a1063c6). (mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i] -> stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).());
122 yourcxx . . $ `melder/melder.h:46:9`! addItem_unsorted_movecxx . . $ SortedOf#addItem_unsorted_move(7a1ccebf8c9794a2). (itemcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).());
123 }
124 yourcxx . . $ `melder/melder.h:46:9`! sortcxx . . $ SortedOf#sort(49f6e7a06ebc5aa8). ();
125 yourcxx . . $ `melder/melder.h:46:9`! unicizecxx . . $ SortedSetOf#unicize(49f6e7a06ebc5aa8). ();
126 return youcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
127 } catch (MelderErrorcxx . . $ MelderError#) {
128 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to StringSet.");
129 }
130 }
131
132 integercxx . . $ integer# OrderedOfString_getNumberOfDifferencescxx . . $ OrderedOfString_getNumberOfDifferences(31bf98e6d26bf24a). (StringListcxx . . $ StringList# me, StringListcxx . . $ StringList# thee) {
133 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! size == thycxx . . $ `melder/melder.h:45:9`! size,
134 U"The two lists should have equal sizes.");
135 integercxx . . $ integer# numberOfDifferences = 0;
136 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; i ++) {
137 const SimpleStringcxx . . $ SimpleString# mystring = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i];
138 const SimpleStringcxx . . $ SimpleString# thystring = thycxx . . $ `melder/melder.h:45:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i];
139 if (Melder_cmpcxx . . $ Melder_cmp(937dacc22d2a7919). (mystring->stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), thystring->stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()) != 0)
140 numberOfDifferences ++;
141 }
142 return numberOfDifferences;
143 }
144
145 double OrderedOfString_getFractionDifferentcxx . . $ OrderedOfString_getFractionDifferent(16d90c4069fbd4a9). (StringListcxx . . $ StringList# me, StringListcxx . . $ StringList# thee) {
146 integercxx . . $ integer# numberOfDifferences = OrderedOfString_getNumberOfDifferencescxx . . $ OrderedOfString_getNumberOfDifferences(31bf98e6d26bf24a). (me, thee);
147 return mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size. == 0 ? 0.0 : (double) numberOfDifferences / mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.;
148 }
149
150 integercxx . . $ integer# OrderedOfString_indexOfItemcxx . . $ OrderedOfString_indexOfItem(928234153fbd418c). (StringListcxx . . $ StringList# me, conststring32cxx . . $ conststring32# str) {
151 integercxx . . $ integer# index = 0;
152 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; i ++) {
153 SimpleStringcxx . . $ SimpleString# mystring = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i];
154 if (Melder_cmpcxx . . $ Melder_cmp(937dacc22d2a7919). (mystring->stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), str) == 0) {
155 index = i;
156 break;
157 }
158 }
159 return index;
160 }
161
162 void OrderedOfString_initWithSequentialNumberscxx . . $ OrderedOfString_initWithSequentialNumbers(58a263e574586e4d). (StringListcxx . . $ StringList# me, integercxx . . $ integer# n) {
163 for (integercxx . . $ integer# i = 1; i <= n; i ++)
164 mycxx . . $ `melder/melder.h:44:9`! addItem_movecxx . . $ CollectionOf#addItem_move(bcc0cff787be24fe). (SimpleString_createcxx . . $ SimpleString_create(d4d4ea089a1063c6). (Melder_integercxx . . $ Melder_integer(7b348ebc2004441d). (i)));
165 }
166
167 void OrderedOfString_changeStringscxx . . $ OrderedOfString_changeStrings(c5178fa59d9873d3). (StringListcxx . . $ StringList# me, char32cxx . . $ char32# *search, char32cxx . . $ char32# *replace, integercxx . . $ integer# maximumNumberOfReplaces, integercxx . . $ integer# *out_numberOfMatches, integercxx . . $ integer# *out_numberOfStringMatches, bool use_regexp) {
168 regexpcxx . . $ regexp# *compiled_search = nullptr;
169 try {
170 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (search,
171 U"The search string should not be empty.");
172 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (replace,
173 U"The replace string should not be empty.");
174
175 if (use_regexp)
176 compiled_search = CompileRE_throwablecxx . . $ CompileRE_throwable(883f0876ef3038d2). (search, 0);
177 integercxx . . $ integer# numberOfMatches = 0, numberOfStringMatches = 0;
178 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; i ++) {
179 SimpleStringcxx . . $ SimpleString# ss = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i];
180 integercxx . . $ integer# nmatches_sub;
181 autostring32cxx . . $ autostring32# r = use_regexp ?
182 replace_regex_STRcxx . . $ replace_regex_STR(8c698ee9a707cbfd). (ss -> stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), compiled_search, replace, maximumNumberOfReplaces, & nmatches_sub) :
183 replace_STRcxx . . $ replace_STR(58ee6b7335d69803). (ss -> stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), search, replace, maximumNumberOfReplaces, & nmatches_sub);
184 /*
185 Change without error.
186 */
187 ss -> stringcxx . . $ structSimpleString#string. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). r.movecxx . . $ _autostring#move(bd43c759570f178f).();
188
189 if (nmatches_sub > 0) {
190 numberOfMatches += nmatches_sub;
191 numberOfStringMatches ++;
192 }
193 }
194 if (use_regexp)
195 freecxx . . $ free(bd21765a0afc8e3c). (compiled_search);
196 if (out_numberOfMatches)
197 *out_numberOfMatches= numberOfMatches;
198 if (out_numberOfStringMatches)
199 *out_numberOfStringMatches= numberOfStringMatches;
200 } catch (MelderErrorcxx . . $ MelderError#) {
201 if (use_regexp)
202 freecxx . . $ free(bd21765a0afc8e3c). (compiled_search);
203 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Replace not completed.");
204 }
205 }
206
207 bool OrderedOfString_containSameElementscxx . . $ OrderedOfString_containSameElements(b2354a120c35201e). (StringListcxx . . $ StringList# me, StringListcxx . . $ StringList# thee) {
208 autoStringSetcxx . . $ autoStringSet# myset = StringList_to_StringSetcxx . . $ StringList_to_StringSet(7909a6d728cc3eb9). (me);
209 autoStringSetcxx . . $ autoStringSet# thyset = StringList_to_StringSetcxx . . $ StringList_to_StringSet(7909a6d728cc3eb9). (thee);
210 return Data_equalcxx . . $ Data_equal(6df46d860ee4054f). (myset.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thyset.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
211 }
212
213 void OrderedOfString_removeOccurrencescxx . . $ OrderedOfString_removeOccurrences(ebb70887ac3e0615). (StringListcxx . . $ StringList# me, conststring32cxx . . $ conststring32# search, bool use_regexp) {
214 if (! search)
215 return;
216 for (integercxx . . $ integer# i = mycxx . . $ `melder/melder.h:44:9`! sizecxx . . $ CollectionOf#size.; i >= 1; i --) {
217 const SimpleStringcxx . . $ SimpleString# ss = mycxx . . $ `melder/melder.h:44:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i];
218 if ((use_regexp && strstr_regexpcxx . . $ strstr_regexp(1231f2366db14449). (ss -> stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), search)) ||
219 (! use_regexp && str32strcxx . . $ str32str(2de17cd8f7c584a6). (ss -> stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), search))) {
220 mycxx . . $ `melder/melder.h:44:9`! removeItemcxx . . $ CollectionOf#removeItem(1624ee8275b10bda). (i);
221 }
222 }
223 }
224
225 /* End of file Collection_extensions.cpp */