Skip to content

Commit

Permalink
improve stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshix-1 committed Sep 15, 2024
1 parent a9d0a08 commit 5d41689
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
49 changes: 28 additions & 21 deletions src/language/word_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use std::num::NonZeroUsize;
use std::ops::{Div, Range};

#[cfg(feature = "pyo3")]
#[allow(clippy::wildcard_imports)]
use pyo3::exceptions::*;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
#[cfg(feature = "pyo3")]
#[allow(clippy::wildcard_imports)]
use pyo3::types::*;
#[cfg(feature = "pyo3")]
use std::hash::{DefaultHasher, Hasher};
Expand Down Expand Up @@ -108,17 +110,18 @@ impl IntoIterator for &WordSequence {
}
}

#[allow(clippy::module_name_repetitions)]
pub trait WordSequenceIndex {
type Output;

fn get(&self, word_sequence: &WordSequence) -> Option<Self::Output>;
fn get(self, word_sequence: &WordSequence) -> Option<Self::Output>;
}

impl WordSequenceIndex for usize {
type Output = &'static str;

#[inline]
fn get(&self, word_sequence: &WordSequence) -> Option<Self::Output> {
fn get(self, word_sequence: &WordSequence) -> Option<Self::Output> {
let data_index: Self =
self.checked_mul(word_sequence.padded_word_byte_count.into())?;
Some(
Expand All @@ -135,11 +138,25 @@ impl WordSequenceIndex for usize {
}
}

impl WordSequenceIndex for isize {
type Output = &'static str;

#[inline]
fn get(self, word_sequence: &WordSequence) -> Option<Self::Output> {
let uindex: Option<usize> = if self >= 0 {
0usize.checked_add_signed(self)
} else {
word_sequence.len().checked_add_signed(self)
};
uindex?.get(word_sequence)
}
}

impl WordSequenceIndex for Range<usize> {
type Output = WordSequence;

#[inline]
fn get(&self, word_sequence: &WordSequence) -> Option<Self::Output> {
fn get(self, word_sequence: &WordSequence) -> Option<Self::Output> {
Some(WordSequence {
data: word_sequence.data.get(
self.start
Expand Down Expand Up @@ -195,25 +212,14 @@ impl WordSequence {
self.len()
}

#[must_use]
#[inline]
#[allow(clippy::needless_pass_by_value)]
pub fn __getitem__(&self, index: GetItemArg) -> PyResult<GetItemResult> {
match index {
GetItemArg::Int(index) => {
let uindex: Option<usize> = if index >= 0 {
usize::try_from(index).ok()
} else {
self.len().checked_add_signed(index)
};
match uindex {
None => Err(PyIndexError::new_err("Invalid index")),
Some(index) => match self.get(index) {
None => {
Err(PyIndexError::new_err("Index out of range"))
}
Some(value) => Ok(GetItemResult::Item(value)),
},
}
}
GetItemArg::Int(index) => match self.get(index) {
None => Err(PyIndexError::new_err("Index out of range")),
Some(value) => Ok(GetItemResult::Item(value)),
},
}
}

Expand All @@ -229,9 +235,9 @@ impl WordSequence {
!self.is_empty()
}

#[must_use]
#[inline]
#[pyo3(signature = (arg, /))]
#[allow(clippy::needless_pass_by_value)]
pub fn index(&self, arg: ContainsArg) -> PyResult<usize> {
match arg {
ContainsArg::StringArg(string) => {
Expand All @@ -256,6 +262,7 @@ impl WordSequence {

#[must_use]
#[inline]
#[pyo3(signature = (arg, /))]
pub fn count(&self, arg: ContainsArg) -> u8 {
u8::from(self.__contains__(arg))
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#![warn(
clippy::missing_const_for_fn,
clippy::nursery,
clippy::option_if_let_else,
clippy::pedantic,
clippy::todo
)]
#![deny(clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::missing_errors_doc, clippy::option_if_let_else)]
mod language;
mod solver;

Expand Down Expand Up @@ -55,6 +54,7 @@ pub fn solve_crossword(
Ok(pattern.solve(language, Some(max_words_to_collect)))
}

#[must_use]
#[cfg(feature = "pyo3")]
#[pyfunction]
#[pyo3(signature = (language, word_length))]
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#![warn(
clippy::missing_const_for_fn,
clippy::nursery,
clippy::option_if_let_else,
clippy::pedantic,
clippy::todo
)]
#![deny(clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::missing_errors_doc, clippy::option_if_let_else)]
mod language;
mod solver;

Expand Down

0 comments on commit 5d41689

Please sign in to comment.