michael@0: // Copyright 2008 Google Inc. michael@0: // All Rights Reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: wan@google.com (Zhanyong Wan) michael@0: // Author: vladl@google.com (Vlad Losev) michael@0: michael@0: // This provides interface PrimeTable that determines whether a number is a michael@0: // prime and determines a next prime number. This interface is used michael@0: // in Google Test samples demonstrating use of parameterized tests. michael@0: michael@0: #ifndef GTEST_SAMPLES_PRIME_TABLES_H_ michael@0: #define GTEST_SAMPLES_PRIME_TABLES_H_ michael@0: michael@0: #include michael@0: michael@0: // The prime table interface. michael@0: class PrimeTable { michael@0: public: michael@0: virtual ~PrimeTable() {} michael@0: michael@0: // Returns true iff n is a prime number. michael@0: virtual bool IsPrime(int n) const = 0; michael@0: michael@0: // Returns the smallest prime number greater than p; or returns -1 michael@0: // if the next prime is beyond the capacity of the table. michael@0: virtual int GetNextPrime(int p) const = 0; michael@0: }; michael@0: michael@0: // Implementation #1 calculates the primes on-the-fly. michael@0: class OnTheFlyPrimeTable : public PrimeTable { michael@0: public: michael@0: virtual bool IsPrime(int n) const { michael@0: if (n <= 1) return false; michael@0: michael@0: for (int i = 2; i*i <= n; i++) { michael@0: // n is divisible by an integer other than 1 and itself. michael@0: if ((n % i) == 0) return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: virtual int GetNextPrime(int p) const { michael@0: for (int n = p + 1; n > 0; n++) { michael@0: if (IsPrime(n)) return n; michael@0: } michael@0: michael@0: return -1; michael@0: } michael@0: }; michael@0: michael@0: // Implementation #2 pre-calculates the primes and stores the result michael@0: // in an array. michael@0: class PreCalculatedPrimeTable : public PrimeTable { michael@0: public: michael@0: // 'max' specifies the maximum number the prime table holds. michael@0: explicit PreCalculatedPrimeTable(int max) michael@0: : is_prime_size_(max + 1), is_prime_(new bool[max + 1]) { michael@0: CalculatePrimesUpTo(max); michael@0: } michael@0: virtual ~PreCalculatedPrimeTable() { delete[] is_prime_; } michael@0: michael@0: virtual bool IsPrime(int n) const { michael@0: return 0 <= n && n < is_prime_size_ && is_prime_[n]; michael@0: } michael@0: michael@0: virtual int GetNextPrime(int p) const { michael@0: for (int n = p + 1; n < is_prime_size_; n++) { michael@0: if (is_prime_[n]) return n; michael@0: } michael@0: michael@0: return -1; michael@0: } michael@0: michael@0: private: michael@0: void CalculatePrimesUpTo(int max) { michael@0: ::std::fill(is_prime_, is_prime_ + is_prime_size_, true); michael@0: is_prime_[0] = is_prime_[1] = false; michael@0: michael@0: for (int i = 2; i <= max; i++) { michael@0: if (!is_prime_[i]) continue; michael@0: michael@0: // Marks all multiples of i (except i itself) as non-prime. michael@0: for (int j = 2*i; j <= max; j += i) { michael@0: is_prime_[j] = false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: const int is_prime_size_; michael@0: bool* const is_prime_; michael@0: michael@0: // Disables compiler warning "assignment operator could not be generated." michael@0: void operator=(const PreCalculatedPrimeTable& rhs); michael@0: }; michael@0: michael@0: #endif // GTEST_SAMPLES_PRIME_TABLES_H_