pub struct Ranged<const MIN: i128, const MAX: i128>{ /* private fields */ }Expand description
A value restricted to the given bounds
Implementations§
Source§impl<const MIN: i128, const MAX: i128> Ranged<MIN, MAX>
impl<const MIN: i128, const MAX: i128> Ranged<MIN, MAX>
Source§impl<const MIN: i128, const MAX: i128> Ranged<MIN, MAX>
impl<const MIN: i128, const MAX: i128> Ranged<MIN, MAX>
Sourcepub const fn expand<const RMIN: i128, const RMAX: i128>(
self,
) -> Ranged<RMIN, RMAX>
pub const fn expand<const RMIN: i128, const RMAX: i128>( self, ) -> Ranged<RMIN, RMAX>
Convert to the Ranged with the wider bounds
Examples found in repository?
26pub fn place_number(pos: Ranged<0, 80>, sudoku_ar: &mut Sudoku) -> bool {
27 pos.iter_up()
28 .find_map(|p| {
29 let (x, y) = (p % r!(9), p / r!(9));
30 if sudoku_ar[x][y] == r!(0) {Some((x,y))} else {None}
31 })
32 .is_none_or(|(x, y)| {
33 for n in r!(1..=9) {
34 if is_valid(n.expand(), x, y, sudoku_ar) {
35 sudoku_ar[x][y] = n.expand();
36 let next = if let Some(next) = (pos + r!(1)).fit() {next} else {return true};
37 if place_number(next,sudoku_ar) {
38 return true;
39 }
40 sudoku_ar[x][y] = r!([]0);
41 }
42 }
43 false
44 })
45}Sourcepub const fn fit<const RMIN: i128, const RMAX: i128>(
self,
) -> Option<Ranged<RMIN, RMAX>>
pub const fn fit<const RMIN: i128, const RMAX: i128>( self, ) -> Option<Ranged<RMIN, RMAX>>
Convert to the other Ranged, returning None if the value is out of range
Examples found in repository?
26pub fn place_number(pos: Ranged<0, 80>, sudoku_ar: &mut Sudoku) -> bool {
27 pos.iter_up()
28 .find_map(|p| {
29 let (x, y) = (p % r!(9), p / r!(9));
30 if sudoku_ar[x][y] == r!(0) {Some((x,y))} else {None}
31 })
32 .is_none_or(|(x, y)| {
33 for n in r!(1..=9) {
34 if is_valid(n.expand(), x, y, sudoku_ar) {
35 sudoku_ar[x][y] = n.expand();
36 let next = if let Some(next) = (pos + r!(1)).fit() {next} else {return true};
37 if place_number(next,sudoku_ar) {
38 return true;
39 }
40 sudoku_ar[x][y] = r!([]0);
41 }
42 }
43 false
44 })
45}Sourcepub const fn fit_max<const RMAX: i128>(self) -> Option<Ranged<MIN, RMAX>>
pub const fn fit_max<const RMAX: i128>(self) -> Option<Ranged<MIN, RMAX>>
Change the MIN value of Ranged bounds, returning None if the value is out of range
Sourcepub const fn fit_min<const RMIN: i128>(self) -> Option<Ranged<RMIN, MAX>>
pub const fn fit_min<const RMIN: i128>(self) -> Option<Ranged<RMIN, MAX>>
Change the MAX value of Ranged bounds, returning None if the value is out of range
Sourcepub const fn fit_less_than<const RMIN: i128, const RMAX: i128>(
self,
other: Ranged<RMIN, RMAX>,
) -> Option<Ranged<MIN, { _ }>>
pub const fn fit_less_than<const RMIN: i128, const RMAX: i128>( self, other: Ranged<RMIN, RMAX>, ) -> Option<Ranged<MIN, { _ }>>
Compares two Ranged values. If self is less than the other, it
returns a Ranged with the same value and shrunk bounds.
Allowed only if the ranges interleave.
Sourcepub const fn fit_less_eq<const RMIN: i128, const RMAX: i128>(
self,
other: Ranged<RMIN, RMAX>,
) -> Option<Ranged<MIN, RMAX>>
pub const fn fit_less_eq<const RMIN: i128, const RMAX: i128>( self, other: Ranged<RMIN, RMAX>, ) -> Option<Ranged<MIN, RMAX>>
Compares two Ranged values. If self is less than or equal to the other, it
returns a Ranged with the same value and shrunk bounds.
Allowed only if the ranges interleave.
Sourcepub const fn fit_greater_than<const RMIN: i128, const RMAX: i128>(
self,
other: Ranged<RMIN, RMAX>,
) -> Option<Ranged<{ _ }, MAX>>
pub const fn fit_greater_than<const RMIN: i128, const RMAX: i128>( self, other: Ranged<RMIN, RMAX>, ) -> Option<Ranged<{ _ }, MAX>>
Compares two Ranged values. If self is greater than the other, it
returns a Ranged with the same value and shrunk bounds.
Allowed only if the ranges interleave.
Sourcepub const fn fit_greater_eq<const RMIN: i128, const RMAX: i128>(
self,
other: Ranged<RMIN, RMAX>,
) -> Option<Ranged<RMIN, MAX>>
pub const fn fit_greater_eq<const RMIN: i128, const RMAX: i128>( self, other: Ranged<RMIN, RMAX>, ) -> Option<Ranged<RMIN, MAX>>
Compares two Ranged values. If self is greater than or equal to the other, it
returns a Ranged with the same value and shrunk bounds.
Allowed only if the ranges interleave.
Sourcepub const fn split<const MID: i128>(self) -> Split<MIN, MID, MAX>
pub const fn split<const MID: i128>(self) -> Split<MIN, MID, MAX>
Simple case analysis for Ranged
Sourcepub const fn split_subtract<const BMIN: i128, const BMAX: i128>(
self,
other: Ranged<BMIN, BMAX>,
) -> SplitByDifference<MIN, MAX, BMIN, BMAX>
pub const fn split_subtract<const BMIN: i128, const BMAX: i128>( self, other: Ranged<BMIN, BMAX>, ) -> SplitByDifference<MIN, MAX, BMIN, BMAX>
Narrow ranges guiding by the subtraction of two values
Allowed only if the ranges overlap.
Source§impl<const MIN: i128, const MAX: i128> Ranged<MIN, MAX>
impl<const MIN: i128, const MAX: i128> Ranged<MIN, MAX>
Sourcepub const fn add<const BMIN: i128, const BMAX: i128>(
self,
rhs: Ranged<BMIN, BMAX>,
) -> Ranged<{ _ }, { _ }>
pub const fn add<const BMIN: i128, const BMAX: i128>( self, rhs: Ranged<BMIN, BMAX>, ) -> Ranged<{ _ }, { _ }>
Adds two ranged integers, proves the result bounds
Sourcepub const fn sub<const BMIN: i128, const BMAX: i128>(
self,
rhs: Ranged<BMIN, BMAX>,
) -> Ranged<{ _ }, { _ }>
pub const fn sub<const BMIN: i128, const BMAX: i128>( self, rhs: Ranged<BMIN, BMAX>, ) -> Ranged<{ _ }, { _ }>
Subtracts two ranged integers, proves the result bounds
Sourcepub const fn mul<const BMIN: i128, const BMAX: i128>(
self,
rhs: Ranged<BMIN, BMAX>,
) -> Ranged<{ _ }, { _ }>
pub const fn mul<const BMIN: i128, const BMAX: i128>( self, rhs: Ranged<BMIN, BMAX>, ) -> Ranged<{ _ }, { _ }>
Multiplies two ranged integers, proves the result bounds
Sourcepub const fn div<const BMIN: i128, const BMAX: i128>(
self,
rhs: Ranged<BMIN, BMAX>,
) -> Ranged<{ _ }, { _ }>
pub const fn div<const BMIN: i128, const BMAX: i128>( self, rhs: Ranged<BMIN, BMAX>, ) -> Ranged<{ _ }, { _ }>
Divides two ranged integers, proves the result bounds
Sourcepub const fn rem<const BMIN: i128, const BMAX: i128>(
self,
rhs: Ranged<BMIN, BMAX>,
) -> Ranged<{ _ }, { _ }>
pub const fn rem<const BMIN: i128, const BMAX: i128>( self, rhs: Ranged<BMIN, BMAX>, ) -> Ranged<{ _ }, { _ }>
Takes a remainder of two ranged integers division, proves the result bounds
Sourcepub const fn min<const BMIN: i128, const BMAX: i128>(
self,
other: Ranged<BMIN, BMAX>,
) -> Ranged<{ _ }, { _ }>
pub const fn min<const BMIN: i128, const BMAX: i128>( self, other: Ranged<BMIN, BMAX>, ) -> Ranged<{ _ }, { _ }>
Returns the minimum of two values
Sourcepub const fn max<const BMIN: i128, const BMAX: i128>(
self,
other: Ranged<BMIN, BMAX>,
) -> Ranged<{ _ }, { _ }>
pub const fn max<const BMIN: i128, const BMAX: i128>( self, other: Ranged<BMIN, BMAX>, ) -> Ranged<{ _ }, { _ }>
Returns the maximum of two values
Sourcepub const fn div_euclid<const BMIN: i128, const BMAX: i128>(
self,
rhs: Ranged<BMIN, BMAX>,
) -> Ranged<{ _ }, { _ }>
pub const fn div_euclid<const BMIN: i128, const BMAX: i128>( self, rhs: Ranged<BMIN, BMAX>, ) -> Ranged<{ _ }, { _ }>
Calculates the quotient of Euclidean division of self by rhs
Sourcepub const fn rem_euclid<const BMIN: i128, const BMAX: i128>(
self,
rhs: Ranged<BMIN, BMAX>,
) -> Ranged<{ _ }, { _ }>
pub const fn rem_euclid<const BMIN: i128, const BMAX: i128>( self, rhs: Ranged<BMIN, BMAX>, ) -> Ranged<{ _ }, { _ }>
Calculates the Euclidean mod of self by rhs
Source§impl<const MIN: i128, const MAX: i128> Ranged<MIN, MAX>
impl<const MIN: i128, const MAX: i128> Ranged<MIN, MAX>
Sourcepub const unsafe fn unchecked_new(n: i128) -> Self
pub const unsafe fn unchecked_new(n: i128) -> Self
Create a Ranged value without checking the bounds.
§Safety
The value (parameter n) must be inside the inclusive range MIN..=MAX.
Having an integer outside the bounds is undefined behavior.
Sourcepub const fn new(n: i128) -> Option<Self>
pub const fn new(n: i128) -> Option<Self>
Create a Ranged value checking the bounds at runtime
Note: due to incomplete features limitations, the function may fail to compile with the ‘trait bound is not satisfied’ if the bounds are not explicitly specified.
§Example
let input = "42".to_string();
let user_input = input.parse()?;
if let Some(input) = Ranged::<1, 100>::new(user_input){
println!("The value is in range 1..=100")
}
else {
println!("The value is too high :(")
}Source§impl<const MIN: i128, const MAX: i128> Ranged<MIN, MAX>
impl<const MIN: i128, const MAX: i128> Ranged<MIN, MAX>
Sourcepub const fn create_const<const V: i128>() -> Self
pub const fn create_const<const V: i128>() -> Self
Sourcepub const fn iter_up(self) -> Iter<MIN, MAX>
pub const fn iter_up(self) -> Iter<MIN, MAX>
Iterate up from current value to Self::MAX (inclusively) using Self as output
Examples found in repository?
26pub fn place_number(pos: Ranged<0, 80>, sudoku_ar: &mut Sudoku) -> bool {
27 pos.iter_up()
28 .find_map(|p| {
29 let (x, y) = (p % r!(9), p / r!(9));
30 if sudoku_ar[x][y] == r!(0) {Some((x,y))} else {None}
31 })
32 .is_none_or(|(x, y)| {
33 for n in r!(1..=9) {
34 if is_valid(n.expand(), x, y, sudoku_ar) {
35 sudoku_ar[x][y] = n.expand();
36 let next = if let Some(next) = (pos + r!(1)).fit() {next} else {return true};
37 if place_number(next,sudoku_ar) {
38 return true;
39 }
40 sudoku_ar[x][y] = r!([]0);
41 }
42 }
43 false
44 })
45}