Skip to content

Instantly share code, notes, and snippets.

@jagt
jagt / virtab.cc
Last active June 8, 2016 13:45
explicitly access virtual table. should be somehow cross platform
#include <iostream>
using namespace std;
typedef void(*action_t)(void);
struct Foo
{
virtual void a() {cout << "a" << endl;}
virtual void b() {cout << "b" << endl;}
virtual ~Foo() {}
@jagt
jagt / singleton.cc
Created June 25, 2013 15:01
c++ singleton
#include <iostream>
#include <memory>
using namespace std;
// http://stackoverflow.com/questions/1008019/c-singleton-design-pattern
class Singleton
{
public:
static Singleton& instance()
{
@jagt
jagt / asUTCTimestamp.js
Last active March 1, 2023 13:13
Naive datetime timestamp(local time treated as UTC) in Python and Javascript
// convert a date to timestamp as if it is a UTC date
// in other words used to remove local timezone info and fake everything as UTC
// must beware the funky api naming
// workaround js UTC date things
// the general rule is treat date as naive datetime with no timezone info
// using UTC timezone is better since it won't change based on user's computer location
function asUTCTimestamp(date) {
return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(),
date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
@jagt
jagt / gist:6380174
Created August 29, 2013 16:14
Setup ActivePerl(win32) with TDM-GCC(mingw32)
@jagt
jagt / autograder.c
Created September 5, 2013 16:38
reddit dailycoder
#include <stdio.h>
#include <stdlib.h>
#define NAMEMAX 32
/* http://www.reddit.com/r/dailyprogrammer/comments/1kphtf/081313_challenge_136_easy_student_management/ */
int main() {
int row, cnt, ix, jx, *bufs;
char *names;
float *avgs, total_sum;
scanf("%d%d", &row, &cnt);
@jagt
jagt / gist:6759127
Created September 30, 2013 03:42
force rmtree
import stat
from os import path, rmdir, remove, chmod, walk
from shutil import rmtree
def force_rmtree(root_dir):
'''
rmtree doesn't work when no write bit in linux or read-only in windows
force_rmtree recursively walk, do chmod and then remove
'''
for root, dirs, files in walk(root_dir, topdown=False):
@jagt
jagt / kvparse.py
Last active December 24, 2015 06:49
handrolled stupid key value parser
from StringIO import StringIO
def kvparse(s):
sin = StringIO(s)
d = {}
c = None
# workaround the nonlocal c by assignment everytime
def _consume():
return sin.read(1)
@jagt
jagt / shitty.cc
Created October 1, 2013 02:36
winsocks example. write a string to connected browser and exit
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
using namespace std;
// write a string to connected browser and exit
int main() {
WSADATA wsa;
if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0) {
cerr << "WSAStartup failed." << endl;
@jagt
jagt / cool.flex
Created October 19, 2013 16:10
cool lang working lex file for flex
/*
* The scanner definition for COOL.
*/
/*
* Stuff enclosed in %{ %} in the first section is copied verbatim to the
* output, so headers and global definitions are placed here to be visible
* to the code in the file. Don't remove anything that was here initially
*/
%{
@jagt
jagt / FixedMap.hx
Created November 11, 2013 17:30
Fixed 2D Map with operator overloading. Out of bound read returns preset empty value, and out of bound write throws error.
/**
* Fixed 2D Map, any out of bound reads returns empty, while out of bound
* writes will raise an error.
*/
private class FixedArrayImpl<T>
{
public var array : Array<T>;
public var size : Int;
public var empty : T;