1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru******************************************************************************
383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius*   Copyright (C) 1997-2012, International Business Machines
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Corporation and others.  All Rights Reserved.
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru******************************************************************************
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   file name:  nfrlist.h
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   encoding:   US-ASCII
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   tab size:   8 (not used)
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   indentation:4
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Modification history
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Date        Name      Comments
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 10/11/2001  Doug      Ported from ICU4J
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifndef NFRLIST_H
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define NFRLIST_H
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/rbnf.h"
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if U_HAVE_RBNF
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uobject.h"
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "nfrule.h"
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cmemory.h"
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_BEGIN
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// unsafe class for internal use only.  assume memory allocations succeed, indexes are valid.
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// should be a template, but we can't use them
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruclass NFRuleList : public UMemory {
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruprotected:
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    NFRule** fStuff;
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint32_t fCount;
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint32_t fCapacity;
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querupublic:
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    NFRuleList(uint32_t capacity = 10)
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        : fStuff(capacity ? (NFRule**)uprv_malloc(capacity * sizeof(NFRule*)) : NULL)
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        , fCount(0)
42b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho        , fCapacity(capacity) {}
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ~NFRuleList() {
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (fStuff) {
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            for(uint32_t i = 0; i < fCount; ++i) {
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                delete fStuff[i];
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            }
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            uprv_free(fStuff);
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
5185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    NFRule* operator[](uint32_t index) const { return fStuff != NULL ? fStuff[index] : NULL; }
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    NFRule* remove(uint32_t index) {
5385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    	if (fStuff == NULL) {
5485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    		return NULL;
5585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    	}
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        NFRule* result = fStuff[index];
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fCount -= 1;
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for (uint32_t i = index; i < fCount; ++i) { // assumes small arrays
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            fStuff[i] = fStuff[i+1];
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return result;
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    void add(NFRule* thing) {
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if (fCount == fCapacity) {
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            fCapacity += 10;
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            fStuff = (NFRule**)uprv_realloc(fStuff, fCapacity * sizeof(NFRule*)); // assume success
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
6885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (fStuff != NULL) {
6985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        	fStuff[fCount++] = thing;
7085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        } else {
7185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        	fCapacity = 0;
7285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        	fCount = 0;
7385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint32_t size() const { return fCount; }
7685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    NFRule* last() const { return (fCount > 0 && fStuff != NULL) ? fStuff[fCount-1] : NULL; }
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    NFRule** release() {
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        add(NULL); // ensure null termination
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        NFRule** result = fStuff;
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fStuff = NULL;
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fCount = 0;
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        fCapacity = 0;
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return result;
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
8583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    void deleteAll() {
8683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        NFRule** tmp = NULL;
8783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        int32_t size = fCount;
8883a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        if (size > 0) {
8983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius            tmp = release();
9083a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius            for (int32_t i = 0; i < size; i++) {
9183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius                delete tmp[i];
9283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius            }
9383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius            if (tmp) {
9483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius                uprv_free(tmp);
9583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius            }
9683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        }
9783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    }
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruprivate:
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    NFRuleList(const NFRuleList &other); // forbid copying of this class
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    NFRuleList &operator=(const NFRuleList &other); // forbid copying of this class
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru};
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_END
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* U_HAVE_RBNF */
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// NFRLIST_H
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif
111