Skip to content

Instantly share code, notes, and snippets.

View momchil-velikov's full-sized avatar

Momchil Velikov momchil-velikov

  • Arm
  • Cambridge, United Kingdom
View GitHub Profile
package main
import (
"fmt"
"io"
"strings"
)
const (
None = iota
@momchil-velikov
momchil-velikov / gist:8207534
Last active September 9, 2016 14:39
ISO C99 grammar
/* ISO/IEC 9988:1999 grammar (ISO C) */
%token ID TYPEDEF_NAME INT_CST STRING_CST SIZEOF ;
%token ARROW INC DEC ;
%token LSHIFT RSHIFT ;
%token LE GE ;
%token EQ NE ;
%token ANDAND OROR ;
#include <unordered_map>
#include <string>
#include <iostream>
void sorted(const std::string &a, const std::string &order) {
std::unordered_map<char, unsigned int> count;
for (auto c : a)
++count[c];
for (auto c: order) {
for (auto i = count[c]; i > 0; --i)
@momchil-velikov
momchil-velikov / delay-call.cc
Last active January 21, 2017 10:53
Delayed function call facility
// c++/clang++ -pthread --std=c++11 delay-call.cc -o delay-call
#include <iostream>
#include <functional>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <queue>
#include <ctime>
package main
import (
"bufio"
"bytes"
"container/stack"
"flag"
"fmt"
gio "graph/io"
"io"
@momchil-velikov
momchil-velikov / weak-leak.cc
Last active November 8, 2015 09:52
Leaking memory with "smart" pointers
#include <memory>
#include <vector>
#include <algorithm>
#include <array>
struct data {
data(char c) { std::fill(begin(blk), end(blk), c); }
std::array<char, (1 << 20)> blk;
};
package main
import (
"bufio"
"container/deque"
"fmt"
gio "graph/io"
"io"
"math"
"os"
#include <iostream>
#include <utility>
template <typename T> constexpr size_t align_to(size_t n) {
return (n + alignof(T) - 1) & -alignof(T);
}
template <typename T> constexpr size_t skip(size_t n) {
return align_to<T>(n) + sizeof(T);
}
#include <iostream>
#include <thread>
#include <deque>
#include <mutex>
#include <condition_variable>
struct queue {
void put(int x) {
std::unique_lock<std::mutex> guard(lock);
q.push_back(x);
package main
import (
"sync"
"sync/atomic"
"time"
)
var pool *sync.Pool