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