~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

Viewing changes to source/common/uvector.h

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
**********************************************************************
 
3
*   Copyright (C) 1999-2001, International Business Machines
 
4
*   Corporation and others.  All Rights Reserved.
 
5
**********************************************************************
 
6
*   Date        Name        Description
 
7
*   10/22/99    alan        Creation.  This is an internal header.
 
8
*                           It should not be exported.
 
9
**********************************************************************
 
10
*/
 
11
 
 
12
#ifndef UVECTOR_H
 
13
#define UVECTOR_H
 
14
 
 
15
#include "unicode/utypes.h"
 
16
#include "uhash.h"
 
17
 
 
18
U_NAMESPACE_BEGIN
 
19
 
 
20
/**
 
21
 * <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector
 
22
 * that is (mostly) compatible with java.util.Vector.
 
23
 *
 
24
 * <p>This is a very simple implementation, written to satisfy an
 
25
 * immediate porting need.  As such, it is not completely fleshed out,
 
26
 * and it aims for simplicity and conformity.  Nonetheless, it serves
 
27
 * its purpose (porting code from java that uses java.util.Vector)
 
28
 * well, and it could be easily made into a more robust vector class.
 
29
 *
 
30
 * <p><b>Design notes</b>
 
31
 *
 
32
 * <p>There is index bounds checking, but little is done about it.  If
 
33
 * indices are out of bounds, either nothing happens, or zero is
 
34
 * returned.  We <em>do</em> avoid indexing off into the weeds.
 
35
 *
 
36
 * <p>There is detection of out of memory, but the handling is very
 
37
 * coarse-grained -- similar to UnicodeString's protocol, but even
 
38
 * coarser.  The class contains <em>one static flag</em> that is set
 
39
 * when any call to <tt>new</tt> returns zero.  This allows the caller
 
40
 * to use several vectors and make just one check at the end to see if
 
41
 * a memory failure occurred.  This is more efficient than making a
 
42
 * check after each call on each vector when doing many operations on
 
43
 * multiple vectors.  The single static flag works best when memory
 
44
 * failures are infrequent, and when recovery options are limited or
 
45
 * nonexistent.
 
46
 *
 
47
 * <p>Since we don't have garbage collection, UVector was given the
 
48
 * option to <em>own</em>its contents.  To employ this, set a deleter
 
49
 * function.  The deleter is called on a void* pointer when that
 
50
 * pointer is released by the vector, either when the vector itself is
 
51
 * destructed, or when a call to setElementAt() overwrites an element,
 
52
 * or when a call to remove() or one of its variants explicitly
 
53
 * removes an element.  If no deleter is set, or the deleter is set to
 
54
 * zero, then it is assumed that the caller will delete elements as
 
55
 * needed.
 
56
 *
 
57
 * <p>In order to implement methods such as contains() and indexOf(),
 
58
 * UVector needs a way to compare objects for equality.  To do so, it
 
59
 * uses a comparison frunction, or "comparer."  If the comparer is not
 
60
 * set, or is set to zero, then all such methods will act as if the
 
61
 * vector contains no element.  That is, indexOf() will always return
 
62
 * -1, contains() will always return FALSE, etc.
 
63
 *
 
64
 * <p><b>To do</b>
 
65
 *
 
66
 * <p>Improve the handling of index out of bounds errors.
 
67
 *
 
68
 * @author Alan Liu
 
69
 */
 
70
class U_COMMON_API UVector {
 
71
    // NOTE: UVector uses the UHashKey (union of void* and int32_t) as
 
72
    // its basic storage type.  It uses UKeyComparator as its
 
73
    // comparison function.  It uses UObjectDeleter as its deleter
 
74
    // function.  These are named for hashtables, but used here as-is
 
75
    // rather than duplicating the type.  This allows sharing of
 
76
    // support functions.
 
77
 
 
78
private:
 
79
    int32_t count;
 
80
 
 
81
    int32_t capacity;
 
82
 
 
83
    UHashTok* elements;
 
84
 
 
85
    UObjectDeleter deleter;
 
86
 
 
87
    UKeyComparator comparer;
 
88
 
 
89
public:
 
90
    UVector(UErrorCode &status);
 
91
 
 
92
    UVector(int32_t initialCapacity, UErrorCode &status);
 
93
 
 
94
    UVector(UObjectDeleter d, UKeyComparator c, UErrorCode &status);
 
95
 
 
96
    UVector(UObjectDeleter d, UKeyComparator c, int32_t initialCapacity, UErrorCode &status);
 
97
    ~UVector();
 
98
 
 
99
    //------------------------------------------------------------
 
100
    // java.util.Vector API
 
101
    //------------------------------------------------------------
 
102
 
 
103
    void addElement(void* obj, UErrorCode &status);
 
104
 
 
105
    void addElement(int32_t elem, UErrorCode &status);
 
106
 
 
107
    void setElementAt(void* obj, int32_t index);
 
108
 
 
109
    void setElementAt(int32_t elem, int32_t index);
 
110
 
 
111
    void insertElementAt(void* obj, int32_t index, UErrorCode &status);
 
112
 
 
113
    void* elementAt(int32_t index) const;
 
114
 
 
115
    int32_t elementAti(int32_t index) const;
 
116
 
 
117
    void* firstElement(void) const;
 
118
 
 
119
    void* lastElement(void) const;
 
120
 
 
121
    int32_t indexOf(void* obj, int32_t startIndex = 0) const;
 
122
 
 
123
    UBool contains(void* obj) const;
 
124
 
 
125
    void removeElementAt(int32_t index);
 
126
 
 
127
    UBool removeElement(void* obj);
 
128
 
 
129
    void removeAllElements();
 
130
 
 
131
    int32_t size(void) const;
 
132
 
 
133
    UBool isEmpty(void) const;
 
134
 
 
135
    UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
 
136
 
 
137
    /**
 
138
     * Change the size of this vector as follows: If newSize is
 
139
     * smaller, then truncate the array, possibly deleting held
 
140
     * elements for i >= newSize.  If newSize is larger, grow the
 
141
     * array, filling in new slows with NULL.
 
142
     */
 
143
    void setSize(int32_t newSize);
 
144
 
 
145
    /**
 
146
     * Fill in the given array with all elements of this vector.
 
147
     */
 
148
    void** toArray(void** result) const;
 
149
 
 
150
    //------------------------------------------------------------
 
151
    // New API
 
152
    //------------------------------------------------------------
 
153
 
 
154
    UObjectDeleter setDeleter(UObjectDeleter d);
 
155
 
 
156
    UKeyComparator setComparer(UKeyComparator c);
 
157
 
 
158
    void* operator[](int32_t index) const;
 
159
 
 
160
    /**
 
161
     * Removes the element at the given index from this vector and
 
162
     * transfer ownership of it to the caller.  After this call, the
 
163
     * caller owns the result and must delete it and the vector entry
 
164
     * at 'index' is removed, shifting all subsequent entries back by
 
165
     * one index and shortening the size of the vector by one.  If the
 
166
     * index is out of range or if there is no item at the given index
 
167
     * then 0 is returned and the vector is unchanged.
 
168
     */
 
169
    void* orphanElementAt(int32_t index);
 
170
 
 
171
private:
 
172
    void _init(int32_t initialCapacity, UErrorCode &status);
 
173
 
 
174
    // Disallow
 
175
    UVector(const UVector&);
 
176
 
 
177
    // Disallow
 
178
    UVector& operator=(const UVector&);
 
179
};
 
180
 
 
181
 
 
182
/**
 
183
 * <p>Ultralightweight C++ implementation of a <tt>void*</tt> stack
 
184
 * that is (mostly) compatible with java.util.Stack.  As in java, this
 
185
 * is merely a paper thin layer around UVector.  See the UVector
 
186
 * documentation for further information.
 
187
 *
 
188
 * <p><b>Design notes</b>
 
189
 *
 
190
 * <p>The element at index <tt>n-1</tt> is (of course) the top of the
 
191
 * stack.
 
192
 *
 
193
 * <p>The poorly named <tt>empty()</tt> method doesn't empty the
 
194
 * stack; it determines if the stack is empty.
 
195
 *
 
196
 * @author Alan Liu
 
197
 */
 
198
class U_COMMON_API UStack : public UVector {
 
199
public:
 
200
    UStack(UErrorCode &status);
 
201
 
 
202
    UStack(int32_t initialCapacity, UErrorCode &status);
 
203
 
 
204
    UStack(UObjectDeleter d, UKeyComparator c, UErrorCode &status);
 
205
 
 
206
    UStack(UObjectDeleter d, UKeyComparator c, int32_t initialCapacity, UErrorCode &status);
 
207
 
 
208
    // It's okay not to have a virtual destructor (in UVector)
 
209
    // because UStack has no special cleanup to do.
 
210
 
 
211
    UBool empty(void) const;
 
212
 
 
213
    void* peek(void) const;
 
214
    
 
215
    void* pop(void);
 
216
    
 
217
    int32_t popi(void);
 
218
    
 
219
    void* push(void* obj, UErrorCode &status);
 
220
 
 
221
    int32_t push(int32_t i, UErrorCode &status);
 
222
 
 
223
    int32_t search(void* obj) const;
 
224
 
 
225
private:
 
226
    // Disallow
 
227
    UStack(const UStack&);
 
228
 
 
229
    // Disallow
 
230
    UStack& operator=(const UStack&);
 
231
};
 
232
 
 
233
 
 
234
// UVector inlines
 
235
 
 
236
inline int32_t UVector::size(void) const {
 
237
    return count;
 
238
}
 
239
 
 
240
inline UBool UVector::isEmpty(void) const {
 
241
    return count == 0;
 
242
}
 
243
 
 
244
inline UBool UVector::contains(void* obj) const {
 
245
    return indexOf(obj) >= 0;
 
246
}
 
247
 
 
248
inline void* UVector::firstElement(void) const {
 
249
    return elementAt(0);
 
250
}
 
251
 
 
252
inline void* UVector::lastElement(void) const {
 
253
    return elementAt(count-1);
 
254
}
 
255
 
 
256
inline void* UVector::operator[](int32_t index) const {
 
257
    return elementAt(index);
 
258
}
 
259
 
 
260
// UStack inlines
 
261
 
 
262
inline UBool UStack::empty(void) const {
 
263
    return isEmpty();
 
264
}
 
265
 
 
266
inline void* UStack::peek(void) const {
 
267
    return lastElement();
 
268
}
 
269
 
 
270
inline void* UStack::push(void* obj, UErrorCode &status) {
 
271
    addElement(obj, status);
 
272
    return obj;
 
273
}
 
274
 
 
275
inline int32_t UStack::push(int32_t i, UErrorCode &status) {
 
276
    addElement(i, status);
 
277
    return i;
 
278
}
 
279
 
 
280
U_NAMESPACE_END
 
281
 
 
282
#endif