Skip to content

Commit

Permalink
borrow implementation from nightly std
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Aug 27, 2023
1 parent c71c834 commit 32eca17
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
31 changes: 31 additions & 0 deletions src/chunk_by.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// TODO: Use https://github.com/rust-lang/rust/issues/80552
pub struct ChunkBy<'a, T: 'a, P> {
pub slice: &'a [T],
pub predicate: P,
}

impl<'a, T: 'a, P> Iterator for ChunkBy<'a, T, P>
where
P: FnMut(&T, &T) -> bool,
{
type Item = &'a [T];

fn next(&mut self) -> Option<Self::Item> {
if self.slice.is_empty() {
None
} else {
let mut len = 1;
let mut iter = self.slice.windows(2);
while let Some([l, r]) = iter.next() {
if (self.predicate)(l, r) {
len += 1
} else {
break;
}
}
let (head, tail) = self.slice.split_at(len);
self.slice = tail;
Some(head)
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#[macro_use]
mod errors;
mod chunk_by;
mod material;
mod table;
mod tablebase;
Expand Down
16 changes: 7 additions & 9 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use positioned_io::{RandomAccessFile, ReadAt, ReadBytesAtExt as _};
use shakmaty::{Bitboard, Color, File, Piece, Position, Rank, Role, Square};

use crate::{
chunk_by::ChunkBy,
errors::{ProbeError, ProbeResult},
material::Material,
types::{DecisiveWdl, MaybeRounded, Metric, Pieces, Syzygy, Wdl, MAX_PIECES},
Expand Down Expand Up @@ -471,16 +472,13 @@ fn group_pieces(pieces: &Pieces) -> ArrayVec<usize, MAX_PIECES> {
}

// The remaining identical pieces are grouped together.
// TODO: Use slice_group_by when stabilized.
let mut last_piece = None;
for piece in &pieces[first_len..] {
if last_piece != Some(piece) {
result.push(1);
last_piece = Some(piece);
} else {
*result.last_mut().unwrap() += 1;
result.extend(
ChunkBy {
slice: &pieces[first_len..],
predicate: |l: &Piece, r: &Piece| l == r,
}
}
.map(|chunk| chunk.len()),
);

result
}
Expand Down

0 comments on commit 32eca17

Please sign in to comment.