Advertisement
Guest User

Untitled

a guest
May 1st, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3.  
  4.  
  5.  
  6. class CachingCalculator
  7. {
  8.     class Arguments
  9.     {
  10.     public:
  11.         Arguments(int firstArgument, int secondArgument)
  12.         {
  13.             this->firstArgument = firstArgument;
  14.             this->secondArgument = secondArgument;
  15.         }
  16.         bool operator==(const Arguments &other) const
  17.           {
  18.             return (firstArgument == other.firstArgument
  19.             && secondArgument == other.secondArgument);
  20.            
  21.           }
  22.         int firstArgument;
  23.         int secondArgument;
  24.     };
  25.  
  26. public:
  27.    
  28.      
  29.      class hash<Arguments>
  30.      {
  31.         public  
  32.         std::size_t operator()(const Arguments& k) const
  33.         {
  34.       using std::size_t;
  35.    
  36.    
  37.       return (pow(k.firstargument,k.secondargument));
  38.          }
  39.     };
  40.  
  41.    
  42.     CachingCalculator(int(*func)(int, int))
  43.     {
  44.         this->func = func;
  45.     }
  46.  
  47.     ~CachingCalculator()
  48.     {
  49.         for (std::unordered_map<Arguments*, int>::iterator it = calculations.begin(); it != calculations.end(); ++it)
  50.         {
  51.             delete it->first;
  52.         }
  53.     }
  54.  
  55.     int calculate(int firstArgument, int secondArgument)
  56.     {
  57.         Arguments* args = new Arguments(firstArgument, secondArgument);
  58.  
  59.         std::unordered_map<Arguments*, int>::iterator it = calculations.find(args);
  60.         if (it != calculations.end())
  61.             return it->second;
  62.         std::cout << "not found\n";
  63.         int calculation = func(firstArgument, secondArgument);
  64.         calculations[args] = calculation;
  65.         return calculation;
  66.     }
  67.  
  68. private:
  69.     std::unordered_map<Arguments*, int> calculations;
  70.     int(*func)(int, int);
  71. };
  72.  
  73. #ifndef RunTests
  74. int modulo(int a, int b)
  75. {
  76.     std::cout << "Function modulo has been called.\n";
  77.     return a % b;
  78. }
  79.  
  80. int main()
  81. {
  82.     CachingCalculator calculator(modulo);
  83.  
  84.     // Function modulo should be called.
  85.     std::cout << calculator.calculate(5, 2) << '\n';
  86.  
  87.     // Function modulo should be called.
  88.     std::cout << calculator.calculate(7, 4) << '\n';
  89.  
  90.     // Function modulo shouldn't be called because we have already made a call with arguments (5, 2)!
  91.     // Instead, result should be pulled from the cache!
  92.     std::cout << calculator.calculate(5, 2) << '\n';
  93. }
  94. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement