home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osp1.exe / src / text / indxcoll.cpp next >
Text File  |  1997-04-02  |  5KB  |  198 lines

  1. // @(#) 1.3 com/src/samples/text/indxcoll.cpp, odtextpart, od96os2, odos29712d 2/3/97 16:50:27 [3/21/97 17:49:31]
  2.  
  3. #include "indxcoll.hpp"
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #ifdef ODDebug
  7. #include <ODDebug.h>
  8. #endif
  9.  
  10. // Constructor and Destructor
  11. IndexedCollection::IndexedCollection()
  12. {
  13.    maxElements = 20;
  14.    numElements = 0;
  15.    elements = (ElementType*) calloc(maxElements, ElementSize);
  16. }
  17.  
  18. IndexedCollection::~IndexedCollection()
  19. {
  20.    free(elements);
  21. }
  22.  
  23. // Retrieval Methods
  24. int IndexedCollection::Count() const
  25. {
  26.    return numElements;
  27. }
  28.  
  29. ElementType  IndexedCollection::First()
  30. {
  31.    if (numElements > 0)
  32.      return elements[0];
  33.    else
  34.      return NULL;
  35. }
  36.  
  37. ElementType  IndexedCollection::FromIndex(int index)
  38. {
  39.    if (index < numElements) {
  40.       return elements[index];
  41.    } else {
  42.       return NULL;
  43.    } /* endif */
  44. }
  45.  
  46. ElementType  IndexedCollection::Next(int index)
  47. {
  48.    if (index < (numElements-1)) {
  49.       return elements[index+1];
  50.    } else {
  51.       return NULL;
  52.    } /* endif */
  53. }
  54. ElementType  IndexedCollection::Previous(int index)
  55. {
  56.    if (index < numElements && index != 0) {
  57.      return elements[index-1];
  58.    } else {
  59.       return NULL;
  60.    } /* endif */
  61. }
  62. ElementType  IndexedCollection::Last()
  63. {
  64.    if (numElements > 0) {
  65.      return elements[numElements -1];
  66.    } else {
  67.      return NULL;
  68.    } /* endif */
  69. }
  70.  
  71. // Addition/Deletion methods
  72.  
  73. void IndexedCollection::AddLast(int numToAdd, ElementType* elemsToAdd)
  74. {
  75. #ifdef ODDebug
  76.        PRINT("INDXCOLL.CPP: AddLast invoked.\n");
  77. #endif
  78.    if ((numElements + numToAdd) > maxElements) {
  79.       ElementType* tempElements = AllocElements(numToAdd);
  80.       memcpy(tempElements, elements, numElements * ElementSize);
  81.       free(elements);
  82.       elements = tempElements;
  83.    } /* endif */
  84.  
  85.    memcpy(&elements[numElements], elemsToAdd, numToAdd * ElementSize);
  86.    numElements += numToAdd;
  87. }
  88.  
  89. void IndexedCollection::AddBefore(int index, int numToAdd, ElementType* elemsToAdd)
  90. {
  91. #ifdef ODDebug
  92.        PRINT("INDXCOLL.CPP: AddBefore invoked.\n");
  93. #endif
  94.    ElementType* tempElements;
  95.  
  96.    // Ensure the index is valid. Invoke AddLast if the index corresponds
  97.    // to the next free element.
  98.    if (index == numElements) {
  99.       AddLast(numToAdd, elemsToAdd);
  100.    } else if (index < numElements) {
  101.        // Allocate a new buffer to copy the old and new data in the proper sequence
  102.        if ((numElements + numToAdd) > maxElements) {
  103.           tempElements = AllocElements(numToAdd);
  104.        } else {
  105.           tempElements = (ElementType*) calloc(maxElements, ElementSize);
  106.        } /* endif */
  107.  
  108.        // Copy the data from the old buffer up to the index
  109.        memcpy(tempElements, elements, index * ElementSize);
  110.        // Copy in the new data
  111.        memcpy(&tempElements[index], elemsToAdd, numToAdd * ElementSize);
  112.        // Copy the data from the old buffer from the index beyond
  113.        memcpy(&tempElements[index+numToAdd], &elements[index], (numElements - index) * ElementSize);
  114.  
  115.        // Replace the old buffer with the new buffer
  116.        free(elements);
  117.        elements = tempElements;
  118.        numElements += numToAdd;
  119.    } else {
  120. #ifdef ODDebug
  121.        PRINT("INDXCOLL.CPP: Index %d is greater than list size %d.\n",index,numElements);
  122. #endif
  123.    } /* endif */
  124.  
  125. }
  126.  
  127. ElementType  IndexedCollection::RemoveLast()
  128. {
  129. #ifdef ODDebug
  130.        PRINT("INDXCOLL.CPP: RemoveLast invoked.\n");
  131. #endif
  132.    ElementType returnElement = NULL;
  133.  
  134.    if (numElements > 0) {
  135.      returnElement = elements[--numElements];
  136.      elements[numElements] = NULL;
  137.    } /* endif */
  138.  
  139.    return returnElement;
  140. }
  141.  
  142. ElementType IndexedCollection::RemoveFrom(int index)
  143. {
  144. #ifdef ODDebug
  145.        PRINT("INDXCOLL.CPP: RemoveFrom invoked.\n");
  146. #endif
  147.    ElementType returnElement = NULL;
  148.  
  149.    if (index < numElements) {
  150.      returnElement = elements[index];
  151.      memcpy(&elements[index], &elements[index+1], (--numElements - index) * ElementSize);
  152.      elements[numElements] = NULL;
  153.    }
  154.  
  155.    return returnElement;
  156. }
  157.  
  158. ElementType* IndexedCollection::RemoveFromTo(int lowIndex, int highIndex)
  159. {
  160. #ifdef ODDebug
  161.        PRINT("INDXCOLL.CPP: RemoveFromTo invoked.\n");
  162. #endif
  163.    ElementType* tempElements = NULL;
  164.    int removeElem;
  165.  
  166.    if (lowIndex < highIndex)
  167.    {
  168.      removeElem = highIndex-lowIndex+1;
  169.      tempElements = (ElementType*) calloc(removeElem, ElementSize);
  170.      memcpy(tempElements, &elements[lowIndex], removeElem * ElementSize);
  171.  
  172.      if (highIndex == numElements-1) {
  173.         memset(&elements[lowIndex], 0, removeElem * ElementSize);
  174.         numElements = lowIndex;
  175.      } else {
  176.         memcpy(&elements[lowIndex], &elements[highIndex+1], (numElements-1 - highIndex) * ElementSize);
  177.         memset(&elements[numElements - removeElem], 0, removeElem * ElementSize);
  178.         numElements -= removeElem;
  179.      } /* endif */
  180.    }
  181.  
  182.    return tempElements;
  183. }
  184.  
  185. // Private Methods
  186. ElementType* IndexedCollection::AllocElements(int numElem)
  187. {
  188. #ifdef ODDebug
  189.        PRINT("INDXCOLL.CPP: AllocElements invoked.\n");
  190. #endif
  191.    // Allocate some new buffer space to accommodate future additions
  192.    if (numElem > 1)
  193.      maxElements += numElem;
  194.    maxElements += 20;
  195.    ElementType* tempElements = (ElementType*) calloc(maxElements, ElementSize);
  196.    return tempElements;
  197. }
  198.