Skip to content

Instantly share code, notes, and snippets.

#include <utility>
#ifndef SCOPE_EXIT_H_
#define SCOPE_EXIT_H_
// modeled slightly after Andrescu’s talk and article(s)
namespace std {
namespace experimental {
template <typename EF> struct scope_exit {
// construction
#include <cstdio>
int main() {
int a = 810092048, b = a, c[] = {'\n\0i%','ziF\0', 'uB\0z', 'F\0zz', 'Bzzi', 'zzu'};
for (int d = 1; d <= 100; ++d, b = (b >>= 2) ? b : a) {
std::printf((char*)c+(b&3)*5, d); std::printf((char*)c+3);
}
}
@socantre
socantre / wrl.cpp
Created April 14, 2015 14:15
small example using windows runtime library in iso c++
//#include <windows.web.http.h>
#include <windows.foundation.h> // ABI::Windows::Foundation::I*
#include <wrl/wrappers/corewrappers.h> //Microsoft::WRL::Wrappers
#include <wrl/client.h> // ComPtr
#include <stdexcept>
int main() {
Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
@socantre
socantre / sample.cpp
Last active August 29, 2015 14:16
Take a uniform sampling of whitespace delimted strings from the standard input. algorithm R
#include <iostream>
#include <random>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <iomanip>
#include <string>
int main(int argc, char *argv[]) {
if (argc < 2 || argv[1] == "--help" || argv[1] == "-h") {
@socantre
socantre / examples.txt
Created February 19, 2015 19:56
commands to set visual studio build environment
%VC12HOME%\vcvarsall.bat amd64
%VC12HOME%\vcvarsall.bat
%VC11HOME%\vcvarsall.bat amd64
%VC11HOME%\vcvarsall.bat
%VC10HOME%\vcvarsall.bat amd64
%VC10HOME%\vcvarsall.bat
%VC9HOME%\vcvarsall.bat amd64
%VC9HOME%\vcvarsall.bat
%VC8HOME%\vcvarsall.bat amd64
%VC8HOME%\vcvarsall.bat
@socantre
socantre / days_from_civil.lua
Created February 19, 2015 18:13
days_from_civil implemented in Lua
--[[
http://howardhinnant.github.io/date_algorithms.html
Returns number of days since civil 1970-01-01. Negative values indicate
days prior to 1970-01-01.
Preconditions: y-m-d represents a date in the civil (Gregorian) calendar
m is in [1, 12]
d is in [1, last_day_of_month(y, m)]
y is "approximately" in
[numeric_limits<Int>::min()/366, numeric_limits<Int>::max()/366]
@socantre
socantre / CMakeLists.txt
Last active September 14, 2016 21:23
CMakeLists.txt for lua
cmake_minimum_required(VERSION 3.3)
project(lua VERSION 5.3.2 LANGUAGES C CXX)
set(LIB_SOURCE src/lapi.c src/lauxlib.c src/lbaselib.c src/lbitlib.c
src/lcode.c src/lcorolib.c src/lctype.c src/ldblib.c
src/ldebug.c src/ldo.c src/ldump.c src/lfunc.c src/lgc.c
src/linit.c src/liolib.c src/llex.c src/lmathlib.c src/lmem.c
src/loadlib.c src/lobject.c src/lopcodes.c src/loslib.c
src/lparser.c src/lstate.c src/lstring.c src/lstrlib.c
src/ltable.c src/ltablib.c src/ltm.c src/lundump.c
@socantre
socantre / newton.cpp
Created February 12, 2015 17:09
newton
#include <iostream>
#include <random>
#include <cassert>
#include <cstdint>
template< typename T, typename F, typename DF >
T newton(T x, F f, DF df)
{
using std::abs;
T y = f(x);
#include <iostream>
#include <random>
#include <cassert>
#include <cstdint>
static int odd_bits(std::uint64_t i) {
auto a = (0xFFFFFFFFu & i) ^ (i >> 32);
auto b = (0x0000FFFFu & a) ^ (a >> 16);
auto c = (0x000000FFu & b) ^ (b >> 8);
auto d = (0x0000000Fu & c) ^ (c >> 4);
@socantre
socantre / check_clock_resolution.cpp
Created October 23, 2014 16:47
see what resolution a chrono clock has
#include <future>
#include <atomic>
#include <iostream>
#include <vector>
#include <chrono>
template<typename Clock>
typename Clock::duration
check_clock_resolution() {
auto s = Clock::now();