home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / clobss.pak / CONTAIN.CPO < prev    next >
Text File  |  1997-07-23  |  4KB  |  143 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  CONTAIN.CPP                                                           */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991, 1993                            */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __IOSTREAM_H )
  11. #include <iostream.h>
  12. #endif    // __IOSTREAM_H
  13.  
  14. #if !defined( CHECKS_H )
  15. #include <checks.h>
  16. #endif  // CHECKS_H
  17.  
  18. #if !defined( __CONTAIN_H )
  19. #include "classlib\obsolete\contain.h"
  20. #endif  // __CONTAIN_H
  21.  
  22. void Container::forEach( iterFuncType actionPtr, void *paramListPtr )
  23. {
  24.     PRECONDITION( actionPtr != 0 );
  25.     ContainerIterator& containerIterator = initIterator();
  26.     while( containerIterator != 0 )
  27.         containerIterator++.forEach( actionPtr, paramListPtr );
  28.     delete &containerIterator;
  29. }
  30.  
  31. Object& Container::firstThat( condFuncType testFuncPtr,
  32.                               void *paramListPtr
  33.                             ) const
  34. {
  35.     PRECONDITION( testFuncPtr != 0 );
  36.     ContainerIterator &containerIterator = initIterator();
  37.     while( containerIterator != 0 )
  38.         {
  39.         Object& testResult =
  40.                 containerIterator++.firstThat( testFuncPtr, paramListPtr );
  41.         if ( testResult != NOOBJECT )
  42.             {
  43.             delete &containerIterator;
  44.             return testResult;
  45.             }
  46.         }
  47.     delete &containerIterator;
  48.     return NOOBJECT;
  49. }
  50.  
  51. Object& Container::lastThat( condFuncType testFuncPtr,
  52.                              void *paramListPtr
  53.                            ) const
  54. {
  55.     PRECONDITION( testFuncPtr != 0 );
  56.     ContainerIterator& containerIterator = initIterator();
  57.     Object *lastMet = ZERO;
  58.     while( containerIterator != 0 )
  59.         {
  60.         Object& testResult =
  61.                 containerIterator++.lastThat( testFuncPtr, paramListPtr );
  62.         if( testResult != NOOBJECT )
  63.             lastMet = &testResult;
  64.         }
  65.     delete &containerIterator;
  66.     return *lastMet;
  67. }
  68.  
  69. int Container::isEqual( const Object& testContainer ) const
  70. {
  71.     PRECONDITION( isA() == testContainer.isA() );
  72.  
  73.     int res = 1;
  74.  
  75.     ContainerIterator& thisIterator = initIterator();
  76.     ContainerIterator& testContainerIterator =
  77.                             ((Container &)(testContainer)).initIterator();
  78.     while( thisIterator != 0 && testContainerIterator != 0 )
  79.         {
  80.         if( thisIterator++ != testContainerIterator++ )
  81.             {
  82.             res = 0;
  83.             break;
  84.             }
  85.         }
  86.  
  87.     if( thisIterator != 0 || testContainerIterator != 0 )
  88.         res = 0;
  89.  
  90.     delete &testContainerIterator;
  91.     delete &thisIterator;
  92.     return res;
  93. }
  94.  
  95. void Container::printOn( ostream& outputStream ) const
  96. {
  97.     ContainerIterator& printIterator = initIterator();
  98.     printHeader( outputStream );
  99.     while( printIterator != 0 )
  100.         {
  101.         printIterator++.printOn( outputStream );
  102.         if ( printIterator != 0 )
  103.             printSeparator( outputStream );
  104.         else
  105.             break;
  106.         }
  107.     printTrailer( outputStream );
  108.     delete &printIterator;
  109. }
  110.  
  111. static void getHashValue( Object& o, void *valPtr )
  112. {
  113.     hashValueType *val = (hashValueType *)valPtr;
  114.     *val += o.hashValue();
  115. }
  116.  
  117. #pragma warn -ncf
  118.  
  119. hashValueType Container::hashValue() const
  120. {
  121.     hashValueType val = 0;
  122.     forEach( getHashValue, &val );
  123.     return val;
  124. }
  125.  
  126. #pragma warn .ncf
  127.  
  128. void Container::printHeader( ostream& outputStream ) const
  129. {
  130.     outputStream << nameOf() << " {\n    ";
  131. }
  132.  
  133. void Container::printSeparator( ostream& outputStream ) const
  134. {
  135.     outputStream << ",\n    ";
  136. }
  137.  
  138. void Container::printTrailer( ostream& outputStream ) const
  139. {
  140.     outputStream << " }\n";
  141. }
  142.  
  143.