]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/nfrlist.h
ICU-62141.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / nfrlist.h
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4******************************************************************************
4388f060 5* Copyright (C) 1997-2012, International Business Machines
b75a7d8f
A
6* Corporation and others. All Rights Reserved.
7******************************************************************************
8* file name: nfrlist.h
f3c0d7a5 9* encoding: UTF-8
b75a7d8f
A
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
30U_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
35class NFRuleList : public UMemory {
36protected:
37 NFRule** fStuff;
38 uint32_t fCount;
39 uint32_t fCapacity;
40public:
73c04bcf 41 NFRuleList(uint32_t capacity = 10)
b75a7d8f
A
42 : fStuff(capacity ? (NFRule**)uprv_malloc(capacity * sizeof(NFRule*)) : NULL)
43 , fCount(0)
4388f060 44 , fCapacity(capacity) {}
b75a7d8f
A
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 }
46f4442e 53 NFRule* operator[](uint32_t index) const { return fStuff != NULL ? fStuff[index] : NULL; }
b75a7d8f 54 NFRule* remove(uint32_t index) {
46f4442e
A
55 if (fStuff == NULL) {
56 return NULL;
57 }
b75a7d8f
A
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 }
46f4442e
A
70 if (fStuff != NULL) {
71 fStuff[fCount++] = thing;
72 } else {
73 fCapacity = 0;
74 fCount = 0;
75 }
b75a7d8f
A
76 }
77 uint32_t size() const { return fCount; }
46f4442e 78 NFRule* last() const { return (fCount > 0 && fStuff != NULL) ? fStuff[fCount-1] : NULL; }
b75a7d8f
A
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 }
4388f060
A
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 }
b75a7d8f
A
100
101private:
102 NFRuleList(const NFRuleList &other); // forbid copying of this class
103 NFRuleList &operator=(const NFRuleList &other); // forbid copying of this class
104};
105
106U_NAMESPACE_END
107
108/* U_HAVE_RBNF */
109#endif
110
111// NFRLIST_H
112#endif