Browse Source

Add bit-math.hpp

master
C. J. Howard 4 years ago
parent
commit
ecc2b47a30
6 changed files with 120 additions and 110 deletions
  1. +96
    -0
      src/game/genetics/bit-math.hpp
  2. +7
    -20
      src/game/genetics/crossover.hpp
  3. +0
    -33
      src/game/genetics/gene.hpp
  4. +14
    -13
      src/game/genetics/mutate.hpp
  5. +0
    -41
      src/game/genetics/popcount.hpp
  6. +3
    -3
      src/game/genetics/zygosity.hpp

+ 96
- 0
src/game/genetics/bit-math.hpp View File

@ -0,0 +1,96 @@
/*
* Copyright (C) 2020 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_BIT_MATH_HPP
#define ANTKEEPER_BIT_MATH_HPP
namespace dna
{
/**
* Reads bits from the least significant bits of a value and returns them in the positions marked by a mask.
*
* @param x Value from which bits should be read.
* @param mask Bit mask indicating where bits should be deposited.
* @return Bits from the least significant bits of @p x in the positions marked by @p mask.
*/
template <class T>
constexpr T bit_deposit(T x, T mask) noexcept;
/**
* Reads bits from a value in the positions marked by a mask and returns them in the least significant bits.
*
* @param x Value from which bits should be read.
* @param mask Bit mask indicating which bits to extract.
* @return Bits of @p x from the positions marked by @p mask in the least significant bits.
*/
template <class T>
constexpr T bit_extract(T x, T mask) noexcept;
/**
* Returns the number of set bits in a value, known as a *population count* or *Hamming weight*.
*
* @param x Value to count.
* @return Number of set bits in @p x.
*/
template <typename T>
constexpr int popcount(T x) noexcept;
template <class T>
constexpr T bit_deposit(T x, T mask) noexcept
{
T result = 0;
for (T i = 1; mask; i <<= 1)
{
if (x & i)
result |= mask & -mask;
mask &= mask - 1;
}
return result;
}
template <class T>
constexpr T bit_extract(T x, T mask) noexcept
{
T result = 0;
for (T i = 1; mask; i <<= 1)
{
if (x & mask & -mask)
result |= i;
mask &= mask - 1;
}
return result;
}
template <typename T>
constexpr int popcount(T x) noexcept
{
int n = 0;
for (; x; ++n)
x &= x - 1;
return n;
}
} // namespace dna
#endif // ANTKEEPER_BIT_MATH_HPP

src/game/genetics/genetic-operators.hpp → src/game/genetics/crossover.hpp View File

@ -17,19 +17,19 @@
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef ANTKEEPER_GENETIC_OPERATORS_HPP
#define ANTKEEPER_GENETIC_OPERATORS_HPP
#ifndef ANTKEEPER_CROSSOVER_HPP
#define ANTKEEPER_CROSSOVER_HPP
namespace dna namespace dna
{ {
/** /**
* Performs a genetic crossover on two blocks of interleaved alleles.
* Performs a genetic crossover between two chromosomes.
* *
* @param a Interleaved allele block of the first parent.
* @param b Interleaved allele block of the second parent.
* @param a Chromosome of the first parent.
* @param b Chromosome of the second parent.
* @param g Uniform random bit generator. * @param g Uniform random bit generator.
* @return Interleaved allele block of the new offspring.
* @return Chromosome of the new offspring.
*/ */
template <class T, class URBG> template <class T, class URBG>
T crossover(T a, T b, URBG&& g) T crossover(T a, T b, URBG&& g)
@ -48,19 +48,6 @@ T crossover(T a, T b, URBG&& g)
return c; return c;
} }
/**
* Mutates a single allele in an interleaved allele block.
*
* @param x Interleaved allele block to mutate.
* @param g Uniform random bit generator.
* @return Mutated copy of @p x.
*/
template <class T, class URBG>
T mutate(T x, URBG&& g)
{
return x ^ (T(1) << (g() % (sizeof(T) << 3)));
}
} // namespace dna } // namespace dna
#endif // ANTKEEPER_GENETIC_OPERATORS_HPP
#endif // ANTKEEPER_CROSSOVER_HPP

+ 0
- 33
src/game/genetics/gene.hpp View File

@ -1,33 +0,0 @@
/*
* Copyright (C) 2020 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_GENE_HPP
#define ANTKEEPER_GENE_HPP
#include <cstdint>
namespace dna
{
/// A 2-bit gene with two alleles.
typedef std::uint8_t gene;
} // namespace dna
#endif // ANTKEEPER_GENE_HPP

src/game/genetics/allele.hpp → src/game/genetics/mutate.hpp View File

@ -17,24 +17,25 @@
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef ANTKEEPER_ALLELE_HPP
#define ANTKEEPER_ALLELE_HPP
#include <cstdint>
#ifndef ANTKEEPER_MUTATE_HPP
#define ANTKEEPER_MUTATE_HPP
namespace dna namespace dna
{ {
/// Enumerates allele types.
enum allele: std::uint8_t
/**
* Mutates a value by flipping a single random bit.
*
* @param x Value to mutate.
* @param g Uniform random bit generator.
* @return Mutated copy of @p x.
*/
template <class T, class URBG>
T mutate(T x, URBG&& g)
{ {
/// Indicates an allele is recessive.
recessive = 0,
/// Indicates an allele is dominant.
dominant = 1
};
return x ^ (T(1) << (g() % (sizeof(T) << 3)));
}
} // namespace dna } // namespace dna
#endif // ANTKEEPER_ALLELE_HPP
#endif // ANTKEEPER_MUTATE_HPP

+ 0
- 41
src/game/genetics/popcount.hpp View File

@ -1,41 +0,0 @@
/*
* Copyright (C) 2020 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_POPCOUNT_HPP
#define ANTKEEPER_POPCOUNT_HPP
namespace dna
{
/// Returns the number of set bits (*population count*) in @p x. Also known as the *Hamming weight*.
template <typename T>
constexpr int popcount(T x) noexcept;
template <typename T>
inline constexpr int popcount(T x) noexcept
{
int n;
for (n = 0; x; ++n)
x &= x - 1;
return n;
}
} // namespace dna
#endif // ANTKEEPER_POPCOUNT_HPP

+ 3
- 3
src/game/genetics/zygosity.hpp View File

@ -22,7 +22,7 @@
namespace dna namespace dna
{ {
/** /**
* Tests a gene for heterozygosity. * Tests a gene for heterozygosity.
* *
@ -44,13 +44,13 @@ bool is_homozygous(T x);
template <class T> template <class T>
inline bool is_heterozygous<T>(T x) inline bool is_heterozygous<T>(T x)
{ {
return x & 1 != (x >> 1) & 1;
return (x & 1) != (x >> 1);
} }
template <class T> template <class T>
inline bool is_homozygous<T>(T x) inline bool is_homozygous<T>(T x)
{ {
return x & 1 == (x >> 1) & 1;
return (x & 1) == (x >> 1);
} }
} // namespace dna } // namespace dna

Loading…
Cancel
Save