Skip to content

Instantly share code, notes, and snippets.

@jdeng
jdeng / topk
Last active December 14, 2015 18:39
Top k elements of a const vector: lambda with variable capture, one pass trick in std::pop_heap, in c++ 11. Compiled with clang 3.1.
#include <random>
#include <vector>
#include <algorithm>
#include <iostream>
#include <time.h>
std::vector<size_t> topk(const std::vector<double>& values, size_t k)
{
size_t n = values.size();
@jdeng
jdeng / map_reduce
Created March 10, 2013 22:09
simple map reduce with C++ 11. The sample maps an integer to a string, and reduces with reverse concatenation.
#include <string>
#include <vector>
#include <functional>
#include <iostream>
template <typename R, typename T, typename M>
auto map_reduce(const std::vector<T>& input, std::function<M(const T&)> mapper, std::function<R(const M& m, const R& r)> reducer, const R& init) -> decltype(reducer(std::declval<const M&>(), std::declval<const R&>()))
{
R r = init;
for (auto const & i: input) r = reducer(mapper(i), r);
@jdeng
jdeng / constructor
Created March 12, 2013 01:37
Constructor inheritance
#include <vector>
#include <iostream>
template <typename Iterator>
struct range: public std::pair<Iterator, Iterator>
{
using Parent = std::pair<Iterator, Iterator>;
template <typename... Arg> range(Arg&& ... arg): Parent(std::forward<Arg>(arg) ...) {}
Iterator begin() { return this->first; }
@jdeng
jdeng / queens.cc
Created December 11, 2013 16:48
N Queens Problem (N <= 16)
//compile with -Ofast -march=native -funroll-loops
#include <stdio.h>
typedef unsigned short uint16_t;
template <int n, int m>
struct queens {
static void f(uint16_t col, uint16_t left, uint16_t right, size_t& count) {
uint16_t mask = ~(col | left | right);
for (int i = 0; i < n; ++i, mask >>= 1) {
@jdeng
jdeng / queens-omp.cc
Last active December 31, 2015 01:39
N Queens with OpenMP
//compile with -fopenmp -Ofast -march=native -funroll-loops
#include <stdio.h>
typedef unsigned int mask_t;
template <int n, int m>
struct queens {
static size_t f(mask_t col, mask_t left, mask_t right) {
mask_t mask = ~(col | left | right);
size_t count = 0;
while (mask) {
@jdeng
jdeng / seq.hh
Created December 12, 2013 19:24
integer sequence c++ 11
//copied from http://stackoverflow.com/questions/17424477/implementation-c14-make-integer-sequence
template<int...> struct seq { using type = seq; };
template<typename T1, typename T2> struct concat;
template<int... I1, int... I2> struct concat<seq<I1...>, seq<I2...>>: seq<I1..., (sizeof...(I1) + I2)...> {};
template<int N> struct gen_seq;
template<int N> struct gen_seq: concat<typename gen_seq<N/2>::type, typename gen_seq<N-N/2>::type>::type {};
template <> struct gen_seq<0>: seq<>{};
template <> struct gen_seq<1>: seq<0>{};
@jdeng
jdeng / error_code.hpp
Created December 19, 2013 20:32
Using std::error_code etc to replace boost's in /usr/local/Cellar/boost/1.54.0/include/boost/system/. You'll need to use an empty system_error.hpp and possibly specialize is_error_code_enum in namespace std::.
#pragma once
#include <system_error>
namespace boost { namespace system {
using error_category = std::error_category;
using error_code = std::error_code;
using error_condition = std::error_condition;
using system_error = std::system_error;
using errc = std::errc;
using std::system_category;
// command line
//"/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o js.pdf 0*.pdf
//quickfix css for angularjs.cn for printing
['ul.pagination', '.aside', '.footer', 'ul.user', '#comments', '#cnzz_stat_icon_1000062427'].forEach(function(e) {$(e).hide();});
$('.pure-u-2-3').css("width", "100%");
$('#main').css("background-color","#ffffff");
$('pre.prettyprint').css('background-color', '#aaaaaa');
$('.panel').css('-webkit-box-shadow', 'none');
@jdeng
jdeng / cluster
Last active June 17, 2020 02:52
clustering by fast search and find of density peak
// generate [0..n-1]
auto seq = [](size_t n) -> std::vector<size_t> {
std::vector<size_t> v(n);
for (size_t i=0; i<n; ++i) v[i] = i;
return v;
};
auto index = seq(n);
// n * n distance matrix
std::vector<D> dists(n * n);
@jdeng
jdeng / println
Created July 9, 2014 01:06
to_string & println with variadic templates
template< class S> void print_with(S& s, char delim) {}
template< class S, typename T> void print_with(S& s, char delim, const T& t) { s<<t; }
template <class S, typename T, typename... Args> void print_with(S& s, char delim, const T& t, const Args& ...args) { s << t; if (delim) s << delim; print_with(s, delim, args...); }
template <typename... Args>
std::string to_string_with(char delim, const Args& ...args) { std::stringstream ss; print_with(ss, delim, args...); return ss.str(); }
template <typename... Args> std::string to_string(const Args& ...args) { return to_string_with('\x00', args...); }
template <typename... Args> std::ostream& print(std::ostream& out, const Args& ...args) { print_with(out, '\x00', args...); return out;}
template <typename... Args> std::ostream& println(std::ostream& out, const Args& ...args) { print_with(out, '\x00', args...); out << std::endl; return out;}