Skip to content

Commit 2073670

Browse files
committed
Rust 2024 edition
1 parent 6c5d544 commit 2073670

File tree

12 files changed

+23
-31
lines changed

12 files changed

+23
-31
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
22
name = "contest-algorithms"
3-
version = "0.3.1-alpha.0"
3+
version = "0.3.1-alpha.1"
44
authors = ["Aram Ebtekar"]
5-
edition = "2021"
5+
edition = "2024"
66

77
description = "Common algorithms and data structures for programming contests"
88
repository = "https://github.com/EbTech/rust-algorithms"

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ Some contest sites and online judges that support Rust:
3535
- [HackerRank](https://www.hackerrank.com/contests)
3636
- [Timus](http://acm.timus.ru/help.aspx?topic=rust)
3737

38-
The following support pre-2018 versions of Rust:
39-
- [Google Kick Start and Code Jam](https://codingcompetitions.withgoogle.com)
40-
41-
For help in getting started, you may check out [some of my past submissions](https://codeforces.com/contest/1168/submission/55200038).
38+
For help in getting started, you may check out [some of my past submissions](https://codeforces.com/contest/1168/submission/55200038) (requires login).
4239

4340
## Programming Language Advocacy
4441

src/graph/flow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl FlowGraph {
143143
let (mut min_cost, mut max_flow) = (0, 0);
144144
loop {
145145
let par = self.mcf_search(s, &flow, &mut pot);
146-
if par[t] == None {
146+
if par[t].is_none() {
147147
break;
148148
}
149149
let (dc, df) = self.mcf_augment(t, &par, &mut flow);

src/graph/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub struct AdjListIterator<'a> {
111111
next_e: Option<usize>,
112112
}
113113

114-
impl<'a> Iterator for AdjListIterator<'a> {
114+
impl Iterator for AdjListIterator<'_> {
115115
type Item = (usize, usize);
116116

117117
/// Produces an outgoing edge and vertex.

src/graph/util.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ impl Graph {
1212
.map(|u| self.adj_list(u))
1313
.collect::<Vec<_>>();
1414
let mut edges = Vec::with_capacity(self.num_e());
15-
self.euler_recurse(u, &mut adj_iters, &mut edges);
15+
Self::euler_recurse(u, &mut adj_iters, &mut edges);
1616
edges.reverse();
1717
edges
1818
}
1919

2020
// Helper function used by euler_path. Note that we can't use a for-loop
2121
// that would consume the adjacency list as recursive calls may need it.
22-
fn euler_recurse(&self, u: usize, adj: &mut [AdjListIterator], edges: &mut Vec<usize>) {
22+
fn euler_recurse(u: usize, adj: &mut [AdjListIterator], edges: &mut Vec<usize>) {
2323
while let Some((e, v)) = adj[u].next() {
24-
self.euler_recurse(v, adj, edges);
24+
Self::euler_recurse(v, adj, edges);
2525
edges.push(e);
2626
}
2727
}
@@ -42,7 +42,7 @@ impl Graph {
4242
// Single-source shortest paths on a directed graph with non-negative weights
4343
pub fn dijkstra(&self, weights: &[u64], u: usize) -> Vec<u64> {
4444
assert_eq!(self.num_e(), weights.len());
45-
let mut dist = vec![u64::max_value(); weights.len()];
45+
let mut dist = vec![u64::MAX; weights.len()];
4646
let mut heap = std::collections::BinaryHeap::new();
4747

4848
dist[u] = 0;
@@ -82,7 +82,7 @@ pub struct DfsIterator<'a> {
8282
adj_iters: Vec<AdjListIterator<'a>>,
8383
}
8484

85-
impl<'a> Iterator for DfsIterator<'a> {
85+
impl Iterator for DfsIterator<'_> {
8686
type Item = (usize, usize);
8787

8888
/// Returns next edge and vertex in the depth-first traversal

src/li_chao.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
/// A structure for answering maximum queries on a set of linear functions. Supports two
2-
/// operations: inserting a linear function and querying for maximum at a given point.
2+
/// operations: inserting a linear function and querying for maximum at a given point.
33
/// The queries can be done in any order, and we can do all the calculations using integers.
44
/// https://cp-algorithms.com/geometry/convex_hull_trick.html#li-chao-tree
55
/// Compared to the code in the above link, this implementation further improves the algorithm by
66
/// reducing the number of nodes to (right - left). This is done by removing the midpoint of a
77
/// segment from both children. Even better, this allows the index of a node to just be the
88
/// midpoint of the interval!
9-
9+
///
1010
/// Just like normal segment trees, this could be modified to a dynamic tree when the range is
1111
/// huge, or if the queries are known in advance the x-coordinates can be compressed.
1212
/// (it can also be made persistent!).
13-
1413
pub struct LiChaoTree {
1514
left: i64,
1615
right: i64,
@@ -23,7 +22,7 @@ impl LiChaoTree {
2322
Self {
2423
left,
2524
right,
26-
lines: vec![(0, std::i64::MIN); (right - left) as usize],
25+
lines: vec![(0, i64::MIN); (right - left) as usize],
2726
}
2827
}
2928

@@ -100,7 +99,7 @@ mod test {
10099
];
101100
let mut li_chao = LiChaoTree::new(0, 6);
102101

103-
assert_eq!(li_chao.evaluate(0), std::i64::MIN);
102+
assert_eq!(li_chao.evaluate(0), i64::MIN);
104103
for (&(slope, intercept), expected) in lines.iter().zip(results.iter()) {
105104
li_chao.max_with(slope, intercept);
106105
let ys: Vec<i64> = xs.iter().map(|&x| li_chao.evaluate(x)).collect();

src/math/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ pub fn canon_egcd(a: i64, b: i64, c: i64) -> Option<(i64, i64, i64)> {
3131

3232
// TODO: deduplicate modular arithmetic code with num::Field
3333
fn pos_mod(n: i64, m: i64) -> i64 {
34-
if n < 0 {
35-
n + m
36-
} else {
37-
n
38-
}
34+
if n < 0 { n + m } else { n }
3935
}
4036
fn mod_mul(a: i64, b: i64, m: i64) -> i64 {
4137
pos_mod((a as i128 * b as i128 % m as i128) as i64, m)

src/range_query/dynamic_arq.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<T: ArqSpec> Default for DynamicArqNode<T> {
2424
Self {
2525
val: T::identity(),
2626
app: None,
27-
down: (usize::max_value(), usize::max_value()),
27+
down: (usize::MAX, usize::MAX),
2828
}
2929
}
3030
}
@@ -97,7 +97,7 @@ impl<T: ArqSpec> DynamicArq<T> {
9797
}
9898

9999
pub fn push(&mut self, (p, s): ArqView) -> (ArqView, ArqView) {
100-
if self.nodes[p].down.0 == usize::max_value() {
100+
if self.nodes[p].down.0 == usize::MAX {
101101
self.nodes.push(DynamicArqNode::default());
102102
self.nodes.push(DynamicArqNode::default());
103103
self.nodes[p].down = (self.nodes.len() - 2, self.nodes.len() - 1)

src/range_query/specs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl ArqSpec for AssignMin {
5050
a.min(b)
5151
}
5252
fn identity() -> Self::S {
53-
i64::max_value()
53+
i64::MAX
5454
}
5555
fn compose(&f: &Self::F, _: &Self::F) -> Self::F {
5656
f

src/range_query/static_arq.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ impl<T: ArqSpec> StaticArq<T> {
5050

5151
fn push(&mut self, p: usize) {
5252
if let Some(ref f) = self.app[p].take() {
53-
let s = ((self.app.len() + p - 1) / p / 2).next_power_of_two() as i64;
53+
let s = (self.app.len().div_ceil(p) / 2).next_power_of_two() as i64;
5454
self.apply(p << 1, f, s);
55-
self.apply(p << 1 | 1, f, s);
55+
self.apply((p << 1) | 1, f, s);
5656
}
5757
}
5858

5959
fn pull(&mut self, p: usize) {
60-
self.val[p] = T::op(&self.val[p << 1], &self.val[p << 1 | 1]);
60+
self.val[p] = T::op(&self.val[p << 1], &self.val[(p << 1) | 1]);
6161
}
6262

6363
fn push_to(&mut self, p: usize) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy