Skip to content

Instantly share code, notes, and snippets.

@yohhoy
yohhoy / ttas_mutex.cpp
Last active January 19, 2017 15:41
Test-And-Test-And-Set lock
#include <thread>
#include <atomic>
class ttas_mutex {
std::atomic<bool> locked_;
public:
ttas_mutex()
: locked_(false) {}
ttas_mutex(const ttas_mutex&) = delete;
ttas_mutex& operator=(const ttas_mutex&) = delete;
@yohhoy
yohhoy / gist:a3ae53709e39e8b4a34c
Last active August 29, 2015 14:09
Declare function in C
#include <stdio.h>
// F is function type with 2 int args and return int.
typedef int F(int, int);
// declare function `add`, `sub`
F add, sub;
int main()
{
#include <iostream>
#include <utility>
struct X {
X() {}
X(const X&) = delete;
X(X&&) /*noexcept(false)*/ {}
};
struct Y {
@yohhoy
yohhoy / aac_parser.py
Last active May 22, 2022 21:43
Parse AAC/ADTS header
#!/usr/bin/env python3
import sys
import struct
if len(sys.argv) < 2:
print("Usage: aac_parer.py <target.aac>")
exit()
aacfile = open(sys.argv[1], 'rb')
frame_no = 1
@yohhoy
yohhoy / ConcReductTest.java
Created December 27, 2014 11:21
concurrent reduction
import java.util.stream.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
public class ConcReductTest {
static public void main(String[] args) {
ConcurrentMap<Integer, AtomicInteger> result =
IntStream.range(1, 10_000_001)
.boxed()
.unordered() // invoke concurrent reduction
@yohhoy
yohhoy / div.c
Last active August 29, 2015 14:12
#include <stdio.h>
#include <assert.h>
void div(int n, int d)
{
int m = 1, q = 0;
assert(0 <= n && 0 < d);
while (d <= n) {
d <<= 1;
m <<= 1;
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
const struct {
int t[3];
char c;
} lut[] = {
{{ 1, 10, 11}, 'O'},
{{ 1, 2, 3}, 'I'},
#include <algorithm>
#include <iostream>
#include <vector>
void pick(const std::vector<int>& v)
{
size_t sz = v.size();
for (size_t n = 1; n <= sz; ++n) {
std::vector<bool> sel(sz);
std::fill_n(sel.begin(), n, true);
#include <iostream>
template<typename T>
struct value_category {
static constexpr auto value = "prvalue";
};
template<typename T>
struct value_category<T&> {
static constexpr auto value = "lvalue";
};
#include <stdio.h>
#include <stdint.h>
void base64_dump(const uint8_t *data, size_t len)
{
static const char lut[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t pos;
for (pos = 0; pos + 3 <= len; pos += 3) {
putchar(lut[ data[pos ] >> 2]);