summaryrefslogtreecommitdiffstats
path: root/intl/icu/source/i18n/nfrlist.h
blob: 1864d4d3bd99e5a36b8b64938c7766b32ce5fa65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
******************************************************************************
*   Copyright (C) 1997-2012, International Business Machines
*   Corporation and others.  All Rights Reserved.
******************************************************************************
*   file name:  nfrlist.h
*   encoding:   UTF-8
*   tab size:   8 (not used)
*   indentation:4
*
* Modification history
* Date        Name      Comments
* 10/11/2001  Doug      Ported from ICU4J
*/

#ifndef NFRLIST_H
#define NFRLIST_H

#include "unicode/rbnf.h"

#if U_HAVE_RBNF

#include "unicode/uobject.h"
#include "nfrule.h"

#include "cmemory.h"

U_NAMESPACE_BEGIN

// unsafe class for internal use only.  assume memory allocations succeed, indexes are valid.
// should be a template, but we can't use them

class NFRuleList : public UMemory {
protected:
    NFRule** fStuff;
    uint32_t fCount;
    uint32_t fCapacity;
public:
    NFRuleList(uint32_t capacity = 10) 
        : fStuff(capacity ? (NFRule**)uprv_malloc(capacity * sizeof(NFRule*)) : nullptr)
        , fCount(0)
        , fCapacity(capacity) {}
    ~NFRuleList() {
        if (fStuff) {
            for(uint32_t i = 0; i < fCount; ++i) {
                delete fStuff[i];
            }
            uprv_free(fStuff);
        }
    }
    NFRule* operator[](uint32_t index) const { return fStuff != nullptr ? fStuff[index] : nullptr; }
    NFRule* remove(uint32_t index) {
    	if (fStuff == nullptr) {
    		return nullptr;
    	}
        NFRule* result = fStuff[index];
        fCount -= 1;
        for (uint32_t i = index; i < fCount; ++i) { // assumes small arrays
            fStuff[i] = fStuff[i+1];
        }
        return result;
    }
    void add(NFRule* thing) {
        if (fCount == fCapacity) {
            fCapacity += 10;
            fStuff = (NFRule**)uprv_realloc(fStuff, fCapacity * sizeof(NFRule*)); // assume success
        }
        if (fStuff != nullptr) {
        	fStuff[fCount++] = thing;
        } else {
        	fCapacity = 0;
        	fCount = 0;
        }
    }
    uint32_t size() const { return fCount; }
    NFRule* last() const { return (fCount > 0 && fStuff != nullptr) ? fStuff[fCount-1] : nullptr; }
    NFRule** release() {
        add(nullptr); // ensure null termination
        NFRule** result = fStuff;
        fStuff = nullptr;
        fCount = 0;
        fCapacity = 0;
        return result;
    }
    void deleteAll() {
        NFRule** tmp = nullptr;
        int32_t size = fCount;
        if (size > 0) {
            tmp = release();
            for (int32_t i = 0; i < size; i++) {
                delete tmp[i];
            }
            if (tmp) {
                uprv_free(tmp);
            }
        }
    }

private:
    NFRuleList(const NFRuleList &other); // forbid copying of this class
    NFRuleList &operator=(const NFRuleList &other); // forbid copying of this class
};

U_NAMESPACE_END

/* U_HAVE_RBNF */
#endif

// NFRLIST_H
#endif