intl/icu/source/common/uvectr64.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/uvectr64.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,277 @@
     1.4 +/*
     1.5 +**********************************************************************
     1.6 +*   Copyright (C) 1999-2010, International Business Machines
     1.7 +*   Corporation and others.  All Rights Reserved.
     1.8 +**********************************************************************
     1.9 +*/
    1.10 +
    1.11 +//
    1.12 +//  UVector64 is a class implementing a vector of 64 bit integers.
    1.13 +//            It is similar to UVector32, but holds int64_t values rather than int32_t.
    1.14 +//            Most of the code is unchanged from UVector.
    1.15 +//
    1.16 +
    1.17 +#ifndef UVECTOR64_H
    1.18 +#define UVECTOR64_H
    1.19 +
    1.20 +#include "unicode/utypes.h"
    1.21 +#include "unicode/uobject.h"
    1.22 +#include "uhash.h"
    1.23 +#include "uassert.h"
    1.24 +
    1.25 +U_NAMESPACE_BEGIN
    1.26 +
    1.27 +
    1.28 +
    1.29 +/**
    1.30 + * <p>Ultralightweight C++ implementation of an <tt>int64_t</tt> vector
    1.31 + * that has a subset of methods from UVector32
    1.32 + *
    1.33 + * <p>This is a very simple implementation, written to satisfy an
    1.34 + * immediate porting need.  As such, it is not completely fleshed out,
    1.35 + * and it aims for simplicity and conformity.  Nonetheless, it serves
    1.36 + * its purpose (porting code from java that uses java.util.Vector)
    1.37 + * well, and it could be easily made into a more robust vector class.
    1.38 + *
    1.39 + * <p><b>Design notes</b>
    1.40 + *
    1.41 + * <p>There is index bounds checking, but little is done about it.  If
    1.42 + * indices are out of bounds, either nothing happens, or zero is
    1.43 + * returned.  We <em>do</em> avoid indexing off into the weeds.
    1.44 + *
    1.45 + * <p>There is detection of out of memory, but the handling is very
    1.46 + * coarse-grained -- similar to UnicodeString's protocol, but even
    1.47 + * coarser.  The class contains <em>one static flag</em> that is set
    1.48 + * when any call to <tt>new</tt> returns zero.  This allows the caller
    1.49 + * to use several vectors and make just one check at the end to see if
    1.50 + * a memory failure occurred.  This is more efficient than making a
    1.51 + * check after each call on each vector when doing many operations on
    1.52 + * multiple vectors.  The single static flag works best when memory
    1.53 + * failures are infrequent, and when recovery options are limited or
    1.54 + * nonexistent.
    1.55 + *
    1.56 + * <p><b>To do</b>
    1.57 + *
    1.58 + * <p>Improve the handling of index out of bounds errors.
    1.59 + *
    1.60 + */
    1.61 +class U_COMMON_API UVector64 : public UObject {
    1.62 +private:
    1.63 +    int32_t   count;
    1.64 +
    1.65 +    int32_t   capacity;
    1.66 +    
    1.67 +    int32_t   maxCapacity;   // Limit beyond which capacity is not permitted to grow.
    1.68 +
    1.69 +    int64_t*  elements;
    1.70 +
    1.71 +public:
    1.72 +    UVector64(UErrorCode &status);
    1.73 +
    1.74 +    UVector64(int32_t initialCapacity, UErrorCode &status);
    1.75 +
    1.76 +    virtual ~UVector64();
    1.77 +
    1.78 +    /**
    1.79 +     * Assign this object to another (make this a copy of 'other').
    1.80 +     * Use the 'assign' function to assign each element.
    1.81 +     */
    1.82 +    void assign(const UVector64& other, UErrorCode &ec);
    1.83 +
    1.84 +    /**
    1.85 +     * Compare this vector with another.  They will be considered
    1.86 +     * equal if they are of the same size and all elements are equal,
    1.87 +     * as compared using this object's comparer.
    1.88 +     */
    1.89 +    UBool operator==(const UVector64& other);
    1.90 +
    1.91 +    /**
    1.92 +     * Equivalent to !operator==()
    1.93 +     */
    1.94 +    inline UBool operator!=(const UVector64& other);
    1.95 +
    1.96 +    //------------------------------------------------------------
    1.97 +    // subset of java.util.Vector API
    1.98 +    //------------------------------------------------------------
    1.99 +
   1.100 +    void addElement(int64_t elem, UErrorCode &status);
   1.101 +
   1.102 +    void setElementAt(int64_t elem, int32_t index);
   1.103 +
   1.104 +    void insertElementAt(int64_t elem, int32_t index, UErrorCode &status);
   1.105 +    
   1.106 +    int64_t elementAti(int32_t index) const;
   1.107 +
   1.108 +    //UBool equals(const UVector64 &other) const;
   1.109 +
   1.110 +    int64_t lastElementi(void) const;
   1.111 +
   1.112 +    //int32_t indexOf(int64_t elem, int32_t startIndex = 0) const;
   1.113 +
   1.114 +    //UBool contains(int64_t elem) const;
   1.115 +
   1.116 +    //UBool containsAll(const UVector64& other) const;
   1.117 +
   1.118 +    //UBool removeAll(const UVector64& other);
   1.119 +
   1.120 +    //UBool retainAll(const UVector64& other);
   1.121 +
   1.122 +    //void removeElementAt(int32_t index);
   1.123 +
   1.124 +    void removeAllElements();
   1.125 +
   1.126 +    int32_t size(void) const;
   1.127 +
   1.128 +    //UBool isEmpty(void) const;
   1.129 +
   1.130 +    // Inline.  Use this one for speedy size check.
   1.131 +    inline UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
   1.132 +
   1.133 +    // Out-of-line, handles actual growth.  Called by ensureCapacity() when necessary.
   1.134 +    UBool expandCapacity(int32_t minimumCapacity, UErrorCode &status);
   1.135 +
   1.136 +    /**
   1.137 +     * Change the size of this vector as follows: If newSize is
   1.138 +     * smaller, then truncate the array, possibly deleting held
   1.139 +     * elements for i >= newSize.  If newSize is larger, grow the
   1.140 +     * array, filling in new slows with zero.
   1.141 +     */
   1.142 +    void setSize(int32_t newSize);
   1.143 +
   1.144 +    //------------------------------------------------------------
   1.145 +    // New API
   1.146 +    //------------------------------------------------------------
   1.147 +
   1.148 +    //UBool containsNone(const UVector64& other) const;
   1.149 +
   1.150 +
   1.151 +    //void sortedInsert(int64_t elem, UErrorCode& ec);
   1.152 +
   1.153 +    /**
   1.154 +     * Returns a pointer to the internal array holding the vector.
   1.155 +     */
   1.156 +    int64_t *getBuffer() const;
   1.157 +
   1.158 +    /**
   1.159 +     * Set the maximum allowed buffer capacity for this vector/stack.
   1.160 +     * Default with no limit set is unlimited, go until malloc() fails.
   1.161 +     * A Limit of zero means unlimited capacity.
   1.162 +     * Units are vector elements (64 bits each), not bytes.
   1.163 +     */
   1.164 +    void setMaxCapacity(int32_t limit);
   1.165 +
   1.166 +    /**
   1.167 +     * ICU "poor man's RTTI", returns a UClassID for this class.
   1.168 +     */
   1.169 +    static UClassID U_EXPORT2 getStaticClassID();
   1.170 +
   1.171 +    /**
   1.172 +     * ICU "poor man's RTTI", returns a UClassID for the actual class.
   1.173 +     */
   1.174 +    virtual UClassID getDynamicClassID() const;
   1.175 +
   1.176 +private:
   1.177 +    void _init(int32_t initialCapacity, UErrorCode &status);
   1.178 +
   1.179 +    // Disallow
   1.180 +    UVector64(const UVector64&);
   1.181 +
   1.182 +    // Disallow
   1.183 +    UVector64& operator=(const UVector64&);
   1.184 +
   1.185 +
   1.186 +    //  API Functions for Stack operations.
   1.187 +    //  In the original UVector, these were in a separate derived class, UStack.
   1.188 +    //  Here in UVector64, they are all together.
   1.189 +public:
   1.190 +    //UBool empty(void) const;   // TODO:  redundant, same as empty().  Remove it?
   1.191 +
   1.192 +    //int64_t peeki(void) const;
   1.193 +    
   1.194 +    int64_t popi(void);
   1.195 +    
   1.196 +    int64_t push(int64_t i, UErrorCode &status);
   1.197 +
   1.198 +    int64_t *reserveBlock(int32_t size, UErrorCode &status);
   1.199 +    int64_t *popFrame(int32_t size);
   1.200 +};
   1.201 +
   1.202 +
   1.203 +// UVector64 inlines
   1.204 +
   1.205 +inline UBool UVector64::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) {
   1.206 +    if ((minimumCapacity >= 0) && (capacity >= minimumCapacity)) {
   1.207 +        return TRUE;
   1.208 +    } else {
   1.209 +        return expandCapacity(minimumCapacity, status);
   1.210 +    }
   1.211 +}
   1.212 +
   1.213 +inline int64_t UVector64::elementAti(int32_t index) const {
   1.214 +    return (0 <= index && index < count) ? elements[index] : 0;
   1.215 +}
   1.216 +
   1.217 +
   1.218 +inline void UVector64::addElement(int64_t elem, UErrorCode &status) {
   1.219 +    if (ensureCapacity(count + 1, status)) {
   1.220 +        elements[count] = elem;
   1.221 +        count++;
   1.222 +    }
   1.223 +}
   1.224 +
   1.225 +inline int64_t *UVector64::reserveBlock(int32_t size, UErrorCode &status) {
   1.226 +    if (ensureCapacity(count+size, status) == FALSE) {
   1.227 +        return NULL;
   1.228 +    }
   1.229 +    int64_t  *rp = elements+count;
   1.230 +    count += size;
   1.231 +    return rp;
   1.232 +}
   1.233 +
   1.234 +inline int64_t *UVector64::popFrame(int32_t size) {
   1.235 +    U_ASSERT(count >= size);
   1.236 +    count -= size;
   1.237 +    if (count < 0) {
   1.238 +        count = 0;
   1.239 +    }
   1.240 +    return elements+count-size;
   1.241 +}
   1.242 +
   1.243 +
   1.244 +
   1.245 +inline int32_t UVector64::size(void) const {
   1.246 +    return count;
   1.247 +}
   1.248 +
   1.249 +inline int64_t UVector64::lastElementi(void) const {
   1.250 +    return elementAti(count-1);
   1.251 +}
   1.252 +
   1.253 +inline UBool UVector64::operator!=(const UVector64& other) {
   1.254 +    return !operator==(other);
   1.255 +}
   1.256 +
   1.257 +inline int64_t *UVector64::getBuffer() const {
   1.258 +    return elements;
   1.259 +}
   1.260 +
   1.261 +
   1.262 +// UStack inlines
   1.263 +
   1.264 +inline int64_t UVector64::push(int64_t i, UErrorCode &status) {
   1.265 +    addElement(i, status);
   1.266 +    return i;
   1.267 +}
   1.268 +
   1.269 +inline int64_t UVector64::popi(void) {
   1.270 +    int64_t result = 0;
   1.271 +    if (count > 0) {
   1.272 +        count--;
   1.273 +        result = elements[count];
   1.274 +    }
   1.275 +    return result;
   1.276 +}
   1.277 +
   1.278 +U_NAMESPACE_END
   1.279 +
   1.280 +#endif

mercurial