Skip to content

Commit f66a551

Browse files
committed
feat: Enhance Karatsuba multiplication with detailed documentation and type safety
1 parent 2c09206 commit f66a551

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

weilycoder/poly/karatsuba.hpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
#include <vector>
88

99
namespace weilycoder {
10+
/**
11+
* @brief Multiply two polynomials using the Karatsuba algorithm.
12+
* @tparam InputIt Iterator type for input polynomials.
13+
* @tparam OutputIt Iterator type for output polynomial.
14+
* @tparam Threshold Size threshold to switch to standard multiplication.
15+
* @param a_begin Iterator to the beginning of the first polynomial.
16+
* @param a_end Iterator to the end of the first polynomial.
17+
* @param b_begin Iterator to the beginning of the second polynomial.
18+
* @param b_end Iterator to the end of the second polynomial.
19+
* @param result_begin Iterator to the beginning of the result polynomial.
20+
*/
1021
template <typename InputIt, typename OutputIt, size_t Threshold = 32>
1122
void karatsuba_multiply(InputIt a_begin, InputIt a_end, InputIt b_begin, InputIt b_end,
1223
OutputIt result_begin) {
@@ -90,10 +101,21 @@ void karatsuba_multiply(InputIt a_begin, InputIt a_end, InputIt b_begin, InputIt
90101
}
91102
}
92103

93-
template <typename T>
104+
/**
105+
* @brief Multiply two polynomials using the Karatsuba algorithm.
106+
* @tparam T Coefficient type of the polynomials.
107+
* @tparam Threshold Size threshold to switch to standard multiplication.
108+
* @param a First polynomial coefficients.
109+
* @param b Second polynomial coefficients.
110+
* @return Resulting polynomial coefficients after multiplication.
111+
*/
112+
template <typename T, size_t Threshold = 32>
94113
std::vector<T> karatsuba_multiply(const std::vector<T> &a, const std::vector<T> &b) {
114+
using I_It = typename std::vector<T>::const_iterator;
115+
using O_It = typename std::vector<T>::iterator;
95116
std::vector<T> result(a.size() + b.size() - 1);
96-
karatsuba_multiply(a.begin(), a.end(), b.begin(), b.end(), result.begin());
117+
karatsuba_multiply<I_It, O_It, Threshold>(a.begin(), a.end(), b.begin(), b.end(),
118+
result.begin());
97119
return result;
98120
}
99121
} // namespace weilycoder

0 commit comments

Comments
 (0)