|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <stdexcept> |
| 4 | +#include <iostream> |
| 5 | +#include <cstdint> |
| 6 | +#include <cstring> |
| 7 | + |
| 8 | +namespace SimpleApp |
| 9 | +{ |
| 10 | + class FunctionPreCacher |
| 11 | + { |
| 12 | + public: |
| 13 | + FunctionPreCacher() = default; |
| 14 | + FunctionPreCacher(const FunctionPreCacher& other) |
| 15 | + { |
| 16 | + x = other.x; |
| 17 | + count = other.count; |
| 18 | + function = other.function; |
| 19 | + |
| 20 | + if (other.values) |
| 21 | + { |
| 22 | + values = new int32_t[other.count]; |
| 23 | + if (values) |
| 24 | + { |
| 25 | + std::memcpy(values, other.values, sizeof(int32_t) * other.count); |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + throw std::runtime_error("FunctionPreCacher new failed!"); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + FunctionPreCacher(FunctionPreCacher&& other) noexcept |
| 34 | + { |
| 35 | + x = other.x; |
| 36 | + count = other.count; |
| 37 | + function = other.function; |
| 38 | + values = other.values; |
| 39 | + |
| 40 | + other.values = nullptr; |
| 41 | + } |
| 42 | + FunctionPreCacher(int32_t x, uint32_t count, int32_t(*function)(int32_t)) |
| 43 | + { |
| 44 | + Setup(x, count, function); |
| 45 | + } |
| 46 | + ~FunctionPreCacher() |
| 47 | + { |
| 48 | + Release(); |
| 49 | + } |
| 50 | + |
| 51 | + FunctionPreCacher& operator=(const FunctionPreCacher& other) |
| 52 | + { |
| 53 | + if (this != &other) |
| 54 | + { |
| 55 | + this->~FunctionPreCacher(); |
| 56 | + this->FunctionPreCacher::FunctionPreCacher(other); |
| 57 | + } |
| 58 | + |
| 59 | + return *this; |
| 60 | + } |
| 61 | + FunctionPreCacher& operator=(FunctionPreCacher&& other) noexcept |
| 62 | + { |
| 63 | + if (this != &other) |
| 64 | + { |
| 65 | + this->~FunctionPreCacher(); |
| 66 | + this->FunctionPreCacher::FunctionPreCacher(std::move(other)); |
| 67 | + } |
| 68 | + |
| 69 | + return *this; |
| 70 | + } |
| 71 | + |
| 72 | + void Setup(int32_t x, uint32_t count, int32_t(*function)(int32_t)) |
| 73 | + { |
| 74 | + Release(); |
| 75 | + this->x = x; |
| 76 | + this->count = count; |
| 77 | + this->function = function; |
| 78 | + } |
| 79 | + FunctionPreCacher& Compute() |
| 80 | + { |
| 81 | + if (function && count > 0) |
| 82 | + { |
| 83 | + Release(); |
| 84 | + values = new int32_t[count]; |
| 85 | + if (values) |
| 86 | + { |
| 87 | + for (uint32_t i = 1; i <= count; i++) |
| 88 | + { |
| 89 | + values[i - 1] = function(i * x); |
| 90 | + } |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + throw std::runtime_error("FunctionPreCacher new failed!"); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return *this; |
| 99 | + } |
| 100 | + void Release() |
| 101 | + { |
| 102 | + if (values) |
| 103 | + { |
| 104 | + delete[] values; |
| 105 | + values = nullptr; |
| 106 | + } |
| 107 | + } |
| 108 | + void Print(std::ostream& os = std::cout) const |
| 109 | + { |
| 110 | + if (values) |
| 111 | + { |
| 112 | + for (uint32_t i = 1; i <= count; i++) |
| 113 | + os << "f(" << i * x << ") = " << values[i - 1] << std::endl; |
| 114 | + } |
| 115 | + } |
| 116 | + int32_t At(uint32_t index) const |
| 117 | + { |
| 118 | + if (index >= count) |
| 119 | + throw std::range_error("FunctionPreCacher index out of range!"); |
| 120 | + return values[index]; |
| 121 | + } |
| 122 | + uint32_t Size() const noexcept |
| 123 | + { |
| 124 | + return count; |
| 125 | + } |
| 126 | + |
| 127 | + FunctionPreCacher& operator()() |
| 128 | + { |
| 129 | + return Compute(); |
| 130 | + } |
| 131 | + FunctionPreCacher& operator()(int32_t x, uint32_t count, int32_t(*function)(int32_t)) |
| 132 | + { |
| 133 | + Setup(x, count, function); |
| 134 | + return Compute(); |
| 135 | + } |
| 136 | + int32_t operator[](uint32_t index) const |
| 137 | + { |
| 138 | + return At(index); |
| 139 | + } |
| 140 | + operator bool() |
| 141 | + { |
| 142 | + return values != nullptr; |
| 143 | + } |
| 144 | + |
| 145 | + private: |
| 146 | + int32_t* values = nullptr; |
| 147 | + int32_t x = 0; |
| 148 | + uint32_t count = 0; |
| 149 | + int32_t(*function)(int32_t x) = nullptr; |
| 150 | + }; |
| 151 | +} |
| 152 | + |
| 153 | +inline std::ostream& operator<<(std::ostream& os, const SimpleApp::FunctionPreCacher& pc) |
| 154 | +{ |
| 155 | + pc.Print(os); |
| 156 | + return os; |
| 157 | +} |
0 commit comments