| 1 | /* Index.cpp |
| 2 | * |
| 3 | * Copyright (C) 2005-2022 David Weenink, 2011,2012,2015-2018,2020,2026 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 3 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 | #include <time.h> |
| 20 | #include "Index.h" |
| 21 | #include "NUM2.h" |
| 22 | #include "Permutation.h" |
| 23 | |
| 24 | #include "oo_DESTROY.h" |
| 25 | #include "Index_def.h" |
| 26 | #include "oo_COPY.h" |
| 27 | #include "Index_def.h" |
| 28 | #include "oo_EQUAL.h" |
| 29 | #include "Index_def.h" |
| 30 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 31 | #include "Index_def.h" |
| 32 | #include "oo_WRITE_TEXT.h" |
| 33 | #include "Index_def.h" |
| 34 | #include "oo_WRITE_BINARY.h" |
| 35 | #include "Index_def.h" |
| 36 | #include "oo_READ_TEXT.h" |
| 37 | #include "Index_def.h" |
| 38 | #include "oo_READ_BINARY.h" |
| 39 | #include "Index_def.h" |
| 40 | #include "oo_DESCRIPTION.h" |
| 41 | #include "Index_def.h" |
| 42 | |
| 43 | static const conststring32 undefinedClassLabel = U""; |
| 44 | Thing_implement (Index, Daata, 0); |
| 45 | |
| 46 | void structIndex :: v1_info () { |
| 47 | structDaata :: v1_info (); |
| 48 | MelderInfo_writeLine (U"Number of items: ", our numberOfItems); |
| 49 | } |
| 50 | |
| 51 | void Index_init (Index me, integer numberOfItems) { |
| 52 | Melder_require (numberOfItems > 0, |
| 53 | U"The index should not be empty."); |
| 54 | my classes = Ordered_create (); |
| 55 | my numberOfItems = numberOfItems; |
| 56 | my classIndex = zero_INTVEC (numberOfItems); |
| 57 | } |
| 58 | |
| 59 | autoIndex Index_extractPart (Index me, integer from, integer to) { |
| 60 | try { |
| 61 | if (from == 0) |
| 62 | from = 1; |
| 63 | if (to == 0) |
| 64 | to = my numberOfItems; |
| 65 | Melder_require (from <= to && from > 0 && to <= my numberOfItems, |
| 66 | U"Range should be in interval [1,", my numberOfItems, U"]."); |
| 67 | |
| 68 | autoIndex thee = Data_copy (me); |
| 69 | thy numberOfItems = to - from + 1; |
| 70 | |
| 71 | for (integer i = 1; i <= thy numberOfItems; i ++) |
| 72 | thy classIndex [i] = my classIndex [from + i - 1]; |
| 73 | return thee; |
| 74 | } catch (MelderError) { |
| 75 | Melder_throw (me, U": part not extracted."); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | Thing_implement (StringsIndex, Index, 0); |
| 80 | |
| 81 | autoStringsIndex StringsIndex_create (integer numberOfItems) { |
| 82 | try { |
| 83 | autoStringsIndex me = Thing_new (StringsIndex); |
| 84 | Index_init (me.get(), numberOfItems); |
| 85 | return me; |
| 86 | } catch (MelderError) { |
| 87 | Melder_throw (U"StringsIndex not created."); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static void StringsIndex_addClass (StringsIndex me, conststring32 classi) { |
| 92 | autoSimpleString ss = SimpleString_create (classi); |
| 93 | my classes -> addItem_move (ss.move()); |
| 94 | } |
| 95 | |
| 96 | autoStringsIndex StringsIndex_createFromSTRVEC (constSTRVEC const& strvec, kStrings_sorting sorting) { |
| 97 | try { |
| 98 | autoStringsIndex me = StringsIndex_create (strvec.size); |
| 99 | autoPermutation p = Permutation_create (strvec.size, true); |
| 100 | if (sorting == kStrings_sorting::ALPHABETICAL) |
| 101 | INTVECindex_inout (p -> p.get(), strvec); |
| 102 | else if (sorting == kStrings_sorting::NUMBER_AWARE) |
| 103 | INTVECindex_numberAware_inout (p -> p.get(), strvec); |
| 104 | integer iclass = 1; |
| 105 | integer index = p -> p [1]; |
| 106 | conststring32 classi = strvec [index]; |
| 107 | StringsIndex_addClass (me.get(), strvec [index]); |
| 108 | my classIndex [index] = iclass; |
| 109 | for (integer i = 2; i <= strvec.size; i ++) { |
| 110 | index = p -> p [i]; |
| 111 | if (Melder_cmp (classi, strvec [index]) != 0) { |
| 112 | StringsIndex_addClass (me.get(), strvec [index]); |
| 113 | classi = strvec [index]; |
| 114 | iclass ++; |
| 115 | } |
| 116 | my classIndex [index] = iclass; |
| 117 | } |
| 118 | return me; |
| 119 | } catch (MelderError) { |
| 120 | Melder_throw (U"Could not create StringsIndex from STRVEC."); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | autoStringsIndex Strings_to_StringsIndex (Strings me, kStrings_sorting sorting) { |
| 125 | try { |
| 126 | autoStringsIndex thee = StringsIndex_createFromSTRVEC (my strings.get(), sorting); |
| 127 | return thee; |
| 128 | } catch (MelderError) { |
| 129 | Melder_throw (me, U": no StringsIndex created."); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | integer Index_getClassIndexFromItemIndex (Index me, integer itemIndex) { |
| 134 | integer result = 0; |
| 135 | if (itemIndex >= 0 && itemIndex <= my numberOfItems) |
| 136 | result = my classIndex [itemIndex]; |
| 137 | return result; |
| 138 | } |
| 139 | |
| 140 | int StringsIndex_getClassIndexFromClassLabel (StringsIndex me, conststring32 klasLabel) { |
| 141 | for (integer i = 1; i <= my classes->size; i ++) { |
| 142 | const SimpleString ss = (SimpleString) my classes->at [i]; // FIXME cast |
| 143 | if (Melder_equ (ss -> string.get(), klasLabel)) |
| 144 | return i; |
| 145 | } |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | conststring32 StringsIndex_getClassLabelFromClassIndex (StringsIndex me, integer klasIndex) { |
| 150 | conststring32 result = undefinedClassLabel; |
| 151 | if (klasIndex > 0 && klasIndex <= my classes->size) { |
| 152 | const SimpleString ss = (SimpleString) my classes->at [klasIndex]; // FIXME cast |
| 153 | result = ss -> string.get(); |
| 154 | } |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | conststring32 StringsIndex_getItemLabelFromItemIndex (StringsIndex me, integer itemNumber) { |
| 159 | conststring32 result = undefinedClassLabel; |
| 160 | if (itemNumber > 0 && itemNumber <= my numberOfItems) { |
| 161 | const integer klas = my classIndex [itemNumber]; |
| 162 | const SimpleString ss = (SimpleString) my classes->at [klas]; // FIXME cast |
| 163 | result = ss -> string.get(); |
| 164 | } |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | integer StringsIndex_countItems (StringsIndex me, integer iclass) { |
| 169 | integer sum = 0; |
| 170 | for (integer i = 1; i <= my numberOfItems; i ++) { |
| 171 | if (my classIndex [i] == iclass) |
| 172 | sum ++; |
| 173 | } |
| 174 | return sum; |
| 175 | } |
| 176 | |
| 177 | autoSTRVEC StringsIndex_listAllClasses ( StringsIndex me) { |
| 178 | try { |
| 179 | autoSTRVEC classes (my classes->size); |
| 180 | for (integer i = 1; i <= my classes->size; i ++) { |
| 181 | const SimpleString ss = (SimpleString) my classes->at [i]; |
| 182 | classes [i] = Melder_dup (ss -> string.get()); |
| 183 | } |
| 184 | return classes; |
| 185 | } catch (MelderError) { |
| 186 | Melder_throw (me, U": could not list all classes."); |
| 187 | } |
| 188 | } |
| 189 | void StringsIndex_sortNumerically (StringsIndex me) { |
| 190 | try { |
| 191 | const integer numberOfClasses = my classes->size; |
| 192 | autoSTRVEC numberstrings (numberOfClasses); |
| 193 | autoVEC numbers = raw_VEC (numberOfClasses); |
| 194 | for (integer i = 1; i <= numberOfClasses; i ++) { |
| 195 | const SimpleString ss = (SimpleString) my classes->at [i]; |
| 196 | numberstrings [i] = Melder_dup (ss -> string.get()); |
| 197 | numbers [i] = Melder_atof (ss -> string.get()); |
| 198 | } |
| 199 | autoPermutation p = Permutation_create (numberOfClasses, true); |
| 200 | NUMsortTogether (numbers.get(), p -> p.get()); |
| 201 | for (integer i = 1; i <= numberOfClasses; i ++) { |
| 202 | autoSimpleString ss = SimpleString_create (numberstrings [i].get()); |
| 203 | my classes-> replaceItem_move (ss.move(), p -> p [i]); |
| 204 | } |
| 205 | for (integer item = 1; item <= classIndex->size; item ++) |
| 206 | my classIndex [item] = p -> p [my classIndex [item]]; |
| 207 | } catch (MelderError) { |
| 208 | Melder_throw (me, U": could not be sorted numerically."); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | |
| 214 | /* End of Index.cpp */ |