Skip to content

Commit

Permalink
a bit more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ognevny committed Nov 9, 2023
1 parent 41f0478 commit 8dc02ef
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 71 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ognlib"
version = "0.5.5"
version = "0.5.6"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Some code that is written to practice Rust"
Expand Down
21 changes: 21 additions & 0 deletions src/algorithm/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ pub fn bin_search<T: Ord>(arr: &[T], targ: T) -> Option<usize> {
/// let nums2 = mask_match(1, 10, "*");
/// assert_eq!(nums2, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
/// ```
///
/// If you wish to apply extra conditions, you can use power of [`Iterator`] for this.
///
/// ```
/// use ognlib::algorithm::extra::mask_match;
///
/// let nums_div_by_169: Vec<usize> = mask_match(1, 1000000000, "123*567?")
/// .iter()
/// .cloned()
/// .filter(|&x| x % 169 == 0)
/// .collect();
///
/// assert_eq!(
/// nums_div_by_169,
/// vec![
/// 12325677, 12385672, 123157567, 123165679, 123225674, 123326567, 123495567, 123515678,
/// 123575673, 123664567, 123833567, 123865677, 123925672,
/// ]
/// )
/// ```
pub fn mask_match(lower: usize, upper: usize, mask: &str) -> Vec<usize> {
assert!(lower <= upper);

Expand Down
37 changes: 21 additions & 16 deletions src/algorithm/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
pub fn bubble<T: Ord>(arr: &mut [T]) {
let n = arr.len();
let mut swapped = false;
for i in 0..n {
for j in 0..n - i - 1 {
if arr[j] > arr[j + 1] {
arr.swap(j + 1, j)
arr.swap(j + 1, j);
swapped = true;
}
}
if !swapped {
break;
}
}
}

Expand Down Expand Up @@ -184,7 +189,7 @@ pub fn cocktail_shaker<T: Ord>(arr: &mut [T]) {
pub fn quicksort<T>(arr: &mut [T])
where
T: Ord + Send,
T: Ord + Send + Clone,
{
if arr.len() <= 1 {
return;
Expand All @@ -197,24 +202,24 @@ where

fn partition<T>(arr: &mut [T]) -> usize
where
T: Ord + Send,
T: Ord + Send + Clone,
{
let len = arr.len();
if len == 0 {
return 0;
}
let pivot_index = len / 2;
let pivot = arr[pivot_index].clone();

arr.swap(pivot_index, len - 1);

let mut store_index = 0;
for i in 0..(len - 1) {
if arr[i] <= arr[len - 1] {
arr.swap(i, store_index);
store_index += 1;
let mut i = 0;
let mut j = len - 1;
loop {
while arr[i] < pivot {
i += 1;
}
while arr[j] > pivot {
j -= 1;
}
if i >= j {
return j;
}
arr.swap(i, j);
}
arr.swap(store_index, len - 1);

store_index
}
4 changes: 2 additions & 2 deletions src/num/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub trait Num {
/// Calculate sum of number digits.
fn sum(self) -> Self;

/// Calculates factorial of number (result is BigInt)
/// Calculates factorial of number (result is num_bigint::BigInt).
fn factorial(self) -> BigInt;
}

Expand Down Expand Up @@ -118,7 +118,7 @@ macro_rules! impl_num {
false
}

/// Factorial of number
/// Factorial of number (result is num_bigint::BigInt).
/// # Examples
///
/// ```
Expand Down
62 changes: 15 additions & 47 deletions src/num/radix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,38 +926,25 @@ impl Radix {
pub fn to_radix(self, k: u8) -> Result<Self, RadixError> {
match k {
0 | 1 | 11.. => Err(RadixError::BaseError(10, k)),
10 => Ok(self.to_dec()?),
10 => Ok(self.to_dec()),
_ =>
if self.base == 10 {
Ok(self.to_radix_from_dec(k)?)
} else {
Ok(self.to_dec()?.to_radix_from_dec(k)?)
Ok(self.to_dec().to_radix_from_dec(k)?)
},
}
}

fn to_dec(self) -> Result<Self, RadixError> {
Ok(Radix::from(
match usize::from_str_radix(&self.number.to_string(), self.base.into()) {
Ok(n) => n,
Err(e) => return Err(RadixError::ParseError(e)),
},
))
}
fn to_dec(self) -> Self { Self::from(dec!(self)) }

fn to_radix_from_dec(mut self, k: u8) -> Result<Self, RadixError> {
let mut res = String::new();
while self.number != 0 {
res.push(RADIX[self.number % (k as usize)]);
self.number /= k as usize;
}
Radix::from_radix(
match res.chars().rev().collect::<String>().parse() {
Ok(n) => n,
Err(e) => return Err(RadixError::ParseError(e)),
},
k,
)
Self::from_radix(res.chars().rev().collect::<String>().parse()?, k)
}

/// Translate [`Radix`] to another [`StringRadix`].
Expand Down Expand Up @@ -988,12 +975,12 @@ impl Radix {
pub fn to_str_radix(self, k: u8) -> Result<StringRadix, RadixError> {
match k {
0 | 1 | 37.. => Err(RadixError::BaseError(36, k)),
10 => Ok(StringRadix::from(self.to_dec()?.number)),
10 => Ok(StringRadix::from(self.to_dec().number)),
_ =>
if self.base == 10 {
Ok(self.to_str_radix_from_dec(k)?)
} else {
Ok(self.to_dec()?.to_str_radix_from_dec(k)?)
Ok(self.to_dec().to_str_radix_from_dec(k)?)
},
}
}
Expand Down Expand Up @@ -1237,38 +1224,28 @@ impl StringRadix {
pub fn to_radix(&mut self, k: u8) -> Result<Self, RadixError> {
match k {
0 | 1 | 37.. => Err(RadixError::BaseError(36, k)),
10 => Ok(Self::from(self.to_dec()?.number)),
10 => Ok(Self::from(self.to_dec().number)),
_ =>
if self.base == 10 {
Ok(Self::from_dec(
&mut Radix::from(match self.number.parse::<usize>() {
Ok(n) => n,
Err(e) => return Err(RadixError::ParseError(e)),
}),
&mut Radix::from(self.number.parse::<usize>()?),
k,
)?)
} else {
Ok(Self::from_dec(&mut self.to_dec()?, k)?)
Ok(Self::from_dec(&mut self.to_dec(), k)?)
},
}
}

fn to_dec(&self) -> Result<Radix, RadixError> {
Ok(Radix::from(
match usize::from_str_radix(&self.number, self.base.into()) {
Ok(n) => n,
Err(e) => return Err(RadixError::ParseError(e)),
},
))
}
fn to_dec(&self) -> Radix { Radix::from(dec!(self)) }

fn from_dec(n: &mut Radix, k: u8) -> Result<Self, RadixError> {
let mut res = String::new();
while n.number != 0 {
res.push(RADIX[n.number % (k as usize)]);
n.number /= k as usize;
}
StringRadix::from_radix(&res.chars().rev().collect::<String>(), k)
Self::from_radix(&res.chars().rev().collect::<String>(), k)
}

/// Translate [`StringRadix`] to another [`Radix`].
Expand Down Expand Up @@ -1299,18 +1276,15 @@ impl StringRadix {
pub fn to_int_radix(&mut self, k: u8) -> Result<Radix, RadixError> {
match k {
0 | 1 | 11.. => Err(RadixError::BaseError(10, k)),
10 => Ok(self.to_dec()?),
10 => Ok(self.to_dec()),
_ =>
if self.base == 10 {
Ok(Self::from_dec_to_int(
&mut Radix::from(match self.number.parse::<usize>() {
Ok(n) => n,
Err(e) => return Err(RadixError::ParseError(e)),
}),
&mut Radix::from(self.number.parse::<usize>()?),
k,
)?)
} else {
Ok(Self::from_dec_to_int(&mut self.to_dec()?, k)?)
Ok(Self::from_dec_to_int(&mut self.to_dec(), k)?)
},
}
}
Expand All @@ -1321,13 +1295,7 @@ impl StringRadix {
res.push(RADIX[n.number % (k as usize)]);
n.number /= k as usize;
}
Radix::from_radix(
match res.chars().rev().collect::<String>().parse() {
Ok(n) => n,
Err(e) => return Err(RadixError::ParseError(e)),
},
k,
)
Radix::from_radix(res.chars().rev().collect::<String>().parse()?, k)
}

/// Sum 2 [`StringRadix`] to new [`Radix`].
Expand Down

0 comments on commit 8dc02ef

Please sign in to comment.