Commits

Evan Gates  committed 8e4a933

add population counter

  • Participants
  • Parent commits 445f6d2

Comments (0)

Files changed (1)

 #define cursor_crd(i, j) printf("\033[%d;%dH", i, j)
 
 
+static void print(void);
 static void print_coord(int i, int j);
-static void print_fps();
-static void print_gen();
-static void print(void);
+static void print_fps(void);
+static void print_gen(void);
+static void print_pop(void);
 static void quit(int sig);
 static void run(void);
 static void sigwinch(int unused);
 
 static char *cur, *nxt, *tmp;
 static char *field_a, *field_b;
-static int i_org, j_org, cols, rows, gen, fd;
+static int i_org, j_org, cols, rows, gen, fd, pop;
 static struct termios set, dfl;
 static char *outfile = NULL, *infile = "/dev/urandom";
 static int fps = 15, size = 32, paused = 0;
-static int show_coord = 0, show_fps = 0, show_gen = 0;
-
-void print_coord(int i, int j)
-{
-	if (!show_coord)
-		return;
-	cursor_org();
-	printf("(%d, %d)", j, i);
-	fflush(stdout);
-}
-
-void print_fps()
-{
-	if (!show_fps)
-		return;
-	cursor_crd(0, MIN(cols, size) * 2 - strlen("fps: xxx"));
-	printf("fps: %03d", fps);
-	fflush(stdout);
-}
-
-void print_gen()
-{
-	if (!show_gen)
-		return;
-	cursor_crd(MIN(rows, size), 1);
-	printf("Generation: %d", gen);
-	fflush(stdout);
-}
+static int show_coord = 0, show_fps = 0, show_gen = 0, show_pop = 0;
 
 void print(void)
 {
 			fputc('\n', stdout);
 	}
 	print_gen();
+	print_pop();
 	print_coord(i_org, j_org);
 	print_fps();
 }
 
+void print_coord(int i, int j)
+{
+	if (!show_coord)
+		return;
+	cursor_org();
+	printf("(%d, %d)", j, i);
+	fflush(stdout);
+}
+
+void print_fps(void)
+{
+	if (!show_fps)
+		return;
+	cursor_crd(0, MIN(cols, size) * 2 - strlen("fps: xxx"));
+	printf("fps: %03d", fps);
+	fflush(stdout);
+}
+
+void print_gen(void)
+{
+	if (!show_gen)
+		return;
+	cursor_crd(MIN(rows, size), 1);
+	printf("Generation: %d", gen);
+	fflush(stdout);
+}
+
+void print_pop(void)
+{
+	if (!show_pop)
+		return;
+	cursor_crd(MIN(rows, size), MIN(cols, size) * 2 - strlen("pop: xxxxxxx"));
+	printf("pop: %07d", pop);
+	fflush(stdout);
+}
+
 void quit(int sig)
 {
 	tcsetattr(0, TCSANOW, &dfl);
 				case 'c' : show_coord = !show_coord; break;
 				case 'r' : show_fps   = !show_fps;   break;
 				case 'g' : show_gen   = !show_gen;   break;
+				case 'u' : show_pop   = !show_pop;   break;
 				case 'j' : i_org += (i_org + rows < size) ? 1 : 0; break;
 				case 'k' : i_org -= (i_org > 0)           ? 1 : 0; break;
 				case 'h' : j_org -= (j_org > 0)           ? 1 : 0; break;
 		}
 		if (paused)
 			stop();
+		pop = 0;
 		for (i = 0; i < size; i++) {
 			for (j = 0; j < size; j++) {
 				for (k = i ? -1 : 0, sum = 0; k < 2 && i + k < size; k++) {
 				}
 				switch (sum) {
 					case 2 : BITTEST(cur + (i * BITNSLOTS(size)), j) ?
-							 BITSET(nxt + (i * BITNSLOTS(size)), j) : BITCLR(nxt + (i * BITNSLOTS(size)), j); break;
-					case 3 : BITSET(nxt + (i * BITNSLOTS(size)), j); break;
+							 BITSET(nxt + (i * BITNSLOTS(size)), j), pop++ : BITCLR(nxt + (i * BITNSLOTS(size)), j); break;
+					case 3 : BITSET(nxt + (i * BITNSLOTS(size)), j); pop++; break;
 					default: BITCLR(nxt + (i * BITNSLOTS(size)), j);
 				}
 			}
 			case 'c' : show_coord = !show_coord;             print_flag = 1; break;
 			case 'r' : show_fps   = !show_fps;               print_flag = 1; break;
 			case 'g' : show_gen   = !show_gen;               print_flag = 1; break;
+			case 'u' : show_pop   = !show_pop;               print_flag = 1; break;
 			case 'J' : BITXOR(cur + i * BITNSLOTS(size), j); print_flag = 1; // fall through
 			case 'j' : i = MIN(i + 1, size - 1);
 					   if (i >= i_org + rows) {
 			case 'p' : paused = 1;             break; // default 0
 			case 'r' : show_fps = 1;           break; // default 0
 			case 's' : size = atoi(argv[++i]); break; // default 32
+			case 'u' : show_pop = 1;           break; // default 0
 			default  : return 1;
 		}
 	}