// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
-#ifdef __WIN16__
-#error "Sorry, Life! will not work in 16-bit Windows"
-#endif
-
// ==========================================================================
// headers, declarations, constants
// ==========================================================================
#define HASH(x, y) (((x >> 3) & 0x7f) << 7) + ((y >> 3) & 0x7f)
-#define HASHSIZE 32768 // hash table size (do not change!)
+#define HASHSIZE 16384 // hash table size (do not change!)
#define CELLBOX 8 // cells in a cellbox (do not change!)
-class CellBox
+class LifeCellBox
{
public:
// members
inline bool SetCell(int dx, int dy, bool alive);
// attributes
- wxInt32 m_x, m_y; // position in universe
- wxUint32 m_live1, m_live2; // alive cells (1 bit per cell)
- wxUint32 m_old1, m_old2; // old values for m_live1, 2
- wxUint32 m_on[8]; // neighbouring info
- wxUint32 m_dead; // been dead for n generations
- CellBox *m_up, *m_dn, *m_lf, *m_rt; // neighbour CellBoxes
- CellBox *m_prev, *m_next; // in linked list
- CellBox *m_hprev, *m_hnext; // in hash table
+ wxInt32 m_x, m_y; // position in universe
+ wxUint32 m_live1, m_live2; // alive cells (1 bit per cell)
+ wxUint32 m_old1, m_old2; // old values for m_live1, 2
+ wxUint32 m_on[8]; // neighbouring info
+ wxUint32 m_dead; // been dead for n generations
+ LifeCellBox *m_up, *m_dn, *m_lf, *m_rt; // neighbour CellBoxes
+ LifeCellBox *m_prev, *m_next; // in linked list
+ LifeCellBox *m_hprev, *m_hnext; // in hash table
};
// IsAlive:
// Returns whether cell dx, dy in this box is alive
//
-bool CellBox::IsAlive(int dx, int dy) const
+bool LifeCellBox::IsAlive(int dx, int dy) const
{
if (dy > 3)
- return (m_live2 & 1 << ((dy - 4) * 8 + dx));
+ return (m_live2 & 1 << ((dy - 4) * 8 + dx)) ? true : false ;
else
- return (m_live1 & 1 << ((dy) * 8 + dx));
+ return (m_live1 & 1 << ((dy) * 8 + dx)) ? true : false ;
}
// SetCell:
-// Sets cell dx, dy in this box to 'alive', returns TRUE if
-// the previous value was different, FALSE if it was the same.
+// Sets cell dx, dy in this box to 'alive', returns true if
+// the previous value was different, false if it was the same.
//
-bool CellBox::SetCell(int dx, int dy, bool alive)
+bool LifeCellBox::SetCell(int dx, int dy, bool alive)
{
if (IsAlive(dx, dy) != alive)
{
// reset this here to avoid updating problems
m_dead = 0;
- return TRUE;
+ return true;
}
else
- return FALSE;
+ return false;
}
Life::Life()
{
// pattern description
- m_name = _("");
- m_rules = _("");
- m_description = _("");
+ m_name = wxEmptyString;
+ m_rules = wxEmptyString;
+ m_description = wxEmptyString;
// pattern data
m_numcells = 0;
- m_boxes = new CellBox *[HASHSIZE];
+ m_boxes = new LifeCellBox *[HASHSIZE];
m_head = NULL;
m_available = NULL;
for (int i = 0; i < HASHSIZE; i++)
m_boxes[i] = NULL;
// state vars for BeginFind & FindMore
- m_cells = new Cell[ARRAYSIZE];
+ m_cells = new LifeCell[ARRAYSIZE];
m_ncells = 0;
- m_findmore = FALSE;
- m_changed = FALSE;
+ m_findmore = false;
+ m_changed = false;
}
Life::~Life()
//
void Life::Clear()
{
- CellBox *c, *nc;
+ LifeCellBox *c, *nc;
// clear the hash table pointers
for (int i = 0; i < HASHSIZE; i++)
m_available = NULL;
// reset state
- m_name = _("");
- m_rules = _("");
- m_description = _("");
+ m_name = wxEmptyString;
+ m_rules = wxEmptyString;
+ m_description = wxEmptyString;
m_numcells = 0;
}
//
bool Life::IsAlive(wxInt32 x, wxInt32 y)
{
- CellBox *c = LinkBox(x, y, FALSE);
+ LifeCellBox *c = LinkBox(x, y, false);
return (c && c->IsAlive( x - c->m_x, y - c->m_y ));
}
//
void Life::SetCell(wxInt32 x, wxInt32 y, bool alive)
{
- CellBox *c = LinkBox(x, y);
+ LifeCellBox *c = LinkBox(x, y);
wxUint32 dx = x - c->m_x;
wxUint32 dy = y - c->m_y;
// Creates a box in x, y, either taking it from the list
// of available boxes, or allocating a new one.
//
-CellBox* Life::CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv)
+LifeCellBox* Life::CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv)
{
- CellBox *c;
+ LifeCellBox *c;
// if there are no available boxes, alloc a few more
if (!m_available)
for (int i = 1; i <= ALLOCBOXES; i++)
{
- c = new CellBox();
+ c = new LifeCellBox();
if (!c)
{
m_available = c->m_next;
// reset everything
- memset((void *) c, 0, sizeof(CellBox));
+ memset((void *) c, 0, sizeof(LifeCellBox));
c->m_x = x;
c->m_y = y;
// it returns NULL or creates a new one, depending on the value
// of the 'create' parameter.
//
-CellBox* Life::LinkBox(wxInt32 x, wxInt32 y, bool create)
+LifeCellBox* Life::LinkBox(wxInt32 x, wxInt32 y, bool create)
{
wxUint32 hv;
- CellBox *c;
+ LifeCellBox *c;
x &= 0xfffffff8;
y &= 0xfffffff8;
for (c = m_boxes[hv]; c; c = c->m_hnext)
if ((c->m_x == x) && (c->m_y == y)) return c;
- // if not found, and (create == TRUE), create a new one
- return create? CreateBox(x, y, hv) : (CellBox*) NULL;
+ // if not found, and (create == true), create a new one
+ return create? CreateBox(x, y, hv) : (LifeCellBox*) NULL;
}
// KillBox:
// Removes this box from the list and the hash table and
// puts it in the list of available boxes.
//
-void Life::KillBox(CellBox *c)
+void Life::KillBox(LifeCellBox *c)
{
wxUint32 hv = HASH(c->m_x, c->m_y);
// Navigation
// --------------------------------------------------------------------------
-Cell Life::FindCenter()
+LifeCell Life::FindCenter()
{
double sx, sy;
int n;
sy = 0.0;
n = 0;
- CellBox *c;
+ LifeCellBox *c;
for (c = m_head; c; c = c->m_next)
if (!c->m_dead)
{
sy = (sy / n) + CELLBOX / 2;
}
- Cell cell;
+ LifeCell cell;
cell.i = (wxInt32) sx;
cell.j = (wxInt32) sy;
return cell;
}
-Cell Life::FindNorth()
+LifeCell Life::FindNorth()
{
wxInt32 x = 0, y = 0;
- bool first = TRUE;
+ bool first = true;
- CellBox *c;
+ LifeCellBox *c;
for (c = m_head; c; c = c->m_next)
if (!c->m_dead && ((first) || (c->m_y < y)))
{
x = c->m_x;
y = c->m_y;
- first = FALSE;
+ first = false;
}
-
- Cell cell;
+
+ LifeCell cell;
cell.i = first? 0 : x + CELLBOX / 2;
cell.j = first? 0 : y + CELLBOX / 2;
return cell;
}
-Cell Life::FindSouth()
+LifeCell Life::FindSouth()
{
wxInt32 x = 0, y = 0;
- bool first = TRUE;
+ bool first = true;
- CellBox *c;
+ LifeCellBox *c;
for (c = m_head; c; c = c->m_next)
if (!c->m_dead && ((first) || (c->m_y > y)))
{
x = c->m_x;
y = c->m_y;
- first = FALSE;
+ first = false;
}
-
- Cell cell;
+
+ LifeCell cell;
cell.i = first? 0 : x + CELLBOX / 2;
cell.j = first? 0 : y + CELLBOX / 2;
return cell;
}
-Cell Life::FindWest()
+LifeCell Life::FindWest()
{
wxInt32 x = 0, y = 0;
- bool first = TRUE;
+ bool first = true;
- CellBox *c;
+ LifeCellBox *c;
for (c = m_head; c; c = c->m_next)
if (!c->m_dead && ((first) || (c->m_x < x)))
{
x = c->m_x;
y = c->m_y;
- first = FALSE;
+ first = false;
}
-
- Cell cell;
+
+ LifeCell cell;
cell.i = first? 0 : x + CELLBOX / 2;
cell.j = first? 0 : y + CELLBOX / 2;
return cell;
}
-Cell Life::FindEast()
+LifeCell Life::FindEast()
{
wxInt32 x = 0, y = 0;
- bool first = TRUE;
+ bool first = true;
- CellBox *c;
+ LifeCellBox *c;
for (c = m_head; c; c = c->m_next)
if (!c->m_dead && ((first) || (c->m_x > x)))
{
x = c->m_x;
y = c->m_y;
- first = FALSE;
+ first = false;
}
-
- Cell cell;
+
+ LifeCell cell;
cell.i = first? 0 : x + CELLBOX / 2;
cell.j = first? 0 : y + CELLBOX / 2;
return cell;
m_x1 = (x1 + 7) & 0xfffffff8;
m_y1 = (y1 + 7) & 0xfffffff8;
- m_findmore = TRUE;
+ m_findmore = true;
m_changed = changed;
}
-bool Life::FindMore(Cell *cells[], size_t *ncells)
+bool Life::FindMore(LifeCell *cells[], size_t *ncells)
{
- CellBox *c;
+ LifeCellBox *c;
*cells = m_cells;
m_ncells = 0;
for ( ; m_y <= m_y1; m_y += 8, m_x = m_x0)
for ( ; m_x <= m_x1; m_x += 8)
{
- if ((c = LinkBox(m_x, m_y, FALSE)) == NULL)
+ if ((c = LinkBox(m_x, m_y, false)) == NULL)
continue;
// check whether there is enough space left in the array
if (m_ncells > (ARRAYSIZE - 64))
{
*ncells = m_ncells;
- return FALSE;
+ return false;
}
DoLine(m_x, m_y , c->m_live1, c->m_old1 );
for ( ; m_y <= m_y1; m_y += 8, m_x = m_x0)
for ( ; m_x <= m_x1; m_x += 8)
{
- if ((c = LinkBox(m_x, m_y, FALSE)) == NULL)
+ if ((c = LinkBox(m_x, m_y, false)) == NULL)
continue;
// check whether there is enough space left in the array
if (m_ncells > (ARRAYSIZE - 64))
{
*ncells = m_ncells;
- return FALSE;
+ return false;
}
DoLine(m_x, m_y , c->m_live1 );
}
*ncells = m_ncells;
- m_findmore = FALSE;
- return TRUE;
+ m_findmore = false;
+ return true;
}
// --------------------------------------------------------------------------
//
bool Life::NextTic()
{
- CellBox *c, *up, *dn, *lf, *rt;
+ LifeCellBox *c, *up, *dn, *lf, *rt;
wxUint32 t1, t2, t3, t4;
- bool changed = FALSE;
+ bool changed = false;
m_numcells = 0;
t2 |= g_tab[ ((t4 & 0x0000ffff) << 4 ) + ((t3 >> 24) & 0xf) ] << 24;
t2 |= g_tab[ ((t4 & 0xffff0000) >> 12) + ((t3 >> 28) & 0xf) ] << 28;
- c->m_on[0] = c->m_on[1] = c->m_on[2] = c->m_on[3] =
+ c->m_on[0] = c->m_on[1] = c->m_on[2] = c->m_on[3] =
c->m_on[4] = c->m_on[5] = c->m_on[6] = c->m_on[7] = 0;
c->m_live1 = t1;
c->m_live2 = t2;
- // count alive cells (TODO: find a better way to do this)
+ // count alive cells
+#if 1
+ wxUint32 t1_, t2_;
+
+ t1_ = (t1 & 0x55555555) + (t1 >> 1 & 0x55555555);
+ t1_ = (t1_ & 0x33333333) + (t1_ >> 2 & 0x33333333);
+
+ t2_ = (t2 & 0x55555555) + (t2 >> 1 & 0x55555555);
+ t2_ = (t2_ & 0x33333333) + (t2_ >> 2 & 0x33333333) + t1_;
+ t2_ = (t2_ & 0x0F0F0F0F) + (t2_ >> 4 & 0x0F0F0F0F);
+ t2_ = (t2_ & 0x00FF00FF) + (t2_ >> 8 & 0x00FF00FF);
+
+ m_numcells += (t2_ & 0xFF) + (t2_ >> 16 & 0xFF);
+#else
+ // Original, slower code
for (int i = 0; i < 32; i++)
{
if (t1 & (1 << i)) m_numcells++;
if (t2 & (1 << i)) m_numcells++;
}
+#endif
changed |= ((t1 ^ c->m_old1) || (t2 ^ c->m_old2));
}
else
{
- CellBox *aux = c->m_next;
+ LifeCellBox *aux = c->m_next;
if (c->m_dead++ > MAXDEAD)
KillBox(c);
// see below
g_tab = new unsigned char [0xfffff];
- if (!g_tab) return FALSE;
+ if (!g_tab) return false;
for (wxUint32 i = 0; i < 0xfffff; i++)
{
g_tab[i] = (unsigned char) live;
}
- return TRUE;
+ return true;
}
void LifeModule::OnExit()
0x11112110,
0x11112121,
0x11112221,
- 0x11112232,
+ 0x11112232,
0x11122100,
0x11122111,
0x11122211,