home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / LIBMAT.ZIP / MATBOX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-16  |  5.1 KB  |  228 lines

  1. /**************************************************/
  2. /*     matbox.c source for matList family         */
  3. /**************************************************/
  4.  
  5.  
  6. /**************************************************/
  7. /*            MatClass Source File                */
  8. /*       Copyright of C. R. Birchenhall           */
  9. /*       University of Manchester, UK.            */
  10. /*   MatClass is freeware. This file should be    */
  11. /* made freely available to users of any software */
  12. /* whose creation is wholly or partly dependent   */
  13. /*                on this file.                   */
  14. /**************************************************/
  15.  
  16. #include "matbox.hpp"
  17.  
  18. matList::matList( void )
  19. {
  20.    sentinel = new matListNode ;
  21.    sentinel->item = 0 ;
  22.    sentinel->next = 0 ;
  23.    current = sentinel ;
  24.    top     = sentinel ;
  25. } // matList
  26.  
  27. matList::matList( matList& list )
  28. {
  29.    list.gotoHead() ;
  30.    matErrNumExit( "matList copy", NIMPL ) ;
  31. } // matList
  32.  
  33. void matList::operator = ( matList& list )
  34. {
  35.    list.gotoHead() ;
  36.    matErrNumExit( "matList op =", NIMPL ) ;
  37. } // matList
  38.  
  39. matList::~matList( void )
  40. {
  41.    current = top ;
  42.    while ( current != sentinel ) {
  43.       top = top->next ;
  44.       delete current ;
  45.       current = top ;
  46.    } // while
  47.    delete sentinel ;
  48. } // ~matList
  49.  
  50. #include <string.h> // for strcpy
  51.  
  52. charArray matList::name( char *newName )
  53. {
  54.    if ( newName != 0 )
  55.       nm = newName ;
  56.    return nm ;
  57. } // matrix::name
  58.  
  59. int matList::store( void* item )
  60. {
  61.    matListNode *temp = new matListNode ;
  62.    if ( temp == 0 )
  63.       return 1 ;
  64.    temp->item = item ;
  65.    temp->next = top ;
  66.    top = temp ;
  67.    return 0 ;
  68. } // matList::store
  69.  
  70. void* matList::examine( void )
  71. {
  72.    if ( current == sentinel )
  73.       return 0 ;
  74.    else
  75.       return current->item ;
  76. } // matList::examine
  77.  
  78. void* matList::retrieve( void )
  79. {
  80.    void *value = examine() ;
  81.    if ( value != 0 )
  82.       remove( value ) ;
  83.    return value ;
  84. } // matList::retrieve
  85.  
  86. int matList::remove( void* target )
  87. {
  88.    if ( top == sentinel )
  89.       return 2 ;
  90.    matListNode *temp, *nxt ;
  91.    if ( top->item == target ) {
  92.       nxt = top->next ;
  93.       if ( current == top )
  94.          current = nxt ;
  95.       delete top ;
  96.       top = nxt ;
  97.       return 0 ;
  98.    } // if
  99.    sentinel->item = target ;
  100.    temp = top ;
  101.    while ( temp->next->item != target )
  102.       temp = temp->next ;
  103.    if ( temp->next == sentinel )
  104.       return 1 ;
  105.    nxt = temp->next ;
  106.    if ( current == nxt )
  107.       current = nxt->next ;
  108.    temp->next = nxt->next ;
  109.    delete nxt ;
  110.    return 0 ;
  111. } // matList::remove
  112.  
  113. int matList::endOfList( void )
  114. {
  115.    return current == sentinel ;
  116. } // matList::endOfList
  117.  
  118. void matList::gotoHead( void )
  119. {
  120.    current = top ;
  121. } // matList:: gotoHead
  122.  
  123. void matList::gotoNext( void )
  124. {
  125.    if ( current != sentinel )
  126.       current = current->next ;
  127. } // matList:: gotoNext
  128.  
  129.  
  130. /***********************************************/
  131. /*   matObjList : container for  matObjects    */
  132. /***********************************************/
  133.  
  134. #ifdef __cplusplus
  135. matObjList::matObjList( void ) : matList() {}
  136. #else
  137. matObjList::matObjList( void ) : () {}
  138. #endif
  139.  
  140. matObjList::~matObjList( void ) {}
  141.  
  142.  
  143. #ifdef __cplusplus
  144. matObjList::matObjList( matObjList& list ) : matList(list) {}
  145. #else
  146. matObjList::matObjList( matObjList& list ) : (list) {}
  147. #endif
  148.  
  149. void matObjList::operator = ( matObjList& list )
  150. {
  151.       matList::operator = ( list ) ;
  152. } // matObjList =
  153.  
  154. int matObjList::append( matObject* objPtr )
  155. {
  156.    return store( (void *) objPtr ) ;
  157. } // matObjList::append
  158.  
  159. matObject* matObjList::peek( void )
  160. {
  161.    return (matObject *) examine() ;
  162. } // matObjList::peek
  163.  
  164. matObject* matObjList::extract( void )
  165. {
  166.    return (matObject *) retrieve() ;
  167. } // matObjList::extract
  168.  
  169. void matObjList::print( outFile& f )
  170. {
  171.    gotoHead() ;
  172.    if ( endOfList() )
  173.       f.write( "List Empty" ).newLine() ;
  174.    else
  175.       while ( !endOfList() ) {
  176.          peek()->info( f ) ;
  177.          gotoNext() ;
  178.       } // while
  179.    f.newLine() ;
  180. } // matObjList
  181.  
  182. /************************************************/
  183. /*     matrixList : container for matrices      */
  184. /************************************************/
  185.  
  186. #ifdef __cplusplus
  187. matrixList::matrixList( void ) : matObjList() {}
  188. #else
  189. matrixList::matrixList( void ) : () {}
  190. #endif
  191.  
  192. matrixList::~matrixList( void ) {}
  193.  
  194. #ifdef __cplusplus
  195. matrixList::matrixList( matrixList& list ) : matObjList(list) {}
  196. #else
  197. matrixList::matrixList( matrixList& list ) : (list) {}
  198. #endif
  199.  
  200. void matrixList::operator = ( matrixList& list )
  201. {
  202.       matObjList::operator = ( list ) ;
  203. } // matrixList =
  204.  
  205. int matrixList::attach( matrix* m )
  206. {
  207.    return store( (void *) m ) ;
  208. } // matrixList::attach
  209.  
  210. matrix* matrixList::view( void )
  211. {
  212.    return (matrix *) examine() ;
  213. } // matrixList::view
  214.  
  215. matrix* matrixList::detach( void )
  216. {
  217.    return (matrix *) retrieve() ;
  218. } // matrixList::detach
  219.  
  220. void matrixList::dump( outFile& f )
  221. {
  222.    gotoHead() ;
  223.    while ( !endOfList() ) {
  224.       view()->put(f) ;
  225.       gotoNext() ;
  226.    } //while
  227. } // matrixList::dump
  228.