home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 10 / ioProg_10.iso / soft / optima / samples.z / CellArray.wxc < prev    next >
Encoding:
Text File  |  1996-05-02  |  8.9 KB  |  406 lines

  1. Save Format v1.3
  2. @begin ClassFile "CellArray"
  3.  Exported 0;
  4.  
  5. @begin-code BaseClassList
  6.  
  7. public WObject
  8.  
  9. @end-code;
  10.  
  11.  @begin UserFunction "CellArray()"
  12.   GencodeSrcLine 16;
  13.   FunctionName "CellArray::CellArray()";
  14.  @end;
  15.  
  16.  @begin UserFunction "Prototype for CellArray()"
  17.   Private 1;
  18.   GencodeSrcLine 54;
  19.   FunctionName "CellArray::Prototype for CellArray()";
  20.  @end;
  21.  
  22.  @begin UserFunction "~CellArray()"
  23.   GencodeSrcLine 21;
  24.   FunctionName "CellArray::~CellArray()";
  25.  @end;
  26.  
  27.  @begin UserFunction "Prototype for ~CellArray()"
  28.   Private 1;
  29.   GencodeSrcLine 56;
  30.   FunctionName "CellArray::Prototype for ~CellArray()";
  31.  @end;
  32.  
  33.  @begin UserFunction "Resize( WSize const & size )"
  34.   GencodeSrcLine 28;
  35.   FunctionName "CellArray::Resize( WSize const & size )";
  36.  @end;
  37.  
  38.  @begin UserFunction "Prototype for Resize( WSize const & size )"
  39.   Private 1;
  40.   GencodeSrcLine 58;
  41.   FunctionName "CellArray::Prototype for Resize( WSize const & size )";
  42.  @end;
  43.  
  44.  @begin UserFunction "Randomize()"
  45.   GencodeSrcLine 56;
  46.   FunctionName "CellArray::Randomize()";
  47.  @end;
  48.  
  49.  @begin UserFunction "Prototype for Randomize()"
  50.   Private 1;
  51.   GencodeSrcLine 60;
  52.   FunctionName "CellArray::Prototype for Randomize()";
  53.  @end;
  54.  
  55.  @begin UserFunction "NextGeneration( Rules const &rules )"
  56.   GencodeSrcLine 64;
  57.   FunctionName "CellArray::NextGeneration( Rules const &rules )";
  58.  @end;
  59.  
  60.  @begin UserFunction "Prototype for NextGeneration( Rules const &rules )"
  61.   Private 1;
  62.   GencodeSrcLine 62;
  63.   FunctionName "CellArray::Prototype for NextGeneration( Rules const &rules )";
  64.  @end;
  65.  
  66.  @begin UserFunction "ToggleCell( int x, int y )"
  67.   GencodeSrcLine 127;
  68.   FunctionName "CellArray::ToggleCell( int x, int y )";
  69.  @end;
  70.  
  71.  @begin UserFunction "Prototype for ToggleCell( int x, int y )"
  72.   Private 1;
  73.   GencodeSrcLine 64;
  74.   FunctionName "CellArray::Prototype for ToggleCell( int x, int y )";
  75.  @end;
  76.  
  77.  @begin UserFunction "Clear()"
  78.   GencodeSrcLine 142;
  79.   FunctionName "CellArray::Clear()";
  80.  @end;
  81.  
  82.  @begin UserFunction "Prototype for Clear()"
  83.   Private 1;
  84.   GencodeSrcLine 66;
  85.   FunctionName "CellArray::Prototype for Clear()";
  86.  @end;
  87.  
  88.  @begin UserFunction "Count()"
  89.   GencodeSrcLine 150;
  90.   FunctionName "CellArray::Count()";
  91.  @end;
  92.  
  93.  @begin UserFunction "Prototype for Count()"
  94.   Private 1;
  95.   GencodeSrcLine 68;
  96.   FunctionName "CellArray::Prototype for Count()";
  97.  @end;
  98.  
  99.  @begin HPPPrefixBlock
  100. @begin-code HPPPrefix
  101.  
  102. // Declarations added here will be included at the top of the .HPP file
  103. class Rules;
  104.  
  105. @end-code;
  106.   GencodeSrcLine 11;
  107.  @end;
  108.  
  109.  @begin CPPPrefixBlock
  110. @begin-code CPPPrefix
  111.  
  112. // Code added here will be included at the top of the .CPP file
  113. #include "Rules.hpp"
  114.  
  115. //  Include definitions for resources.
  116. #include "WRes.h"
  117.  
  118. @end-code;
  119.   GencodeSrcLine 11;
  120.  @end;
  121.  
  122.  @begin ClassContentsBlock
  123. @begin-code ClassContents
  124.  
  125.     enum {
  126.         OFF = 0,
  127.         ON = 1,
  128.         NEXT = 2,
  129.         PREV = 4,
  130.     };
  131.     
  132.     char *First()
  133.     {
  134.         return( _array );
  135.     }
  136.     char *Next( char *p ) {
  137.         return( p + 1 );
  138.     }
  139.     bool IsOn( char *p ) {
  140.         return( ( *p & ON ) != 0 );
  141.     }
  142.     bool WasOn( char *p ) {
  143.         return( ( *p & PREV ) != 0 );
  144.     }
  145.     bool FlippedOn( char *p ) {
  146.         return( ( *p & (ON+PREV) ) == ON );
  147.     }
  148.     bool FlippedOff( char *p ) {
  149.         return( ( *p & (ON+PREV) ) == PREV );
  150.     }
  151.     
  152.     public:
  153.         // add your public instance data here
  154.     private:
  155.             WSize _size;
  156.         char *_array;
  157.         int _count;
  158.         // add your private instance data here
  159.     protected:
  160.         // add your protected instance data here
  161.         
  162.  
  163. @end-code;
  164.   GencodeSrcLine 17;
  165.  @end;
  166.  
  167. @begin-code GeneratedClassContents
  168.  
  169.  
  170. @end-code;
  171.  
  172. @begin-code Code "CellArray::CellArray()"
  173.  
  174. CellArray::CellArray()
  175. {
  176.     _count = 0;
  177.     _array = NULL;
  178. }
  179.  
  180. @end-code;
  181.  
  182. @begin-code Code "CellArray::Prototype for CellArray()"
  183.  
  184.     public:
  185.         CellArray();
  186.  
  187. @end-code;
  188.  
  189. @begin-code Code "CellArray::~CellArray()"
  190.  
  191. CellArray::~CellArray()
  192. {
  193.     if( _array != NULL ) {
  194.         _array -= _size.w;
  195.         delete [] _array;
  196.     }
  197. }
  198.  
  199. @end-code;
  200.  
  201. @begin-code Code "CellArray::Prototype for ~CellArray()"
  202.  
  203.     public:
  204.         ~CellArray();
  205.  
  206. @end-code;
  207.  
  208. @begin-code Code "CellArray::Resize( WSize const & size )"
  209.  
  210. void CellArray::Resize( WSize const & size )
  211. {
  212.     if( _size == size ) return;
  213.     char *oldArray = _array;
  214.     WSize oldSize = _size;
  215.     _size = size;
  216.     // allocate an extra row above the top and below the bottom
  217.     _array = new char[ ( _size.h + 2 ) * _size.w ];
  218.     memset( _array, 0, ( _size.h + 2 ) * _size.w );
  219.     _array += _size.w;
  220.     if( oldArray != NULL ) {
  221.         WSize min;
  222.         char *op, *np;
  223.         min.w = _size.w < oldSize.w ? _size.w : oldSize.w;
  224.         min.h = _size.h < oldSize.h ? _size.h : oldSize.h;
  225.         op = oldArray;
  226.         np = _array;
  227.         for( int i = 0; i < min.h; ++i ) {
  228.             for( int j = 0; j < min.w; ++j ) {
  229.                 *np++ = *op++;
  230.             }
  231.             op += oldSize.w - min.w;
  232.             np += _size.w - min.w;
  233.         }
  234.         oldArray -= oldSize.w;
  235.         delete [] oldArray;
  236.     }
  237. }
  238.  
  239. @end-code;
  240.  
  241. @begin-code Code "CellArray::Prototype for Resize( WSize const & size )"
  242.  
  243.     public:
  244.         void Resize( WSize const & size );
  245.  
  246. @end-code;
  247.  
  248. @begin-code Code "CellArray::Randomize()"
  249.  
  250. void CellArray::Randomize()
  251. {
  252.     char *end = _array + _size.w * _size.h;
  253.     for( char *p = _array; p < end; ++p ) {
  254.         *p = rand() < RAND_MAX / 4 ? ON : OFF;
  255.         if( *p ) ++_count;
  256.     }
  257. }
  258.  
  259. @end-code;
  260.  
  261. @begin-code Code "CellArray::Prototype for Randomize()"
  262.  
  263.     public:
  264.         void Randomize();
  265.  
  266. @end-code;
  267.  
  268. @begin-code Code "CellArray::NextGeneration( Rules const &rules )"
  269.  
  270. void CellArray::NextGeneration( Rules const &rules )
  271. {
  272.     
  273.     // notice that we allocated an extra row on the top and bottom to allow us to
  274.     // wrap the universe around without worrying about going off the start/end of memory
  275.     
  276.     char *p;
  277.     char *end = _array + _size.w * _size.h;
  278.     
  279.     // copy the first row after the last row and the last row before the first row to simulate wrap
  280.     char *lastRow = end - _size.w;
  281.     char *preTopRow = _array - _size.w;
  282.     char *topRow = _array;
  283.     char *postLastRow = end;
  284.     for( int i = 0; i < _size.w; ++i ) {
  285.         *preTopRow++ = *lastRow++;
  286.         *postLastRow++ = *topRow++;
  287.     }
  288.  
  289.     // make a pointer for each neighbour of the cell, and march them along
  290.     // the array simultaneously.  The avoids the MANY multiplies involved
  291.     // in the 8 double indexes for each neighbour calculation
  292.     char *a = _array - 1;
  293.     char *b = _array + 1;
  294.     char *c = _array - _size.w - 1;
  295.     char *d = _array - _size.w;
  296.     char *e = _array - _size.w + 1;
  297.     char *f = _array + _size.w - 1;
  298.     char *g = _array + _size.w;
  299.     char *h = _array + _size.w + 1;
  300.     for( p = _array; p < end; ++p ) {
  301.         int neighbors = (*a&ON)+(*b&ON)+(*c&ON)+(*d&ON)+(*e&ON)+(*f&ON)+(*g&ON)+(*h&ON);
  302.         if( *p & ON ) {
  303.             if( rules.Death( neighbors ) ) {
  304.                 *p &= ~NEXT;
  305.             } else {
  306.                 *p |= NEXT;
  307.             }
  308.         } else {
  309.             if( rules.Birth( neighbors ) ) {
  310.                 *p |= NEXT;
  311.             } else {
  312.                 *p &= ~NEXT;
  313.             }
  314.         }
  315.         ++a;++b;++c;++d;++e;++f;++g;++h;
  316.     }
  317.     _count = 0;
  318.     for( p = _array; p < end; ++p ) {
  319.         if( *p & ON ) {
  320.             *p |= PREV;
  321.         } else {
  322.             *p &= ~PREV;
  323.         }
  324.         if( *p & NEXT ) {
  325.             ++_count;
  326.             *p |= ON;
  327.         } else {
  328.             *p &= ~ON;
  329.         }
  330.         *p &= ~NEXT;
  331.     }
  332. }
  333.  
  334. @end-code;
  335.  
  336. @begin-code Code "CellArray::Prototype for NextGeneration( Rules const &rules )"
  337.  
  338.     public:
  339.         void NextGeneration( Rules const &rules );
  340.  
  341. @end-code;
  342.  
  343. @begin-code Code "CellArray::ToggleCell( int x, int y )"
  344.  
  345. bool CellArray::ToggleCell( int x, int y )
  346. {
  347.     char *p = _array + y * _size.w + x;
  348.     bool on;
  349.     *p ^= ON;
  350.     on = ( *p & ON ) != 0;
  351.     if( on ) {
  352.         *p &= ~PREV;
  353.         ++_count;
  354.     } else {
  355.         *p |= PREV;
  356.         --_count;
  357.     }
  358.     return( on );
  359. }
  360.  
  361. @end-code;
  362.  
  363. @begin-code Code "CellArray::Prototype for ToggleCell( int x, int y )"
  364.  
  365.     public:
  366.         bool ToggleCell( int x, int y );
  367.  
  368. @end-code;
  369.  
  370. @begin-code Code "CellArray::Clear()"
  371.  
  372. void CellArray::Clear()
  373. {
  374.     _count = 0;
  375.     char *end = _array + _size.w * _size.h;
  376.     for( char *p = _array; p < end; ++p ) {
  377.         *p = OFF;
  378.     }
  379. }
  380.  
  381. @end-code;
  382.  
  383. @begin-code Code "CellArray::Prototype for Clear()"
  384.  
  385.     public:
  386.         void Clear();
  387.  
  388. @end-code;
  389.  
  390. @begin-code Code "CellArray::Count()"
  391.  
  392. int CellArray::Count()
  393. {
  394.     return( _count );    
  395. }
  396.  
  397. @end-code;
  398.  
  399. @begin-code Code "CellArray::Prototype for Count()"
  400.  
  401.     public:
  402.         int Count();
  403.  
  404. @end-code;
  405. @end;
  406.