fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <type_traits>
  6. using namespace std;
  7.  
  8. template<typename T>
  9. struct ISort{
  10. /* oh god these diagrams suck level */
  11. virtual vector<T> sort(T *, size_t) const = 0;
  12. };
  13.  
  14. template<typename T>
  15. struct BubbleSortAsc: ISort<T>{
  16. virtual vector<T> sort(T *array, size_t size) const override{
  17. vector<T> vec(array, array+size);
  18. for(auto i = vec.begin(); i != vec.end(); ++i)
  19. for(auto j = vec.begin(); j < i; ++j)
  20. if(*i > *i)
  21. iter_swap(i, j);
  22. return vec;
  23. }
  24. };
  25.  
  26. template<typename T>
  27. struct BubbleSortDesc: ISort<T>{
  28. virtual vector<T> sort(T *array, size_t size) const override{
  29. vector<T> vec(array, array+size);
  30. for(auto i = vec.begin(); i != vec.end(); ++i)
  31. for(auto j = vec.begin(); j < i; ++j)
  32. if(*i < *i)
  33. iter_swap(i, j);
  34. return vec;
  35. }
  36. };
  37.  
  38. /* second parameter is not in the diagram, but it should be there */
  39. template<typename T, typename TSort>
  40. class Collection{
  41. public:
  42. Collection(const TSort &sort): _sort(sort){}
  43. /* hell no level of diagram */
  44. virtual void push(T) = 0;
  45. /* should be named "pop" */
  46. virtual T peek() = 0;
  47. /* should be named just "sort" */
  48. void sortCollection(){
  49. _data = _sort.sort(_data.data(), _data.size());
  50. }
  51. private:
  52. vector<T> _data;
  53. const TSort &_sort;
  54. protected:
  55. vector<T> &data(){ return _data; }
  56. };
  57.  
  58. template<typename T, typename TSort>
  59. class LIFO: public Collection<T, TSort>{
  60. using Collection<T, TSort>::data;
  61. public:
  62. LIFO(const TSort &sort): Collection<T, TSort>(sort){}
  63. virtual void push(T element) override{
  64. data().push_back(element);
  65. }
  66. virtual T peek() override{
  67. auto result = data().front();
  68. data().erase(data().begin());
  69. return result;
  70. }
  71. };
  72.  
  73. template<typename T, typename TSort>
  74. class FIFO: public Collection<T, TSort>{
  75. using Collection<T, TSort>::data;
  76. public:
  77. FIFO(const TSort &sort): Collection<T, TSort>(sort){}
  78. virtual void push(T element) override{
  79. data().insert(data().begin(), element);
  80. }
  81. virtual T peek() override{
  82. auto result = data().front();
  83. data().erase(data().begin());
  84. return result;
  85. }
  86. };
  87.  
  88.  
  89. int main(){
  90. BubbleSortDesc<double> bsDesc;
  91. FIFO<double, BubbleSortDesc<double>> fifo(bsDesc);
  92.  
  93. fifo.push(1.3);
  94. fifo.push(1.2);
  95. fifo.push(1.1);
  96. cout << std::fixed << fifo.peek() << " " << fifo.peek() << " " << fifo.peek() << endl;
  97.  
  98. fifo.push(1.1);
  99. fifo.push(1.3);
  100. fifo.push(1.2);
  101. fifo.sortCollection();
  102. cout << std::fixed << fifo.peek() << " " << fifo.peek() << " " << fifo.peek() << endl;
  103.  
  104. BubbleSortAsc<int> bsAsc;
  105. LIFO<int, BubbleSortAsc<int>> lifo(bsAsc);
  106.  
  107. lifo.push(3);
  108. lifo.push(2);
  109. lifo.push(1);
  110. cout << std::fixed << lifo.peek() << " " << lifo.peek() << " " << lifo.peek() << endl;
  111.  
  112. lifo.push(1);
  113. lifo.push(3);
  114. lifo.push(2);
  115. lifo.sortCollection();
  116. cout << std::fixed << lifo.peek() << " " << lifo.peek() << " " << lifo.peek() << endl;
  117.  
  118. return 0;
  119. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
1.100000 1.200000 1.300000
1.200000 1.300000 1.100000
3 2 1
1 3 2