Skip to content

Instantly share code, notes, and snippets.

@AndreasDM
AndreasDM / main.cpp
Created March 22, 2026 19:58
fun preprocessor trick
#include <cstdio>
extern "C" const char arr[];
asm(
#include "foo"
);
/* Assume that the file "foo" has the content:
R"(
@AndreasDM
AndreasDM / main.cpp
Last active April 9, 2025 13:43
Coroutine
struct coroutine {
struct promise_type;
using handle_type = coroutine_handle<promise_type>;
struct promise_type {
coroutine get_return_object()
{ return { coroutine::handle_type::from_promise(*this) }; }
suspend_always initial_suspend () noexcept { return {}; }
suspend_always final_suspend () noexcept { return {}; }
@AndreasDM
AndreasDM / chessboard.cpp
Created November 26, 2024 11:10
OpenGL draw chessboard
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
const char * vertex_shader_src =
R"(#version 330 core
layout (location = 0) in vec2 vertex;
uniform mat4 ortho;
@AndreasDM
AndreasDM / chatroom.cpp
Last active October 10, 2024 20:51
Chat Room
#include <fmt/core.h>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
@AndreasDM
AndreasDM / chopstringify.cpp
Created August 5, 2019 02:25
chop stringify
#include <iostream>
#include <array>
#include <string>
#include <vector>
#include <numeric>
namespace {
namespace helper {
using std::to_string;
@AndreasDM
AndreasDM / chop.cpp
Last active August 5, 2019 02:29
compiletime chop
#include <string>
#include <iostream>
#include <array>
#include <vector>
#include <numeric>
#include <iterator>
#include <boost/type_index.hpp>
using namespace boost::typeindex;
template <std::size_t N, std::size_t M, typename T>
@AndreasDM
AndreasDM / tic.cpp
Last active June 25, 2019 19:24
tic tac toe
#include <algorithm>
#include <iostream>
#include <vector>
enum { p1, p2 };
int main() {
std::cout.sync_with_stdio(false);
typedef unsigned short Board;
@AndreasDM
AndreasDM / binarytree.c
Created June 15, 2019 15:34
binary tree C
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
typedef struct Node Node;
struct Node {
int key;
char* val;
Node* left;
@AndreasDM
AndreasDM / main.c
Created June 9, 2019 15:24
hash table C
#include "stdio.h"
#include "string.h"
#define m 97
int n = 0;
char* keys[m] = { 0 };
long values[m] = { 0 };
/* hash function for strings
@AndreasDM
AndreasDM / init.cpp
Created April 26, 2019 14:50
recursive init (useless fun)
#include <iostream>
#include <vector>
#include <list>
#include <numeric>
#include <deque>
using namespace std;
auto head = [](auto v) { return decltype(v){ v.front() }; };
auto tail = [](auto v) { return decltype(v){ next(begin(v)), end(v) }; };
auto print = [](auto const& v) { for (auto const& i : v) cout << boolalpha << i << ' '; cout << '\n'; };