1. Список говнокодов пользователя grillow1337

    Всего: 2

  2. C++ / Говнокод #28299

    −1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    #include <iostream>
    #include <string>
    #include <thread>
    #include <mutex>
    #include <condition_variable>
    #include <atomic>
    #include <queue>
    
    
    std::condition_variable messages_cv;
    std::mutex messages_mtx;
    std::queue<std::string> messages;
    
    std::atomic_bool running = true; // TODO: stop_token when c++20
    
    
    void pull_messages_thread() {
        bool should_run = running;
        while (should_run) {
            std::unique_lock lock(messages_mtx);
        
            messages_cv.wait(lock, []{ return !messages.empty() || !running; });
            if (messages.empty()) return;
        
            auto message = std::move(messages.front());
            messages.pop();
        
            should_run = running || !messages.empty();
            lock.unlock();
        
            std::cout << "Processing\t" + message + "\n";
            std::this_thread::sleep_for(std::chrono::milliseconds(20));
            std::cout << "Processed\t" + message + "\n";
        }
    }
    
    
    int main() {
        std::thread puller(pull_messages_thread);
        for (std::size_t i = 0; i < 10; ++i) {
            std::string message = "Message<" + std::to_string(i) + ">";
    
            std::unique_lock lock(messages_mtx);
            std::cout << "Storing \t" + message + "\n";
            messages.emplace(std::move(message));
            lock.unlock();
            messages_cv.notify_all();
    
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    
        // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    
        running = false;
        messages_cv.notify_all();
        puller.join();
    }
    
    /*
    
    Storing 	Message<0>
    Processing	Message<0>
    Storing 	Message<1>
    Storing 	Message<2>
    Processed	Message<0>
    Processing	Message<1>
    Storing 	Message<3>
    Processed	Message<1>
    Processing	Message<2>
    Storing 	Message<4>
    Storing 	Message<5>
    Processed	Message<2>
    Processing	Message<3>
    Storing 	Message<6>
    Storing 	Message<7>
    Processed	Message<3>
    Processing	Message<4>
    Storing 	Message<8>
    Storing 	Message<9>
    Processed	Message<4>
    Processing	Message<5>
    Processed	Message<5>
    Processing	Message<6>
    Processed	Message<6>
    Processing	Message<7>
    Processed	Message<7>
    Processing	Message<8>
    Processed	Message<8>
    Processing	Message<9>
    Processed	Message<9>
    
     */

    grillow1337, 27 Июля 2022

    Комментарии (0)
  3. C++ / Говнокод #27536

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    class Solution {
    public:
        std::vector<std::vector<int>> diagonalSort(std::vector<std::vector<int>> & mat) {
            if (!mat.size()) return mat;
            
            const size_t rl = mat[0].size();
            const size_t cl = mat.size();
            
            sort(mat, rl, cl, 0, 0);
            for (size_t i = 1; i < rl; ++i) {
                sort(mat, rl, cl, 0, i);
            }
            for (size_t i = 1; i < cl; ++i) {
                sort(mat, rl, cl, i, 0);
            }
            
            return mat;
        }
    private:
        void sort(std::vector<std::vector<int>> & mat, size_t rl, size_t cl, size_t i, size_t j) {
            const size_t len = std::min(rl - j, cl - i);
            const size_t endj = j + len;
            const size_t endi = i + len;
            std::sort(diag_iter<false>{&mat, i, j}, diag_iter<false>{&mat, endi, endj});
        }
        
        template <bool isConst>
        class diag_iter {
            std::vector<std::vector<int>> *base;
            size_t i, j;
            using T = int;
        public:
            using iterator_category = std::forward_iterator_tag;
            using difference_type   = std::ptrdiff_t;
            using value_type        = T;
            using pointer           = T*;
            using reference         = typename std::conditional<isConst, const T&, T&>::type;
            diag_iter(std::vector<std::vector<int>> *base, size_t i, size_t j) : base(base), i(i), j(j) { }
            diag_iter(const diag_iter&) = default;
            diag_iter& operator=(const diag_iter&) = default;
            ~diag_iter() = default;
            reference operator*() const { return (*base)[i][j]; }
            diag_iter& operator++() { i++; j++; return *this; }
            friend bool operator== (const diag_iter& a, const diag_iter& b) { return a.i == b.i && a.j == b.j; };
            friend bool operator!= (const diag_iter& a, const diag_iter& b) { return !(a == b); };
            pointer operator->() const { return &(this->operator*()); }
            diag_iter operator++(int) { diag_iter tmp = *this; ++(*this); return tmp; }
            diag_iter() = default;
            diag_iter& operator--() { i--; j--; return *this; }
            diag_iter operator--(int) { diag_iter tmp = *this; --(*this); return tmp; }
            diag_iter& operator+=(difference_type n) { i += n; j += n; return *this; }
            friend diag_iter operator+(diag_iter it, difference_type n) { return it += n; }
            diag_iter& operator-=(difference_type n) { i -= n; j -= n; return *this; }
            diag_iter operator-(difference_type n) const { return diag_iter(*this) -= n; }
            friend difference_type operator-(const diag_iter& a, const diag_iter& b) { return (b.j * b.base->size() + b.i) - (a.j * a.base->size() + a.i); }
            reference operator[](difference_type n) const { return *(*this + n); }
            friend bool operator<(const diag_iter& a, const diag_iter& b) { return b - a > 0; }
            friend bool operator>(const diag_iter& a, const diag_iter& b) { return b < a; }
            friend bool operator>=(const diag_iter& a, const diag_iter& b) { return !(a < b); }
            friend bool operator<=(const diag_iter& a, const diag_iter& b) { return !(a > b); }
        };
    };

    https://leetcode.com/problems/sort-the-matrix-diagonally/

    Сортировка через итераторы оказалась примерно в три раза медленнее, чем через копирование в вектор, сортировку его и копирование обратно.

    grillow1337, 25 Июля 2021

    Комментарии (18)