1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/*
**********************************************************************
* Copyright (C) 1999-2006, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Date Name Description
* 10/22/99 alan Creation. This is an internal header.
* It should not be exported.
**********************************************************************
*/
#ifndef UVECTOR_H
#define UVECTOR_H
#include "unicode/utypes.h"
#include "unicode/uobject.h"
#include "uhash.h"
U_NAMESPACE_BEGIN
/**
* A token comparison function.
* @param tok1 A token (object or integer)
* @param tok2 A token (object or integer)
* @return 0 if the two tokens are equal, -1 if tok1 is < tok2, or
* +1 if tok1 is > tok2.
*/
typedef int8_t U_CALLCONV USortComparator(UHashTok tok1,
UHashTok tok2);
/**
* A token assignment function. It may copy an integer, copy
* a pointer, or clone a pointer, as appropriate.
* @param dst The token to be assigned to
* @param src The token to assign from
*/
typedef void U_CALLCONV UTokenAssigner(UHashTok *dst,
UHashTok *src);
/**
* <p>Ultralightweight C++ implementation of a <tt>void*</tt> vector
* that is (mostly) compatible with java.util.Vector.
*
* <p>This is a very simple implementation, written to satisfy an
* immediate porting need. As such, it is not completely fleshed out,
* and it aims for simplicity and conformity. Nonetheless, it serves
* its purpose (porting code from java that uses java.util.Vector)
* well, and it could be easily made into a more robust vector class.
*
* <p><b>Design notes</b>
*
* <p>There is index bounds checking, but little is done about it. If
* indices are out of bounds, either nothing happens, or zero is
* returned. We <em>do</em> avoid indexing off into the weeds.
*
* <p>There is detection of out of memory, but the handling is very
* coarse-grained -- similar to UnicodeString's protocol, but even
* coarser. The class contains <em>one static flag</em> that is set
* when any call to <tt>new</tt> returns zero. This allows the caller
* to use several vectors and make just one check at the end to see if
* a memory failure occurred. This is more efficient than making a
* check after each call on each vector when doing many operations on
* multiple vectors. The single static flag works best when memory
* failures are infrequent, and when recovery options are limited or
* nonexistent.
*
* <p>Since we don't have garbage collection, UVector was given the
* option to <em>own</em>its contents. To employ this, set a deleter
* function. The deleter is called on a void* pointer when that
* pointer is released by the vector, either when the vector itself is
* destructed, or when a call to setElementAt() overwrites an element,
* or when a call to remove() or one of its variants explicitly
* removes an element. If no deleter is set, or the deleter is set to
* zero, then it is assumed that the caller will delete elements as
* needed.
*
* <p>In order to implement methods such as contains() and indexOf(),
* UVector needs a way to compare objects for equality. To do so, it
* uses a comparison frunction, or "comparer." If the comparer is not
* set, or is set to zero, then all such methods will act as if the
* vector contains no element. That is, indexOf() will always return
* -1, contains() will always return FALSE, etc.
*
* <p><b>To do</b>
*
* <p>Improve the handling of index out of bounds errors.
*
* @author Alan Liu
*/
class U_COMMON_API UVector : public UObject {
// NOTE: UVector uses the UHashKey (union of void* and int32_t) as
// its basic storage type. It uses UKeyComparator as its
// comparison function. It uses UObjectDeleter as its deleter
// function. These are named for hashtables, but used here as-is
// rather than duplicating the type. This allows sharing of
// support functions.
private:
int32_t count;
int32_t capacity;
UHashTok* elements;
UObjectDeleter *deleter;
UKeyComparator *comparer;
public:
UVector(UErrorCode &status);
UVector(int32_t initialCapacity, UErrorCode &status);
UVector(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status);
UVector(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status);
virtual ~UVector();
/**
* Assign this object to another (make this a copy of 'other').
* Use the 'assign' function to assign each element.
*/
void assign(const UVector& other, UTokenAssigner *assign, UErrorCode &ec);
/**
* Compare this vector with another. They will be considered
* equal if they are of the same size and all elements are equal,
* as compared using this object's comparer.
*/
UBool operator==(const UVector& other);
/**
* Equivalent to !operator==()
*/
inline UBool operator!=(const UVector& other);
//------------------------------------------------------------
// java.util.Vector API
//------------------------------------------------------------
void addElement(void* obj, UErrorCode &status);
void addElement(int32_t elem, UErrorCode &status);
void setElementAt(void* obj, int32_t index);
void setElementAt(int32_t elem, int32_t index);
void insertElementAt(void* obj, int32_t index, UErrorCode &status);
void insertElementAt(int32_t elem, int32_t index, UErrorCode &status);
void* elementAt(int32_t index) const;
int32_t elementAti(int32_t index) const;
UBool equals(const UVector &other) const;
void* firstElement(void) const;
void* lastElement(void) const;
int32_t lastElementi(void) const;
int32_t indexOf(void* obj, int32_t startIndex = 0) const;
int32_t indexOf(int32_t obj, int32_t startIndex = 0) const;
UBool contains(void* obj) const;
UBool contains(int32_t obj) const;
UBool containsAll(const UVector& other) const;
UBool removeAll(const UVector& other);
UBool retainAll(const UVector& other);
void removeElementAt(int32_t index);
UBool removeElement(void* obj);
void removeAllElements();
int32_t size(void) const;
UBool isEmpty(void) const;
UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status);
/**
* Change the size of this vector as follows: If newSize is
* smaller, then truncate the array, possibly deleting held
* elements for i >= newSize. If newSize is larger, grow the
* array, filling in new slots with NULL.
*/
void setSize(int32_t newSize);
/**
* Fill in the given array with all elements of this vector.
*/
void** toArray(void** result) const;
//------------------------------------------------------------
// New API
//------------------------------------------------------------
UObjectDeleter *setDeleter(UObjectDeleter *d);
UKeyComparator *setComparer(UKeyComparator *c);
void* operator[](int32_t index) const;
/**
* Removes the element at the given index from this vector and
* transfer ownership of it to the caller. After this call, the
* caller owns the result and must delete it and the vector entry
* at 'index' is removed, shifting all subsequent entries back by
* one index and shortening the size of the vector by one. If the
* index is out of range or if there is no item at the given index
* then 0 is returned and the vector is unchanged.
*/
void* orphanElementAt(int32_t index);
/**
* Returns true if this vector contains none of the elements
* of the given vector.
* @param other vector to be checked for containment
* @return true if the test condition is met
*/
UBool containsNone(const UVector& other) const;
/**
* Insert the given object into this vector at its sorted position
* as defined by 'compare'. The current elements are assumed to
* be sorted already.
*/
void sortedInsert(void* obj, USortComparator *compare, UErrorCode& ec);
/**
* Insert the given integer into this vector at its sorted position
* as defined by 'compare'. The current elements are assumed to
* be sorted already.
*/
void sortedInsert(int32_t obj, USortComparator *compare, UErrorCode& ec);
/**
* ICU "poor man's RTTI", returns a UClassID for this class.
*/
static UClassID U_EXPORT2 getStaticClassID();
/**
* ICU "poor man's RTTI", returns a UClassID for the actual class.
*/
virtual UClassID getDynamicClassID() const;
private:
void _init(int32_t initialCapacity, UErrorCode &status);
int32_t indexOf(UHashTok key, int32_t startIndex = 0, int8_t hint = 0) const;
void sortedInsert(UHashTok tok, USortComparator *compare, UErrorCode& ec);
// Disallow
UVector(const UVector&);
// Disallow
UVector& operator=(const UVector&);
};
/**
* <p>Ultralightweight C++ implementation of a <tt>void*</tt> stack
* that is (mostly) compatible with java.util.Stack. As in java, this
* is merely a paper thin layer around UVector. See the UVector
* documentation for further information.
*
* <p><b>Design notes</b>
*
* <p>The element at index <tt>n-1</tt> is (of course) the top of the
* stack.
*
* <p>The poorly named <tt>empty()</tt> method doesn't empty the
* stack; it determines if the stack is empty.
*
* @author Alan Liu
*/
class U_COMMON_API UStack : public UVector {
public:
UStack(UErrorCode &status);
UStack(int32_t initialCapacity, UErrorCode &status);
UStack(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status);
UStack(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status);
virtual ~UStack();
// It's okay not to have a virtual destructor (in UVector)
// because UStack has no special cleanup to do.
UBool empty(void) const;
void* peek(void) const;
int32_t peeki(void) const;
void* pop(void);
int32_t popi(void);
void* push(void* obj, UErrorCode &status);
int32_t push(int32_t i, UErrorCode &status);
/*
If the object o occurs as an item in this stack,
this method returns the 1-based distance from the top of the stack.
*/
int32_t search(void* obj) const;
/**
* ICU "poor man's RTTI", returns a UClassID for this class.
*/
static UClassID U_EXPORT2 getStaticClassID();
/**
* ICU "poor man's RTTI", returns a UClassID for the actual class.
*/
virtual UClassID getDynamicClassID() const;
private:
// Disallow
UStack(const UStack&);
// Disallow
UStack& operator=(const UStack&);
};
// UVector inlines
inline int32_t UVector::size(void) const {
return count;
}
inline UBool UVector::isEmpty(void) const {
return count == 0;
}
inline UBool UVector::contains(void* obj) const {
return indexOf(obj) >= 0;
}
inline UBool UVector::contains(int32_t obj) const {
return indexOf(obj) >= 0;
}
inline void* UVector::firstElement(void) const {
return elementAt(0);
}
inline void* UVector::lastElement(void) const {
return elementAt(count-1);
}
inline int32_t UVector::lastElementi(void) const {
return elementAti(count-1);
}
inline void* UVector::operator[](int32_t index) const {
return elementAt(index);
}
inline UBool UVector::operator!=(const UVector& other) {
return !operator==(other);
}
// UStack inlines
inline UBool UStack::empty(void) const {
return isEmpty();
}
inline void* UStack::peek(void) const {
return lastElement();
}
inline int32_t UStack::peeki(void) const {
return lastElementi();
}
inline void* UStack::push(void* obj, UErrorCode &status) {
addElement(obj, status);
return obj;
}
inline int32_t UStack::push(int32_t i, UErrorCode &status) {
addElement(i, status);
return i;
}
U_NAMESPACE_END
#endif
|