zoukankan      html  css  js  c++  java
  • 素数缓冲求法,可重用类 Anthony

      #include <iostream>
      #include <algorithm>
      #include <vector>
      #include <iterator>
      
      template <typename T>
      class PrimerCache
    - {
          typedef T   ValueType;
          typedef std::vector<ValueType> IntArray;
          typedef typename IntArray::const_iterator IntPtr;
      
          static IntArray mCache;
           
      protected:
          bool Test(ValueType num) const
    -     {
              // make sure : half  < mCache.back()
              // !!! IncreaseCache(half) promised this !!!        
      
              ValueType half = num / 2 + 1;
             
              for(IntPtr i = mCache.begin(); *i < half && i != mCache.end();  ++i)
    -         {
                  if( num % (*i) == 0)
    -             {
                       return false;
                  }
              }         
              return true;
          }
      
          void IncreaseCache(ValueType num) const
    -     {
              ValueType upper = mCache.back();
               
              while (upper < num)
    -         {
                  upper++;             
                  if( Test(upper))
    -             {
                      mCache.push_back(upper);
                  }
                   
              }         
          }
      
          bool InCache(ValueType num) const
    -     {
              return std::binary_search(mCache.begin(), mCache.end(), num);
          }
      public:
          PrimerCache(ValueType lowerNum = 1000)
    -     {
              IsPrimer(lowerNum);
          }
      
          bool IsPrimer(ValueType num) const
    -     {
              ValueType biggestPrimer = mCache.back() + 1;
      
              if( biggestPrimer > num)
    -         {
                  return InCache(num);
              }
              else
    -         {
                  IncreaseCache(num);
                  return Test(num);  
              }
          }
          friend std::ostream& operator<<(std::ostream& out, PrimerCache const& primerCache)
    -     {
              IntArray& cache = primerCache.mCache;
              out << "Primer Cache Has " << cache.size() << " elements, they are " << std::endl;  
              std::copy(cache.begin(), cache.end(), std::ostream_iterator<int>(out, ","));
              return out;
          }
      };
      //the one and only cache;
      template <typename T>
      typename PrimerCache<T>::IntArray PrimerCache<T>::mCache = IntArray(1, 2);
      
      template <typename CacheType>
      void TestPrimer(CacheType& cache, int num)
    - {
          int test = num;
          std::cout<< test << " is primer? "<< std::boolalpha << cache.IsPrimer(test) << std::endl;
      }
      
      
      int main()
    - {
          PrimerCache<int> primer;
          int test;
      
          TestPrimer(primer, 1);
          TestPrimer(primer, 2);
          TestPrimer(primer, 4);
          TestPrimer(primer, 5);
          TestPrimer(primer, 99);
          TestPrimer(primer, 97);
          TestPrimer(primer, 101);
          TestPrimer(primer, 102);
          TestPrimer(primer, 98);
          TestPrimer(primer, 96);
           
      
          std::cout << primer << std::endl;
      
          std::cout << "primer is " << sizeof ( PrimerCache<int>) << std::endl;
      
          PrimerCache<short> p(10);
          
          TestPrimer(p, 1);
          TestPrimer(p, 2);
      
          std::cout << p << std::endl;
          return 0;
      }
      
  • 相关阅读:
    IE8"开发人员工具"使用详解下(浏览器模式、文本模式、JavaScript调试、探查器)
    SQL Server重新利用数据表的空间
    IE8“开发人员工具”使用详解上
    eclipse启动时提示"Failed to load the jni shared library"
    Python学习中遇到的问题(更新中...)
    “ORA12514: TNS: 监听程序当前无法识别连接描述符中请求的服务”问题的解决
    UVa 10881 Piotr's Ants
    (转)设计模式 经典书籍必备推荐
    eclipse的配置
    用python计算文件夹大小
  • 原文地址:https://www.cnblogs.com/ahuangliang/p/5309271.html
Copyright © 2011-2022 走看看