Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .empty
Empty file.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ path = "src/lib.rs"
rand = "0.8"
image = "0.25"
image-compare = "0.5.0"
rayon = "1.8.0"
boyer-moore-magiclen = "0.2.20"

[dev-dependencies]
divan = { version = "4.0.2", package = "codspeed-divan-compat" }
Expand Down
16 changes: 9 additions & 7 deletions src/bfs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::collections::{HashSet, VecDeque};

/// A simple graph represented as an adjacency list
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -27,21 +27,23 @@ impl Graph {
/// Returns the order in which nodes were visited
pub fn bfs_naive(graph: &Graph, start: usize) -> Vec<usize> {
let mut visited = HashSet::new();
let mut queue = Vec::new(); // Using Vec instead of VecDeque - intentionally inefficient!
let mut queue = VecDeque::new(); // Using Vec instead of VecDeque - intentionally inefficient!
let mut result = Vec::new();

queue.push(start);
queue.reserve(graph.num_nodes());
result.reserve(graph.num_nodes());
visited.reserve(graph.num_nodes());

queue.push_back(start);
visited.insert(start);

while !queue.is_empty() {
// remove(0) is O(n) - this makes BFS slow!
let node = queue.remove(0);
while let Some(node) = queue.pop_front() {
result.push(node);

if let Some(neighbors) = graph.adjacency.get(node) {
for &neighbor in neighbors {
if visited.insert(neighbor) {
queue.push(neighbor);
queue.push_back(neighbor);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/dna_matcher.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use boyer_moore_magiclen::BMByte;
use rayon::prelude::*;

/// Naive approach: Read the entire file as a string and filter lines
pub fn naive_dna_matcher(genome: &str, pattern: &str) -> Vec<String> {
let bmb = BMByte::from(pattern).unwrap();
genome
.lines()
.par_lines()
.filter(|line| !line.starts_with('>')) // Skip headers
.filter(|line| line.contains(pattern))
.filter(|line| !bmb.find_in(line, 1).is_empty())
.map(|s| s.to_string())
.collect()
}
Expand Down
27 changes: 16 additions & 11 deletions src/lut_filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ mod naive {
let (width, height) = img.dimensions();
let mut output = ImageBuffer::new(width, height);

for (x, y, pixel) in img.enumerate_pixels() {
let r = pixel[0] as f32;
let g = pixel[1] as f32;
let b = pixel[2] as f32;
let mut lut = [0.0; 256];
for i in 0..256 {
lut[i] = ((i as f32 - 128.0) * (1.0 + contrast)) + 128.0 + brightness as f32;
}

for (x, y, pixel) in img.enumerate_pixels() {
// Apply contrast and brightness (5 FP ops per channel!)
let r = ((r - 128.0) * (1.0 + contrast)) + 128.0 + brightness as f32;
let g = ((g - 128.0) * (1.0 + contrast)) + 128.0 + brightness as f32;
let b = ((b - 128.0) * (1.0 + contrast)) + 128.0 + brightness as f32;
let r = lut[pixel[0] as usize];
let g = lut[pixel[1] as usize];
let b = lut[pixel[2] as usize];

output.put_pixel(
x,
Expand All @@ -68,16 +69,20 @@ mod naive {
}

/// Naive implementation: Apply gamma correction
/// This is VERY slow because powf() is expensive!
pub fn apply_gamma(img: &RgbImage, gamma: f32) -> RgbImage {
let (width, height) = img.dimensions();
let mut output = ImageBuffer::new(width, height);

let mut lut = [0.0; 256];
for i in 0..256 {
lut[i] = (i as f32 / 255.0).powf(1.0 / gamma) * 255.0;
}

for (x, y, pixel) in img.enumerate_pixels() {
// powf() is VERY expensive - this is why we need a LUT!
let r = (pixel[0] as f32 / 255.0).powf(1.0 / gamma) * 255.0;
let g = (pixel[1] as f32 / 255.0).powf(1.0 / gamma) * 255.0;
let b = (pixel[2] as f32 / 255.0).powf(1.0 / gamma) * 255.0;
let r = lut[pixel[0] as usize];
let g = lut[pixel[1] as usize];
let b = lut[pixel[2] as usize];

output.put_pixel(x, y, Rgb([r as u8, g as u8, b as u8]));
}
Expand Down