Skip to content

Commit

Permalink
reconfigure rustfmt.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
ognevny committed Feb 3, 2024
1 parent f4204b6 commit 9abe78e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 82 deletions.
13 changes: 10 additions & 3 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
edition = "2021"
max_width = 100
array_width = 92
use_small_heuristics = "Max"
tab_spaces = 4
newline_style = "unix" # f*** windows style
newline_style = "Unix"
reorder_imports = true
use_field_init_shorthand = true
use_try_shorthand = true
match_block_trailing_comma = true

# they seem to be stable in nearest future, but for now we use it only for nightly CI check
unstable_features = true
version = "Two"
condense_wildcard_suffixes = true
comment_width = 100
comment_width = 0
doc_comment_code_block_width = 100
format_code_in_doc_comments = true
format_macro_bodies = true
format_macro_matchers = true
format_strings = true
fn_single_line = true
overflow_delimited_expr = true
reorder_impl_items = true
imports_granularity = "One"
inline_attribute_width = 100
match_arm_blocks = false
normalize_comments = true
where_single_line = true
wrap_comments = true
46 changes: 25 additions & 21 deletions src/algorithm/extra.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
//! A list of algorithms that is not categorized currently.
//! A
//! list
//! of
//! algorithms
//! that
//! is
//! not
//! categorized
//! currently.
/// A binary search algorithm (sorted array is requiered).
/// A
/// binary
/// search
/// algorithm
/// (sorted
/// array
/// is
/// requiered).
/// # Examples
///
/// ```
Expand Down Expand Up @@ -36,10 +51,7 @@ pub fn bin_search<T: Ord>(arr: &[T], targ: T) -> Option<usize> {
/// use ognlib::algorithm::extra::mask_match;
///
/// let nums1 = mask_match(100, 200, "1?0");
/// assert_eq!(
/// nums1,
/// vec![100, 110, 120, 130, 140, 150, 160, 170, 180, 190]
/// );
/// assert_eq!(nums1, vec![100, 110, 120, 130, 140, 150, 160, 170, 180, 190]);
///
/// let nums2 = mask_match(1, 10, "*");
/// assert_eq!(nums2, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
Expand All @@ -51,18 +63,13 @@ pub fn bin_search<T: Ord>(arr: &[T], targ: T) -> Option<usize> {
/// ```
/// use {ognlib::algorithm::extra::mask_match, rayon::prelude::*};
///
/// let nums_div_by_169: Vec<usize> = mask_match(1, 1000000000, "123*567?")
/// .into_par_iter()
/// .filter(|&x| x % 169 == 0)
/// .collect();
/// let nums_div_by_169: Vec<usize> =
/// mask_match(1, 1000000000, "123*567?").into_par_iter().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,
/// ]
/// )
/// 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> {
Expand All @@ -73,8 +80,5 @@ pub fn mask_match(lower: usize, upper: usize, mask: &str) -> Vec<usize> {
let mask = mask.replace('*', ".*");
let mask = mask.replace('?', ".?");
let re = Regex::new(&("^".to_owned() + &mask + "$")).unwrap();
(lower..=upper)
.into_par_iter()
.filter(|num| re.is_match(&num.to_string()))
.collect()
(lower..=upper).into_par_iter().filter(|num| re.is_match(&num.to_string())).collect()
}
11 changes: 2 additions & 9 deletions src/algorithm/prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ impl Prime for isize {
/// ```
/// use ognlib::algorithm::prime::{sqrtest, PrimeStatus, PrimeStatusError};
///
/// assert_eq!(
/// sqrtest(1).unwrap_err().to_string(),
/// "This number is neither prime nor composite",
/// );
/// assert_eq!(sqrtest(1).unwrap_err().to_string(), "This number is neither prime nor composite",);
/// assert_eq!(sqrtest(13).ok(), Some(PrimeStatus::Prime));
/// assert_eq!(sqrtest(455).ok(), Some(PrimeStatus::Composite));
/// ```
Expand Down Expand Up @@ -174,11 +171,7 @@ pub fn wilson_th(n: isize) -> Result<PrimeStatus, PrimeStatusError> {
fact = (fact * i) % n;
}

if fact + 1 == BigInt::from(n) {
Ok(PrimeStatus::Prime)
} else {
Ok(PrimeStatus::Composite)
}
if fact + 1 == BigInt::from(n) { Ok(PrimeStatus::Prime) } else { Ok(PrimeStatus::Composite) }
}

/// Miller-Rabin's prime test. From
Expand Down
12 changes: 3 additions & 9 deletions src/algorithm/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ pub fn insertion<T: Ord>(arr: &mut [T]) {
/// ```
pub fn merge<T>(slice: &mut [T])
where
T: Ord + Clone + Copy + Send + Sync,
{
where T: Ord + Clone + Copy + Send + Sync {
let len = slice.len();
if len < 2 {
return;
Expand Down Expand Up @@ -188,9 +186,7 @@ pub fn cocktail_shaker<T: Ord>(arr: &mut [T]) {
/// ```
pub fn quick<T>(arr: &mut [T])
where
T: Ord + Send + Clone,
{
where T: Ord + Send + Clone {
if arr.len() <= 1 {
return;
}
Expand All @@ -201,9 +197,7 @@ where
}

fn partition<T>(arr: &mut [T]) -> usize
where
T: Ord + Send + Clone,
{
where T: Ord + Send + Clone {
let len = arr.len();
let pivot_index = len / 2;
let pivot = arr[pivot_index].clone();
Expand Down
3 changes: 1 addition & 2 deletions src/num/methods.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Functions for operations with number digits. It has already been tested, that Iterators are less
//! quick, than `while` loops in these cases.
#[cfg(feature = "num-bigint")]
use {num_bigint::BigInt, rayon::prelude::*};
#[cfg(feature = "num-bigint")] use {num_bigint::BigInt, rayon::prelude::*};

pub trait Num {
/// Represent number as bool like in C.
Expand Down
8 changes: 2 additions & 6 deletions src/num/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use std::ops::{Mul, MulAssign, Rem, RemAssign};
/// ```
pub fn bin_pow<N>(mut b: N, mut e: u8) -> N
where
N: MulAssign + From<u8> + Copy,
{
where N: MulAssign + From<u8> + Copy {
let mut v = N::from(1);
while e != 0 {
if (e & 1) != 0 {
Expand All @@ -42,9 +40,7 @@ where
/// ```
pub fn modpow<N>(mut b: N, mut e: usize, m: N) -> N
where
N: Mul<Output = N> + Rem<Output = N> + RemAssign + From<u8> + Copy + Eq,
{
where N: Mul<Output = N> + Rem<Output = N> + RemAssign + From<u8> + Copy + Eq {
let mut result = N::from(1);
b %= m;
while e != 0 {
Expand Down
40 changes: 8 additions & 32 deletions src/num/radix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ impl FromStr for Radix {
/// ```
fn from_str(number: &str) -> Result<Self, Self::Err> {
Ok(Self {
number: number.parse()?,
base: 10,
})
Ok(Self { number: number.parse()?, base: 10 })
}
}

Expand Down Expand Up @@ -125,10 +122,7 @@ impl FromStr for StringRadix {
/// ```
fn from_str(number: &str) -> Result<Self, Self::Err> {
Ok(Self {
number: number.parse::<usize>()?.to_string(),
base: 10,
})
Ok(Self { number: number.parse::<usize>()?.to_string(), base: 10 })
}
}

Expand Down Expand Up @@ -892,10 +886,7 @@ impl Radix {
/// assert_eq!(n.base(), 2);
///
/// let e = Radix::from_radix(444, 3).unwrap_err();
/// assert_eq!(
/// e.to_string(),
/// "Number contains a digit (4) that is more or equal than base (3)",
/// );
/// assert_eq!(e.to_string(), "Number contains a digit (4) that is more or equal than base (3)",);
/// ```
pub fn from_radix(number: usize, base: u8) -> Result<Self, RadixError> {
Expand Down Expand Up @@ -1160,10 +1151,7 @@ impl StringRadix {
pub fn new(base: u8) -> Result<Self, RadixError> {
match base {
0 | 1 | 37.. => Err(RadixError::BaseError(36, base)),
_ => Ok(Self {
number: "0".to_owned(),
base,
}),
_ => Ok(Self { number: "0".to_owned(), base }),
}
}

Expand All @@ -1186,10 +1174,7 @@ impl StringRadix {
/// assert_eq!(n.base(), 2);
///
/// let e = StringRadix::from_radix("129", 9).unwrap_err();
/// assert_eq!(
/// e.to_string(),
/// "Number contains a digit (9) that is more or equal than base (9)",
/// );
/// assert_eq!(e.to_string(), "Number contains a digit (9) that is more or equal than base (9)",);
/// ```
pub fn from_radix(number: &str, base: u8) -> Result<Self, RadixError> {
Expand All @@ -1205,10 +1190,7 @@ impl StringRadix {
}) {
err
} else {
Ok(Self {
number: number.to_owned(),
base,
})
Ok(Self { number: number.to_owned(), base })
}
},
}
Expand Down Expand Up @@ -1287,10 +1269,7 @@ impl StringRadix {
10 => Ok(Self::from(self.to_dec().number)),
_ =>
if self.base == 10 {
Ok(Self::from_dec(
&mut Radix::from(self.number.parse::<usize>()?),
k,
)?)
Ok(Self::from_dec(&mut Radix::from(self.number.parse::<usize>()?), k)?)
} else {
Ok(Self::from_dec(&mut self.to_dec(), k)?)
},
Expand Down Expand Up @@ -1340,10 +1319,7 @@ impl StringRadix {
10 => Ok(self.to_dec()),
_ =>
if self.base == 10 {
Ok(Self::from_dec_to_int(
&mut Radix::from(self.number.parse::<usize>()?),
k,
)?)
Ok(Self::from_dec_to_int(&mut Radix::from(self.number.parse::<usize>()?), k)?)
} else {
Ok(Self::from_dec_to_int(&mut self.to_dec(), k)?)
},
Expand Down

0 comments on commit 9abe78e

Please sign in to comment.