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