shushus

Спринт 10. Хеш-функции.

Mar 8th, 2023
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <array>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. class VehiclePlate {
  12. public:
  13.     VehiclePlate(char l0, char l1, int digits, char l2, int region) : letters_{l0, l1, l2}
  14.                                                                     , digits_(digits)
  15.                                                                     , region_(region) {}
  16.  
  17.     string ToString() const {
  18.         ostringstream out;
  19.         out << letters_[0] << letters_[1];
  20.         out << setfill('0') << right << setw(3) << digits_;
  21.         out << letters_[2] << setw(2) << region_;
  22.  
  23.         return out.str();
  24.     }
  25.  
  26.     int Hash() const {
  27.         return digits_;
  28.     }
  29.    
  30.     bool operator==(const VehiclePlate& other) const {
  31.         return (this->letters_ == other.letters_)
  32.                && (this->digits_ == other.digits_)
  33.                && (this->region_ == other.region_);
  34.     }
  35.  
  36. private:
  37.     array<char, 3> letters_;
  38.     int digits_;
  39.     int region_;
  40. };
  41.  
  42. ostream& operator<<(ostream& out, VehiclePlate plate) {
  43.     out << plate.ToString();
  44.     return out;
  45. }
  46.  
  47. template <typename T>
  48. class HashableContainer {
  49. public:
  50.     void Insert(T elem) {
  51.         int index = elem.Hash();
  52.  
  53.         if (index >= int(elements_.size())) {
  54.             elements_.resize(index * 2 + 1);
  55.         }
  56.  
  57.         auto helper = find(elements_[index].begin(),
  58.                            elements_[index].end(),
  59.                            elem);
  60.        
  61.         if (elements_[index].end() == helper) {
  62.             elements_[index].push_back(move(elem));
  63.         } else {
  64.             return;
  65.         }
  66.     }
  67.  
  68.     void PrintAll(ostream& out) const {
  69.         for (auto& e : elements_) {
  70.             for (auto& value : e) {
  71.                 out << value << endl;
  72.             }
  73.         }
  74.     }
  75.  
  76.     const auto& GetVector() const {
  77.         return elements_;
  78.     }
  79.  
  80. private:
  81.     vector<vector<T>> elements_;
  82. };
  83.  
  84. int main() {
  85.     HashableContainer<VehiclePlate> plate_base;
  86.     plate_base.Insert({'B', 'H', 840, 'E', 99});
  87.     plate_base.Insert({'O', 'K', 942, 'K', 78});
  88.     plate_base.Insert({'O', 'K', 942, 'K', 78});
  89.     plate_base.Insert({'O', 'K', 942, 'K', 78});
  90.     plate_base.Insert({'O', 'K', 942, 'K', 78});
  91.     plate_base.Insert({'H', 'E', 968, 'C', 79});
  92.     plate_base.Insert({'T', 'A', 326, 'X', 83});
  93.     plate_base.Insert({'H', 'H', 831, 'P', 116});
  94.     plate_base.Insert({'P', 'M', 884, 'K', 23});
  95.     plate_base.Insert({'O', 'C', 34, 'P', 24});
  96.     plate_base.Insert({'M', 'Y', 831, 'M', 43});
  97.     plate_base.Insert({'K', 'T', 478, 'P', 49});
  98.     plate_base.Insert({'X', 'P', 850, 'A', 50});
  99.  
  100.     plate_base.PrintAll(cout);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment