]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/nfrlist.h
ICU-511.35.tar.gz
[apple/icu.git] / icuSources / i18n / nfrlist.h
CommitLineData
b75a7d8f
A
1/*
2******************************************************************************
4388f060 3* Copyright (C) 1997-2012, International Business Machines
b75a7d8f
A
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
28U_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
33class NFRuleList : public UMemory {
34protected:
35 NFRule** fStuff;
36 uint32_t fCount;
37 uint32_t fCapacity;
38public:
73c04bcf 39 NFRuleList(uint32_t capacity = 10)
b75a7d8f
A
40 : fStuff(capacity ? (NFRule**)uprv_malloc(capacity * sizeof(NFRule*)) : NULL)
41 , fCount(0)
4388f060 42 , fCapacity(capacity) {}
b75a7d8f
A
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 }
46f4442e 51 NFRule* operator[](uint32_t index) const { return fStuff != NULL ? fStuff[index] : NULL; }
b75a7d8f 52 NFRule* remove(uint32_t index) {
46f4442e
A
53 if (fStuff == NULL) {
54 return NULL;
55 }
b75a7d8f
A
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 }
46f4442e
A
68 if (fStuff != NULL) {
69 fStuff[fCount++] = thing;
70 } else {
71 fCapacity = 0;
72 fCount = 0;
73 }
b75a7d8f
A
74 }
75 uint32_t size() const { return fCount; }
46f4442e 76 NFRule* last() const { return (fCount > 0 && fStuff != NULL) ? fStuff[fCount-1] : NULL; }
b75a7d8f
A
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 }
4388f060
A
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 }
b75a7d8f
A
98
99private:
100 NFRuleList(const NFRuleList &other); // forbid copying of this class
101 NFRuleList &operator=(const NFRuleList &other); // forbid copying of this class
102};
103
104U_NAMESPACE_END
105
106/* U_HAVE_RBNF */
107#endif
108
109// NFRLIST_H
110#endif