Skip to content

Instantly share code, notes, and snippets.

View abonander's full-sized avatar

Austin Bonander abonander

View GitHub Profile
@abonander
abonander / SMS Sender thread
Last active December 21, 2015 13:18
The part of my code that deals with threading. The sendMessage() method can be called from any thread and at any time. It posts a new message to send to the message queue and starts the sender thread if it isn't running. The sender thread polls the queue with a timeout, and processes the messages. If there are no more messages in the queue, the …
private final LinkedBlockingQueue<Message> messageQueue = new LinkedBlockingQueue<Message>();
// The sender argument is an enum describing who sent the message: the user, the app, or the person on the other end.
public void sendMessage(String address, String message, Sender sender) {
messageQueue.offer(Message.create(address, message, sender));
startSenderThread();
}
private Thread senderThread;
@abonander
abonander / Sorta.scala
Last active June 4, 2021 11:26
Popular sorting algorithms reimplemented in Scala
package org.logician.sorta
import scala.util.Random
import scala.math
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
/**
* Created with IntelliJ IDEA.
* User: Austin
Reading data from 'callgrind.out.3548'...
--------------------------------------------------------------------------------
Profile data file 'callgrind.out.3548' (creator: callgrind-3.10.0.SVN)
--------------------------------------------------------------------------------
I1 cache:
D1 cache:
LL cache:
Timerange: Basic block 0 - 29927136284
Trigger: Program termination
Profiled target: ./img-dup --dir=/home/austin/Dropbox/ (PID 3548, part 1)
@abonander
abonander / solve_tri.rs
Last active August 29, 2015 14:06
Triangle Solver in Rust
use std::num::FloatMath;
struct Tri {
sides: [Option<f32>, ..3],
angles: [Option<f32>, ..3],
ambiguous: Option<Box<Tri>>,
}
impl Tri {
fn from_vec(vals: &[Option<f32>]) -> Tri {
@abonander
abonander / prepend.rs
Created November 10, 2014 09:35
Prepend trait + impl for `Vec<T>` and `String`
trait Prepend<T> {
fn prepend(&mut self, t: T);
}
impl<S: Str> Prepend<S> for String {
fn prepend(&mut self, s: S) {
unsafe {
self.as_mut_vec().prepend(s.as_slice().as_bytes());
}
}
@abonander
abonander / borrowcheck.rs
Created December 4, 2014 22:01
Fooling the Borrowchecker with `Vec` and `HashMap`, pretty-printed. Source: https://github.com/rust-lang/rust/issues/19537
#![feature(phase)]
#![no_std]
#![feature(globs)]
#[phase(plugin, link)]
extern crate "std" as std;
#[prelude_import]
use std::prelude::*;
use std::collections::HashMap;
struct Foo<'a> {
use std::io::{self, Read, Write};
fn xor(x: char, y: char) -> char {
// Rust lacks the ternary operator.
// Since if-blocks are expressions, it'd be redundant.
if x != y { '1' } else { '0' }
}
fn or(x: char, y: char) -> char {
if x == '1' || y == '1' { '1' } else { '0' }
PS C:\Users\Austin\Rust\wingui> cargo rustc -p winapi -- -Ztime-passes
Compiling winapi v0.2.4
time: 0.225; rss: 66MB parsing
time: 0.066; rss: 71MB configuration 1
time: 0.000; rss: 71MB recursion limit
time: 0.003; rss: 71MB gated macro checking
time: 0.000; rss: 71MB crate injection
time: 0.011; rss: 73MB macro loading
time: 0.000; rss: 73MB plugin loading
time: 0.000; rss: 73MB plugin registration
#![cfg_attr(test, feature(test))]
#[cfg(test)]
#[macro_use]
extern crate lazy_static;
use std::mem;
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
pub const INIT: AtomicBitSet = AtomicBitSet {
@abonander
abonander / commands_and_output.txt
Created February 7, 2016 04:05
Test of `posix_fadvise()` for Rust
$ # Generate 8 MB of random data
$ head -c 8M < /dev/urandom > data.txt
$ # Flush and clear the cache so we get the data from disk
$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches && cargo script posix_fadvise_test.rs
3
Compiling posix_fadvise_test v0.1.0 (file:///home/austin/.cargo/script-cache/file-posix_fadvise_test-8d6a7e80a3131286)
Advised result: HashResult { hash: [173, 168, 157, 204, 185, 126, 165, 177, 195, 111, 94, 179, 252, 59, 25, 169], time: Duration { secs: 0, nanos: 10050785 } }
$ # Comment out the call to advise_willneed, save, flush again and re-run
$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches && cargo script posix_fadvise_test.rs
3