| 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 | |
| 18 | #ifndef NFRLIST_H | 
| 19 | #define NFRLIST_H | 
| 20 | |
| 21 | #include "unicode/rbnf.h" | 
| 22 | |
| 23 | #if U_HAVE_RBNF | 
| 24 | |
| 25 | #include "unicode/uobject.h" | 
| 26 | #include "nfrule.h" | 
| 27 | |
| 28 | #include "cmemory.h" | 
| 29 | |
| 30 | U_NAMESPACE_BEGIN | 
| 31 | |
| 32 | // unsafe class for internal use only. assume memory allocations succeed, indexes are valid. | 
| 33 | // should be a template, but we can't use them | 
| 34 | |
| 35 | class NFRuleList : public UMemory { | 
| 36 | protected: | 
| 37 | NFRule** fStuff; | 
| 38 | uint32_t fCount; | 
| 39 | uint32_t fCapacity; | 
| 40 | public: | 
| 41 | NFRuleList(uint32_t capacity = 10) | 
| 42 | : fStuff(capacity ? (NFRule**)uprv_malloc(capacity * sizeof(NFRule*)) : NULL) | 
| 43 | , fCount(0) | 
| 44 | , fCapacity(capacity) {} | 
| 45 | ~NFRuleList() { | 
| 46 | if (fStuff) { | 
| 47 | for(uint32_t i = 0; i < fCount; ++i) { | 
| 48 | delete fStuff[i]; | 
| 49 | } | 
| 50 | uprv_free(fStuff); | 
| 51 | } | 
| 52 | } | 
| 53 | NFRule* operator[](uint32_t index) const { return fStuff != NULL ? fStuff[index] : NULL; } | 
| 54 | NFRule* remove(uint32_t index) { | 
| 55 | if (fStuff == NULL) { | 
| 56 | return NULL; | 
| 57 | } | 
| 58 | NFRule* result = fStuff[index]; | 
| 59 | fCount -= 1; | 
| 60 | for (uint32_t i = index; i < fCount; ++i) { // assumes small arrays | 
| 61 | fStuff[i] = fStuff[i+1]; | 
| 62 | } | 
| 63 | return result; | 
| 64 | } | 
| 65 | void add(NFRule* thing) { | 
| 66 | if (fCount == fCapacity) { | 
| 67 | fCapacity += 10; | 
| 68 | fStuff = (NFRule**)uprv_realloc(fStuff, fCapacity * sizeof(NFRule*)); // assume success | 
| 69 | } | 
| 70 | if (fStuff != NULL) { | 
| 71 | fStuff[fCount++] = thing; | 
| 72 | } else { | 
| 73 | fCapacity = 0; | 
| 74 | fCount = 0; | 
| 75 | } | 
| 76 | } | 
| 77 | uint32_t size() const { return fCount; } | 
| 78 | NFRule* last() const { return (fCount > 0 && fStuff != NULL) ? fStuff[fCount-1] : NULL; } | 
| 79 | NFRule** release() { | 
| 80 | add(NULL); // ensure null termination | 
| 81 | NFRule** result = fStuff; | 
| 82 | fStuff = NULL; | 
| 83 | fCount = 0; | 
| 84 | fCapacity = 0; | 
| 85 | return result; | 
| 86 | } | 
| 87 | void deleteAll() { | 
| 88 | NFRule** tmp = NULL; | 
| 89 | int32_t size = fCount; | 
| 90 | if (size > 0) { | 
| 91 | tmp = release(); | 
| 92 | for (int32_t i = 0; i < size; i++) { | 
| 93 | delete tmp[i]; | 
| 94 | } | 
| 95 | if (tmp) { | 
| 96 | uprv_free(tmp); | 
| 97 | } | 
| 98 | } | 
| 99 | } | 
| 100 | |
| 101 | private: | 
| 102 | NFRuleList(const NFRuleList &other); // forbid copying of this class | 
| 103 | NFRuleList &operator=(const NFRuleList &other); // forbid copying of this class | 
| 104 | }; | 
| 105 | |
| 106 | U_NAMESPACE_END | 
| 107 | |
| 108 | /* U_HAVE_RBNF */ | 
| 109 | #endif | 
| 110 | |
| 111 | // NFRLIST_H | 
| 112 | #endif | 
| 113 | 
