Skip to content

Commit

Permalink
Fix a few clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Oct 12, 2024
1 parent aeaefd9 commit f09956d
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 93 deletions.
7 changes: 4 additions & 3 deletions pineappl/src/boc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ pub enum ScaleFuncForm {

impl ScaleFuncForm {
/// TODO
#[must_use]
pub fn calc(&self, node_values: &[NodeValues], kinematics: &[Kinematics]) -> Option<Vec<f64>> {
match self {
ScaleFuncForm::NoScale => None,
&ScaleFuncForm::Scale(index) => Some(if node_values.is_empty() {
Self::NoScale => None,
&Self::Scale(index) => Some(if node_values.is_empty() {
// TODO: empty subgrid should have as many node values as dimensions
Vec::new()
} else {
Expand All @@ -55,7 +56,7 @@ impl ScaleFuncForm {
.unwrap()]
.values()
}),
ScaleFuncForm::QuadraticSum(_, _) => todo!(),
Self::QuadraticSum(_, _) => todo!(),
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions pineappl/src/convolutions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl<'a> ConvolutionCache<'a> {
matches!(kin, &Kinematics::X(index) if index == idx).then_some(node_values)
})
// UNWRAP: guaranteed by the grid constructor
.unwrap()
.unwrap_or_else(|| unreachable!())
.values()
.iter()
.map(|xd| {
Expand Down Expand Up @@ -343,7 +343,8 @@ pub enum ConvType {

impl ConvType {
/// TODO
pub fn new(polarized: bool, time_like: bool) -> Self {
#[must_use]
pub const fn new(polarized: bool, time_like: bool) -> Self {
match (polarized, time_like) {
(false, false) => Self::UnpolPDF,
(false, true) => Self::UnpolFF,
Expand All @@ -362,12 +363,14 @@ pub struct Conv {

impl Conv {
/// Constructor.
pub fn new(conv_type: ConvType, pid: i32) -> Self {
#[must_use]
pub const fn new(conv_type: ConvType, pid: i32) -> Self {
Self { conv_type, pid }
}

/// TODO
pub fn with_pid(&self, pid: i32) -> Self {
#[must_use]
pub const fn with_pid(&self, pid: i32) -> Self {
Self {
conv_type: self.conv_type,
pid,
Expand All @@ -390,7 +393,8 @@ impl Conv {
}

/// Return the convolution type of this convolution.
pub fn conv_type(&self) -> ConvType {
#[must_use]
pub const fn conv_type(&self) -> ConvType {
self.conv_type
}
}
Expand Down
2 changes: 1 addition & 1 deletion pineappl/src/empty_subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mod tests {
fn create_empty() {
let mut subgrid = EmptySubgridV1;
assert!(subgrid.is_empty());
subgrid.merge(&mut EmptySubgridV1.into(), None);
subgrid.merge(&EmptySubgridV1.into(), None);
subgrid.scale(2.0);
subgrid.symmetrize(1, 2);
assert_eq!(
Expand Down
68 changes: 31 additions & 37 deletions pineappl/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::interpolation::Interp;
use super::lagrange_subgrid::LagrangeSubgridV2;
use super::packed_subgrid::PackedQ1X2SubgridV1;
use super::pids::{self, PidBasis};
use super::subgrid::{Subgrid, SubgridEnum};
use super::subgrid::{NodeValues, Subgrid, SubgridEnum};
use super::v0;
use bitflags::bitflags;
use float_cmp::{approx_eq, assert_approx_eq};
Expand Down Expand Up @@ -206,12 +206,14 @@ impl Grid {
}

/// TODO
#[must_use]
pub fn kinematics(&self) -> &[Kinematics] {
&self.kinematics
}

/// TODO
pub fn scales(&self) -> &Scales {
#[must_use]
pub const fn scales(&self) -> &Scales {
&self.scales
}

Expand All @@ -232,7 +234,7 @@ impl Grid {
channel_mask: &[bool],
xi: &[(f64, f64, f64)],
) -> Vec<f64> {
convolution_cache.setup(self, &xi).unwrap();
convolution_cache.setup(self, xi).unwrap();

let bin_indices = if bin_indices.is_empty() {
(0..self.bin_info().bins()).collect()
Expand Down Expand Up @@ -351,19 +353,12 @@ impl Grid {
let node_values: Vec<_> = subgrid
.node_values()
.iter()
.map(|node_values| node_values.values())
.map(NodeValues::values)
.collect();
// TODO: generalize this to N dimensions
assert_eq!(node_values.len(), 3);

convolution_cache.set_grids(
self,
&subgrid.node_values(),
&node_values[0].iter().copied().collect::<Vec<_>>(),
xir,
xif,
xia,
);
convolution_cache.set_grids(self, &subgrid.node_values(), &node_values[0], xir, xif, xia);

let dim: Vec<_> = node_values.iter().map(Vec::len).collect();
let mut array = ArrayD::zeros(dim);
Expand Down Expand Up @@ -672,7 +667,7 @@ impl Grid {
if self.subgrids[[self_i, self_j, self_k]].is_empty() {
mem::swap(&mut self.subgrids[[self_i, self_j, self_k]], subgrid);
} else {
self.subgrids[[self_i, self_j, self_k]].merge(&mut *subgrid, None);
self.subgrids[[self_i, self_j, self_k]].merge(subgrid, None);
}
}

Expand Down Expand Up @@ -1022,14 +1017,13 @@ impl Grid {
.iter()
.enumerate()
.tuple_combinations()
.filter_map(|((idx_a, conv_a), (idx_b, conv_b))| {
(conv_a == conv_b).then(|| (idx_a, idx_b))
})
.filter(|((_, conv_a), (_, conv_b))| conv_a == conv_b)
.map(|((idx_a, _), (idx_b, _))| (idx_a, idx_b))
.collect();

let (idx_a, idx_b) = match pairs.as_slice() {
&[] => return,
&[pair] => pair,
let (idx_a, idx_b) = match *pairs.as_slice() {
[] => return,
[pair] => pair,
_ => panic!("more than two equal convolutions found"),
};
let a_subgrid = self
Expand Down Expand Up @@ -1222,24 +1216,6 @@ impl Grid {
xi: (f64, f64, f64),
alphas_table: &AlphasTable,
) -> Result<FkTable, GridError> {
use super::evolution::EVOLVE_INFO_TOL_ULPS;

let mut lhs: Option<Self> = None;
// Q2 slices we use
let mut used_op_fac1 = Vec::new();
// Q2 slices we encounter, but possibly don't use
let mut op_fac1 = Vec::new();
// Q2 slices needed by the grid
let grid_fac1: Vec<_> = self
.evolve_info(order_mask)
.fac1
.into_iter()
// TODO: also take care of the fragmentation scale
.map(|fac| xi.1 * xi.1 * fac)
.collect();

let mut perm = Vec::new();

struct Iter<T> {
iters: Vec<T>,
}
Expand Down Expand Up @@ -1268,6 +1244,24 @@ impl Grid {
}
}

use super::evolution::EVOLVE_INFO_TOL_ULPS;

let mut lhs: Option<Self> = None;
// Q2 slices we use
let mut used_op_fac1 = Vec::new();
// Q2 slices we encounter, but possibly don't use
let mut op_fac1 = Vec::new();
// Q2 slices needed by the grid
let grid_fac1: Vec<_> = self
.evolve_info(order_mask)
.fac1
.into_iter()
// TODO: also take care of the fragmentation scale
.map(|fac| xi.1 * xi.1 * fac)
.collect();

let mut perm = Vec::new();

for result in zip_n(slices) {
let (infos, operators): (Vec<OperatorSliceInfo>, Vec<CowArray<'_, f64, Ix4>>) = result
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions pineappl/src/lagrange_subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Subgrid for LagrangeSubgridV2 {
fn fill(&mut self, interps: &[Interp], ntuple: &[f64], weight: f64) {
debug_assert_eq!(interps.len(), ntuple.len());

if interpolation::interpolate(interps, &ntuple, weight, &mut self.array) {
if interpolation::interpolate(interps, ntuple, weight, &mut self.array) {
// TODO: make this more general
let q2 = ntuple[0];
if self.static_q2 == 0.0 {
Expand All @@ -45,7 +45,7 @@ impl Subgrid for LagrangeSubgridV2 {
fn node_values(&self) -> Vec<NodeValues> {
self.interps
.iter()
.map(|interp| NodeValues::UseThese(interp.node_values().to_vec()))
.map(|interp| NodeValues::UseThese(interp.node_values()))
.collect()
}

Expand Down
16 changes: 13 additions & 3 deletions pineappl/src/packed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct PackedArray<T> {
impl<T: Copy + Default + PartialEq> PackedArray<T> {
/// Constructs a new and empty `PackedArray` of shape `shape`.
#[must_use]
pub fn new(shape: Vec<usize>) -> Self {
pub const fn new(shape: Vec<usize>) -> Self {
Self {
entries: vec![],
start_indices: vec![],
Expand Down Expand Up @@ -90,8 +90,13 @@ impl<T: Copy + Default + PartialEq> PackedArray<T> {
.map(|(indices, entry)| (indices, *entry))
}

/// TODO
///
/// # Panics
///
/// TODO
// TODO: rewrite this method into `sub_block_iter_mut() -> impl Iterator<Item = &mut f64>`
#[must_use]
pub fn sub_block_idx(
&self,
start_index: &[usize],
Expand Down Expand Up @@ -170,6 +175,11 @@ fn ravel_multi_index(multi_index: &[usize], shape: &[usize]) -> usize {
}

/// TODO
///
/// # Panics
///
/// TODO
#[must_use]
pub fn unravel_index(mut index: usize, shape: &[usize]) -> Vec<usize> {
assert!(index < shape.iter().product());
let mut indices = vec![0; shape.len()];
Expand Down Expand Up @@ -200,7 +210,7 @@ impl<T: Copy + Default + PartialEq> Index<&[usize]> for PackedArray<T> {
self.shape
);

let raveled_index = ravel_multi_index(&index, &self.shape);
let raveled_index = ravel_multi_index(index, &self.shape);
let point = self.start_indices.partition_point(|&i| i <= raveled_index);

assert!(
Expand Down Expand Up @@ -399,7 +409,7 @@ impl<T: Clone + Copy + Default + PartialEq> IndexMut<&[usize]> for PackedArray<T
// `threshold_distance`:
// -> we insert the element as a new group

let raveled_index = ravel_multi_index(&index, &self.shape);
let raveled_index = ravel_multi_index(index, &self.shape);

// To determine which groups the new element is close to, `point` is the index of the
// start_index of the first group after the new element. `point` is 0 if no elements before
Expand Down
6 changes: 3 additions & 3 deletions pineappl/src/packed_subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Subgrid for PackedQ1X2SubgridV1 {
}

self.array = array;
self.node_values = new_node_values.clone();
self.node_values.clone_from(&new_node_values);
}

for (mut indices, value) in other.indexed_iter() {
Expand Down Expand Up @@ -241,7 +241,7 @@ mod tests {
vec![
NodeValues::UseThese(vec![1.0]),
NodeValues::UseThese(x.clone()),
NodeValues::UseThese(x.clone()),
NodeValues::UseThese(x),
],
)
.into();
Expand All @@ -257,7 +257,7 @@ mod tests {
assert_eq!(grid2.indexed_iter().nth(2), Some((vec![0, 3, 1], 2.0)));
assert_eq!(grid2.indexed_iter().nth(3), Some((vec![0, 3, 4], 4.0)));

grid1.merge(&mut grid2, None);
grid1.merge(&grid2, None);

// the luminosity function is symmetric, so after symmetrization the result must be
// unchanged
Expand Down
16 changes: 10 additions & 6 deletions pineappl/src/subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl NodeValues {
pub fn extend(&mut self, other: &Self) {
match (self, other) {
// (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => (),
(NodeValues::UseThese(a), NodeValues::UseThese(b)) => {
(Self::UseThese(a), Self::UseThese(b)) => {
a.extend_from_slice(b);
a.sort_by(|lhs, rhs| lhs.partial_cmp(rhs).unwrap());
// TODO: use some tolerance
Expand All @@ -34,36 +34,40 @@ impl NodeValues {
}

/// TODO
#[must_use]
pub fn len(&self) -> usize {
match self {
// NodeValues::UseFromGrid => unimplemented!(),
NodeValues::UseThese(a) => a.len(),
Self::UseThese(a) => a.len(),
}
}

/// TODO
#[must_use]
pub fn find(&self, value: f64) -> Option<usize> {
match self {
// NodeValues::UseFromGrid => unimplemented!(),
NodeValues::UseThese(a) => a.iter().position(|&x|
Self::UseThese(a) => a.iter().position(|&x|
// approx_eq!(f64, x, value, ulps = EVOLVE_INFO_TOL_ULPS)
x == value),
}
}

/// TODO
#[must_use]
pub fn get(&self, index: usize) -> f64 {
match self {
// NodeValues::UseFromGrid => unimplemented!(),
NodeValues::UseThese(a) => a[index],
Self::UseThese(a) => a[index],
}
}

/// TODO
#[must_use]
pub fn values(&self) -> Vec<f64> {
match self {
// NodeValues::UseFromGrid => unimplemented!(),
NodeValues::UseThese(a) => a.clone(),
Self::UseThese(a) => a.clone(),
}
}
}
Expand All @@ -72,7 +76,7 @@ impl PartialEq for NodeValues {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
// (NodeValues::UseFromGrid, NodeValues::UseFromGrid) => true,
(NodeValues::UseThese(a), NodeValues::UseThese(b)) => {
(Self::UseThese(a), Self::UseThese(b)) => {
(a.len() == b.len())
&& a.iter().zip(b).all(
// TODO: use some tolerance
Expand Down
2 changes: 1 addition & 1 deletion pineappl/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn read_uncompressed_v0(mut reader: impl BufRead) -> Result<Grid, GridError>
convolutions: convolutions
.into_iter()
//.map(|conv| conv.unwrap_or(Convolution::None))
.filter_map(|conv| conv)
.flatten()
.collect(),
pid_basis: grid
.key_values()
Expand Down
Loading

0 comments on commit f09956d

Please sign in to comment.