Skip to content

Instantly share code, notes, and snippets.

@alexeiz
alexeiz / named_args.cpp
Created July 19, 2012 18:43
Named arguments in C++ (a bare bones concept)
#include <iostream>
#include <ostream>
#define DEFINE_NAMED_ARG(type, arg_name, arg_type) \
struct type \
{ \
type(arg_type val = arg_type()) \
: val_(val) \
{} \
\
@alexeiz
alexeiz / current_utc_time.cpp
Last active October 7, 2015 14:58 — forked from jbenet/current_utc_time.c
workaround for clock_gettime in os x (mach)
#include <time.h>
#include <sys/time.h>
#include <iostream>
#include <ostream>
// POSIX Mach
// clock_gettime clock_get_time
// CLOCK_REALTIME CALENDAR_CLOCK
// CLOCK_MONOTONIC REALTIME_CLOCK
@alexeiz
alexeiz / matrix-search.cpp
Created August 12, 2012 21:51
Search in ordered matrix
// This is a simple example of an algorithm that does a binary search in
// a [m, n] matrix containing integer numbers, ordered such that for each
// i, j and k, l that i<=k and j<=l, m[i][j] <= m[k][l].
// The matrix is pregenerated and the dimensions are fixed for simplicity.
// However the algorithm itself is generic enough to handle any ordered matrix.
#include <iostream>
#include <ostream>
#include <iomanip>
#include <vector>
@alexeiz
alexeiz / thr-singleton.cpp
Created August 16, 2012 16:39
Thread-safe one-time initialization in C++11
// C++11 has thread-safe function local statics, but this code is just
// to illustrate how one would do a thread-safe one-time initialization
// using atomic variables.
#include <atomic>
#include <thread>
#include <cassert>
#include <vector>
using namespace std;
@alexeiz
alexeiz / output.txt
Created October 26, 2012 21:53
Analysis of passing function parameters by-value vs by-reference in C++
testing: by value
* lvalue
lvalue: cctor
lvalue: mctor
* xvalue
xvalue: mctor
xvalue: mctor
* rvalue
rvalue: mctor
* done
@alexeiz
alexeiz / overloadset.cpp
Created November 5, 2012 07:16
Lambda overload set
#include <iostream>
template <typename ...Funcs>
struct overload_set;
template <typename Head, typename ...Tail>
struct overload_set<Head, Tail...> : Head, overload_set<Tail...>
{
overload_set(Head head, Tail... tail)
: Head{head}
@alexeiz
alexeiz / iscallable.cpp
Created December 18, 2012 16:15
Test if a type is a callable, i.e. objects of that type can be called as functions. Also shows how to do type tests with decltype instead of sizeof.
#include <type_traits>
template<typename F, typename ...Ts>
class is_callable
{
template<typename G, typename ...Us>
static
decltype(
typename std::add_pointer<decltype(std::declval<G>()(std::declval<Us>()...))>::type{},
std::true_type{})
@alexeiz
alexeiz / htop-solarized-patch.diff
Created January 28, 2013 17:19
Patch for the default color scheme for htop that works well with the solarized terminal color theme.
diff -u -r a/CRT.c b/CRT.c
--- a/CRT.c 2012-10-19 14:59:28.000000000 -0400
+++ b/CRT.c 2013-01-28 12:08:36.812929000 -0500
@@ -562,7 +562,7 @@
CRT_colors[LED_COLOR] = ColorPair(Green,Black);
CRT_colors[TASKS_RUNNING] = A_BOLD | ColorPair(Green,Black);
CRT_colors[PROCESS] = A_NORMAL;
- CRT_colors[PROCESS_SHADOW] = A_BOLD | ColorPair(Black,Black);
+ CRT_colors[PROCESS_SHADOW] = A_BOLD | ColorPair(Green,Black);
CRT_colors[PROCESS_TAG] = A_BOLD | ColorPair(Yellow,Black);
@alexeiz
alexeiz / gcharts.html
Created March 20, 2013 06:00
An example of using Google Chart Tools from CoffeeScript. This is a self-contained html page that displays a table and a column chart. It uses coffeescript code to prepare and display the data. The code is translated into js on the fly in the browser.
<html>
<head>
<title>CoffeeScript charts</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.load("visualization", "1", {packages:["table"]});
</script>
@alexeiz
alexeiz / null_stream.cpp
Created May 5, 2013 04:37
Redirect cout to a null sink using boost::iostreams.
#include <boost/iostreams/stream_buffer.hpp>
#include <boost/iostreams/device/null.hpp>
#include <iostream>
namespace io = boost::iostreams;
int main()
{
io::stream_buffer<io::null_sink> null_buf{io::null_sink()};
std::cout.rdbuf(&null_buf);