Skip to content

Commit

Permalink
Move code from generate.hpp to generate.cpp (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch authored Jul 1, 2024
1 parent 80a6ad9 commit 60d585f
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 29 deletions.
71 changes: 51 additions & 20 deletions include/generate.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///
/// @file generate.hpp
///
/// Copyright (C) 2023 Kim Walisch, <kim.walisch@gmail.com>
/// Copyright (C) 2024 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
Expand All @@ -10,53 +10,84 @@
#ifndef GENERATE_HPP
#define GENERATE_HPP

#include <primesieve.hpp>
#include <Vector.hpp>

#include <type_traits>
#include <stdint.h>

namespace primecount {

/// Generate a vector with the primes <= max.
/// defined in generate.cpp
Vector<int32_t> generate_primes_i32(int64_t max);
Vector<uint32_t> generate_primes_u32(int64_t max);
Vector<int64_t> generate_primes_i64(int64_t max);
Vector<uint64_t> generate_primes_u64(int64_t max);
Vector<int32_t> generate_n_primes_i32(int64_t n);

/// Returns a vector with the primes <= max.
/// The primes vector uses 1-indexing i.e. primes[1] = 2.
///
template <typename T>
typename std::enable_if<std::is_same<T, int32_t>::value, Vector<int32_t>>::type
generate_primes(int64_t max)
{
return generate_primes_i32(max);
}

/// Returns a vector with the primes <= max.
/// The primes vector uses 1-indexing i.e. primes[1] = 2.
///
template <typename T>
typename std::enable_if<std::is_same<T, uint32_t>::value, Vector<uint32_t>>::type
generate_primes(int64_t max)
{
return generate_primes_u32(max);
}

/// Returns a vector with the primes <= max.
/// The primes vector uses 1-indexing i.e. primes[1] = 2.
///
template <typename T>
typename std::enable_if<std::is_same<T, int64_t>::value, Vector<int64_t>>::type
generate_primes(int64_t max)
{
return generate_primes_i64(max);
}

/// Returns a vector with the primes <= max.
/// The primes vector uses 1-indexing i.e. primes[1] = 2.
///
template <typename T>
Vector<T> generate_primes(int64_t max)
typename std::enable_if<std::is_same<T, uint64_t>::value, Vector<uint64_t>>::type
generate_primes(int64_t max)
{
Vector<T> primes;
primes.resize(1);
primes[0] = 0;
primesieve::generate_primes(max, &primes);
return primes;
return generate_primes_u64(max);
}

/// Generate a vector with the first n primes.
/// Returns a vector with the first n primes.
/// The primes vector uses 1-indexing i.e. primes[1] = 2.
//
template <typename T>
Vector<T> generate_n_primes(int64_t n)
typename std::enable_if<std::is_same<T, int32_t>::value, Vector<int32_t>>::type
generate_n_primes(int64_t n)
{
Vector<T> primes;
primes.reserve(n + 1);
primes.push_back(0);
primesieve::generate_n_primes(n, &primes);
return primes;
return generate_n_primes_i32(n);
}

/// Generate a vector with Möbius function values
/// Returns a vector with Möbius function values
Vector<int32_t> generate_moebius(int64_t max);

/// Generate a vector with the least prime
/// Returns a vector with the least prime
/// factors of the integers <= max.
///
Vector<int32_t> generate_lpf(int64_t max);

/// Generate a vector with the largest prime
/// Returns a vector with the largest prime
/// factors of the integers <= max.
///
Vector<int32_t> generate_mpf(int64_t max);

/// Generate a vector with the prime counts <= max
/// Returns a vector with the prime counts <= max
/// using the sieve of Eratosthenes.
///
Vector<int32_t> generate_pi(int64_t max);
Expand Down
69 changes: 65 additions & 4 deletions src/generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,74 @@
#include <generate.hpp>
#include <int128_t.hpp>
#include <isqrt.hpp>
#include <primesieve.hpp>
#include <Vector.hpp>

#include <stdint.h>

namespace primecount {

/// Generate a vector with the prime counts <= max
/// Returns a vector with the primes <= max.
/// The primes vector uses 1-indexing i.e. primes[1] = 2.
///
Vector<int32_t> generate_primes_i32(int64_t max)
{
Vector<int32_t> primes;
primes.resize(1);
primes[0] = 0;
primesieve::generate_primes(max, &primes);
return primes;
}

/// Returns a vector with the primes <= max.
/// The primes vector uses 1-indexing i.e. primes[1] = 2.
///
Vector<uint32_t> generate_primes_u32(int64_t max)
{
Vector<uint32_t> primes;
primes.resize(1);
primes[0] = 0;
primesieve::generate_primes(max, &primes);
return primes;
}

/// Returns a vector with the primes <= max.
/// The primes vector uses 1-indexing i.e. primes[1] = 2.
///
Vector<int64_t> generate_primes_i64(int64_t max)
{
Vector<int64_t> primes;
primes.resize(1);
primes[0] = 0;
primesieve::generate_primes(max, &primes);
return primes;
}

/// Returns a vector with the primes <= max.
/// The primes vector uses 1-indexing i.e. primes[1] = 2.
///
Vector<uint64_t> generate_primes_u64(int64_t max)
{
Vector<uint64_t> primes;
primes.resize(1);
primes[0] = 0;
primesieve::generate_primes(max, &primes);
return primes;
}

/// Returns a vector with the first n primes.
/// The primes vector uses 1-indexing i.e. primes[1] = 2.
//
Vector<int32_t> generate_n_primes_i32(int64_t n)
{
Vector<int32_t> primes;
primes.reserve(n + 1);
primes.push_back(0);
primesieve::generate_n_primes(n, &primes);
return primes;
}

/// Returns a vector with the prime counts <= max
/// using the sieve of Eratosthenes
///
Vector<int32_t> generate_pi(int64_t max)
Expand Down Expand Up @@ -44,7 +105,7 @@ Vector<int32_t> generate_pi(int64_t max)
return pi;
}

/// Generate a vector with Möbius function values.
/// Returns a vector with Möbius function values.
/// This implementation is based on code by Rick Sladkey:
/// https://mathoverflow.net/q/99545
///
Expand Down Expand Up @@ -81,7 +142,7 @@ Vector<int32_t> generate_moebius(int64_t max)
return mu;
}

/// Generate a vector with the least prime factors
/// Returns a vector with the least prime factors
/// of the integers <= max.
/// @Examples: lfp(2) = 2, lpf(15) = 3
///
Expand Down Expand Up @@ -116,7 +177,7 @@ Vector<int32_t> generate_lpf(int64_t max)
return lpf;
}

/// Generate a vector with the largest prime factors
/// Returns a vector with the largest prime factors
/// of the integers <= max.
/// @Examples: mfp(2) = 2, mpf(15) = 5
///
Expand Down
2 changes: 1 addition & 1 deletion test/generate_lpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int main()

auto max = dist(gen);
auto lpf = generate_lpf(max);
auto primes = generate_primes<int>(max);
auto primes = generate_primes<int32_t>(max);

for (int i = 2; i <= max; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion test/lmo/BinaryIndexedTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main()
int size = dist(gen);
size = next_power_of_2(size);

auto primes = generate_primes<int>(isqrt(size));
auto primes = generate_primes<int32_t>(isqrt(size));
std::vector<int> sieve(size, 1);
BinaryIndexedTree tree;

Expand Down
2 changes: 1 addition & 1 deletion test/phi_tiny.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main()
int64_t size = dist(gen);
int64_t x = size - 1;

auto primes = generate_n_primes<int>(max_a);
auto primes = generate_n_primes<int32_t>(max_a);
std::vector<char> sieve(size, 1);

for (int a = 1; a <= max_a; a++)
Expand Down
2 changes: 1 addition & 1 deletion test/sieve1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int main()
int low = 0;
int high = dist(gen);

auto primes = generate_primes<int>(isqrt(high));
auto primes = generate_primes<int32_t>(isqrt(high));
auto segment_size = Sieve::get_segment_size(high - low);

Sieve sieve(low, segment_size, primes.size());
Expand Down
2 changes: 1 addition & 1 deletion test/sieve2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main()
int low = 0;
int high = dist(gen);

auto primes = generate_primes<int>(isqrt(high));
auto primes = generate_primes<int32_t>(isqrt(high));
auto segment_size = Sieve::get_segment_size(high - low);

Sieve sieve(low, segment_size, primes.size());
Expand Down

0 comments on commit 60d585f

Please sign in to comment.