Skip to content

Instantly share code, notes, and snippets.

@ramntry
ramntry / simple_daytime_server.cpp
Created October 3, 2012 15:07
Simple Boost.Asio based synchronous multithreaded server
#include <ctime>
#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/format.hpp>
#include <boost/smart_ptr.hpp>
@ramntry
ramntry / automemoization.hs
Last active September 25, 2022 13:05
Auto-memoization in Haskell by State monad
import Control.Monad.State
import qualified Data.Map as Map
recall :: Ord a => a -> State (Map.Map a r) (Maybe r)
recall n = do
memory <- get
return (Map.lookup n memory)
memorize :: Ord a => a -> r -> State (Map.Map a r) ()
memorize n result = do
#include <iostream>
#include <vector>
class Tracker {
public:
void record(int val, int index);
int get_last_index(int val);
std::pair<int, int> get_stats() const;
private:
@ramntry
ramntry / main.cpp
Created May 5, 2019 05:55
Very pretty triangle (OpenGL)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#pragma clang diagnostic pop
#include <iostream>
@ramntry
ramntry / float.cpp
Last active August 9, 2018 05:50
IEEE-754 single precision software implementation
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <bitset>
#include <limits>
#include <cassert>
using namespace std;
struct single {
double foo(double *a, int n) {
double x = 0.0;
double y = 0.0;
for (int i = 0; i < n; i += 2) {
x += a[i];
y += a[i + 1];
}
return x * y;
}
  // |  Expression   |     ConstantRange      |       KnownBits       |
  // |---------------|------------------------|-----------------------|
  // | i8 4 * X      | [L: 0, U: 253)         | XXXX XX00             |
  // |               |   => Min: 0, Max: 252  |   => Min: 0, Max: 252 |
  // |               |                        |                       |
  // | i8 4 * X + 5  | [L: 5, U: 2) (wrapped) | YYYY YY01             |

// | (101) | => Min: 0, Max: 255 | => Min: 1, Max: 253 |

#import <Foundation/Foundation.h>
#include <stdio.h>
@interface NSString (MyPrintable)
- (const char *)toCString;
@end
@implementation NSString (MyPrintable)
// NSString is a library provided class that doesn't have
// a toCString method. We're dynamically extending the class
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
template <typename T>
class Counter : public std::iterator<std::output_iterator_tag, T> {
size_t &Value;
public:
%dag = type { { { i8, { i8 } }, { { i8, { i8 } }, { i8 } } }, { { i8, { i8 } }, { i8 } } }
define void @test_const(%dag* %dst) {
entry:
%updated = insertvalue
; Check that we're visiting constants with shared parts
; (deduplicated via LLVMContext, forming a proper DAG) correctly:
%dag {
{ { i8, { i8 } }, { { i8, { i8 } }, { i8 } } } {
{ i8, { i8 } } {