Categories.cpp 3.8 KB

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