home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / common / uvector.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  4.0 KB  |  178 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.
  7. **********************************************************************
  8. */
  9.  
  10. #include "uvector.h"
  11. #include "cmemory.h"
  12.  
  13. bool_t UVector::outOfMemory = FALSE;
  14.  
  15. UVector::UVector(int32_t initialCapacity) :
  16.     capacity(0),
  17.     count(0),
  18.     elements(0),
  19.     deleter(0),
  20.     comparer(0) {
  21.  
  22.     _init(initialCapacity);
  23. }
  24.  
  25. UVector::UVector(Deleter d, Comparer c, int32_t initialCapacity) :
  26.     capacity(0),
  27.     count(0),
  28.     elements(0),
  29.     deleter(d),
  30.     comparer(c) {
  31.    
  32.     _init(initialCapacity);
  33. }
  34.  
  35. void UVector::_init(int32_t initialCapacity) {
  36.     elements = new void*[initialCapacity];
  37.     if (elements == 0) {
  38.         outOfMemory = TRUE;
  39.     } else {
  40.         capacity = initialCapacity;
  41.     }
  42. }
  43.  
  44. UVector::~UVector() {
  45.     removeAllElements();
  46.     delete[] elements;
  47.     elements = 0;
  48. }
  49.  
  50. void UVector::addElement(void* obj) {
  51.     if (ensureCapacity(count)) {
  52.         elements[count++] = obj;
  53.     }
  54. }
  55.  
  56. void UVector::setElementAt(void* obj, int32_t index) {
  57.     if (0 <= index && index < count) {
  58.         if (elements[index] != 0 && deleter != 0) {
  59.             (*deleter)(elements[index]);
  60.         }
  61.         elements[index] = obj;
  62.     }
  63.     /* else index out of range */
  64. }
  65.  
  66. void UVector::insertElementAt(void* obj, int32_t index) {
  67.     // must have 0 <= index <= count
  68.     if (0 <= index && index <= count && ensureCapacity(count)) {
  69.         for (int32_t i=count; i>index; --i) {
  70.             elements[i] = elements[i-1];
  71.         }
  72.         elements[index] = obj;
  73.     }
  74.     /* else index out of range */
  75. }
  76.  
  77. void* UVector::elementAt(int32_t index) const {
  78.     return (0 <= index && index < count) ? elements[index] : 0;
  79. }
  80.  
  81. void UVector::removeElementAt(int32_t index) {
  82.     if (0 <= index && index < count) {
  83.         if (deleter != 0) {
  84.             (*deleter)(elements[index]);
  85.         }
  86.         for (int32_t i=index; i<count; ++i) {
  87.             elements[i] = elements[i+1];
  88.         }
  89.     }
  90.     /* else index out of range */
  91. }
  92.  
  93. bool_t UVector::removeElement(void* obj) {
  94.     int32_t i = indexOf(obj);
  95.     if (i >= 0) {
  96.         removeElementAt(i);
  97.         return TRUE;
  98.     }
  99.     return FALSE;
  100. }
  101.  
  102. void UVector::removeAllElements() {
  103.     if (deleter != 0) {
  104.         for (int32_t i=0; i<count; ++i) {
  105.             if (elements[i] != 0) {
  106.                 (*deleter)(elements[i]);
  107.             }
  108.         }
  109.     }
  110.     count = 0;
  111. }
  112.  
  113. int32_t UVector::indexOf(void* obj, int32_t startIndex) const {
  114.     if (comparer != 0) {
  115.         for (int32_t i=startIndex; i<count; ++i) {
  116.             if ((*comparer)(obj, elements[i])) {
  117.                 return i;
  118.             }
  119.         }
  120.     }
  121.     return -1;
  122. }
  123.  
  124. bool_t UVector::ensureCapacity(int32_t minimumCapacity) {
  125.     if (capacity >= minimumCapacity) {
  126.         return TRUE;
  127.     } else {
  128.         int32_t newCap = capacity * 2;
  129.         void** newElems = new void*[newCap];
  130.         if (newElems == 0) {
  131.             outOfMemory = TRUE;
  132.             return FALSE;
  133.         }
  134.         icu_memcpy(newElems, elements, sizeof(void*) * count);
  135.         delete[] elements;
  136.         elements = newElems;
  137.         capacity = newCap;
  138.         return TRUE;
  139.     }
  140. }
  141.  
  142. UVector::Deleter UVector::setDeleter(Deleter d) {
  143.     Deleter old = deleter;
  144.     deleter = d;
  145.     return old;
  146. }
  147.  
  148. UVector::Comparer UVector::setComparer(Comparer d) {
  149.     Comparer old = comparer;
  150.     comparer = d;
  151.     return old;
  152. }
  153.  
  154. bool_t UVector::isOutOfMemory() {
  155.     return outOfMemory;
  156. }
  157.  
  158. UStack::UStack(int32_t initialCapacity) :
  159.     UVector(initialCapacity) {
  160. }
  161.  
  162. UStack::UStack(Deleter d, Comparer c, int32_t initialCapacity) :
  163.     UVector(d, c, initialCapacity) {
  164. }
  165.  
  166. void* UStack::pop() {
  167.     void* obj = lastElement();
  168.     if (obj != 0) {
  169.         removeElementAt(size() - 1);
  170.     }
  171.     return obj;
  172. }
  173.  
  174. int32_t UStack::search(void* obj) const {
  175.     int32_t i = indexOf(obj);
  176.     return (i >= 0) ? size() - i : i;
  177. }
  178.