Skip to content

Instantly share code, notes, and snippets.

View martinmroz's full-sized avatar

Martin Mroz martinmroz

  • San Francisco, CA
View GitHub Profile
@martinmroz
martinmroz / NSString+Bcbp.m
Created December 29, 2021 07:15
Rust -> NSString -> String Bridge
/*
* Copyright (C) 2019 Martin Mroz
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@martinmroz
martinmroz / dump_memory_region.rs
Last active November 1, 2020 03:42
Dump MemoryRegion
struct MemoryRegion {
pub start: u32,
pub end: u32,
pub buffer: Vec<u8>,
pub name: String,
}
/// I32HEX supports record types:
/// - Data
/// Specifies up to 255 bytes of data.
/*
* tube_display.c
* Project TubeClock/Display
*
* Created Friday, September 3, 2010 by Martin Mroz.
* Copyright (C) 2010 Martin Mroz, All Rights Reserved.
*/
#include <avr/pgmspace.h>
#include "hardware/tube_display.h"
pub fn levenshtein_distance(source: &str, target: &str) -> usize {
if source.len() == 0 || target.len() == 0 {
return std::cmp::max(
source.chars().count(),
target.chars().count()
);
}
let source_chars: Vec<char> = source.chars().collect();
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_flux_over_byte_string() {
assert_eq!(
flux_over_byte_string("testing one two three".as_bytes()),
Some(Flux::new(CharType::NotSpace, 4, 0, CharType::NotSpace))
);
}
@martinmroz
martinmroz / performance_3.csv
Created October 27, 2019 02:44
Final performance numbers.
file wc time wc mem wc_rs time wc_rs mem
empty.txt 0.00s 671kB 0.00s 786kB
big.txt 0.02s 1.72MB 0.01s 1.53MB
huge.txt 0.32s 1.72MB 0.16s 1.61MB
@martinmroz
martinmroz / rayon.rs
Created October 27, 2019 02:39
Parallelized version of our counter.
/// Computes the flux over the provided input byte string.
fn flux_over_byte_string<T>(input: T) -> Option<Flux>
where
T: AsRef<[u8]> {
input.as_ref()
.par_iter()
.cloned()
.map(Flux::from)
.fold(|| None, |acc, next| span_opt(acc, Some(next)))
.reduce(|| None, |acc, next| span_opt(acc, next))
@martinmroz
martinmroz / functional.rs
Created October 27, 2019 02:35
A functional take on generating a span over an entire byte string
/// Computes the flux over the provided input byte string.
fn flux_over_byte_string<T>(input: T) -> Option<Flux>
where
T: AsRef<[u8]> {
input.as_ref()
.iter()
.cloned()
.map(Flux::from)
.fold(None, |acc, next| span_opt(acc, Some(next)))
}
@martinmroz
martinmroz / span.rs
Created October 27, 2019 02:32
Span joining two Flux instances together
fn span(self, rhs: Flux) -> Self {
let words = {
// If the span is formed along a non-space to non-space boundary the word count is one less than the sum.
if let (CharType::NotSpace, CharType::NotSpace) = (self.rightmost_char_type, rhs.leftmost_char_type) {
self.words + rhs.words - 1
} else {
self.words + rhs.words
}
};
Flux::new(self.leftmost_char_type, words, self.lines + rhs.lines, rhs.rightmost_char_type)
@martinmroz
martinmroz / character.rs
Last active October 27, 2019 02:25
Mapping a single character onto a Flux Monoid
impl From<u8> for Flux {
fn from(other: u8) -> Self {
if other.is_ascii_whitespace() {
// A line-feed is considered an ASCII whitespace character by `is_ascii_whitespace`.
let lines = if other == ('\n' as u8) { 1 } else { 0 };
Flux::new(CharType::IsSpace, 0, lines, CharType::IsSpace)
} else {
Flux::new(CharType::NotSpace, 1, 0, CharType::NotSpace)
}
}