home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / i18n / tables.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  34.9 KB  |  1,462 lines

  1. /*
  2. *****************************************************************************************
  3. *                                                                                       *
  4. * COPYRIGHT:                                                                            *
  5. *   (C) Copyright Taligent, Inc.,  1996                                                 *
  6. *   (C) Copyright International Business Machines Corporation,  1996-1998               *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.                  *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure             *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                              *
  10. *                                                                                       *
  11. *****************************************************************************************
  12. */
  13. //===============================================================================
  14. //
  15. // File tables.cpp
  16. //
  17. // Contains:
  18. //  EntryPair               - Represents a contracting-character string
  19. //  PointerToPatternEntry   - a smart pointer to a PatternEntry
  20. //
  21. //  VectorOfInt             - Dynamic array classes, in lieu of templates
  22. //  VectorOfPointer 
  23. //  VectorOfPToExpandTable
  24. //  VectorOfPToContractElement
  25. //  VectorOfPointersToPatternEntry
  26. //              
  27. // All of these classes are fairly small and self-explanatory, so they don't
  28. // contain too many internal comments.
  29. //
  30. // Created by: Helena Shih
  31. //
  32. // Modification History:
  33. //
  34. //  Date        Name        Description
  35. //  2/5/97      aliu        Added streamIn and streamOut methods to EntryPair,
  36. //                          VectorOfInt, VectorOfPToExpandTable, VectorOfPToContractElement,
  37. //                          VectorOfPToContractTable.  These are used by TableCollation
  38. //                          streamIn and streamOut methods.
  39. //  2/11/97     aliu        Moved declarations out of for loop initializer.
  40. //  3/5/97      aliu        Made VectorOfPointersToPatternEntry::at() inline.
  41. //  6/18/97     helena      Added VectorOfPointer for MergeCollation.
  42. //  6/23/97     helena      Added comments to make code more readable.  Since 
  43. //                          this is converted from a templatized version, the comment
  44. //                          is only added to one class.
  45. //  8/04/98        erm            Added EntryPair::fwd.
  46. //===============================================================================
  47. #ifndef _TABLES
  48. #include "tables.h"
  49. #endif
  50.  
  51. #ifndef _PTNENTRY
  52. #include "ptnentry.h"
  53. #endif
  54.  
  55. #ifndef _FILESTRM
  56. #include "filestrm.h"
  57. #endif
  58.  
  59. #ifndef _UNISTRM
  60. #include "unistrm.h"
  61. #endif
  62.  
  63. //=======================================================================================
  64. // METHODS ON EntryPair
  65. //=======================================================================================
  66.  
  67. void EntryPair::streamOut(FileStream* os) const
  68. {
  69.     if (!T_FileStream_error(os))
  70.     {
  71.         UnicodeStringStreamer::streamOut(&entryName, os);
  72.         T_FileStream_write(os, &value, sizeof(value));
  73.         T_FileStream_write(os, &fwd, sizeof(fwd));
  74.     }
  75. }
  76.  
  77. void EntryPair::streamIn(FileStream* is)
  78. {
  79.     if (!T_FileStream_error(is))
  80.     {
  81.         UnicodeStringStreamer::streamIn(&entryName, is);
  82.         T_FileStream_read(is, &value, sizeof(value));
  83.         T_FileStream_read(is, &fwd, sizeof(fwd));
  84.     }
  85. }
  86.  
  87. //=======================================================================================
  88. // METHODS ON VectorOfInt
  89. //=======================================================================================
  90.  
  91. VectorOfInt::VectorOfInt(int32_t    initialSize)
  92. : fSize(0),
  93.   fCapacity(0),
  94.   fElements(0),
  95.   fBogus(FALSE)
  96. {
  97.     if (initialSize != 0) {
  98.         resize(initialSize);
  99.         if (fBogus) return;
  100.     }
  101. }
  102.  
  103. // Copy constructor
  104. VectorOfInt::VectorOfInt(const VectorOfInt& that)
  105. : fSize(that.fSize),
  106.   fCapacity(that.fCapacity),
  107.   fElements(0),
  108.   fBogus(FALSE)
  109. {
  110.     fElements = new int32_t[fCapacity];
  111.     if (!fElements) {
  112.         fBogus = TRUE;
  113.         return;
  114.     }
  115.     int32_t*    to = fElements;
  116.     int32_t*    from = that.fElements;
  117.     int32_t*    end = &(fElements[fCapacity]);
  118.  
  119.     while (to < end)
  120.         *to++ = *from++;
  121. }
  122.  
  123. VectorOfInt::~VectorOfInt()
  124. {
  125.     delete [] fElements;
  126. }
  127.  
  128. // assignment operator
  129. const VectorOfInt&
  130. VectorOfInt::operator=(const VectorOfInt&   that)
  131. {
  132.     if (this != &that) {
  133.         // resize if necessary
  134.         if (fCapacity < that.fSize) {
  135.             delete [] fElements;
  136.             fElements = 0;
  137.             fElements = new int32_t[that.fCapacity];
  138.             if (!fElements) { 
  139.                 fBogus = TRUE;
  140.                 return *this;
  141.             }
  142.             fCapacity = that.fCapacity;
  143.         }
  144.  
  145.         int32_t*    to = fElements;
  146.         int32_t*    from = that.fElements;
  147.         int32_t*    cutover = &(fElements[that.fCapacity]);
  148.         int32_t*    end = &(fElements[fCapacity]);
  149.  
  150.         while (to < cutover)
  151.             *to++ = *from++;
  152.         while (to < end)
  153.             *to++ = 0;
  154.  
  155.         fSize = that.fSize;
  156.     }
  157.     return *this;
  158. }
  159.  
  160. bool_t
  161. VectorOfInt::isBogus() const
  162. {
  163.     return fBogus;
  164. }
  165.  
  166. bool_t
  167. VectorOfInt::operator==(const VectorOfInt&  that)
  168. {
  169.     if (this == &that) return TRUE;
  170.     if (fSize != that.fSize) return FALSE;
  171.     for (int32_t i = 0; i < fSize; i++) {
  172.         if (fElements[i] != that.fElements[i])
  173.             return FALSE;
  174.     }
  175.     return TRUE;
  176. }
  177.  
  178. bool_t
  179. VectorOfInt::operator!=(const VectorOfInt& that)
  180. {
  181.     return !(*this == that);
  182. }
  183.  
  184. // replace the element at the index
  185. void
  186. VectorOfInt::atPut( int32_t     index,
  187.                 const int32_t&  value)
  188. {
  189.     if (index >= fSize) {
  190.         if (index >= fCapacity) {
  191.             resize(index + 1);
  192.             if (fBogus) return;
  193.         }
  194.         else
  195.             fSize = index + 1;
  196.     }
  197.     fElements[index] = value;
  198. }
  199.  
  200. // insert the element at the index, shift down the following elements
  201. void
  202. VectorOfInt::atInsert(  int32_t         index,
  203.                         const   int32_t&    value)
  204. {
  205.     if (fSize + 1 >= fCapacity) {
  206.         resize(fSize + 1);
  207.         if (fBogus) return;
  208.     } else {
  209.         fSize++;
  210.     }
  211.     int32_t i;
  212.     for (i = fSize - 2 ; i >= index; i--)
  213.     {
  214.         fElements[i+1] = fElements[i];
  215.     }
  216.     fElements[index] = value;
  217. }
  218.  
  219. // Resize the element array.  Create a new array and copy the elements over 
  220. // then discard the old array.
  221. void
  222. VectorOfInt::resize(int32_t newSize)
  223. {
  224.     int32_t     newCapacity;
  225.  
  226.     newCapacity = newSize / GROWTH_RATE;
  227.     if (newCapacity < 10)
  228.         newCapacity = 10;
  229.     newCapacity += newSize;
  230.  
  231.     int32_t*    newArray = 0;
  232.     newArray    = new int32_t[newCapacity];
  233.     if (!newArray) {
  234.         fBogus = TRUE;
  235.         return;
  236.     }
  237.  
  238.     int32_t*    iter = newArray;
  239.     int32_t*    cutover = &(newArray[fCapacity]);
  240.     int32_t*    end = &(newArray[newCapacity]);
  241.     int32_t*    from = fElements;
  242.  
  243.     while (iter < cutover)
  244.         *iter++ = *from++;
  245.     while (iter < end)
  246.         *iter++ = 0;
  247.  
  248.     delete [] fElements;
  249.     fElements = newArray;
  250.     fSize = newSize;
  251.     fCapacity = newCapacity;
  252. }
  253.  
  254. // Do not detect the out of bounds error, try to do the right thing
  255. // by resizing the array.
  256. int32_t&
  257. VectorOfInt::operator[](int32_t index)
  258. {
  259.     if (index >= fSize) {
  260.         if (index >= fCapacity) {
  261.             resize(index + 1);
  262.             if (fBogus) return fElements[0]; // HSYS : Is this correct?
  263.         }
  264.         else
  265.             fSize = index + 1;
  266.     }
  267.     return fElements[index];
  268. }
  269.  
  270. void
  271. VectorOfInt::streamOut(FileStream* os) const
  272. {
  273.     if (!T_FileStream_error(os))
  274.     {
  275.         T_FileStream_write(os, &fSize, sizeof(fSize));
  276.         T_FileStream_write(os, fElements, sizeof(*fElements) * fSize);
  277.     }
  278. }
  279.  
  280. void
  281. VectorOfInt::streamIn(FileStream* is)
  282. {
  283.     if (!T_FileStream_error(is))
  284.     {
  285.         int32_t newSize;
  286.         T_FileStream_read(is, &newSize, sizeof(newSize));
  287.         resize(newSize);
  288.         if (fBogus) return;
  289.         T_FileStream_read(is, fElements, sizeof(*fElements) * newSize);
  290.     }
  291. }
  292.  
  293. //=======================================================================================
  294. // METHODS ON VectorOfPointer
  295. //=======================================================================================
  296.  
  297. VectorOfPointer::VectorOfPointer(int32_t    initialSize)
  298. : fSize(0),
  299.   fCapacity(0),
  300.   fElements(0),
  301.   fBogus(FALSE)
  302. {
  303.     if (initialSize != 0) {
  304.         resize(initialSize);
  305.         if (fBogus) return;
  306.     }
  307. }
  308.  
  309. VectorOfPointer::VectorOfPointer(const VectorOfPointer& that)
  310. : fSize(that.fSize),
  311.   fCapacity(that.fCapacity),
  312.   fElements(0),
  313.   fBogus(FALSE)
  314. {
  315.     fElements = new void*[fCapacity];
  316.     if (!fElements) {
  317.         fBogus = TRUE;
  318.         return;
  319.     }
  320.     void**  to = fElements;
  321.     void**  from = that.fElements;
  322.     void**  end = &(fElements[fCapacity]);
  323.  
  324.     while (to < end)
  325.         *to++ = *from++;
  326. }
  327.  
  328. VectorOfPointer::~VectorOfPointer()
  329. {
  330.     delete [] fElements;
  331. }
  332.  
  333. const VectorOfPointer&
  334. VectorOfPointer::operator=(const VectorOfPointer&   that)
  335. {
  336.     if (this != &that) {
  337.         if (fCapacity < that.fSize) {
  338.             delete [] fElements;
  339.             fElements = 0;
  340.             fElements = new void*[that.fCapacity];
  341.             if (!fElements) { 
  342.                 fBogus = TRUE;
  343.                 return *this;
  344.             }
  345.             fCapacity = that.fCapacity;
  346.         }
  347.  
  348.         void**  to = fElements;
  349.         void**  from = that.fElements;
  350.         void**  cutover = &(fElements[that.fCapacity]);
  351.         void**  end = &(fElements[fCapacity]);
  352.  
  353.         while (to < cutover)
  354.             *to++ = *from++;
  355.         while (to < end)
  356.             *to++ = 0;
  357.  
  358.         fSize = that.fSize;
  359.     }
  360.     return *this;
  361. }
  362.  
  363. bool_t
  364.  
  365. VectorOfPointer::isBogus() const
  366. {
  367.     return fBogus;
  368. }
  369.  
  370. bool_t
  371. VectorOfPointer::operator==(const VectorOfPointer&  that)
  372. {
  373.     if (this == &that) return TRUE;
  374.     if (fSize != that.fSize) return FALSE;
  375.     for (int32_t i = 0; i < fSize; i++) {
  376.         if (fElements[i] != that.fElements[i])
  377.             return FALSE;
  378.     }
  379.     return TRUE;
  380. }
  381.  
  382. bool_t
  383. VectorOfPointer::operator!=(const VectorOfPointer& that)
  384. {
  385.     return !(*this == that);
  386. }
  387. void
  388. VectorOfPointer::atPut( int32_t     index,
  389.                         const void*&    value)
  390. {
  391.     if (index >= fSize) {
  392.         if (index >= fCapacity) {
  393.             resize(index + 1);
  394.             if (fBogus) return;
  395.         }
  396.         else
  397.             fSize = index + 1;
  398.     }
  399.     fElements[index] = (void*)value;
  400. }
  401.  
  402. void
  403. VectorOfPointer::atInsert(  int32_t         index,
  404.                             const   void*&  value)
  405. {
  406.     if (fSize + 1 >= fCapacity) {
  407.         resize(fSize + 1);
  408.         if (fBogus) return;
  409.     } else {
  410.         fSize++;
  411.     }
  412.     int32_t i;
  413.     for (i = fSize - 2 ; i >= index; i--)
  414.     {
  415.         fElements[i+1] = fElements[i];
  416.     }
  417.     fElements[index] = (void*)value;
  418. }
  419.  
  420. void
  421. VectorOfPointer::resize(int32_t newSize)
  422. {
  423.     int32_t     newCapacity;
  424.  
  425.     newCapacity = newSize / GROWTH_RATE;
  426.     if (newCapacity < 10)
  427.         newCapacity = 10;
  428.     newCapacity += newSize;
  429.  
  430.     void**  newArray = 0;
  431.     newArray    = new void*[newCapacity];
  432.     if (!newArray) {
  433.         fBogus = TRUE;
  434.         return;
  435.     }
  436.  
  437.     void**  iter = newArray;
  438.     void**  cutover = &(newArray[fCapacity]);
  439.     void**  end = &(newArray[newCapacity]);
  440.     void**  from = fElements;
  441.  
  442.     while (iter < cutover)
  443.         *iter++ = *from++;
  444.     while (iter < end)
  445.         *iter++ = 0;
  446.  
  447.     delete [] fElements;
  448.     fElements = newArray;
  449.     fSize = newSize;
  450.     fCapacity = newCapacity;
  451. }
  452.  
  453. void*&
  454. VectorOfPointer::operator[](int32_t index)
  455. {
  456.     if (index >= fSize) {
  457.         if (index >= fCapacity) {
  458.             resize(index + 1);
  459.             if (fBogus) return fElements[0]; // HSYS : Is this correct?
  460.         }
  461.         else
  462.             fSize = index + 1;
  463.     }
  464.     return fElements[index];
  465. }
  466. //=======================================================================================
  467. // METHODS ON VectorOfPToExpandTable
  468. //=======================================================================================
  469.  
  470. VectorOfPToExpandTable::VectorOfPToExpandTable(int32_t  initialSize)
  471. : fSize(0),
  472.   fCapacity(0),
  473.   fElements(0),
  474.   fBogus(FALSE)
  475. {
  476.     if (initialSize != 0) {
  477.         resize(initialSize);
  478.         if (fBogus) return;
  479.     }
  480. }
  481.  
  482. VectorOfPToExpandTable::VectorOfPToExpandTable(const VectorOfPToExpandTable& that)
  483. : fSize(that.fSize),
  484.   fCapacity(that.fCapacity),
  485.   fElements(0),
  486.   fBogus(FALSE)
  487. {
  488.     fElements = new VectorOfInt*[fCapacity];
  489.     if (!fElements) {
  490.         fBogus = TRUE;
  491.         return;
  492.     }
  493.     VectorOfInt**       to = fElements;
  494.     VectorOfInt**       from = that.fElements;
  495.     VectorOfInt**       end = &(fElements[fCapacity]);
  496.  
  497.     while (to < end) {
  498.         if (*from == 0)
  499.             *to++ = *from++;
  500.         else
  501.             // We actually DUPLICATE the items pointed to by "that"
  502.             *to = new VectorOfInt(*(*from++));
  503.             if ((*to)->isBogus()) {
  504.                 delete [] fElements;
  505.                 fElements = 0;
  506.                 return;
  507.             }
  508.             to++;
  509.     }
  510. }
  511.  
  512. VectorOfPToExpandTable::~VectorOfPToExpandTable()
  513. {
  514.     VectorOfInt**       iter = fElements;
  515.     VectorOfInt**       end = &(fElements[fSize]);
  516.  
  517.     while (iter < end)
  518.         delete *iter++;
  519.  
  520.     delete [] fElements;
  521. }
  522.  
  523. bool_t
  524. VectorOfPToExpandTable::isBogus() const
  525. {
  526.     return fBogus;
  527. }
  528.  
  529. const VectorOfPToExpandTable&
  530. VectorOfPToExpandTable::operator=(const VectorOfPToExpandTable& that)
  531. {
  532.     if (this != &that) {
  533.         if (fCapacity < that.fSize) {
  534.             delete [] fElements;
  535.             fElements = 0;
  536.             fElements = new VectorOfInt*[that.fCapacity];
  537.             if (!fElements) {
  538.                 fBogus = TRUE;
  539.                 return *this;
  540.             }
  541.             fCapacity = that.fCapacity;
  542.         }
  543.  
  544.         VectorOfInt**       to = fElements;
  545.         VectorOfInt**       from = that.fElements;
  546.         VectorOfInt**       cutover = &(fElements[that.fCapacity]);
  547.         VectorOfInt**       end = &(fElements[fCapacity]);
  548.  
  549.         while (to < cutover) {
  550.             delete *to;
  551.             if (*from == 0)
  552.                 *to++ = *from++;
  553.             else {
  554.                 *to = new VectorOfInt(*(*from++));
  555.                 if ((*to)->isBogus()) {
  556.                     delete [] fElements;
  557.                     fElements = 0;
  558.                     return *this;
  559.                 }
  560.                 to++;
  561.             }
  562.         }
  563.         while (to < end) {
  564.             delete *to;
  565.             *to++ = 0;
  566.         }
  567.  
  568.         fSize = that.fSize;
  569.     }
  570.     return *this;
  571. }
  572.  
  573. PToExpandTable
  574. VectorOfPToExpandTable::operator[](int32_t  index)
  575. {
  576.     if (index >= fSize) {
  577.         if (index >= fCapacity) {
  578.             resize(index + 1);
  579.             if (fBogus) return fElements[0];  // Always return the first element
  580.         }
  581.         else
  582.             fSize = index + 1;
  583.     }
  584.     return fElements[index];
  585. }
  586.  
  587. void
  588. VectorOfPToExpandTable::atPut(  int32_t         index,
  589.                                 VectorOfInt*    value)
  590. {
  591.     if (index >= fSize) {
  592.         if (index >= fCapacity) {
  593.             resize(index + 1);
  594.             if (fBogus) return;
  595.         }
  596.         else
  597.             fSize = index + 1;
  598.     }
  599.  
  600.     delete fElements[index];
  601.     fElements[index] = value;
  602. }
  603.  
  604. VectorOfInt*
  605. VectorOfPToExpandTable::orphanAt(int32_t    index)
  606. {
  607.     if (index > fSize)
  608.         return 0;
  609.     else {
  610.         VectorOfInt*        returnVal = fElements[index];
  611.         fElements[index] = 0;
  612.         return returnVal;
  613.     }
  614. }
  615.  
  616. void
  617. VectorOfPToExpandTable::resize(int32_t  newSize)
  618. {
  619.     int32_t     newCapacity;
  620.  
  621.     newCapacity = newSize / GROWTH_RATE;
  622.     if (newCapacity < 10)
  623.         newCapacity = 10;
  624.     newCapacity += newSize;
  625.  
  626.     VectorOfInt**       newArray = 0;
  627.     newArray = new VectorOfInt*[newCapacity];
  628.     if (!newArray) {
  629.         fBogus = TRUE;
  630.         return;
  631.     }
  632.     VectorOfInt**       iter = newArray;
  633.     VectorOfInt**       cutover = &(newArray[fCapacity]);
  634.     VectorOfInt**       end = &(newArray[newCapacity]);
  635.     VectorOfInt**       from = fElements;
  636.  
  637.     while (iter < cutover)
  638.         *iter++ = *from++;
  639.     while (iter < end)
  640.         *iter++ = 0;
  641.  
  642.     delete [] fElements;
  643.     fElements = newArray;
  644.     fSize = newSize;
  645.     fCapacity = newCapacity;
  646. }
  647.  
  648. void
  649. VectorOfPToExpandTable::streamOut(FileStream* os) const
  650. {
  651.     if (!T_FileStream_error(os))
  652.     {
  653.         T_FileStream_write(os, &fSize, sizeof(fSize));
  654.         int32_t i;
  655.         for (i=0; i<fSize; ++i)
  656.         {
  657.             char isNull = (fElements[i] == 0);
  658.             T_FileStream_write(os, &isNull, sizeof(isNull));
  659.             if (!isNull) fElements[i]->streamOut(os);
  660.         }
  661.     }
  662. }
  663.  
  664. void
  665. VectorOfPToExpandTable::streamIn(FileStream* is)
  666. {
  667.     if (!T_FileStream_error(is))
  668.     {
  669.         int32_t newSize;
  670.         T_FileStream_read(is, &newSize, sizeof(newSize));
  671.         resize(newSize);
  672.         if (fBogus) return;
  673.         int32_t i;
  674.         for (i=0; i<newSize; ++i)
  675.         {
  676.             char isNull;
  677.             T_FileStream_read(is, &isNull, sizeof(isNull));
  678.             if (isNull)
  679.             {
  680.                 delete fElements[i];
  681.                 fElements[i] = 0;
  682.             }
  683.             else
  684.             {
  685.                 if (fElements[i] == 0) fElements[i] = new VectorOfInt;
  686.                 fElements[i]->streamIn(is);
  687.                 if (fElements[i]->isBogus()) {
  688.                     fBogus = TRUE;
  689.                     return;
  690.                 }
  691.             }
  692.         }
  693.     }
  694. }
  695.  
  696. //=======================================================================================
  697. // METHODS ON VectorOfPToContractElement
  698. //=======================================================================================
  699.  
  700. VectorOfPToContractElement::VectorOfPToContractElement(int32_t  initialSize)
  701. : fSize(0),
  702.   fCapacity(0),
  703.   fElements(0),
  704.   fBogus(FALSE)
  705. {
  706.     if (initialSize != 0) {
  707.         resize(initialSize);
  708.         if (fBogus) return;
  709.     }
  710. }
  711.  
  712. VectorOfPToContractElement::VectorOfPToContractElement(const VectorOfPToContractElement&    that)
  713. : fSize(that.fSize),
  714.   fCapacity(that.fCapacity),
  715.   fElements(0),
  716.   fBogus(FALSE)
  717. {
  718.     fElements = new EntryPair*[fCapacity];
  719.     if (!fElements) {
  720.         fBogus = TRUE;
  721.         return;
  722.     }
  723.     EntryPair**     to = fElements;
  724.     EntryPair**     from = that.fElements;
  725.     EntryPair**     end = &(fElements[fCapacity]);
  726.  
  727.     while (to < end) {
  728.         if (*from == 0)
  729.             *to++ = *from++;
  730.         else
  731.             // We actually DUPLICATE the items pointed to by "that"
  732.             *to++ = new EntryPair(*(*from++));
  733.     }
  734. }
  735.  
  736. VectorOfPToContractElement::~VectorOfPToContractElement()
  737. {
  738.     EntryPair**     iter = fElements;
  739.     EntryPair**     end = &(fElements[fSize]);
  740.  
  741.     while (iter < end)
  742.         delete *iter++;
  743.  
  744.     delete [] fElements;
  745. }
  746.  
  747. bool_t
  748. VectorOfPToContractElement::isBogus() const
  749. {
  750.     return fBogus;
  751. }
  752.  
  753. const VectorOfPToContractElement&
  754. VectorOfPToContractElement::operator=(const VectorOfPToContractElement& that)
  755. {
  756.     if (this != &that) {
  757.         if (fCapacity < that.fSize) {
  758.             delete [] fElements;
  759.             fElements = 0;
  760.             fElements = new EntryPair*[that.fCapacity];
  761.             if (!fElements) {
  762.                 fBogus = TRUE;
  763.                 return *this;
  764.             }
  765.             fCapacity = that.fCapacity;
  766.         }
  767.  
  768.         EntryPair**     to = fElements;
  769.         EntryPair**     from = that.fElements;
  770.         EntryPair**     cutover = &(fElements[that.fCapacity]);
  771.         EntryPair**     end = &(fElements[fCapacity]);
  772.  
  773.         while (to < cutover) {
  774.             delete *to;
  775.             if (*from == 0)
  776.                 *to++ = *from++;
  777.             else
  778.                 *to++ = new EntryPair(*(*from++));
  779.         }
  780.         while (to < end) {
  781.             delete *to;
  782.             *to++ = 0;
  783.         }
  784.  
  785.         fSize = that.fSize;
  786.     }
  787.     return *this;
  788. }
  789.  
  790. PToContractElement
  791. VectorOfPToContractElement::operator[](int32_t  index)
  792. {
  793.     if (index >= fSize) {
  794.         if (index >= fCapacity) {
  795.             resize(index + 1);
  796.             if (fBogus) return fElements[0];
  797.         }
  798.         else
  799.             fSize = index + 1;
  800.     }
  801.     return fElements[index];
  802. }
  803.  
  804. void
  805. VectorOfPToContractElement::atPut(  int32_t     index,
  806.                                     EntryPair*      value)
  807. {
  808.     if (index >= fSize) {
  809.         if (index >= fCapacity) {
  810.             resize(index + 1); 
  811.             if (fBogus) return;
  812.         }
  813.         else
  814.             fSize = index + 1;
  815.     }
  816.  
  817.     delete fElements[index];
  818.     fElements[index] = value;
  819. }
  820.  
  821. void
  822. VectorOfPToContractElement::atInsert(   int32_t     index,
  823.                                         EntryPair*  value)
  824. {
  825.     if (fSize + 1 >= fCapacity) {
  826.         resize(fSize + 1);
  827.         if (fBogus) return;
  828.     } else {
  829.         fSize++;
  830.     }
  831.     int32_t i;
  832.     for (i = fSize - 2 ; i >= index; i--)
  833.     {
  834.         fElements[i+1] = fElements[i];
  835.     }
  836.     fElements[index] = value;
  837. }
  838.  
  839. EntryPair*
  840. VectorOfPToContractElement::orphanAt(int32_t    index)
  841. {
  842.     if (index > fSize)
  843.         return 0;
  844.     else {
  845.         EntryPair*      returnVal = fElements[index];
  846.         fElements[index] = 0;
  847.         return returnVal;
  848.     }
  849. }
  850.  
  851. void
  852. VectorOfPToContractElement::resize(int32_t  newSize)
  853. {
  854.     int32_t     newCapacity;
  855.  
  856.     newCapacity = newSize / GROWTH_RATE;
  857.     if (newCapacity < 10)
  858.         newCapacity = 10;
  859.     newCapacity += newSize;
  860.  
  861.     EntryPair**     newArray = 0;
  862.     newArray = new EntryPair*[newCapacity];
  863.     if (!newArray) {
  864.         fBogus = TRUE;
  865.         return;
  866.     }
  867.     EntryPair**     iter = newArray;
  868.     EntryPair**     cutover = &(newArray[fCapacity]);
  869.     EntryPair**     end = &(newArray[newCapacity]);
  870.     EntryPair**     from = fElements;
  871.  
  872.     while (iter < cutover)
  873.         *iter++ = *from++;
  874.     while (iter < end)
  875.         *iter++ = 0;
  876.  
  877.     delete [] fElements;
  878.     fElements = newArray;
  879.     fSize = newSize;
  880.     fCapacity = newCapacity;
  881. }
  882.  
  883. void
  884. VectorOfPToContractElement::streamOut(FileStream* os) const
  885. {
  886.     if (!T_FileStream_error(os))
  887.     {
  888.         T_FileStream_write(os, &fSize, sizeof(fSize));
  889.         int32_t i;
  890.         for (i=0; i<fSize; ++i)
  891.         {
  892.             char isNull = (fElements[i] == 0);
  893.             T_FileStream_write(os, &isNull, sizeof(isNull));
  894.             if (!isNull) fElements[i]->streamOut(os);
  895.         }
  896.     }
  897. }
  898.  
  899. void
  900. VectorOfPToContractElement::streamIn(FileStream* is)
  901. {
  902.     if (!T_FileStream_error(is))
  903.     {
  904.         int32_t newSize;
  905.         T_FileStream_read(is, &newSize, sizeof(newSize));
  906.         resize(newSize);
  907.         if (fBogus) return;
  908.         int32_t i;
  909.         for (i=0; i<newSize; ++i)
  910.         {
  911.             char isNull;
  912.             T_FileStream_read(is, &isNull, sizeof(isNull));
  913.             if (isNull)
  914.             {
  915.                 delete fElements[i];
  916.                 fElements[i] = 0;
  917.             }
  918.             else
  919.             {
  920.                 if (fElements[i] == 0) fElements[i] = new EntryPair;
  921.                 fElements[i]->streamIn(is);
  922.             }
  923.         }
  924.     }
  925. }
  926.  
  927. //=======================================================================================
  928. // METHODS ON VectorOfPToContractTable
  929. //=======================================================================================
  930.  
  931. VectorOfPToContractTable::VectorOfPToContractTable(int32_t  initialSize)
  932. : fSize(0),
  933.   fCapacity(0),
  934.   fElements(0),
  935.   fBogus(FALSE)
  936. {
  937.     if (initialSize != 0) {
  938.         resize(initialSize);
  939.         if (fBogus) return;
  940.     }
  941. }
  942.  
  943. VectorOfPToContractTable::VectorOfPToContractTable(const VectorOfPToContractTable&  that)
  944. : fSize(that.fSize),
  945.   fCapacity(that.fCapacity),
  946.   fElements(0),
  947.   fBogus(FALSE)
  948. {
  949.     fElements = new VectorOfPToContractElement*[fCapacity];
  950.     if (!fElements) {
  951.         fBogus = TRUE;
  952.         return;
  953.     }
  954.     VectorOfPToContractElement**        to = fElements;
  955.     VectorOfPToContractElement**        from = that.fElements;
  956.     VectorOfPToContractElement**        end = &(fElements[fCapacity]);
  957.  
  958.     while (to < end) {
  959.         if (*from == 0)
  960.             *to++ = *from++;
  961.         else {
  962.             // We actually DUPLICATE the items pointed to by "that"
  963.             *to = new VectorOfPToContractElement(*(*from++));
  964.             if ((*to)->isBogus()) {
  965.                 delete [] fElements;
  966.                 fElements = 0;
  967.                 return;
  968.             }
  969.             to++;
  970.         }
  971.     }
  972. }
  973.  
  974. VectorOfPToContractTable::~VectorOfPToContractTable()
  975. {
  976.     VectorOfPToContractElement**        iter = fElements;
  977.     VectorOfPToContractElement**        end = &(fElements[fSize]);
  978.  
  979.     while (iter < end)
  980.         delete *iter++;
  981.  
  982.     delete [] fElements;
  983. }
  984.  
  985. bool_t
  986. VectorOfPToContractTable::isBogus() const
  987. {
  988.     return fBogus;
  989. }
  990.  
  991. const VectorOfPToContractTable&
  992. VectorOfPToContractTable::operator=(const VectorOfPToContractTable& that)
  993. {
  994.     if (this != &that) {
  995.         if (fCapacity < that.fSize) {
  996.             delete [] fElements;
  997.             fElements = 0;
  998.             fElements = new VectorOfPToContractElement*[that.fCapacity];
  999.             if (!fElements) {
  1000.                 fBogus = TRUE;
  1001.                 return *this;
  1002.             }
  1003.             fCapacity = that.fCapacity;
  1004.         }
  1005.  
  1006.         VectorOfPToContractElement**        to = fElements;
  1007.         VectorOfPToContractElement**        from = that.fElements;
  1008.         VectorOfPToContractElement**        cutover = &(fElements[that.fCapacity]);
  1009.         VectorOfPToContractElement**        end = &(fElements[fCapacity]);
  1010.  
  1011.         while (to < cutover) {
  1012.             delete *to;
  1013.             if (*from == 0)
  1014.                 *to++ = *from++;
  1015.             else {
  1016.                 *to = new VectorOfPToContractElement(*(*from++));
  1017.                 if ((*to)->isBogus()) {
  1018.                     delete [] fElements;
  1019.                     fElements = 0;
  1020.                     return *this;
  1021.                 }
  1022.                 to++;
  1023.             }
  1024.         }
  1025.         while (to < end) {
  1026.             delete *to;
  1027.             *to++ = 0;
  1028.         }
  1029.  
  1030.         fSize = that.fSize;
  1031.     }
  1032.     return *this;
  1033. }
  1034.  
  1035. void
  1036. VectorOfPToContractTable::atPut(    int32_t     index,
  1037.                                     VectorOfPToContractElement*     value)
  1038. {
  1039.     if (index >= fSize) {
  1040.         if (index >= fCapacity) {
  1041.             resize(index + 1);
  1042.             if (fBogus) return;
  1043.         }
  1044.         else
  1045.             fSize = index + 1;
  1046.     }
  1047.  
  1048.     delete fElements[index];
  1049.     fElements[index] = value;
  1050. }
  1051.  
  1052. PToContractTable
  1053. VectorOfPToContractTable::operator[](int32_t    index)
  1054. {
  1055.     if (index >= fSize) {
  1056.         if (index >= fCapacity) {
  1057.             resize(index + 1);
  1058.             if (fBogus) return fElements[0];
  1059.         }
  1060.         else
  1061.             fSize = index + 1;
  1062.     }
  1063.     return fElements[index];
  1064. }
  1065.  
  1066. VectorOfPToContractElement*
  1067. VectorOfPToContractTable::orphanAt(int32_t  index)
  1068. {
  1069.     if (index > fSize)
  1070.         return 0;
  1071.     else {
  1072.         VectorOfPToContractElement*     returnVal = fElements[index];
  1073.         fElements[index] = 0;
  1074.         return returnVal;
  1075.     }
  1076. }
  1077.  
  1078. void
  1079. VectorOfPToContractTable::resize(int32_t    newSize)
  1080. {
  1081.     int32_t     newCapacity;
  1082.  
  1083.     newCapacity = newSize / GROWTH_RATE;
  1084.     if (newCapacity < 10)
  1085.         newCapacity = 10;
  1086.     newCapacity += newSize;
  1087.  
  1088.     VectorOfPToContractElement**        newArray = 0;
  1089.     newArray = new VectorOfPToContractElement*[newCapacity];
  1090.     if (!newArray) {
  1091.         fBogus = TRUE;
  1092.         return;
  1093.     }
  1094.     VectorOfPToContractElement**        iter = newArray;
  1095.     VectorOfPToContractElement**        cutover = &(newArray[fCapacity]);
  1096.     VectorOfPToContractElement**        end = &(newArray[newCapacity]);
  1097.     VectorOfPToContractElement**        from = fElements;
  1098.  
  1099.     while (iter < cutover)
  1100.         *iter++ = *from++;
  1101.     while (iter < end)
  1102.         *iter++ = 0;
  1103.  
  1104.     delete [] fElements;
  1105.     fElements = newArray;
  1106.     fSize = newSize;
  1107.     fCapacity = newCapacity;
  1108. }
  1109.  
  1110. void
  1111. VectorOfPToContractTable::streamOut(FileStream* os) const
  1112. {
  1113.     if (!T_FileStream_error(os))
  1114.     {
  1115.         T_FileStream_write(os, &fSize, sizeof(fSize));
  1116.         int32_t i;
  1117.         for (i=0; i<fSize; ++i)
  1118.         {
  1119.             char isNull = (fElements[i] == 0);
  1120.             T_FileStream_write(os, &isNull, sizeof(isNull));
  1121.             if (!isNull) fElements[i]->streamOut(os);
  1122.         }
  1123.     }
  1124. }
  1125.  
  1126. void
  1127. VectorOfPToContractTable::streamIn(FileStream* is)
  1128. {
  1129.     if (!T_FileStream_error(is))
  1130.     {
  1131.         int32_t newSize;
  1132.         T_FileStream_read(is, &newSize, sizeof(newSize));
  1133.         resize(newSize);
  1134.         if (fBogus) return;
  1135.         int32_t i;
  1136.         for (i=0; i<newSize; ++i)
  1137.         {
  1138.             char isNull;
  1139.             T_FileStream_read(is, &isNull, sizeof(isNull));
  1140.             if (isNull)
  1141.             {
  1142.                 delete fElements[i];
  1143.                 fElements[i] = 0;
  1144.             }
  1145.             else
  1146.             {
  1147.                 if (fElements[i] == 0) fElements[i] = new VectorOfPToContractElement;
  1148.                 fElements[i]->streamIn(is);
  1149.             }
  1150.         }
  1151.     }
  1152. }
  1153.  
  1154. //=======================================================================================
  1155. // METHODS ON PointerToPatternEntry
  1156. //=======================================================================================
  1157.  
  1158. PointerToPatternEntry::PointerToPatternEntry(PatternEntry*& value)
  1159. : fValue(value)
  1160. {
  1161. }
  1162.  
  1163. PointerToPatternEntry::PointerToPatternEntry(const PointerToPatternEntry&   that)
  1164. : fValue(that.fValue)
  1165. {
  1166. }
  1167.  
  1168. PointerToPatternEntry::~PointerToPatternEntry()
  1169. {
  1170. }
  1171.  
  1172. const PointerToPatternEntry&
  1173. PointerToPatternEntry::operator=(PatternEntry*  newValue)
  1174. {
  1175.     delete fValue;
  1176.     fValue = newValue;
  1177.     return *this;
  1178. }
  1179.  
  1180. const PointerToPatternEntry&
  1181. PointerToPatternEntry::operator=(const PointerToPatternEntry&   pointerToNewValue)
  1182. {
  1183.     delete fValue;
  1184.     fValue = (PatternEntry*)(pointerToNewValue);
  1185.     return *this;
  1186. }
  1187.  
  1188. PointerToPatternEntry::operator PatternEntry*() const
  1189. {
  1190.     return fValue;
  1191. }
  1192.  
  1193. //=======================================================================================
  1194. // METHODS ON VectorOfPointersToPatternEntry
  1195. //=======================================================================================
  1196. VectorOfPointersToPatternEntry::VectorOfPointersToPatternEntry(int32_t  initialSize)
  1197. : fSize(0),
  1198.   fCapacity(0),
  1199.   fElements(0),
  1200.   fBogus(FALSE)
  1201. {
  1202.     if (initialSize != 0) {
  1203.         resize(initialSize);
  1204.         if (fBogus) return;
  1205.     }
  1206. }
  1207.  
  1208. VectorOfPointersToPatternEntry::VectorOfPointersToPatternEntry(const VectorOfPointersToPatternEntry& that)
  1209. : fSize(that.fSize),
  1210.   fCapacity(that.fCapacity),
  1211.   fElements(0),
  1212.   fBogus(FALSE)
  1213. {
  1214.     fElements = new PatternEntry*[fCapacity];
  1215.     if (!fElements) {
  1216.         fBogus = TRUE;
  1217.         return;
  1218.     }
  1219.     PatternEntry**      to = fElements;
  1220.     PatternEntry**      from = that.fElements;
  1221.     PatternEntry**      end = &(fElements[fCapacity]);
  1222.  
  1223.     while (to < end) {
  1224.         if (*from == 0)
  1225.             *to++ = *from++;
  1226.         else
  1227.             // We actually DUPLICATE the items pointed to by "that"
  1228.             *to++ = new PatternEntry(*(*from++));
  1229.     }
  1230. }
  1231.  
  1232. VectorOfPointersToPatternEntry::~VectorOfPointersToPatternEntry()
  1233. {
  1234.     PatternEntry**      iter = fElements;
  1235.     PatternEntry**      end = &(fElements[fSize]);
  1236.  
  1237.     while (iter < end)
  1238.         delete *iter++;
  1239.  
  1240.     delete [] fElements;
  1241. }
  1242.  
  1243. bool_t
  1244. VectorOfPointersToPatternEntry::isBogus() const
  1245. {
  1246.     return fBogus;
  1247. }
  1248.  
  1249. const VectorOfPointersToPatternEntry&
  1250. VectorOfPointersToPatternEntry::operator=(const VectorOfPointersToPatternEntry& that)
  1251. {
  1252.     if (this != &that) {
  1253.         if (fCapacity < that.fSize) {
  1254.             delete [] fElements;
  1255.             fElements = 0;
  1256.             fElements = new PatternEntry*[that.fCapacity];
  1257.             if (!fElements) {
  1258.                 fBogus = TRUE;
  1259.                 return *this;
  1260.             }
  1261.             fCapacity = that.fCapacity;
  1262.         }
  1263.  
  1264.         PatternEntry**      to = fElements;
  1265.         PatternEntry**      from = that.fElements;
  1266.         PatternEntry**      cutover = &(fElements[that.fCapacity]);
  1267.         PatternEntry**      end = &(fElements[fCapacity]);
  1268.  
  1269.         while (to < cutover) {
  1270.             delete *to;
  1271.             if (*from == 0)
  1272.                 *to++ = *from++;
  1273.             else
  1274.                 *to++ = new PatternEntry(*(*from++));
  1275.         }
  1276.         while (to < end) {
  1277.             delete *to;
  1278.             *to++ = 0;
  1279.         }
  1280.  
  1281.         fSize = that.fSize;
  1282.     }
  1283.     return *this;
  1284. }
  1285.  
  1286. PatternEntry*
  1287. VectorOfPointersToPatternEntry::operator[](int32_t  index) const
  1288. {
  1289.     return (index < fCapacity) ? fElements[index] : 0;
  1290. }
  1291.  
  1292. PointerToPatternEntry
  1293. VectorOfPointersToPatternEntry::operator[](int32_t  index)
  1294. {
  1295.     if (index >= fSize) {
  1296.         if (index >= fCapacity) {
  1297.             resize(index + 1);
  1298.             if (fBogus) return fElements[0];
  1299.         }
  1300.         else
  1301.             fSize = index + 1;
  1302.     }
  1303.     return fElements[index];
  1304. }
  1305.  
  1306. void
  1307. VectorOfPointersToPatternEntry::atPut(  int32_t         index,
  1308.                                         PatternEntry*   value)
  1309. {
  1310.     if (index >= fSize) {
  1311.         if (index >= fCapacity) {
  1312.             resize(index + 1);
  1313.             if (fBogus) return;
  1314.         }
  1315.         else
  1316.             fSize = index + 1;
  1317.     }
  1318.  
  1319.     delete fElements[index];
  1320.     fElements[index] = value;
  1321. }
  1322.  
  1323. void
  1324. VectorOfPointersToPatternEntry::atInsert(   int32_t         index,
  1325.                                             PatternEntry*   value)
  1326. {
  1327.     if (fSize + 1 >= fCapacity) {
  1328.         resize(fSize + 1);
  1329.         if (fBogus) return;
  1330.     } else {
  1331.         fSize++;
  1332.     }
  1333.     int32_t i;
  1334.     for (i = fSize - 2 ; i >= index; i--)
  1335.     {
  1336.         fElements[i+1] = fElements[i];
  1337.     }
  1338.     fElements[index] = value;
  1339. }
  1340.  
  1341. PatternEntry*
  1342. VectorOfPointersToPatternEntry::orphanAt(int32_t    index)
  1343. {
  1344.     if (index > fSize)
  1345.         return 0;
  1346.     else {
  1347.         PatternEntry*   returnVal = fElements[index];
  1348.         fElements[index] = 0;
  1349.         return returnVal;
  1350.     }
  1351. }
  1352.  
  1353. void
  1354. VectorOfPointersToPatternEntry::clear()
  1355. {
  1356.     int32_t i;
  1357.  
  1358.     for (i = 0; i < fSize; i += 1)
  1359.     {
  1360.         delete fElements[i];
  1361.         fElements[i] = NULL;
  1362.     }
  1363.  
  1364.     fSize = 0;
  1365. }
  1366.  
  1367. int32_t
  1368. VectorOfPointersToPatternEntry::size() const
  1369. {
  1370.     return fSize;
  1371. }
  1372.  
  1373. int32_t
  1374. VectorOfPointersToPatternEntry::indexOf(const PatternEntry* value) const
  1375. {
  1376.     int32_t i;
  1377.  
  1378.     if (value == NULL)
  1379.     {
  1380.         for (i = 0; i < fSize; i += 1)
  1381.         {
  1382.             if (fElements[i] == NULL)
  1383.             {
  1384.                 return i;
  1385.             }
  1386.         }
  1387.     }
  1388.     else
  1389.     {
  1390.         for (i = 0; i < fSize; i += 1)
  1391.         {
  1392.             if (fElements[i] != NULL && value->equals(*fElements[i]))
  1393.             {
  1394.                 return i;
  1395.             }
  1396.         }
  1397.     }
  1398.  
  1399.     return -1;
  1400. }
  1401.  
  1402. int32_t
  1403. VectorOfPointersToPatternEntry::lastIndexOf(const PatternEntry* value) const
  1404. {
  1405.     int32_t i;
  1406.  
  1407.     if (value == NULL)
  1408.     {
  1409.         for (i = fSize - 1; i >= 0; i -= 1)
  1410.         {
  1411.             if (fElements[i] == NULL)
  1412.             {
  1413.                 return i;
  1414.             }
  1415.         }
  1416.     }
  1417.     else
  1418.     {
  1419.         for (i = fSize - 1; i >= 0; i -= 1)
  1420.         {
  1421.             if (fElements[i] != NULL && value->equals(*fElements[i]))
  1422.             {
  1423.                 return i;
  1424.             }
  1425.         }
  1426.     }
  1427.  
  1428.     return -1;
  1429. }
  1430.  
  1431. void
  1432. VectorOfPointersToPatternEntry::resize(int32_t  newSize)
  1433. {
  1434.     int32_t     newCapacity;
  1435.  
  1436.     newCapacity = newSize / GROWTH_RATE;
  1437.     if (newCapacity < 10)
  1438.         newCapacity = 10;
  1439.     newCapacity += newSize;
  1440.  
  1441.     PatternEntry**      newArray = 0;
  1442.     newArray = new PatternEntry*[newCapacity];
  1443.     if (!newArray) {
  1444.         fBogus = TRUE;
  1445.         return;
  1446.     }
  1447.     PatternEntry**      iter = newArray;
  1448.     PatternEntry**      cutover = &(newArray[fCapacity]);
  1449.     PatternEntry**      end = &(newArray[newCapacity]);
  1450.     PatternEntry**      from = fElements;
  1451.  
  1452.     while (iter < cutover)
  1453.         *iter++ = *from++;
  1454.     while (iter < end)
  1455.         *iter++ = 0;
  1456.  
  1457.     delete [] fElements;
  1458.     fElements = newArray;
  1459.     fSize = newSize;
  1460.     fCapacity = newCapacity;
  1461. }
  1462.