Skip to content

Instantly share code, notes, and snippets.

View Youka's full-sized avatar

Youka Youka

  • Germany
  • 00:11 (UTC +02:00)
View GitHub Profile
@Youka
Youka / data_stack.h
Created October 12, 2014 03:23
Data stack
#ifndef DATA_STACK_H // Include guard
#define DATA_STACK_H
#include <stdlib.h> // size_t, NULL, malloc, free
// Stack node
typedef struct data_stack_node{
void* data; // Userdata
size_t data_len; // Userdata byte length
struct data_stack_node* prev; // Reference to previous node
@Youka
Youka / goertzel_freq.lua
Created October 12, 2014 03:28
Goertzel frequency analyzer
-- Frequency analyzer
local function goertzel(samples, sample_rate, freq)
-- Get coefficient from target and maximal frequency
local coeff = 2 * math.cos(2 * math.pi * freq / sample_rate)
-- Process samples
local s_prev, s_prev2, s = 0, 0
for i=1, #samples do
s = samples[i] + coeff * s_prev - s_prev2
s_prev2, s_prev = s_prev, s
end
@Youka
Youka / dirty_data.c
Last active August 29, 2015 14:07
2d data are dirty
#include <stdint.h> // uint8_t, uint16_t, uintptr_t
#include <string.h> // memset
// Data are dirty?
int is_dirty_naive(const uint16_t width, const uint16_t height, const uint16_t stride, const uint8_t* data){
for(uint16_t row = 0; row < height; ++row){
for(uint16_t col = 0; col < width; ++col)
if(data[col] > 0)
return 1;
data += stride;
@Youka
Youka / timeout_exec.php
Created October 12, 2014 05:50
Execution with timeout
<?PHP
function timeout_exec($cmd, $stdin = "", &$stdout, &$stderr, $timeout = 0){
// Initialize output
$stdout = "";
$stderr = "";
// Start process
$pipes = array();
$process = proc_open($cmd, array(array('pipe','r'),array('pipe','w'),array('pipe','w')), $pipes);
if(!is_resource($process))
@Youka
Youka / cache.hpp
Last active August 29, 2015 14:07
Cache for pairs
#pragma once
#include <deque>
#include <algorithm>
template<typename Key, typename Value>
class Cache{
private:
std::deque<std::pair<Key,Value>> data;
const unsigned int max_size;
@Youka
Youka / main.cpp
Created May 18, 2015 18:47
MinGW wifstream
#include <ext/stdio_filebuf.h> // fstream (all sorts of IO stuff) + stdio_filebuf (=streambuf)
#include <fcntl.h> // _O_RDONLY
#include <iostream> // cout
int main(){
__gnu_cxx::stdio_filebuf<char> wfile_buf(_wopen(L"D:\\...\\の.txt", _O_RDONLY), std::ios_base::in);
std::istream wfile_stream(&wfile_buf);
wfile_stream.seekg(0, std::ios_base::end);
std::cout << wfile_stream.tellg();
return 0;
@Youka
Youka / java_env.bat
Created June 13, 2015 17:02
Small java environment setup in console
@echo off
rem Set console appearance
title Java environment (%cd%)
color 0C
rem Set console environment
if "%~1"=="" (
set JAVA_BIN=C:\Program Files\Java\jdk1.8.0_45\bin
) else (
set JAVA_BIN=%~1
)
@Youka
Youka / main.c
Last active August 29, 2015 14:25
Performance test of vector division by unsigned short integers - x86 vs. SSE
#include <emmintrin.h> // SSE2
static unsigned short x[8] = {0, 55, 2, 62003, 786, 5555, 123, 32111}; // Dividend
__attribute__((noinline)) static void test_div_x86(unsigned i){
for(; i; --i)
x[0] /= i,
x[1] /= i,
x[2] /= i,
x[3] /= i,
@Youka
Youka / main.c
Created September 28, 2015 02:15
Calculate Pi
#include <stdlib.h>
#include <stdio.h>
#ifdef __SSE2__
#include <emmintrin.h>
#endif
static double calc_pi(unsigned n){
double pi, x;
#ifdef __SSE2__
const __m128d four = _mm_set1_pd(4.0);
@Youka
Youka / string_replace.c
Last active January 13, 2016 19:57
C string find&replace
#include <string.h>
#include <stdlib.h>
// Replace string in string by creating a new one
char* str_replace(char* original, const char* find, const char* replacement, const char free_original){
// Initializations
int found_count = 0;
const size_t find_len = strlen(find), replacement_len = strlen(replacement);
char* result, *presult;
const char* poriginal = original, *found;