media/webrtc/trunk/testing/gtest/samples/prime_tables.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/samples/prime_tables.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,123 @@
     1.4 +// Copyright 2008 Google Inc.
     1.5 +// All Rights Reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +//
    1.33 +// Author: wan@google.com (Zhanyong Wan)
    1.34 +// Author: vladl@google.com (Vlad Losev)
    1.35 +
    1.36 +// This provides interface PrimeTable that determines whether a number is a
    1.37 +// prime and determines a next prime number. This interface is used
    1.38 +// in Google Test samples demonstrating use of parameterized tests.
    1.39 +
    1.40 +#ifndef GTEST_SAMPLES_PRIME_TABLES_H_
    1.41 +#define GTEST_SAMPLES_PRIME_TABLES_H_
    1.42 +
    1.43 +#include <algorithm>
    1.44 +
    1.45 +// The prime table interface.
    1.46 +class PrimeTable {
    1.47 + public:
    1.48 +  virtual ~PrimeTable() {}
    1.49 +
    1.50 +  // Returns true iff n is a prime number.
    1.51 +  virtual bool IsPrime(int n) const = 0;
    1.52 +
    1.53 +  // Returns the smallest prime number greater than p; or returns -1
    1.54 +  // if the next prime is beyond the capacity of the table.
    1.55 +  virtual int GetNextPrime(int p) const = 0;
    1.56 +};
    1.57 +
    1.58 +// Implementation #1 calculates the primes on-the-fly.
    1.59 +class OnTheFlyPrimeTable : public PrimeTable {
    1.60 + public:
    1.61 +  virtual bool IsPrime(int n) const {
    1.62 +    if (n <= 1) return false;
    1.63 +
    1.64 +    for (int i = 2; i*i <= n; i++) {
    1.65 +      // n is divisible by an integer other than 1 and itself.
    1.66 +      if ((n % i) == 0) return false;
    1.67 +    }
    1.68 +
    1.69 +    return true;
    1.70 +  }
    1.71 +
    1.72 +  virtual int GetNextPrime(int p) const {
    1.73 +    for (int n = p + 1; n > 0; n++) {
    1.74 +      if (IsPrime(n)) return n;
    1.75 +    }
    1.76 +
    1.77 +    return -1;
    1.78 +  }
    1.79 +};
    1.80 +
    1.81 +// Implementation #2 pre-calculates the primes and stores the result
    1.82 +// in an array.
    1.83 +class PreCalculatedPrimeTable : public PrimeTable {
    1.84 + public:
    1.85 +  // 'max' specifies the maximum number the prime table holds.
    1.86 +  explicit PreCalculatedPrimeTable(int max)
    1.87 +      : is_prime_size_(max + 1), is_prime_(new bool[max + 1]) {
    1.88 +    CalculatePrimesUpTo(max);
    1.89 +  }
    1.90 +  virtual ~PreCalculatedPrimeTable() { delete[] is_prime_; }
    1.91 +
    1.92 +  virtual bool IsPrime(int n) const {
    1.93 +    return 0 <= n && n < is_prime_size_ && is_prime_[n];
    1.94 +  }
    1.95 +
    1.96 +  virtual int GetNextPrime(int p) const {
    1.97 +    for (int n = p + 1; n < is_prime_size_; n++) {
    1.98 +      if (is_prime_[n]) return n;
    1.99 +    }
   1.100 +
   1.101 +    return -1;
   1.102 +  }
   1.103 +
   1.104 + private:
   1.105 +  void CalculatePrimesUpTo(int max) {
   1.106 +    ::std::fill(is_prime_, is_prime_ + is_prime_size_, true);
   1.107 +    is_prime_[0] = is_prime_[1] = false;
   1.108 +
   1.109 +    for (int i = 2; i <= max; i++) {
   1.110 +      if (!is_prime_[i]) continue;
   1.111 +
   1.112 +      // Marks all multiples of i (except i itself) as non-prime.
   1.113 +      for (int j = 2*i; j <= max; j += i) {
   1.114 +        is_prime_[j] = false;
   1.115 +      }
   1.116 +    }
   1.117 +  }
   1.118 +
   1.119 +  const int is_prime_size_;
   1.120 +  bool* const is_prime_;
   1.121 +
   1.122 +  // Disables compiler warning "assignment operator could not be generated."
   1.123 +  void operator=(const PreCalculatedPrimeTable& rhs);
   1.124 +};
   1.125 +
   1.126 +#endif  // GTEST_SAMPLES_PRIME_TABLES_H_

mercurial