prime_tables.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2008 Google Inc.
  2. // All Rights Reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: wan@google.com (Zhanyong Wan)
  31. // Author: vladl@google.com (Vlad Losev)
  32. // This provides interface PrimeTable that determines whether a number is a
  33. // prime and determines a next prime number. This interface is used
  34. // in Google Test samples demonstrating use of parameterized tests.
  35. #ifndef GTEST_SAMPLES_PRIME_TABLES_H_
  36. #define GTEST_SAMPLES_PRIME_TABLES_H_
  37. #include <algorithm>
  38. // The prime table interface.
  39. class PrimeTable {
  40. public:
  41. virtual ~PrimeTable() {}
  42. // Returns true iff n is a prime number.
  43. virtual bool IsPrime(int n) const = 0;
  44. // Returns the smallest prime number greater than p; or returns -1
  45. // if the next prime is beyond the capacity of the table.
  46. virtual int GetNextPrime(int p) const = 0;
  47. };
  48. // Implementation #1 calculates the primes on-the-fly.
  49. class OnTheFlyPrimeTable : public PrimeTable {
  50. public:
  51. virtual bool IsPrime(int n) const {
  52. if (n <= 1) return false;
  53. for (int i = 2; i*i <= n; i++) {
  54. // n is divisible by an integer other than 1 and itself.
  55. if ((n % i) == 0) return false;
  56. }
  57. return true;
  58. }
  59. virtual int GetNextPrime(int p) const {
  60. for (int n = p + 1; n > 0; n++) {
  61. if (IsPrime(n)) return n;
  62. }
  63. return -1;
  64. }
  65. };
  66. // Implementation #2 pre-calculates the primes and stores the result
  67. // in an array.
  68. class PreCalculatedPrimeTable : public PrimeTable {
  69. public:
  70. // 'max' specifies the maximum number the prime table holds.
  71. explicit PreCalculatedPrimeTable(int max)
  72. : is_prime_size_(max + 1), is_prime_(new bool[max + 1]) {
  73. CalculatePrimesUpTo(max);
  74. }
  75. virtual ~PreCalculatedPrimeTable() { delete[] is_prime_; }
  76. virtual bool IsPrime(int n) const {
  77. return 0 <= n && n < is_prime_size_ && is_prime_[n];
  78. }
  79. virtual int GetNextPrime(int p) const {
  80. for (int n = p + 1; n < is_prime_size_; n++) {
  81. if (is_prime_[n]) return n;
  82. }
  83. return -1;
  84. }
  85. private:
  86. void CalculatePrimesUpTo(int max) {
  87. ::std::fill(is_prime_, is_prime_ + is_prime_size_, true);
  88. is_prime_[0] = is_prime_[1] = false;
  89. for (int i = 2; i <= max; i++) {
  90. if (!is_prime_[i]) continue;
  91. // Marks all multiples of i (except i itself) as non-prime.
  92. for (int j = 2*i; j <= max; j += i) {
  93. is_prime_[j] = false;
  94. }
  95. }
  96. }
  97. const int is_prime_size_;
  98. bool* const is_prime_;
  99. // Disables compiler warning "assignment operator could not be generated."
  100. void operator=(const PreCalculatedPrimeTable& rhs);
  101. };
  102. #endif // GTEST_SAMPLES_PRIME_TABLES_H_