nfrlist.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. * Copyright (C) 1997-2012, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. ******************************************************************************
  8. * file name: nfrlist.h
  9. * encoding: UTF-8
  10. * tab size: 8 (not used)
  11. * indentation:4
  12. *
  13. * Modification history
  14. * Date Name Comments
  15. * 10/11/2001 Doug Ported from ICU4J
  16. */
  17. #ifndef NFRLIST_H
  18. #define NFRLIST_H
  19. #include "unicode/rbnf.h"
  20. #if U_HAVE_RBNF
  21. #include "unicode/uobject.h"
  22. #include "nfrule.h"
  23. #include "cmemory.h"
  24. U_NAMESPACE_BEGIN
  25. // unsafe class for internal use only. assume memory allocations succeed, indexes are valid.
  26. // should be a template, but we can't use them
  27. class NFRuleList : public UMemory {
  28. protected:
  29. NFRule** fStuff;
  30. uint32_t fCount;
  31. uint32_t fCapacity;
  32. public:
  33. NFRuleList(uint32_t capacity = 10)
  34. : fStuff(capacity ? (NFRule**)uprv_malloc(capacity * sizeof(NFRule*)) : nullptr)
  35. , fCount(0)
  36. , fCapacity(capacity) {}
  37. ~NFRuleList() {
  38. if (fStuff) {
  39. for(uint32_t i = 0; i < fCount; ++i) {
  40. delete fStuff[i];
  41. }
  42. uprv_free(fStuff);
  43. }
  44. }
  45. NFRule* operator[](uint32_t index) const { return fStuff != nullptr ? fStuff[index] : nullptr; }
  46. NFRule* remove(uint32_t index) {
  47. if (fStuff == nullptr) {
  48. return nullptr;
  49. }
  50. NFRule* result = fStuff[index];
  51. fCount -= 1;
  52. for (uint32_t i = index; i < fCount; ++i) { // assumes small arrays
  53. fStuff[i] = fStuff[i+1];
  54. }
  55. return result;
  56. }
  57. void add(NFRule* thing) {
  58. if (fCount == fCapacity) {
  59. fCapacity += 10;
  60. fStuff = (NFRule**)uprv_realloc(fStuff, fCapacity * sizeof(NFRule*)); // assume success
  61. }
  62. if (fStuff != nullptr) {
  63. fStuff[fCount++] = thing;
  64. } else {
  65. fCapacity = 0;
  66. fCount = 0;
  67. }
  68. }
  69. uint32_t size() const { return fCount; }
  70. NFRule* last() const { return (fCount > 0 && fStuff != nullptr) ? fStuff[fCount-1] : nullptr; }
  71. NFRule** release() {
  72. add(nullptr); // ensure null termination
  73. NFRule** result = fStuff;
  74. fStuff = nullptr;
  75. fCount = 0;
  76. fCapacity = 0;
  77. return result;
  78. }
  79. void deleteAll() {
  80. NFRule** tmp = nullptr;
  81. int32_t size = fCount;
  82. if (size > 0) {
  83. tmp = release();
  84. for (int32_t i = 0; i < size; i++) {
  85. delete tmp[i];
  86. }
  87. if (tmp) {
  88. uprv_free(tmp);
  89. }
  90. }
  91. }
  92. private:
  93. NFRuleList(const NFRuleList &other); // forbid copying of this class
  94. NFRuleList &operator=(const NFRuleList &other); // forbid copying of this class
  95. };
  96. U_NAMESPACE_END
  97. /* U_HAVE_RBNF */
  98. #endif
  99. // NFRLIST_H
  100. #endif