💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.7 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_DNA_MUTATE_HPP
  20. #define ANTKEEPER_DNA_MUTATE_HPP
  21. #include <algorithm>
  22. #include <iterator>
  23. #include <random>
  24. namespace dna {
  25. /**
  26. * Applies the given function to a randomly selected element in a range.
  27. *
  28. * @param first,last Range of elements to mutate.
  29. * @param unary_op Unary operation function object that will be applied.
  30. * @param g Uniform random bit generator.
  31. * @return Iterator to the mutated element.
  32. */
  33. template <class ForwardIt, class UnaryOperation, class URBG>
  34. ForwardIt mutate(ForwardIt first, ForwardIt last, UnaryOperation unary_op, URBG&& g);
  35. /**
  36. * Applies the given function to a random selection of elements in a range.
  37. *
  38. * @param first,last Range of elements to mutate.
  39. * @param count Number of elements to mutate.
  40. * @param unary_op Unary operation function object that will be applied.
  41. * @param g Uniform random bit generator.
  42. */
  43. template <class ForwardIt, class Size, class UnaryOperation, class URBG>
  44. void mutate_n(ForwardIt first, ForwardIt last, Size count, UnaryOperation unary_op, URBG&& g);
  45. template <class ForwardIt, class UnaryOperation, class URBG>
  46. ForwardIt mutate(ForwardIt first, ForwardIt last, UnaryOperation unary_op, URBG&& g)
  47. {
  48. typedef typename std::iterator_traits<ForwardIt>::difference_type difference_t;
  49. std::uniform_int_distribution<difference_t> distribution(0, std::distance(first, last) - 1);
  50. std::advance(first, distribution(g));
  51. *first = unary_op(*first);
  52. return first;
  53. }
  54. template <class ForwardIt, class Size, class UnaryOperation, class URBG>
  55. void mutate_n(ForwardIt first, ForwardIt last, Size count, UnaryOperation unary_op, URBG&& g)
  56. {
  57. typedef typename std::iterator_traits<ForwardIt>::difference_type difference_t;
  58. std::uniform_int_distribution<difference_t> distribution(0, std::distance(first, last) - 1);
  59. ForwardIt mutation;
  60. while (count)
  61. {
  62. mutation = first;
  63. std::advance(mutation, distribution(g));
  64. *mutation = unary_op(*mutation);
  65. --count;
  66. }
  67. }
  68. } // namespace dna
  69. #endif // ANTKEEPER_DNA_MUTATE_HPP