Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mikea committed Nov 28, 2024
1 parent a5d3ff7 commit 88ebdaf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 52 deletions.
16 changes: 10 additions & 6 deletions src/cards.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{
fmt::{Debug, Write},
ops::{BitAnd, BitOr, Shl, Shr, Sub},
simd::{num::SimdUint, u64x4, Simd}, str::FromStr,
simd::{num::SimdUint, u64x4, Simd},
str::FromStr,
};

use regex::Regex;
Expand Down Expand Up @@ -605,7 +606,7 @@ impl CardSet {
};

// order of Suit values
pub(crate) const SUITS: [CardSet; 4] = [Self::C, Self::D, Self::H, Self::S];
pub(crate) const SUITS: [CardSet; 4] = [Self::C, Self::D, Self::H, Self::S];
const SUIT_MASKS: u64x4 =
Simd::from_array([Self::C.mask, Self::D.mask, Self::H.mask, Self::S.mask]);

Expand Down Expand Up @@ -786,9 +787,11 @@ impl CardSet {
fn contains(self, c: Card) -> bool {
!(CardSet::from(c) & self).is_empty()
}

pub(crate) fn without(self, other: CardSet) -> CardSet {
CardSet { mask: self.mask & !other.mask }
CardSet {
mask: self.mask & !other.mask,
}
}
}

Expand Down Expand Up @@ -1145,14 +1148,15 @@ impl Deal {
self.get(Player::S).to_bpn_masked(mask)
)
}

pub(crate) fn add(&mut self, p: Player, card: Card) {
let p = p.index();
self.hands[p] |= CardSet::from(card).mask;
}

pub(crate) fn format_as_bpn(&self) -> String {
format!("{} {} {} {}",
format!(
"{} {} {} {}",
&CardSet::from(self.hands[0]).to_bpn(),
&CardSet::from(self.hands[1]).to_bpn(),
&CardSet::from(self.hands[2]).to_bpn(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub use play::*;
pub mod analyze;
mod bits;
pub mod counter;
pub mod minmax;
pub mod observers;
pub mod search;
pub mod trans_table;
mod ui;
pub mod minmax;

#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u8)]
Expand Down
10 changes: 5 additions & 5 deletions src/minmax.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{CardMap, Deal, Pair, PlayOfCards, PlayState, PlayedCardsNT, PlayedCardsT, Player, Suit, Trick};
use crate::{
CardMap, Deal, Pair, PlayOfCards, PlayState, PlayedCardsNT, PlayedCardsT, Player, Suit, Trick,
};

pub(crate) trait Lattice<T> {
fn bottom() -> T;
Expand Down Expand Up @@ -103,16 +105,14 @@ mod tests {

#[test]
fn small_deal_4() {
let deal = Deal::try_from_pbn("A.KQ9.. .J.K.KQ K.T8..J .A.A.AT")
.unwrap();
let deal = Deal::try_from_pbn("A.KQ9.. .J.K.KQ K.T8..J .A.A.AT").unwrap();
let tricks = minmax(&deal, Some(Suit::S), Player::S);
assert_eq!(1, tricks);
}

#[test]
fn small_deal_5() {
let deal = Deal::try_from_pbn(".KQT.. .J..KQ A.9..J .A..AT")
.unwrap();
let deal = Deal::try_from_pbn(".KQT.. .J..KQ A.9..J .A..AT").unwrap();
let tricks = minmax(&deal, Some(Suit::S), Player::W);
assert_eq!(2, tricks);
}
Expand Down
35 changes: 16 additions & 19 deletions src/observers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,16 @@ impl Observer for Dot {
&state.deal.format_as_bpn()
)
.unwrap();
writeln!(self.writer, "\"{}\" -> \"{}\" [label=\"{}\"]", &parent_id, &id, self.path.last().unwrap()).unwrap();
writeln!(
self.writer,
"\"{}\" -> \"{}\" [label=\"{}\"]",
&parent_id,
&id,
self.path.last().unwrap()
)
.unwrap();
if state.play.is_empty() {
writeln!(
self.writer,
"\"{}\" [shape=box]",
&id,
)
.unwrap();
writeln!(self.writer, "\"{}\" [shape=box]", &id,).unwrap();
}
}
}
Expand All @@ -368,15 +370,15 @@ impl Observer for Dot {
self.path.push(format!("{}", card.symbol()));
}

fn child_exit(&mut self, card: Card) {
fn child_exit(&mut self, _card: Card) {
self.path.pop();
}

fn update_table<PoC: PlayOfCards>(
&mut self,
state: &PlayState<PoC>,
rel_mask: CardSet,
ab: AB,
_state: &PlayState<PoC>,
_rel_mask: CardSet,
_ab: AB,
) {
}

Expand All @@ -391,12 +393,7 @@ impl Observer for Dot {
fn max_tricks_cutoff(&mut self) {
if self.search {
let id = self.path.join(" ");
writeln!(
self.writer,
"\"{}\" [style=filled fillcolor=\"red\"]",
&id,
)
.unwrap();
writeln!(self.writer, "\"{}\" [style=filled fillcolor=\"red\"]", &id,).unwrap();
}
}

Expand All @@ -414,7 +411,7 @@ impl Observer for Dot {

fn search_cutoff(&mut self) {}

fn search_finished(&mut self, init: &InitialPosition) {
fn search_finished(&mut self, _init: &InitialPosition) {
writeln!(self.writer, "}}").unwrap();
self.search = false;
}
Expand All @@ -424,7 +421,7 @@ impl Observer for Dot {
self.search = true;
}

fn tt_hit<PoC: PlayOfCards>(&mut self, state: &PlayState<PoC>, rel_mask: CardSet, ab: AB) {}
fn tt_hit<PoC: PlayOfCards>(&mut self, _state: &PlayState<PoC>, _rel_mask: CardSet, _ab: AB) {}

fn tt_lower_cutoff(&mut self) {
if self.search {
Expand Down
50 changes: 29 additions & 21 deletions src/trans_table.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{
cmp::{max, min},
collections::HashMap,
hash::Hash, str::FromStr,
hash::Hash,
str::FromStr,
};

use rand::seq::SliceRandom;
Expand Down Expand Up @@ -34,7 +35,7 @@ impl AB {
fn intersects(self, other: AB) -> bool {
max(self.a, other.a) <= min(self.b, other.b)
}

#[must_use]
pub fn contains(&self, x: u8) -> bool {
x >= self.a && x <= self.b
Expand Down Expand Up @@ -82,12 +83,16 @@ impl From<&Deal> for Distr {
let h = (deal & CardSet::H).count();
let s = (deal & CardSet::S).count();

let c = u64::from(c[3]) | u64::from(c[2]) << 4 | u64::from(c[1]) << 8 | u64::from(c[0]) << 12;
let d = u64::from(d[3]) | u64::from(d[2]) << 4 | u64::from(d[1]) << 8 | u64::from(d[0]) << 12;
let h = u64::from(h[3]) | u64::from(h[2]) << 4 | u64::from(h[1]) << 8 | u64::from(h[0]) << 12;
let s = u64::from(s[3]) | u64::from(s[2]) << 4 | u64::from(s[1]) << 8 | u64::from(s[0]) << 12;
let c =
u64::from(c[3]) | u64::from(c[2]) << 4 | u64::from(c[1]) << 8 | u64::from(c[0]) << 12;
let d =
u64::from(d[3]) | u64::from(d[2]) << 4 | u64::from(d[1]) << 8 | u64::from(d[0]) << 12;
let h =
u64::from(h[3]) | u64::from(h[2]) << 4 | u64::from(h[1]) << 8 | u64::from(h[0]) << 12;
let s =
u64::from(s[3]) | u64::from(s[2]) << 4 | u64::from(s[1]) << 8 | u64::from(s[0]) << 12;

let distr = (c) | (d) << 16 | (h) << 32 | (s) << 48;
let distr = (c) | (d) << 16 | (h) << 32 | (s) << 48;
Distr { distr }
}
}
Expand All @@ -98,7 +103,7 @@ impl FromStr for Distr {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let re = Regex::new(r"(.*)\.(.*)\.(.*)\.(.*)").unwrap();

let players:Vec<_> = s.split(' ').collect();
let players: Vec<_> = s.split(' ').collect();
if players.len() != 4 {
return Err(format!("bad input: {s}"));
}
Expand All @@ -113,13 +118,13 @@ impl FromStr for Distr {
return Err(format!("bad input: {}", players[i]));
};
let (_, [s1, h1, d1, c1]) = captures.extract();
c |= (c1.len() as u64) << ((3 - i)*4);
d |= (d1.len() as u64) << ((3 - i)*4);
h |= (h1.len() as u64) << ((3 - i)*4);
s |= (s1.len() as u64) << ((3 - i)*4);
c |= (c1.len() as u64) << ((3 - i) * 4);
d |= (d1.len() as u64) << ((3 - i) * 4);
h |= (h1.len() as u64) << ((3 - i) * 4);
s |= (s1.len() as u64) << ((3 - i) * 4);
}

let distr = (c) | (d) << 16 | (h) << 32 | (s) << 48;
let distr = (c) | (d) << 16 | (h) << 32 | (s) << 48;
Ok(Distr { distr })
}
}
Expand Down Expand Up @@ -188,12 +193,12 @@ impl Pattern {
let mut card_map = CardMap::default();
deal.promote_all(&mut card_map)
}

#[must_use]
pub fn next(&self) -> Player {
self.key.next
}

#[must_use]
pub fn ab(&self) -> AB {
self.entry.ab
Expand All @@ -215,7 +220,13 @@ impl FromStr for Pattern {
let mask = deal.replace('x', "");
let mask = Deal::try_from_pbn(&mask)?;
let distr = Distr::from_str(deal)?;
Ok(Pattern { key: Key { distr, next }, entry: Entry{ mask, ab: AB { a, b }} })
Ok(Pattern {
key: Key { distr, next },
entry: Entry {
mask,
ab: AB { a, b },
},
})
}
}

Expand Down Expand Up @@ -244,7 +255,7 @@ impl TransTable for UnsyncTable {
"inconsistent entries:\n {:?} {} = {:?}\n vs\n {:?} {} = {:?}\n deal = {deal:?}",
state.next,
deal.format_bpn_masked(entry.mask.present()),
entry.ab,
entry.ab,
state.next,
deal.format_bpn_masked(prev_result.0),
prev_result.1,
Expand All @@ -271,10 +282,7 @@ impl TransTable for UnsyncTable {
let deal = &state.deal;
let deal = deal & rel_mask;
debug_assert_eq!(deal.present(), rel_mask);
let new_entry = Entry {
mask: deal,
ab,
};
let new_entry = Entry { mask: deal, ab };
let Some(entries) = self.cache.get_mut(&key) else {
let entries = vec![new_entry];
self.cache.insert(key, entries);
Expand Down

0 comments on commit 88ebdaf

Please sign in to comment.