Index.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Index.cpp
  2. *
  3. * Copyright (C) 2005-2011, 2015-2018 David Weenink
  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 20050724
  20. djmw 20061212 Changed info to Melder_writeLine<x> format.
  21. djmw 20070102
  22. djmw 20071012 Added: o_CAN_WRITE_AS_ENCODING.h
  23. djmw 20110304 Thing_new
  24. */
  25. #include <time.h>
  26. #include "Index.h"
  27. #include "NUM2.h"
  28. #include "oo_DESTROY.h"
  29. #include "Index_def.h"
  30. #include "oo_COPY.h"
  31. #include "Index_def.h"
  32. #include "oo_EQUAL.h"
  33. #include "Index_def.h"
  34. #include "oo_CAN_WRITE_AS_ENCODING.h"
  35. #include "Index_def.h"
  36. #include "oo_WRITE_TEXT.h"
  37. #include "Index_def.h"
  38. #include "oo_WRITE_BINARY.h"
  39. #include "Index_def.h"
  40. #include "oo_READ_TEXT.h"
  41. #include "Index_def.h"
  42. #include "oo_READ_BINARY.h"
  43. #include "Index_def.h"
  44. #include "oo_DESCRIPTION.h"
  45. #include "Index_def.h"
  46. static const conststring32 undefinedClassLabel = U"";
  47. Thing_implement (Index, Daata, 0);
  48. void structIndex :: v_info () {
  49. structDaata :: v_info ();
  50. MelderInfo_writeLine (U"Number of items: ", our numberOfItems);
  51. }
  52. void Index_init (Index me, integer numberOfItems) {
  53. Melder_require (numberOfItems > 0, U"The index should not be empty.");
  54. my classes = Ordered_create ();
  55. my numberOfItems = numberOfItems;
  56. my classIndex = INTVECzero (numberOfItems);
  57. }
  58. autoIndex Index_extractPart (Index me, integer from, integer to) {
  59. try {
  60. if (from == 0) {
  61. from = 1;
  62. }
  63. if (to == 0) {
  64. to = my numberOfItems;
  65. }
  66. Melder_require (from <= to && from > 0 && to <= my numberOfItems, U"Range should be in interval [1,", my numberOfItems, U"].");
  67. autoIndex thee = Data_copy (me);
  68. thy numberOfItems = to - from + 1;
  69. for (integer i = 1; i <= thy numberOfItems; i ++) {
  70. thy classIndex [i] = my classIndex [from + i - 1];
  71. }
  72. return thee;
  73. } catch (MelderError) {
  74. Melder_throw (me, U": part not extracted.");
  75. }
  76. }
  77. Thing_implement (StringsIndex, Index, 0);
  78. autoStringsIndex StringsIndex_create (integer numberOfItems) {
  79. try {
  80. autoStringsIndex me = Thing_new (StringsIndex);
  81. Index_init (me.get(), numberOfItems);
  82. return me;
  83. } catch (MelderError) {
  84. Melder_throw (U"StringsIndex not created.");
  85. }
  86. }
  87. integer Index_getClassIndexFromItemIndex (Index me, integer itemIndex) {
  88. integer result = 0;
  89. if (itemIndex >= 0 && itemIndex <= my numberOfItems) {
  90. result = my classIndex [itemIndex];
  91. }
  92. return result;
  93. }
  94. int StringsIndex_getClassIndexFromClassLabel (StringsIndex me, conststring32 klasLabel) {
  95. for (integer i = 1; i <= my classes->size; i ++) {
  96. SimpleString ss = (SimpleString) my classes->at [i]; // FIXME cast
  97. if (Melder_equ (ss -> string.get(), klasLabel)) {
  98. return i;
  99. }
  100. }
  101. return 0;
  102. }
  103. conststring32 StringsIndex_getClassLabelFromClassIndex (StringsIndex me, integer klasIndex) {
  104. conststring32 result = undefinedClassLabel;
  105. if (klasIndex > 0 && klasIndex <= my classes -> size) {
  106. SimpleString ss = (SimpleString) my classes->at [klasIndex]; // FIXME cast
  107. result = ss -> string.get();
  108. }
  109. return result;
  110. }
  111. conststring32 StringsIndex_getItemLabelFromItemIndex (StringsIndex me, integer itemNumber) {
  112. conststring32 result = undefinedClassLabel;
  113. if (itemNumber > 0 && itemNumber <= my numberOfItems) {
  114. integer klas = my classIndex [itemNumber];
  115. SimpleString ss = (SimpleString) my classes->at [klas]; // FIXME cast
  116. result = ss -> string.get();
  117. }
  118. return result;
  119. }
  120. integer StringsIndex_countItems (StringsIndex me, int iclass) {
  121. integer sum = 0;
  122. for (integer i = 1; i <= my numberOfItems; i ++) {
  123. if (my classIndex [i] == iclass) {
  124. sum ++;
  125. }
  126. }
  127. return sum;
  128. }
  129. /* End of Index.cpp */