Line data Source code
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 0 : NFRuleList(uint32_t capacity = 10)
42 0 : : fStuff(capacity ? (NFRule**)uprv_malloc(capacity * sizeof(NFRule*)) : NULL)
43 : , fCount(0)
44 0 : , fCapacity(capacity) {}
45 0 : ~NFRuleList() {
46 0 : if (fStuff) {
47 0 : for(uint32_t i = 0; i < fCount; ++i) {
48 0 : delete fStuff[i];
49 : }
50 0 : uprv_free(fStuff);
51 : }
52 0 : }
53 0 : 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 0 : void add(NFRule* thing) {
66 0 : if (fCount == fCapacity) {
67 0 : fCapacity += 10;
68 0 : fStuff = (NFRule**)uprv_realloc(fStuff, fCapacity * sizeof(NFRule*)); // assume success
69 : }
70 0 : if (fStuff != NULL) {
71 0 : fStuff[fCount++] = thing;
72 : } else {
73 0 : fCapacity = 0;
74 0 : fCount = 0;
75 : }
76 0 : }
77 0 : uint32_t size() const { return fCount; }
78 0 : NFRule* last() const { return (fCount > 0 && fStuff != NULL) ? fStuff[fCount-1] : NULL; }
79 0 : NFRule** release() {
80 0 : add(NULL); // ensure null termination
81 0 : NFRule** result = fStuff;
82 0 : fStuff = NULL;
83 0 : fCount = 0;
84 0 : fCapacity = 0;
85 0 : return result;
86 : }
87 0 : void deleteAll() {
88 0 : NFRule** tmp = NULL;
89 0 : int32_t size = fCount;
90 0 : if (size > 0) {
91 0 : tmp = release();
92 0 : for (int32_t i = 0; i < size; i++) {
93 0 : delete tmp[i];
94 : }
95 0 : if (tmp) {
96 0 : uprv_free(tmp);
97 : }
98 : }
99 0 : }
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
|