Use btrees to make everything more consistent

This commit is contained in:
Gwilym Inzani 2024-09-25 13:13:51 +01:00
parent afa9d47f0c
commit 62020e692f
4 changed files with 19 additions and 27 deletions

View file

@ -1,6 +1,6 @@
use std::{fmt, str::FromStr}; use std::{fmt, str::FromStr};
#[derive(Clone, Copy, PartialEq, Eq, Hash)] #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Colour { pub struct Colour {
pub r: u8, pub r: u8,
pub g: u8, pub g: u8,

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, hash::BuildHasher}; use std::collections::BTreeMap;
use crate::{colour::Colour, image_loader::Image}; use crate::{colour::Colour, image_loader::Image};
@ -42,7 +42,7 @@ pub struct DeduplicatedData {
pub transformation: Transformation, pub transformation: Transformation,
} }
#[derive(Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
struct Tile { struct Tile {
data: [Colour; 64], data: [Colour; 64],
} }
@ -99,9 +99,7 @@ pub(crate) fn deduplicate_image(input: &Image, can_flip: bool) -> (Image, Vec<De
let mut deduplication_data = vec![]; let mut deduplication_data = vec![];
let all_tiles = Tile::split_image(input); let all_tiles = Tile::split_image(input);
let mut existing_tiles = HashMap::new(); let mut existing_tiles = BTreeMap::new();
let hasher = std::collections::hash_map::RandomState::new();
for tile in all_tiles { for tile in all_tiles {
let (tile, transformation) = if can_flip { let (tile, transformation) = if can_flip {
@ -109,22 +107,13 @@ pub(crate) fn deduplicate_image(input: &Image, can_flip: bool) -> (Image, Vec<De
let hflipped = tile.hflipped(); let hflipped = tile.hflipped();
let vhflipped = vflipped.hflipped(); let vhflipped = vflipped.hflipped();
// find the one with the smallest hash let minimum = (&tile).min(&vflipped).min(&hflipped).min(&vhflipped);
let tile_hash = hasher.hash_one(&tile);
let vflipped_hash = hasher.hash_one(&vflipped);
let hflipped_hash = hasher.hash_one(&hflipped);
let vhflipped_hash = hasher.hash_one(&vhflipped);
let minimum = tile_hash if minimum == &tile {
.min(vflipped_hash)
.min(hflipped_hash)
.min(vhflipped_hash);
if minimum == tile_hash {
(tile, Transformation::none()) (tile, Transformation::none())
} else if minimum == vflipped_hash { } else if minimum == &vflipped {
(vflipped, Transformation::vflipped()) (vflipped, Transformation::vflipped())
} else if minimum == hflipped_hash { } else if minimum == &hflipped {
(hflipped, Transformation::hflipped()) (hflipped, Transformation::hflipped())
} else { } else {
(vhflipped, Transformation::vhflipped()) (vhflipped, Transformation::vhflipped())

View file

@ -1,10 +1,13 @@
use crate::colour::Colour; use crate::colour::Colour;
use std::{collections::HashSet, fmt}; use std::{
collections::{BTreeSet, HashSet},
fmt,
};
const MAX_COLOURS: usize = 256; const MAX_COLOURS: usize = 256;
const MAX_COLOURS_PER_PALETTE: usize = 16; const MAX_COLOURS_PER_PALETTE: usize = 16;
#[derive(Debug, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub(crate) struct Palette16 { pub(crate) struct Palette16 {
colours: Vec<Colour>, colours: Vec<Colour>,
} }
@ -156,7 +159,7 @@ impl Palette16Optimiser {
palette.add_colour(transparent_colour); palette.add_colour(transparent_colour);
palette palette
}) })
.collect::<HashSet<Palette16>>() .collect::<BTreeSet<Palette16>>()
.into_iter() .into_iter()
.map(|palette| palette.colours) .map(|palette| palette.colours)
.collect::<Vec<_>>(); .collect::<Vec<_>>();

View file

@ -1,4 +1,4 @@
use std::collections::HashSet; use std::collections::BTreeSet;
use crate::{ use crate::{
colour::Colour, colour::Colour,
@ -7,13 +7,13 @@ use crate::{
}; };
pub struct Palette256 { pub struct Palette256 {
colours: HashSet<Colour>, colours: BTreeSet<Colour>,
} }
impl Palette256 { impl Palette256 {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
colours: HashSet::new(), colours: BTreeSet::new(),
} }
} }
@ -41,8 +41,8 @@ impl Palette256 {
.cloned() .cloned()
.collect(); .collect();
let current_colours_set = HashSet::from_iter(optimised_palette_colours.iter().cloned()); let current_colours_set = BTreeSet::from_iter(optimised_palette_colours.iter().cloned());
let new_colours: HashSet<_> = self let new_colours: BTreeSet<_> = self
.colours .colours
.symmetric_difference(&current_colours_set) .symmetric_difference(&current_colours_set)
.collect(); .collect();