From 39d22fc4fbf321f1cc5f82b24f2c8f8304cdcae5 Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Sun, 16 Jan 2022 16:21:27 +0000 Subject: [PATCH] Move constructor to implementation Signed-off-by: Omar Shrit --- .../core/kernels/pspectrum_string_kernel.hpp | 59 +------------------ .../kernels/pspectrum_string_kernel_impl.hpp | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 57 deletions(-) diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp index dc5ff24415..d76acb24a2 100644 --- a/src/mlpack/core/kernels/pspectrum_string_kernel.hpp +++ b/src/mlpack/core/kernels/pspectrum_string_kernel.hpp @@ -72,64 +72,9 @@ class PSpectrumStringKernel * @param datasets Sets of string data. * @param p The length of substrings to search. */ - PSpectrumStringKernel(const std::vector >& datasets, - const size_t p) : p(p) - { - // We have to assemble the counts of substrings. This is not a particularly - // fast operation, unfortunately, but it only needs to be done once. - Log::Info << "Assembling counts of substrings of length " << p << "." - << std::endl; + inline PSpectrumStringKernel(const std::vector >& datasets, + const size_t p); - // Resize for number of datasets. - counts.resize(datasets.size()); - - for (size_t dataset = 0; dataset < datasets.size(); ++dataset) - { - const std::vector& set = datasets[dataset]; - - // Resize for number of strings in dataset. - counts[dataset].resize(set.size()); - - // Inspect each string in the dataset. - for (size_t index = 0; index < set.size(); ++index) - { - // Convenience references. - const std::string& str = set[index]; - std::map& mapping = counts[dataset][index]; - - size_t start = 0; - while ((start + p) <= str.length()) - { - std::string sub = str.substr(start, p); - - // Convert all characters to lowercase. - bool invalid = false; - for (size_t j = 0; j < p; ++j) - { - if (!isalnum(sub[j])) - { - invalid = true; - break; // Only consider substrings with alphanumerics. - } - - sub[j] = tolower(sub[j]); - } - - // Increment position in string. - ++start; - - if (!invalid) - { - // Add to the map. - ++mapping[sub]; - } - } - } - } - - Log::Info << "Substring extraction complete." << std::endl; - } - /** * Evaluate the kernel for the string indices given. As mentioned in the * class documentation, a and b should be 2-element vectors, where the first diff --git a/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp b/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp index cf04b8bdd6..616ae8c498 100644 --- a/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp +++ b/src/mlpack/core/kernels/pspectrum_string_kernel_impl.hpp @@ -22,6 +22,65 @@ namespace mlpack { namespace kernel { +inline PSpectrumStringKernel::PSpectrumStringKernel( + const std::vector >& datasets, + const size_t p) : p(p) +{ + // We have to assemble the counts of substrings. This is not a particularly + // fast operation, unfortunately, but it only needs to be done once. + Log::Info << "Assembling counts of substrings of length " << p << "." + << std::endl; + + // Resize for number of datasets. + counts.resize(datasets.size()); + + for (size_t dataset = 0; dataset < datasets.size(); ++dataset) + { + const std::vector& set = datasets[dataset]; + + // Resize for number of strings in dataset. + counts[dataset].resize(set.size()); + + // Inspect each string in the dataset. + for (size_t index = 0; index < set.size(); ++index) + { + // Convenience references. + const std::string& str = set[index]; + std::map& mapping = counts[dataset][index]; + + size_t start = 0; + while ((start + p) <= str.length()) + { + std::string sub = str.substr(start, p); + + // Convert all characters to lowercase. + bool invalid = false; + for (size_t j = 0; j < p; ++j) + { + if (!isalnum(sub[j])) + { + invalid = true; + break; // Only consider substrings with alphanumerics. + } + + sub[j] = tolower(sub[j]); + } + + // Increment position in string. + ++start; + + if (!invalid) + { + // Add to the map. + ++mapping[sub]; + } + } + } + } + + Log::Info << "Substring extraction complete." << std::endl; +} + /** * Evaluate the kernel for the string indices given. As mentioned in the class * documentation, a and b should be 2-element vectors, where the first element