| 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" |
| 29 |
#include "NUM2.h" |
| 30 |
|
| 31 |
void Collection_Permutation_permuteItems_inplace (Collection me, Permutation him) { |
| 32 |
Melder_require (my size == his numberOfElements, |
| 33 |
U"The number of elements must be equal."); |
| 34 |
try { |
| 35 |
autoCollection thee = Collection_create (); |
| 36 |
thy classInfo = my classInfo; |
| 37 |
thy _initializeOwnership (false); |
| 38 |
thy _grow (my size); |
| 39 |
for (integer pos = 1; pos <= my size; pos ++) { |
| 40 |
const integer oldPos = Permutation_getValueAtIndex (him, pos); |
| 41 |
thy at [pos] = my at [oldPos]; |
| 42 |
} |
| 43 |
for (integer pos = 1; pos <= my size; pos ++) { |
| 44 |
my at [pos] = thy at [pos]; |
| 45 |
} |
| 46 |
} catch (MelderError) { |
| 47 |
Melder_throw (me, U": not permuted in-place."); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
autoCollection Collection_Permutation_permuteItems (Collection me, Permutation him) { |
| 52 |
try { |
| 53 |
Melder_require (my size == his numberOfElements, |
| 54 |
U"The number of elements must be equal."); |
| 55 |
|
| 56 |
autoCollection thee = Data_copy (me); |
| 57 |
Collection_Permutation_permuteItems_inplace (thee.get(), him); |
| 58 |
return thee; |
| 59 |
} catch (MelderError) { |
| 60 |
Melder_throw (me, U": not permuted."); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
void Collection_permuteItems_inplace (Collection me) { |
| 65 |
try { |
| 66 |
autoPermutation p = Permutation_create (my size, false); |
| 67 |
Collection_Permutation_permuteItems_inplace (me, p.get()); |
| 68 |
} catch (MelderError) { |
| 69 |
Melder_throw (me, U": items not permuted inplace."); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
autoCollection Collection_permuteItems (Collection me) { |
| 74 |
try { |
| 75 |
autoPermutation p = Permutation_create (my size, false); |
| 76 |
autoCollection thee = Collection_Permutation_permuteItems (me, p.get()); |
| 77 |
return thee; |
| 78 |
} catch (MelderError) { |
| 79 |
Melder_throw (me, U": items not permuted."); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/****************** class OrderedOfString ******************/ |
| 84 |
|
| 85 |
int OrderedOfString_append (StringList me, conststring32 append) { |
| 86 |
try { |
| 87 |
if (! append) |
| 88 |
return 1; // BUG: lege string appenden?? |
| 89 |
autoSimpleString item = SimpleString_create (append); |
| 90 |
my addItem_move (item.move()); |
| 91 |
return 1; |
| 92 |
} catch (MelderError) { |
| 93 |
Melder_throw (me, U": text not appended."); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
autoStringList OrderedOfString_joinItems (StringList me, StringList thee) { |
| 98 |
try { |
| 99 |
Melder_require (my size == thy size, |
| 100 |
U"sizes should be equal."); |
| 101 |
|
| 102 |
autoStringList him = Data_copy (me); // FIXME: this copies *all* the data from me, and only the strings from thee |
| 103 |
|
| 104 |
for (integer i = 1; i <= my size; i ++) { |
| 105 |
const SimpleString hisCategory = his at [i], thyCategory = thy at [i]; |
| 106 |
const integer hisLength = Melder_length (hisCategory -> string.get()); |
| 107 |
const integer thyLength = Melder_length (thyCategory -> string.get()); |
| 108 |
hisCategory -> string. resize (hisLength + thyLength); |
| 109 |
str32cpy (& hisCategory -> string [hisLength], thyCategory -> string.get()); |
| 110 |
} |
| 111 |
return him; |
| 112 |
} catch (MelderError) { |
| 113 |
Melder_throw (U"Items not joined."); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
autoStringSet StringList_to_StringSet (StringList me) { |
| 118 |
try { |
| 119 |
autoStringSet you = StringSet_create (); |
| 120 |
for (integer i = 1; i <= my size; i ++) { |
| 121 |
autoSimpleString item = SimpleString_create (my at [i] -> string.get()); |
| 122 |
your addItem_unsorted_move (item.move()); |
| 123 |
} |
| 124 |
your sort (); |
| 125 |
your unicize (); |
| 126 |
return you; |
| 127 |
} catch (MelderError) { |
| 128 |
Melder_throw (me, U": not converted to StringSet."); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
integer OrderedOfString_getNumberOfDifferences (StringList me, StringList thee) { |
| 133 |
Melder_require (my size == thy size, |
| 134 |
U"The two lists should have equal sizes."); |
| 135 |
integer numberOfDifferences = 0; |
| 136 |
for (integer i = 1; i <= my size; i ++) { |
| 137 |
const SimpleString mystring = my at [i]; |
| 138 |
const SimpleString thystring = thy at [i]; |
| 139 |
if (Melder_cmp (mystring->string.get(), thystring->string.get()) != 0) |
| 140 |
numberOfDifferences ++; |
| 141 |
} |
| 142 |
return numberOfDifferences; |
| 143 |
} |
| 144 |
|
| 145 |
double OrderedOfString_getFractionDifferent (StringList me, StringList thee) { |
| 146 |
integer numberOfDifferences = OrderedOfString_getNumberOfDifferences (me, thee); |
| 147 |
return my size == 0 ? 0.0 : (double) numberOfDifferences / my size; |
| 148 |
} |
| 149 |
|
| 150 |
integer OrderedOfString_indexOfItem (StringList me, conststring32 str) { |
| 151 |
integer index = 0; |
| 152 |
for (integer i = 1; i <= my size; i ++) { |
| 153 |
SimpleString mystring = my at [i]; |
| 154 |
if (Melder_cmp (mystring->string.get(), str) == 0) { |
| 155 |
index = i; |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
return index; |
| 160 |
} |
| 161 |
|
| 162 |
void OrderedOfString_initWithSequentialNumbers (StringList me, integer n) { |
| 163 |
for (integer i = 1; i <= n; i ++) |
| 164 |
my addItem_move (SimpleString_create (Melder_integer (i))); |
| 165 |
} |
| 166 |
|
| 167 |
void OrderedOfString_changeStrings (StringList me, char32 *search, char32 *replace, integer maximumNumberOfReplaces, integer *out_numberOfMatches, integer *out_numberOfStringMatches, bool use_regexp) { |
| 168 |
regexp *compiled_search = nullptr; |
| 169 |
try { |
| 170 |
Melder_require (search, |
| 171 |
U"The search string should not be empty."); |
| 172 |
Melder_require (replace, |
| 173 |
U"The replace string should not be empty."); |
| 174 |
|
| 175 |
if (use_regexp) |
| 176 |
compiled_search = CompileRE_throwable (search, 0); |
| 177 |
integer numberOfMatches = 0, numberOfStringMatches = 0; |
| 178 |
for (integer i = 1; i <= my size; i ++) { |
| 179 |
SimpleString ss = my at [i]; |
| 180 |
integer nmatches_sub; |
| 181 |
autostring32 r = use_regexp ? |
| 182 |
replace_regex_STR (ss -> string.get(), compiled_search, replace, maximumNumberOfReplaces, & nmatches_sub) : |
| 183 |
replace_STR (ss -> string.get(), search, replace, maximumNumberOfReplaces, & nmatches_sub); |
| 184 |
/* |
| 185 |
Change without error. |
| 186 |
*/ |
| 187 |
ss -> string = r.move(); |
| 188 |
|
| 189 |
if (nmatches_sub > 0) { |
| 190 |
numberOfMatches += nmatches_sub; |
| 191 |
numberOfStringMatches ++; |
| 192 |
} |
| 193 |
} |
| 194 |
if (use_regexp) |
| 195 |
free (compiled_search); |
| 196 |
if (out_numberOfMatches) |
| 197 |
*out_numberOfMatches= numberOfMatches; |
| 198 |
if (out_numberOfStringMatches) |
| 199 |
*out_numberOfStringMatches= numberOfStringMatches; |
| 200 |
} catch (MelderError) { |
| 201 |
if (use_regexp) |
| 202 |
free (compiled_search); |
| 203 |
Melder_throw (U"Replace not completed."); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
bool OrderedOfString_containSameElements (StringList me, StringList thee) { |
| 208 |
autoStringSet myset = StringList_to_StringSet (me); |
| 209 |
autoStringSet thyset = StringList_to_StringSet (thee); |
| 210 |
return Data_equal (myset.get(), thyset.get()); |
| 211 |
} |
| 212 |
|
| 213 |
void OrderedOfString_removeOccurrences (StringList me, conststring32 search, bool use_regexp) { |
| 214 |
if (! search) |
| 215 |
return; |
| 216 |
for (integer i = my size; i >= 1; i --) { |
| 217 |
const SimpleString ss = my at [i]; |
| 218 |
if ((use_regexp && strstr_regexp (ss -> string.get(), search)) || |
| 219 |
(! use_regexp && str32str (ss -> string.get(), search))) { |
| 220 |
my removeItem (i); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/* End of file Collection_extensions.cpp */ |