DAPPLE Example: autom
Source code:
// @TITLE "autom.cc -- little cellular automaton"
//
// autom -- parallel, 1-dimensional, cellular automaton
// written using the Data-Parallel Programming Library for Education (DAPPLE)
//
// This program uses a vector of bits. Initially all bits are zero,
// except for the middle bit. In each iteration, every bit looks
// at its neighbors; it becomes a 1 if the neighbors are different, and
// 0 otherwise, regardless of its own state. When all iterations are
// printed, one per line, the result is fairly pretty. To print them,
// I map the bit vector into a character vector.
//
// David Kotz 1994
// $Id: autom.cc,v 2.13 94/09/29 12:17:46 dfk Exp Locker: dfk $
#include
#include
#include
#include "dapple.h"
const int N = 79;
const int ITERATIONS = 31;
// which VP am I? useful for VP-specific math
const intVector VP(N, Identity);
int mapchar(const int c) { // map it to something printable
return (c == 0 ? ' ' : 'X');
}
int
main(int argc, char **argv)
{
// 1 means life there, 0 means nothing
intVector cells(VP == N/2); // only the middle one will be 1
cout << charVector(apply(mapchar, cells)) << endl;
for (int i = 0; i < ITERATIONS; i++) {
cells = (shift(cells, 1) != shift(cells, -1));
cout << charVector(apply(mapchar, cells)) << endl;
}
return(0);
}
Demonstration:
autom
X
X X
X X
X X X X
X X
X X X X
X X X X
X X X X X X X X
X X
X X X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X X X X X X X X X
X X
X X X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X X X X X X X X X
X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X X X X X X X X X
X X X X X X X X
X X X X X X X X X X X X X X X X
X X X X X X X X X X X X X X X X
X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X