Кресты / Говнокод #29207 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  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
#include <iostream>
#include <type_traits>
#include <mutex>
#include <string>
#include <memory>
#include <vector>
#include <chrono>

void bad_function(void* data) {
   ///  НИКОГДА ТАК НЕ ПИШИ
    if (data) {
        std::cout << *(std::string*)data << "\n";
    }
}

template<typename T = std::string,
         typename Alloc = std::allocator<T>,
         typename = std::enable_if_t<std::is_constructible_v<T, const char*>>,
         typename Clock = std::chrono::high_resolution_clock,
         typename TimePoint = typename Clock::time_point>
class GoodFunctionImpl {
private:
    static std::recursive_mutex mtx_;
    Alloc alloc_;
    std::vector<T, Alloc> buffer_;
    std::string context_;
    TimePoint init_time_;
    
    template<typename U>
    using is_valid_type = std::conjunction<
        std::is_same<std::decay_t<U>, T>,
        std::is_constructible<T, U>
    >;
    
    void internal_log(const std::string& msg) {
        buffer_.push_back(T(msg.c_str()));
    }
    
public:
    GoodFunctionImpl() : init_time_(Clock::now()) {
        internal_log("GoodFunctionImpl initialized");
    }
    
    template<typename U,
             typename = std::enable_if_t<is_valid_type<U>::value>>
    decltype(auto) execute(U&& input) {
        std::lock_guard<std::recursive_mutex> lock(mtx_);
        
        internal_log("Processing started");
        
        T processed = std::forward<U>(input);
        
        auto duration = Clock::now() - init_time_;
        auto duration_ms = std::chrono::duration_cast<
            std::chrono::milliseconds>(duration).count();
        
        std::cout << processed << " (runtime: " 
                  << duration_ms << "ms)\n";
        
        internal_log("Processing completed");
        
        return processed;
    }
    
    size_t get_buffer_size() const { return buffer_.size(); }
};

template<typename T, typename Alloc, typename, typename Clock, typename TimePoint>
std::recursive_mutex GoodFunctionImpl<T, Alloc, Clock, TimePoint>::mtx_;

void good_function(const std::string& input) {
    // Пиши ВОТ ТАК
    // так делают все
    // это стабильно и надежно
    // так надо
    GoodFunctionImpl<> impl;
    auto result = impl.execute(input);
    
    std::cout << "Buffer entries: " 
              << impl.get_buffer_size() << "\n";
}

lisp-worst-code lisp-worst-code, (Updated )

Комментарии (8, +8)

Кресты / Говнокод #29202 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  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
void makeShape(ShapeArguments shape)
{
    if (shape.type == ShapeType::Square)
    {
        throw Square(shape);
    }

    if (shape.type == ShapeType::Cicle)
    {
        throw Circle(shape);
    }
}

void handleShape(ShapeArguments shape)
{
    try
    {
        makeShape(shape);
    }
    catch (const Square& square)
    {
        // Work with square
    }
    catch (const Circle& circle)
    {
        // Work with circle
    }
}

factory

kcalbCube kcalbCube, (Updated )

Комментарии (2, +2)

Кресты / Говнокод #29176 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
SparseMatrix<double> mat(rows,cols);
for (int k=0; k<mat.outerSize(); ++k)
  for (SparseMatrix<double>::InnerIterator it(mat,k); it; ++it)
  {
    it.value();
    it.row();   // row index
    it.col();   // col index (here it is equal to k)
    it.index(); // inner index, here it is equal to it.row()
  }

Random access to the elements of a sparse object can be done through the coeffRef(i,j) function. However, this function involves a quite expensive binary search. In most cases, one only wants to iterate over the non-zeros elements. This is achieved by a standard loop over the outer dimension, and then by iterating over the non-zeros of the current inner vector via an InnerIterator. Thus, the non-zero entries have to be visited in the same order than the storage order.

CHayT CHayT, (Updated )

Комментарии (4, +4)

Кресты / Говнокод #29105 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  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
// https://github.com/ggml-org/llama.cpp/blob/f4c3dd5daa3a79f713813cf1aabdc5886071061d/examples/simple/simple.cpp#L23

    // parse command line arguments

    {
        int i = 1;
        for (; i < argc; i++) {
            if (strcmp(argv[i], "-m") == 0) {
                if (i + 1 < argc) {
                    model_path = argv[++i];
                } else {
                    print_usage(argc, argv);
                    return 1;
                }
            } else if (strcmp(argv[i], "-n") == 0) {
                if (i + 1 < argc) {
                    try {
                        n_predict = std::stoi(argv[++i]);
                    } catch (...) {
                        print_usage(argc, argv);
                        return 1;
                    }
                } else {
                    print_usage(argc, argv);
                    return 1;
                }
            } else if (strcmp(argv[i], "-ngl") == 0) {
                if (i + 1 < argc) {
                    try {
                        ngl = std::stoi(argv[++i]);
                    } catch (...) {
                        print_usage(argc, argv);
                        return 1;
                    }
                } else {
                    print_usage(argc, argv);
                    return 1;
                }
            } else {
                // prompt starts here
                break;
            }
        }
        if (model_path.empty()) {
            print_usage(argc, argv);
            return 1;
        }
        if (i < argc) {
            prompt = argv[i++];
            for (; i < argc; i++) {
                prompt += " ";
                prompt += argv[i];
            }
        }
    }

Парсинг аргументов командной строки

j123123 j123123, (Updated )

Комментарии (8, +8)

Кресты / Говнокод #29054 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  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
xxx: Теперь сделайте так, чтобы цифры выводились следующим образом (используя программу из предыдущего задания):
            1
         2 1
      3 2 1
   4 3 2 1
5 4 3 2 1

yyy:

#include <print>

inline constexpr std::size_t kSize = 5;

template <std::size_t N, std::size_t NN>
constexpr auto operator+(const std::array<char, N>& first, const std::array<char, NN>& second) -> std::array<char, N + NN> {
  std::array<char, N + NN> response;  // NOLINT
  std::ranges::copy(first, response.begin());
  std::ranges::copy(second, response.begin() + first.size());
  return response;
};

auto main() -> int {
  []<std::size_t... Is>(std::index_sequence<Is...>) {
    // clang-format off
    ([&]<std::size_t... IIs, std::size_t... IIIs>(std::index_sequence<IIs...>, std::index_sequence<IIIs...>) {
      constexpr std::format_string<decltype(IIs)...> fmt = [] {
        static constexpr auto response = ((std::ignore = IIIs, std::array{' ', ' '}) + ... + ((std::ignore = IIs, std::array{'{', '}', ' '}) + ... + std::array{'\0'}));
        return response.begin();
      }();
      constexpr auto v = Is;
      std::println(fmt, (v - IIs + 1)...);
    }(std::make_index_sequence<Is + 1>(), std::make_index_sequence<kSize - Is - 1>()), ...);
    // clang-format on
  }(std::make_index_sequence<kSize>());
};

Fluttie Fluttie, (Updated )

Комментарии (1, +1)