ChtLine

Struct ChtLine 

Source
pub struct ChtLine<T> {
    slope: T,
    intercept: T,
}

Fields§

§slope: T§intercept: T

Implementations§

Source§

impl<T> ChtLine<T>
where T: Copy + PartialOrd + Add<Output = T> + Sub<Output = T> + Mul<Output = T>,

Source

pub fn new(a: T, b: T) -> Self

Examples found in repository?
crates/competitive/src/algorithm/convex_hull_trick.rs (line 80)
78    pub fn add_line(&mut self, a: T, b: T) {
79        if self.deq.is_empty() {
80            self.deq.push_back(ChtLine::new(a, b));
81        } else if let Some(l) = self.deq.back()
82            && l.slope >= a
83        {
84            let line = ChtLine::new(a, b);
85            while {
86                let k = self.deq.len();
87                k > 1 && self.deq[k - 2].check(&self.deq[k - 1], &line)
88            } {
89                self.deq.pop_back();
90            }
91            self.deq.push_back(line);
92        } else if let Some(l) = self.deq.front()
93            && l.slope <= a
94        {
95            let line = ChtLine::new(a, b);
96            while {
97                let k = self.deq.len();
98                k > 1 && line.check(&self.deq[0], &self.deq[1])
99            } {
100                self.deq.pop_front();
101            }
102            self.deq.push_front(line);
103        } else {
104            panic!("a must be monotonic");
105        }
106    }
Source

pub fn value(&self, x: T) -> T

Examples found in repository?
crates/competitive/src/algorithm/convex_hull_trick.rs (line 114)
108    pub fn query_min(&mut self, x: T) -> T {
109        assert!(!self.deq.is_empty(), "no lines added");
110        match self.query_mode {
111            ChtQueryMode::Increasing => {
112                while {
113                    let k = self.deq.len();
114                    k > 1 && self.deq[0].value(x) >= self.deq[1].value(x)
115                } {
116                    self.deq.pop_front();
117                }
118                self.deq.front().unwrap().value(x)
119            }
120            ChtQueryMode::Decreasing => {
121                while {
122                    let k = self.deq.len();
123                    k > 1 && self.deq[k - 1].value(x) >= self.deq[k - 2].value(x)
124                } {
125                    self.deq.pop_back();
126                }
127                self.deq.back().unwrap().value(x)
128            }
129            ChtQueryMode::Any => {
130                let mut l = 0;
131                let mut r = self.deq.len() - 1;
132                while l < r {
133                    let m = (l + r) / 2;
134                    if self.deq[m].value(x) >= self.deq[m + 1].value(x) {
135                        l = m + 1;
136                    } else {
137                        r = m;
138                    }
139                }
140                self.deq[l].value(x)
141            }
142        }
143    }
Source

pub fn check(&self, l1: &Self, l2: &Self) -> bool

Examples found in repository?
crates/competitive/src/algorithm/convex_hull_trick.rs (line 87)
78    pub fn add_line(&mut self, a: T, b: T) {
79        if self.deq.is_empty() {
80            self.deq.push_back(ChtLine::new(a, b));
81        } else if let Some(l) = self.deq.back()
82            && l.slope >= a
83        {
84            let line = ChtLine::new(a, b);
85            while {
86                let k = self.deq.len();
87                k > 1 && self.deq[k - 2].check(&self.deq[k - 1], &line)
88            } {
89                self.deq.pop_back();
90            }
91            self.deq.push_back(line);
92        } else if let Some(l) = self.deq.front()
93            && l.slope <= a
94        {
95            let line = ChtLine::new(a, b);
96            while {
97                let k = self.deq.len();
98                k > 1 && line.check(&self.deq[0], &self.deq[1])
99            } {
100                self.deq.pop_front();
101            }
102            self.deq.push_front(line);
103        } else {
104            panic!("a must be monotonic");
105        }
106    }

Trait Implementations§

Source§

impl<T: Clone> Clone for ChtLine<T>

Source§

fn clone(&self) -> ChtLine<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for ChtLine<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for ChtLine<T>

Source§

fn default() -> ChtLine<T>

Returns the “default value” for a type. Read more
Source§

impl<T: PartialEq> PartialEq for ChtLine<T>

Source§

fn eq(&self, other: &ChtLine<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for ChtLine<T>

Source§

impl<T> StructuralPartialEq for ChtLine<T>

Auto Trait Implementations§

§

impl<T> Freeze for ChtLine<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ChtLine<T>
where T: RefUnwindSafe,

§

impl<T> Send for ChtLine<T>
where T: Send,

§

impl<T> Sync for ChtLine<T>
where T: Sync,

§

impl<T> Unpin for ChtLine<T>
where T: Unpin,

§

impl<T> UnwindSafe for ChtLine<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.