| 1 | /* Categories.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2018 David Weenink, 2015,2017,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 20020315 GPL header |
| 21 | djmw 20110304 Thing_new |
| 22 | */ |
| 23 | |
| 24 | #include "Categories.h" |
| 25 | |
| 26 | void structCategories :: v1_info () { |
| 27 | structDaata :: v1_info (); |
| 28 | MelderInfo_writeLine (U"Number of strings: ", our size); |
| 29 | autoStringSet set = StringList_to_StringSet (this); |
| 30 | MelderInfo_writeLine (U"Number of unique categories: ", set->size); |
| 31 | } |
| 32 | |
| 33 | void structCategories :: v1_readText (MelderReadText a_text, int /*formatVersion*/) { |
| 34 | integer l_size = texgeti32 (a_text); |
| 35 | if (l_size == 0) |
| 36 | (void) 0; |
| 37 | else if (l_size < 0) |
| 38 | Melder_throw (U"Size cannot be negative."); |
| 39 | else |
| 40 | our _grow (l_size); |
| 41 | |
| 42 | for (integer i = 1; i <= l_size; i ++) { |
| 43 | autoSimpleString itemi = Thing_new (SimpleString); |
| 44 | itemi -> v1_readText (a_text, 0); |
| 45 | our addItemAtPosition_move (itemi.move(), i); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void structCategories :: v1_writeText (MelderFile file) { |
| 50 | texputi32 (file, our size, U"size"); |
| 51 | for (integer i = 1; i <= our size; i ++) { |
| 52 | SimpleString data = our at [i]; |
| 53 | texputintro (file, U"item [", Melder_integer (i), U"]:"); |
| 54 | data -> structSimpleString :: v1_writeText (file); |
| 55 | texexdent (file); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | Thing_implement (Categories, StringList, 0); |
| 60 | |
| 61 | autoCategories Categories_create () { |
| 62 | try { |
| 63 | autoCategories me = Thing_new (Categories); |
| 64 | return me; |
| 65 | } catch (MelderError) { |
| 66 | Melder_throw (U"Categories not created."); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | autoCategories Categories_createWithSequentialNumbers (integer n) { |
| 71 | try { |
| 72 | autoCategories me = Thing_new (Categories); |
| 73 | OrderedOfString_initWithSequentialNumbers (me.get(), n); |
| 74 | return me; |
| 75 | } catch (MelderError) { |
| 76 | Melder_throw (U"Sequential number Categories not created."); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | autoCategories Categories_selectUniqueItems (Categories me) { |
| 81 | try { |
| 82 | autoStringSet set = StringList_to_StringSet (me); |
| 83 | autoCategories thee = Categories_create (); |
| 84 | for (integer i = 1; i <= set->size; i ++) { |
| 85 | autoSimpleString item = Data_copy (set->at [i]); |
| 86 | thy addItem_move (item.move()); |
| 87 | } |
| 88 | return thee; |
| 89 | } catch (MelderError) { |
| 90 | Melder_throw (me, U": no unique categories created."); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void Categories_drawItem (Categories me, Graphics g, integer position, double xWC, double yWC) { |
| 95 | if (position < 1 || position > my size) |
| 96 | return; |
| 97 | SimpleString item = my at [position]; |
| 98 | Graphics_text (g, xWC, yWC, item -> string.get()); |
| 99 | } |
| 100 | |
| 101 | /* TableOfReal_Rowlabels_to_Categories ??? */ |
| 102 | autoCategories TableOfReal_to_CategoriesRow (TableOfReal me) { |
| 103 | try { |
| 104 | autoCategories thee = Categories_create (); |
| 105 | for (integer i = 1; i <= my numberOfRows; i ++) { |
| 106 | if (my rowLabels [i]) { |
| 107 | autoSimpleString s = SimpleString_create (my rowLabels [i].get()); |
| 108 | thy addItem_move (s.move()); |
| 109 | } |
| 110 | } |
| 111 | return thee; |
| 112 | } catch (MelderError) { |
| 113 | Melder_throw (me, U": row labels not converted to Categories."); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | autoCategories TableOfReal_to_CategoriesColumn (TableOfReal me) { |
| 118 | try { |
| 119 | autoCategories thee = Categories_create (); |
| 120 | for (integer i = 1; i <= my numberOfColumns; i ++) { |
| 121 | if (my columnLabels [i]) { |
| 122 | autoSimpleString s = SimpleString_create (my columnLabels [i].get()); |
| 123 | thy addItem_move (s.move()); |
| 124 | } |
| 125 | } |
| 126 | return thee; |
| 127 | } catch (MelderError) { |
| 128 | Melder_throw (me, U": column labels not converted to Categories."); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* End of file Categories.cpp */ |