os/textandloc/fontservices/textshaperplugin/IcuSource/common/uvectr32.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
**********************************************************************
sl@0
     3
*   Copyright (C) 1999-2005, International Business Machines
sl@0
     4
*   Corporation and others.  All Rights Reserved.
sl@0
     5
**********************************************************************
sl@0
     6
*/
sl@0
     7
sl@0
     8
//
sl@0
     9
//  UVector32 is a class implementing a vector of 32 bit integers.
sl@0
    10
//            It is similar to UVector, but holds int32_t values rather than pointers.
sl@0
    11
//            Most of the code is unchanged from UVector.
sl@0
    12
//
sl@0
    13
sl@0
    14
#ifndef UVECTOR32_H
sl@0
    15
#define UVECTOR32_H
sl@0
    16
sl@0
    17
#include "unicode/utypes.h"
sl@0
    18
#include "unicode/uobject.h"
sl@0
    19
#include "uhash.h"
sl@0
    20
#include "uassert.h"
sl@0
    21
sl@0
    22
U_NAMESPACE_BEGIN
sl@0
    23
sl@0
    24
sl@0
    25
sl@0
    26
/**
sl@0
    27
 * <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector
sl@0
    28
 * that is (mostly) compatible with java.util.Vector.
sl@0
    29
 *
sl@0
    30
 * <p>This is a very simple implementation, written to satisfy an
sl@0
    31
 * immediate porting need.  As such, it is not completely fleshed out,
sl@0
    32
 * and it aims for simplicity and conformity.  Nonetheless, it serves
sl@0
    33
 * its purpose (porting code from java that uses java.util.Vector)
sl@0
    34
 * well, and it could be easily made into a more robust vector class.
sl@0
    35
 *
sl@0
    36
 * <p><b>Design notes</b>
sl@0
    37
 *
sl@0
    38
 * <p>There is index bounds checking, but little is done about it.  If
sl@0
    39
 * indices are out of bounds, either nothing happens, or zero is
sl@0
    40
 * returned.  We <em>do</em> avoid indexing off into the weeds.
sl@0
    41
 *
sl@0
    42
 * <p>There is detection of out of memory, but the handling is very
sl@0
    43
 * coarse-grained -- similar to UnicodeString's protocol, but even
sl@0
    44
 * coarser.  The class contains <em>one static flag</em> that is set
sl@0
    45
 * when any call to <tt>new</tt> returns zero.  This allows the caller
sl@0
    46
 * to use several vectors and make just one check at the end to see if
sl@0
    47
 * a memory failure occurred.  This is more efficient than making a
sl@0
    48
 * check after each call on each vector when doing many operations on
sl@0
    49
 * multiple vectors.  The single static flag works best when memory
sl@0
    50
 * failures are infrequent, and when recovery options are limited or
sl@0
    51
 * nonexistent.
sl@0
    52
 *
sl@0
    53
 * <p><b>To do</b>
sl@0
    54
 *
sl@0
    55
 * <p>Improve the handling of index out of bounds errors.
sl@0
    56
 *
sl@0
    57
 * @author Alan Liu
sl@0
    58
 */
sl@0
    59
class U_COMMON_API UVector32 : public UObject {
sl@0
    60
private:
sl@0
    61
    int32_t   count;
sl@0
    62
sl@0
    63
    int32_t   capacity;
sl@0
    64
sl@0
    65
    int32_t*  elements;
sl@0
    66
sl@0
    67
public:
sl@0
    68
    UVector32(UErrorCode &status);
sl@0
    69
sl@0
    70
    UVector32(int32_t initialCapacity, UErrorCode &status);
sl@0
    71
sl@0
    72
    virtual ~UVector32();
sl@0
    73
sl@0
    74
    /**
sl@0
    75
     * Assign this object to another (make this a copy of 'other').
sl@0
    76
     * Use the 'assign' function to assign each element.
sl@0
    77
     */
sl@0
    78
    void assign(const UVector32& other, UErrorCode &ec);
sl@0
    79
sl@0
    80
    /**
sl@0
    81
     * Compare this vector with another.  They will be considered
sl@0
    82
     * equal if they are of the same size and all elements are equal,
sl@0
    83
     * as compared using this object's comparer.
sl@0
    84
     */
sl@0
    85
    UBool operator==(const UVector32& other);
sl@0
    86
sl@0
    87
    /**
sl@0
    88
     * Equivalent to !operator==()
sl@0
    89
     */
sl@0
    90
    inline UBool operator!=(const UVector32& other);
sl@0
    91
sl@0
    92
    //------------------------------------------------------------
sl@0
    93
    // java.util.Vector API
sl@0
    94
    //------------------------------------------------------------
sl@0
    95
sl@0
    96
    void addElement(int32_t elem, UErrorCode &status);
sl@0
    97
sl@0
    98
    void setElementAt(int32_t elem, int32_t index);
sl@0
    99
sl@0
   100
    void insertElementAt(int32_t elem, int32_t index, UErrorCode &status);
sl@0
   101
    
sl@0
   102
    int32_t elementAti(int32_t index) const;
sl@0
   103
sl@0
   104
    UBool equals(const UVector32 &other) const;
sl@0
   105
sl@0
   106
    int32_t lastElementi(void) const;
sl@0
   107
sl@0
   108
    int32_t indexOf(int32_t elem, int32_t startIndex = 0) const;
sl@0
   109
sl@0
   110
    UBool contains(int32_t elem) const;
sl@0
   111
sl@0
   112
    UBool containsAll(const UVector32& other) const;
sl@0
   113
sl@0
   114
    UBool removeAll(const UVector32& other);
sl@0
   115
sl@0
   116
    UBool retainAll(const UVector32& other);
sl@0
   117
sl@0
   118
    void removeElementAt(int32_t index);
sl@0
   119
sl@0
   120
    void removeAllElements();
sl@0
   121
sl@0
   122
    int32_t size(void) const;
sl@0
   123
sl@0
   124
    UBool isEmpty(void) const;
sl@0
   125
sl@0
   126
    // Inline.  Use this one for speedy size check.
sl@0
   127
    inline UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
sl@0
   128
sl@0
   129
    // Out-of-line, handles actual growth.  Called by ensureCapacity() when necessary.
sl@0
   130
    UBool expandCapacity(int32_t minimumCapacity, UErrorCode &status);
sl@0
   131
sl@0
   132
    /**
sl@0
   133
     * Change the size of this vector as follows: If newSize is
sl@0
   134
     * smaller, then truncate the array, possibly deleting held
sl@0
   135
     * elements for i >= newSize.  If newSize is larger, grow the
sl@0
   136
     * array, filling in new slows with zero.
sl@0
   137
     */
sl@0
   138
    void setSize(int32_t newSize);
sl@0
   139
sl@0
   140
    //------------------------------------------------------------
sl@0
   141
    // New API
sl@0
   142
    //------------------------------------------------------------
sl@0
   143
sl@0
   144
    /**
sl@0
   145
     * Returns true if this vector contains none of the elements
sl@0
   146
     * of the given vector.
sl@0
   147
     * @param other vector to be checked for containment
sl@0
   148
     * @return true if the test condition is met
sl@0
   149
     */
sl@0
   150
    UBool containsNone(const UVector32& other) const;
sl@0
   151
sl@0
   152
sl@0
   153
    /**
sl@0
   154
     * Insert the given integer into this vector at its sorted position.
sl@0
   155
     * The current elements are assumed to be sorted already.
sl@0
   156
     */
sl@0
   157
    void sortedInsert(int32_t elem, UErrorCode& ec);
sl@0
   158
sl@0
   159
    /**
sl@0
   160
     * Returns a pointer to the internal array holding the vector.
sl@0
   161
     */
sl@0
   162
    int32_t *getBuffer() const;
sl@0
   163
sl@0
   164
    /**
sl@0
   165
     * ICU "poor man's RTTI", returns a UClassID for this class.
sl@0
   166
     *
sl@0
   167
     * @draft ICU 2.2
sl@0
   168
     */
sl@0
   169
    static UClassID U_EXPORT2 getStaticClassID();
sl@0
   170
sl@0
   171
    /**
sl@0
   172
     * ICU "poor man's RTTI", returns a UClassID for the actual class.
sl@0
   173
     *
sl@0
   174
     * @draft ICU 2.2
sl@0
   175
     */
sl@0
   176
    virtual UClassID getDynamicClassID() const;
sl@0
   177
sl@0
   178
private:
sl@0
   179
    void _init(int32_t initialCapacity, UErrorCode &status);
sl@0
   180
sl@0
   181
    // Disallow
sl@0
   182
    UVector32(const UVector32&);
sl@0
   183
sl@0
   184
    // Disallow
sl@0
   185
    UVector32& operator=(const UVector32&);
sl@0
   186
sl@0
   187
sl@0
   188
    //  API Functions for Stack operations.
sl@0
   189
    //  In the original UVector, these were in a separate derived class, UStack.
sl@0
   190
    //  Here in UVector32, they are all together.
sl@0
   191
public:
sl@0
   192
    UBool empty(void) const;   // TODO:  redundant, same as empty().  Remove it?
sl@0
   193
sl@0
   194
    int32_t peeki(void) const;
sl@0
   195
    
sl@0
   196
    int32_t popi(void);
sl@0
   197
    
sl@0
   198
    int32_t push(int32_t i, UErrorCode &status);
sl@0
   199
sl@0
   200
    int32_t *reserveBlock(int32_t size, UErrorCode &status);
sl@0
   201
    int32_t *popFrame(int32_t size);
sl@0
   202
};
sl@0
   203
sl@0
   204
sl@0
   205
// UVector32 inlines
sl@0
   206
sl@0
   207
inline UBool UVector32::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) {
sl@0
   208
    if (capacity >= minimumCapacity) {
sl@0
   209
        return TRUE;
sl@0
   210
    } else {
sl@0
   211
        return expandCapacity(minimumCapacity, status);
sl@0
   212
    }
sl@0
   213
}
sl@0
   214
sl@0
   215
inline int32_t UVector32::elementAti(int32_t index) const {
sl@0
   216
    return (0 <= index && index < count) ? elements[index] : 0;
sl@0
   217
}
sl@0
   218
sl@0
   219
sl@0
   220
inline void UVector32::addElement(int32_t elem, UErrorCode &status) {
sl@0
   221
    if (ensureCapacity(count + 1, status)) {
sl@0
   222
        elements[count] = elem;
sl@0
   223
        count++;
sl@0
   224
    }
sl@0
   225
}
sl@0
   226
sl@0
   227
inline int32_t *UVector32::reserveBlock(int32_t size, UErrorCode &status) {
sl@0
   228
    ensureCapacity(count+size, status);
sl@0
   229
    int32_t  *rp = elements+count;
sl@0
   230
    count += size;
sl@0
   231
    return rp;
sl@0
   232
}
sl@0
   233
sl@0
   234
inline int32_t *UVector32::popFrame(int32_t size) {
sl@0
   235
    U_ASSERT(count >= size);
sl@0
   236
    count -= size;
sl@0
   237
    if (count < 0) {
sl@0
   238
        count = 0;
sl@0
   239
    }
sl@0
   240
    return elements+count-size;
sl@0
   241
}
sl@0
   242
sl@0
   243
sl@0
   244
sl@0
   245
inline int32_t UVector32::size(void) const {
sl@0
   246
    return count;
sl@0
   247
}
sl@0
   248
sl@0
   249
inline UBool UVector32::isEmpty(void) const {
sl@0
   250
    return count == 0;
sl@0
   251
}
sl@0
   252
sl@0
   253
inline UBool UVector32::contains(int32_t obj) const {
sl@0
   254
    return indexOf(obj) >= 0;
sl@0
   255
}
sl@0
   256
sl@0
   257
inline int32_t UVector32::lastElementi(void) const {
sl@0
   258
    return elementAti(count-1);
sl@0
   259
}
sl@0
   260
sl@0
   261
inline UBool UVector32::operator!=(const UVector32& other) {
sl@0
   262
    return !operator==(other);
sl@0
   263
}
sl@0
   264
sl@0
   265
inline int32_t *UVector32::getBuffer() const {
sl@0
   266
    return elements;
sl@0
   267
}
sl@0
   268
sl@0
   269
sl@0
   270
// UStack inlines
sl@0
   271
sl@0
   272
inline UBool UVector32::empty(void) const {
sl@0
   273
    return isEmpty();
sl@0
   274
}
sl@0
   275
sl@0
   276
inline int32_t UVector32::peeki(void) const {
sl@0
   277
    return lastElementi();
sl@0
   278
}
sl@0
   279
sl@0
   280
inline int32_t UVector32::push(int32_t i, UErrorCode &status) {
sl@0
   281
    addElement(i, status);
sl@0
   282
    return i;
sl@0
   283
}
sl@0
   284
sl@0
   285
inline int32_t UVector32::popi(void) {
sl@0
   286
    int32_t result = 0;
sl@0
   287
    if (count > 0) {
sl@0
   288
        count--;
sl@0
   289
        result = elements[count];
sl@0
   290
    }
sl@0
   291
    return result;
sl@0
   292
}
sl@0
   293
sl@0
   294
U_NAMESPACE_END
sl@0
   295
sl@0
   296
#endif