Skip to content

Instantly share code, notes, and snippets.

@socantre
socantre / levenshtein_distance.hpp
Created November 7, 2010 18:45
compute the Levenshtein distance between an n length string and an m length string in O(n) memory (not O(nm)).
#include <iterator>
#include <vector>
template<typename RandomAccessIterator1,typename RandomAccessIterator2,typename Comparator>
int levenshtein_distance(RandomAccessIterator1 begin1,RandomAccessIterator1 end1,
RandomAccessIterator2 begin2,RandomAccessIterator2 end2,
Comparator cmp)
{
std::vector<int> values;
@socantre
socantre / multi_core_test.cpp
Created February 22, 2012 01:35
try out multiple threads
#include <climits>
#include <future>
#include <vector>
#include <thread>
#include <chrono>
#include <iostream>
#include <numeric>
#include <cassert>
struct busy_increment {
@socantre
socantre / gist:3472964
Created August 26, 2012 01:26
bit_cast
#include <cstring> // memcpy
#include <type_traits> // is_trivially_copyable
// http://src.chromium.org/viewvc/chrome/trunk/src/base/basictypes.h?view=markup
// bit_cast<Dest,Source> is a template function that implements the
// equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in
// very low-level functions like the protobuf library and fast math
// support.
//
@socantre
socantre / ltr_types.cpp
Last active October 13, 2015 06:48
Left-to-right type syntax in C++ with type aliases
template<typename T> using ptr = T*;
template<typename T> using ref = T&;
template<typename T> using rref = T&&;
template<typename T> using constant = T const;
template<typename T> using volat = T volatile;
template<typename T, class C> using mem_ptr = T C::*;
template<std::intmax_t N, typename T> using raw_array = T[N];
template<typename RetT, typename... Args> using func = RetT(Args...);
extern "C" template<typename T> using c_linkage = T;
@socantre
socantre / overload_set.hpp
Created November 28, 2012 21:24
c++ overload set type
template <class... Fs> struct overload_set;
template <class F>
struct overload_set<F> : F
{
overload_set(F &&x) : F(std::forward<F>(x)) {}
using F::operator();
};
template <class F, class... Fs>
@socantre
socantre / on_scope_exit.cpp
Created December 2, 2012 07:36
Code to make unique_ptr easy to use for generic cleanup code
#include <utility> // std::forward
#include <memory> // std::unique_ptr
#include <type_traits> // std::remove_reference
#include <iostream> // std::cout
// The deleter types used with unique_ptr to do the cleanup
template<typename Handle, typename CleanupF>
struct Janitor : CleanupF {
using pointer = Handle;
@socantre
socantre / .bashrc
Last active December 11, 2015 23:18
bash prompt:{exit code of previous command} [hostname username pwd] date and time (right justified)
# vi input mode (bash)
set -o vi
# Mac OS X 10.10.5
export PS1='\n$(cat <<< $? > ~/.exit_code)$(/bin/date "+%l:%M:%S %p %z %A %d %B %Y" | awk "{ printf \"%*s\", $(tput cols),\$0}")\r{$(cat ~/.exit_code)} [\h \u \w] \n$ '
# Windows, mintty
export PS1='\[\e[31;40m\]\n{$?} [\h \u \w]\[\e[0m\]\[\e[31;40m\]\n`/bin/date --utc --iso-8601=seconds`\[\e[0m\]\n$ '
# Time with preexec/precmd
@socantre
socantre / type_name.cpp
Created February 3, 2013 17:09
"human readable" C++ types
#include <string>
#include <iostream> // cout
#include <cstdint> // intmax_t
#include <typeinfo>
#include <memory> // unique_ptr
#include <cstdlib> // free
#include <cxxabi.h> // __cxa_demangle
extern "C" template<typename T> using c_linkage = T;
@socantre
socantre / gist:5798407
Created June 17, 2013 16:59
Example of semaphore using c++11 condition variables
#include <iostream>
#include <future>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <array>
#include <chrono>
int fib(int n) {
@socantre
socantre / gist:5851322
Last active December 18, 2015 21:59
for_each_i: algorithm to iterator over a range, with an index.
template<class InputIterator, class Function>
Function for_each_i(InputIterator first, InputIterator last, Function f) {
using index_type = std::iterator_traits<InputIterator>::difference_type i;
for (index_type i {}; first!=last; ++first, ++i) {
f(*first, i);
}
return std::move(f);
}
/*