home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / common / uvector.h < prev   
Encoding:
C/C++ Source or Header  |  1999-10-27  |  6.2 KB  |  242 lines

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