Example #1
0
int live_or_die(board_t *self, int row, int col) {
	int curr = board_get(self, row, col);
	int top_row = CIRC(row - 1, self->num_rows);
	int bottom_row = (row + 1)%self->num_rows;
	int left_col = CIRC(col - 1, self->num_cols);
	int right_col = (col + 1)%self->num_cols;

	int neighbors = board_get(self, top_row, left_col) +
					board_get(self, top_row, col) +
					board_get(self, top_row, right_col) +
					board_get(self, row, left_col) +
					board_get(self, row, right_col) +
					board_get(self, bottom_row, left_col) +
					board_get(self, bottom_row, col) +
					board_get(self, bottom_row, right_col);

	if (curr == 1 && neighbors < 2)
		return 0; // dies of loneliness
	else if (curr == 1 && neighbors > 3)
		return 0; // dies of overcrowding
	else if (curr == 1 && (neighbors == 2 || neighbors == 3))
		return 1; // stays alive
	else if (curr == 0 && neighbors == 3)
		return 1; // born

	return 0; // stays dead
}
Example #2
0
static size_t computeoffset(
	double threshold,
	double penalty,
	bool reverse,
	const unsigned char *seq,
	size_t seq_length,
	size_t size,
	base_score score,
	const panda_nt *primer,
	size_t primerlen) {
	/* Circular buffer of probabilities of primer alignment indexed by the offset. */
	double probabilities[primerlen];
	double bestpr = exp(primerlen * threshold);
	size_t bestindex = 0;
	size_t index;
	if (primerlen > seq_length) {
		return 0;
	}

	for (index = 0; index < primerlen; index++) {
		probabilities[index] = -INFINITY;
	}

	for (index = 0; index < seq_length; index++) {
		ptrdiff_t x;
		double last_pr = exp(probabilities[CIRC(index, primerlen)] / (index + 1)) - index * penalty;
		/* The last bucket in the buffer holds the probability of a complete alignment. If it so better than we have seen previously, store it. */
		if (last_pr > bestpr) {
			bestpr = last_pr;
			bestindex = index + 1;
		}
		probabilities[CIRC(index, primerlen)] = 0;
		for (x = (ptrdiff_t) (primerlen > index ? index : primerlen - 1); x >= 0; x--) {
			if (!PANDA_NT_IS_N(primer[x])) {
				panda_nt nt;
				double p;
				double notp;
				score(&seq[size * (reverse ? (seq_length - index - 1) : index)], &nt, &p, &notp);
				probabilities[CIRC(index - x, primerlen)] += ((nt & primer[x]) != 0) ? p : notp;
			}
		}
	}
	return bestindex;
}