Skip to content

Instantly share code, notes, and snippets.

View dmatveev's full-sized avatar
🌿

Dmitry Matveev dmatveev

🌿
View GitHub Profile
@dmatveev
dmatveev / gist:7516380
Created November 17, 2013 18:23
ProTip: scoped preprocessor definitions
#include <stdio.h>
/* #define FOO 33 */
#ifdef FOO
# pragma push_macro ("FOO")
# define FOO_WRAPPED
# undef FOO
#endif
@dmatveev
dmatveev / gist:6200371
Last active December 20, 2015 21:49
I really like the things I can do easily with C++11x. An example: some RAII madness. Just replace begin/end/unwinding with lock/unlock/etc operations else, and you will get an utility for transactional access to multiple objects.
/* g++ -std=c++11 wrap.cc */
#include <functional>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
typedef std::function<void ()> Action;
typedef std::function<void (Action)> Context;
@dmatveev
dmatveev / gist:6008120
Last active December 19, 2015 19:39
Heap sort on C++ and on Haskell
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <utility>
class binheap {
std::size_t heap_size;
static std::size_t heap_parent (std::size_t i) {
@dmatveev
dmatveev / gist:5993223
Created July 14, 2013 04:36
Yet another Haskell ST-powered QuickSort implementation
{-# LANGUAGE ScopedTypeVariables #-}
module MergeSort where
import Control.Monad.ST
import Control.Monad (forM_, when)
import Data.Array.ST
import Data.STRef
-- Required for tests only
@dmatveev
dmatveev / gist:2295597
Created April 3, 2012 21:26
GFileMonitor sample application. Watches the specified path, registers events and prints it on the screen.
#include <glib.h>
#include <gio/gio.h>
#include <string.h>
static GMainLoop *gMainLoop = NULL;
char *
decode (GFileMonitorEvent ev)
{
char *fmt = g_malloc0 (1024);
import Data.List (isSuffixOf)
import Data.Char (ord)
import Control.Monad (forM)
import System.IO (IOMode(..), withFile, hPutStrLn)
import System.FilePath (dropExtension)
import System.Directory (getDirectoryContents)
target :: FilePath -> Bool
target p = ".htm" `isSuffixOf` p
@dmatveev
dmatveev / gist:1922080
Created February 27, 2012 06:57
A poor man's monad
(>>?) :: (Ord a, Num a) => Maybe a -> (a -> Maybe b) -> Maybe b
Nothing >>? _ = Nothing
Just v >>? f = if v > 0 then f v else Nothing
decr :: Int -> Maybe Int
decr a = Just (a - 1)
ex1 = (Just 1) >>? decr >>? decr >>? decr
@dmatveev
dmatveev / ChainedHash.hs
Created January 15, 2012 18:20
Chained hash table implementation
module ChainedHash
(
createHash
, with
, without
, withMany
, withoutMany
, at
) where
@dmatveev
dmatveev / gist:1267238
Created October 6, 2011 11:58
Simple logging code snippet
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
static CRITICAL_SECTION crit;
void log_init()
{
static volatile LONG done = 0;
if (done == 0) {
@dmatveev
dmatveev / gfiletest.c
Created September 11, 2011 11:53
GFileMonitor test for gio/kqueue GSoC project
#include <glib.h>
#include <glib/gprintf.h>
#include <gio/gio.h>
#include <string.h> /* memset, strlen */
#include <stdlib.h> /* system */
#define MAX_EXPECTED_EVENTS 10
#ifndef EVENT_EXPECT_TIME
# define EVENT_EXPECT_TIME 5