intl/icu/source/i18n/nfrlist.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/i18n/nfrlist.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/*
     1.5 +******************************************************************************
     1.6 +*   Copyright (C) 1997-2012, International Business Machines
     1.7 +*   Corporation and others.  All Rights Reserved.
     1.8 +******************************************************************************
     1.9 +*   file name:  nfrlist.h
    1.10 +*   encoding:   US-ASCII
    1.11 +*   tab size:   8 (not used)
    1.12 +*   indentation:4
    1.13 +*
    1.14 +* Modification history
    1.15 +* Date        Name      Comments
    1.16 +* 10/11/2001  Doug      Ported from ICU4J
    1.17 +*/
    1.18 +
    1.19 +#ifndef NFRLIST_H
    1.20 +#define NFRLIST_H
    1.21 +
    1.22 +#include "unicode/rbnf.h"
    1.23 +
    1.24 +#if U_HAVE_RBNF
    1.25 +
    1.26 +#include "unicode/uobject.h"
    1.27 +#include "nfrule.h"
    1.28 +
    1.29 +#include "cmemory.h"
    1.30 +
    1.31 +U_NAMESPACE_BEGIN
    1.32 +
    1.33 +// unsafe class for internal use only.  assume memory allocations succeed, indexes are valid.
    1.34 +// should be a template, but we can't use them
    1.35 +
    1.36 +class NFRuleList : public UMemory {
    1.37 +protected:
    1.38 +    NFRule** fStuff;
    1.39 +    uint32_t fCount;
    1.40 +    uint32_t fCapacity;
    1.41 +public:
    1.42 +    NFRuleList(uint32_t capacity = 10) 
    1.43 +        : fStuff(capacity ? (NFRule**)uprv_malloc(capacity * sizeof(NFRule*)) : NULL)
    1.44 +        , fCount(0)
    1.45 +        , fCapacity(capacity) {}
    1.46 +    ~NFRuleList() {
    1.47 +        if (fStuff) {
    1.48 +            for(uint32_t i = 0; i < fCount; ++i) {
    1.49 +                delete fStuff[i];
    1.50 +            }
    1.51 +            uprv_free(fStuff);
    1.52 +        }
    1.53 +    }
    1.54 +    NFRule* operator[](uint32_t index) const { return fStuff != NULL ? fStuff[index] : NULL; }
    1.55 +    NFRule* remove(uint32_t index) {
    1.56 +    	if (fStuff == NULL) {
    1.57 +    		return NULL;
    1.58 +    	}
    1.59 +        NFRule* result = fStuff[index];
    1.60 +        fCount -= 1;
    1.61 +        for (uint32_t i = index; i < fCount; ++i) { // assumes small arrays
    1.62 +            fStuff[i] = fStuff[i+1];
    1.63 +        }
    1.64 +        return result;
    1.65 +    }
    1.66 +    void add(NFRule* thing) {
    1.67 +        if (fCount == fCapacity) {
    1.68 +            fCapacity += 10;
    1.69 +            fStuff = (NFRule**)uprv_realloc(fStuff, fCapacity * sizeof(NFRule*)); // assume success
    1.70 +        }
    1.71 +        if (fStuff != NULL) {
    1.72 +        	fStuff[fCount++] = thing;
    1.73 +        } else {
    1.74 +        	fCapacity = 0;
    1.75 +        	fCount = 0;
    1.76 +        }
    1.77 +    }
    1.78 +    uint32_t size() const { return fCount; }
    1.79 +    NFRule* last() const { return (fCount > 0 && fStuff != NULL) ? fStuff[fCount-1] : NULL; }
    1.80 +    NFRule** release() {
    1.81 +        add(NULL); // ensure null termination
    1.82 +        NFRule** result = fStuff;
    1.83 +        fStuff = NULL;
    1.84 +        fCount = 0;
    1.85 +        fCapacity = 0;
    1.86 +        return result;
    1.87 +    }
    1.88 +    void deleteAll() {
    1.89 +        NFRule** tmp = NULL;
    1.90 +        int32_t size = fCount;
    1.91 +        if (size > 0) {
    1.92 +            tmp = release();
    1.93 +            for (int32_t i = 0; i < size; i++) {
    1.94 +                delete tmp[i];
    1.95 +            }
    1.96 +            if (tmp) {
    1.97 +                uprv_free(tmp);
    1.98 +            }
    1.99 +        }
   1.100 +    }
   1.101 +
   1.102 +private:
   1.103 +    NFRuleList(const NFRuleList &other); // forbid copying of this class
   1.104 +    NFRuleList &operator=(const NFRuleList &other); // forbid copying of this class
   1.105 +};
   1.106 +
   1.107 +U_NAMESPACE_END
   1.108 +
   1.109 +/* U_HAVE_RBNF */
   1.110 +#endif
   1.111 +
   1.112 +// NFRLIST_H
   1.113 +#endif

mercurial