+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*
**********************************************************************
-* Copyright (C) 1999-2003, International Business Machines
+* Copyright (C) 1999-2016, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Date Name Description
#include "unicode/utypes.h"
#include "unicode/uobject.h"
-#include "uhash.h"
+#include "cmemory.h"
+#include "uarrsort.h"
+#include "uelement.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>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
+ * uses a comparison function, 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.
*/
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
+ // its basic storage type. It uses UElementsAreEqual 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
int32_t capacity;
- UHashTok* elements;
+ UElement* elements;
UObjectDeleter *deleter;
- UKeyComparator *comparer;
+ UElementsAreEqual *comparer;
public:
UVector(UErrorCode &status);
UVector(int32_t initialCapacity, UErrorCode &status);
- UVector(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status);
+ UVector(UObjectDeleter *d, UElementsAreEqual *c, UErrorCode &status);
- UVector(UObjectDeleter *d, UKeyComparator *c, int32_t initialCapacity, UErrorCode &status);
+ UVector(UObjectDeleter *d, UElementsAreEqual *c, int32_t initialCapacity, UErrorCode &status);
- ~UVector();
+ 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);
+ void assign(const UVector& other, UElementAssigner *assign, UErrorCode &ec);
/**
* Compare this vector with another. They will be considered
UBool equals(const UVector &other) const;
- void* firstElement(void) const;
+ inline void* firstElement(void) const;
- void* lastElement(void) const;
+ inline void* lastElement(void) const;
- int32_t lastElementi(void) const;
+ inline 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;
+ inline UBool contains(void* obj) const;
- UBool contains(int32_t obj) const;
+ inline UBool contains(int32_t obj) const;
UBool containsAll(const UVector& other) const;
void removeAllElements();
- int32_t size(void) const;
+ inline int32_t size(void) const;
- UBool isEmpty(void) const;
+ inline 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 slows with NULL.
+ * array, filling in new slots with NULL.
*/
- void setSize(int32_t newSize);
+ void setSize(int32_t newSize, UErrorCode &status);
/**
* Fill in the given array with all elements of this vector.
UObjectDeleter *setDeleter(UObjectDeleter *d);
- UKeyComparator *setComparer(UKeyComparator *c);
+ UElementsAreEqual *setComparer(UElementsAreEqual *c);
- void* operator[](int32_t index) const;
+ inline void* operator[](int32_t index) const;
/**
* Removes the element at the given index from this vector and
* as defined by 'compare'. The current elements are assumed to
* be sorted already.
*/
- void sortedInsert(void* obj, USortComparator *compare, UErrorCode& ec);
+ void sortedInsert(void* obj, UElementComparator *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);
+ void sortedInsert(int32_t obj, UElementComparator *compare, UErrorCode& ec);
+
+ /**
+ * Sort the contents of the vector, assuming that the contents of the
+ * vector are of type int32_t.
+ */
+ void sorti(UErrorCode &ec);
+
+ /**
+ * Sort the contents of this vector, using a caller-supplied function
+ * to do the comparisons. (It's confusing that
+ * UVector's UElementComparator function is different from the
+ * UComparator function type defined in uarrsort.h)
+ */
+ void sort(UElementComparator *compare, UErrorCode &ec);
+
+ /**
+ * Stable sort the contents of this vector using a caller-supplied function
+ * of type UComparator to do the comparison. Provides more flexibility
+ * than UVector::sort() because an additional user parameter can be passed to
+ * the comparison function.
+ */
+ void sortWithUComparator(UComparator *compare, const void *context, UErrorCode &ec);
/**
* ICU "poor man's RTTI", returns a UClassID for this class.
- *
- * @draft ICU 2.2
*/
- static inline UClassID getStaticClassID() { return (UClassID)&fgClassID; }
+ static UClassID U_EXPORT2 getStaticClassID();
/**
* ICU "poor man's RTTI", returns a UClassID for the actual class.
- *
- * @draft ICU 2.2
*/
- virtual inline UClassID getDynamicClassID() const { return getStaticClassID(); }
+ 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;
+ int32_t indexOf(UElement key, int32_t startIndex = 0, int8_t hint = 0) const;
- void sortedInsert(UHashTok tok, USortComparator *compare, UErrorCode& ec);
+ void sortedInsert(UElement e, UElementComparator *compare, UErrorCode& ec);
// Disallow
UVector(const UVector&);
// Disallow
UVector& operator=(const UVector&);
- /**
- * The address of this static class variable serves as this class's ID
- * for ICU "poor man's RTTI".
- */
- static const char fgClassID;
};
UStack(int32_t initialCapacity, UErrorCode &status);
- UStack(UObjectDeleter *d, UKeyComparator *c, UErrorCode &status);
+ UStack(UObjectDeleter *d, UElementsAreEqual *c, UErrorCode &status);
+
+ UStack(UObjectDeleter *d, UElementsAreEqual *c, int32_t initialCapacity, 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;
+ inline UBool empty(void) const;
- void* peek(void) const;
+ inline void* peek(void) const;
- int32_t peeki(void) const;
+ inline int32_t peeki(void) const;
void* pop(void);
int32_t popi(void);
- void* push(void* obj, UErrorCode &status);
+ inline void* push(void* obj, UErrorCode &status);
- int32_t push(int32_t i, UErrorCode &status);
+ inline 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.
- *
- * @draft ICU 2.2
*/
- static inline UClassID getStaticClassID() { return (UClassID)&fgClassID; }
+ static UClassID U_EXPORT2 getStaticClassID();
/**
* ICU "poor man's RTTI", returns a UClassID for the actual class.
- *
- * @draft ICU 2.2
*/
- virtual inline UClassID getDynamicClassID() const { return getStaticClassID(); }
+ virtual UClassID getDynamicClassID() const;
private:
// Disallow
// Disallow
UStack& operator=(const UStack&);
-
- /**
- * The address of this static class variable serves as this class's ID
- * for ICU "poor man's RTTI".
- */
- static const char fgClassID;
};