Skip to content

Commit

Permalink
Add primes implementation to test
Browse files Browse the repository at this point in the history
  • Loading branch information
robertgoss committed Sep 30, 2023
1 parent b60f3ad commit edd7b41
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub fn libprimes_getversion(major : *mut u32, minor : *mut u32, micro : *mut u32
pub fn libprimes_getlasterror(instance : BaseHandle, error_message_buffer_size : usize, error_message_needed_chars : *mut usize, error_message_buffer : *mut u8, has_error : *mut u8) -> i32 {
// Convert parameter instance to be used as an argument
if instance.is_null() { return LIBPRIMES_ERROR_INVALIDPARAM; }
let _handle_instance = unsafe {&*instance};
let _optional_instance = _handle_instance.as_base();
let _handle_instance = unsafe {&mut *instance};
let _optional_instance = _handle_instance.as_mut_base();
if _optional_instance.is_none() { return LIBPRIMES_ERROR_INVALIDPARAM; }
let _instance = _optional_instance.unwrap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,7 @@ pub trait Wrapper {
// * @param[out] error_message - Message of the last error
// * @param[return] has_error - Is there a last error to query
//
fn get_last_error(instance : & dyn Base, error_message : &mut String) -> bool;

// acquire_instance
//
// Acquire shared ownership of an Instance
// * @param[in] instance - Instance Handle
//
fn acquire_instance(instance : & dyn Base);

// release_instance
//
// Releases shared ownership of an Instance
// * @param[in] instance - Instance Handle
//
fn release_instance(instance : & dyn Base);
fn get_last_error(instance : &mut dyn Base, error_message : &mut String) -> bool;

// create_factorization_calculator
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Interface version: 1.2.0


use libprimes_interfaces::*;
use libprimes_sieve_calculator::CSieveCalculator;
use libprimes_factorization_calculator::CFactorizationCalculator;

// Wrapper struct to implement the wrapper trait for global methods
pub struct CWrapper;
Expand All @@ -30,7 +32,9 @@ impl Wrapper for CWrapper {
// * @param[out] micro - returns the micro version of this library
//
fn get_version(_major : &mut u32, _minor : &mut u32, _micro : &mut u32) {
unimplemented!();
*_major = 1;
*_minor = 1;
*_micro = 1;
}

// get_last_error
Expand All @@ -40,26 +44,8 @@ impl Wrapper for CWrapper {
// * @param[out] error_message - Message of the last error
// * @param[return] has_error - Is there a last error to query
//
fn get_last_error(_instance : & dyn Base, _error_message : &mut String) -> bool {
unimplemented!();
}

// acquire_instance
//
// Acquire shared ownership of an Instance
// * @param[in] instance - Instance Handle
//
fn acquire_instance(_instance : & dyn Base) {
unimplemented!();
}

// release_instance
//
// Releases shared ownership of an Instance
// * @param[in] instance - Instance Handle
//
fn release_instance(_instance : & dyn Base) {
unimplemented!();
fn get_last_error(_instance : &mut dyn Base, _error_message : &mut String) -> bool {
_instance.get_last_error_message(_error_message)
}

// create_factorization_calculator
Expand All @@ -68,7 +54,7 @@ impl Wrapper for CWrapper {
// * @param[return] instance - New FactorizationCalculator instance
//
fn create_factorization_calculator() -> Box<dyn FactorizationCalculator> {
unimplemented!();
Box::new(CFactorizationCalculator::new())
}

// create_sieve_calculator
Expand All @@ -77,7 +63,7 @@ impl Wrapper for CWrapper {
// * @param[return] instance - New SieveCalculator instance
//
fn create_sieve_calculator() -> Box<dyn SieveCalculator> {
unimplemented!();
Box::new(CSieveCalculator::new())
}

// set_journal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ impl Base for CBase {
// * @param[return] class_type_id - Class type as a 64 bits integer
//
fn class_type_id(&mut self) -> u64 {
unimplemented!();
0
}
}


impl CBase {
pub fn new() -> CBase {
CBase {
last_error : None
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use libprimes_base::CBase;

// Stub struct to implement the Calculator trait
pub struct CCalculator {
parent : CBase
parent : CBase,
value : u64,
progress_callback : ProgressCallback
}

// Implementation of parent traits via parent
Expand All @@ -32,7 +34,7 @@ impl Base for CCalculator {
// * @param[return] class_type_id - Class type as a 64 bits integer
//
fn class_type_id(&mut self) -> u64 {
self.parent.class_type_id()
1
}

// get_last_error_message
Expand Down Expand Up @@ -72,7 +74,7 @@ impl Calculator for CCalculator {
// * @param[return] value - The current value of this Calculator
//
fn get_value(&mut self) -> u64 {
unimplemented!();
self.value
}

// set_value
Expand All @@ -81,7 +83,7 @@ impl Calculator for CCalculator {
// * @param[in] value - The value to be factorized
//
fn set_value(&mut self, _value : u64) {
unimplemented!();
self.value = _value;
}

// calculate
Expand All @@ -98,7 +100,29 @@ impl Calculator for CCalculator {
// * @param[in] progress_callback - The progress callback
//
fn set_progress_callback(&mut self, _progress_callback : ProgressCallback) {
unimplemented!();
self.progress_callback = _progress_callback
}
}


unsafe extern "C" fn dummy_progress(_par : f32, _val : *mut u8) {

}

impl CCalculator {
pub fn new() -> CCalculator {
CCalculator {
parent : CBase::new(),
value : 0,
progress_callback : dummy_progress
}
}

pub fn progress_abort(&self) -> bool {
let mut res : u8 = 0;
unsafe {
(self.progress_callback)(0_f32, &mut res as *mut u8)
}
res != 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use libprimes_calculator::CCalculator;

// Stub struct to implement the FactorizationCalculator trait
pub struct CFactorizationCalculator {
parent : CCalculator
parent : CCalculator,
prime_factors : Vec<PrimeFactor>
}

// Implementation of parent traits via parent
Expand Down Expand Up @@ -49,7 +50,26 @@ impl Calculator for CFactorizationCalculator {
// Performs the specific calculation of this Calculator
//
fn calculate(&mut self) {
self.parent.calculate()
self.prime_factors.clear();
let mut val = self.get_value();
let mut n = 0_u64;
while n < val {
if self.parent.progress_abort() {
return;
}
let mut prime_factor = PrimeFactor {
prime : n,
multiplicity : 0
};
while val % n == 0 {
val = val / n;
prime_factor.multiplicity += 1;
}
if prime_factor.multiplicity > 0 {
self.prime_factors.push(prime_factor)
}
n += 1
}
}

// set_progress_callback
Expand All @@ -69,7 +89,7 @@ impl Base for CFactorizationCalculator {
// * @param[return] class_type_id - Class type as a 64 bits integer
//
fn class_type_id(&mut self) -> u64 {
self.parent.class_type_id()
2
}

// get_last_error_message
Expand Down Expand Up @@ -109,7 +129,15 @@ impl FactorizationCalculator for CFactorizationCalculator {
// * @param[out] prime_factors - The prime factors of this number
//
fn get_prime_factors(&mut self, _prime_factors : &mut Vec<PrimeFactor>) {
unimplemented!();
*_prime_factors = self.prime_factors.clone()
}
}

impl CFactorizationCalculator {
pub fn new() -> CFactorizationCalculator {
CFactorizationCalculator {
parent : CCalculator::new(),
prime_factors : Vec::new()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use libprimes_calculator::CCalculator;

// Stub struct to implement the SieveCalculator trait
pub struct CSieveCalculator {
parent : CCalculator
parent : CCalculator,
primes : Vec<u64>
}

// Implementation of parent traits via parent
Expand Down Expand Up @@ -49,7 +50,25 @@ impl Calculator for CSieveCalculator {
// Performs the specific calculation of this Calculator
//
fn calculate(&mut self) {
self.parent.calculate()
self.primes.clear();
let val = self.get_value();
let mut sieved : Vec<bool> = vec![false; (val+1) as usize];
sieved[0] = true;
sieved[1] = true;
let val_sqrt = (val as f64).sqrt() as u64 + 1;
for i in 2_u64..val_sqrt {
if self.parent.progress_abort() {
return
}
if !sieved[i as usize] {
self.primes.push(i);
let mut mul : u64 = i*i;
while mul <= val {
sieved[mul as usize] = true;
mul *= i;
}
}
}
}

// set_progress_callback
Expand All @@ -69,7 +88,7 @@ impl Base for CSieveCalculator {
// * @param[return] class_type_id - Class type as a 64 bits integer
//
fn class_type_id(&mut self) -> u64 {
self.parent.class_type_id()
3
}

// get_last_error_message
Expand Down Expand Up @@ -109,7 +128,15 @@ impl SieveCalculator for CSieveCalculator {
// * @param[out] primes - The primes lower or equal to the sieve's value
//
fn get_primes(&mut self, _primes : &mut Vec<u64>) {
unimplemented!();
*_primes = self.primes.clone()
}
}

impl CSieveCalculator {
pub fn new() -> CSieveCalculator {
CSieveCalculator {
parent : CCalculator::new(),
primes : Vec::new()
}
}
}

0 comments on commit edd7b41

Please sign in to comment.