Skip to content

Instantly share code, notes, and snippets.

View oprypin's full-sized avatar

Oleh Prypin oprypin

View GitHub Profile
@oprypin
oprypin / workaround.nim
Last active August 29, 2015 14:13
Negative indices workaround
proc `..<`(a, b: int): auto =
if a < 0:
raise newException(IndexError, "Left index == " & $a & ", out of range")
if b <= 0:
return 1 .. 0
if a >= b:
return a .. <a
return a .. <b
proc `.!`(a, b: int): auto =
@oprypin
oprypin / base64_urlsafe.nim
Created January 19, 2015 10:34
urlsafe base64
import strutils
from base64 import nil
proc encodeUrlSafe(s: string): string =
result = base64.encode(s, newLine="")
while result.endsWith("="):
result = result.substr(0, result.high-1)
result = result.replace('+', '-').replace('/', '_')
proc decodeUrlSafe(s: string): string =
@oprypin
oprypin / cool_module.nim
Last active August 29, 2015 14:14
Make a Nimble module importable in all your Nim projects
proc cool*() =
echo "cool"
import itertools
def readable_size(nbytes, i=True):
sizes = ("B KiB MiB GiB TiB" if i else "B KB MB GB TB").split()
last = sizes[-1]
d = 1024 if i else 1000
for sz in sizes:
if nbytes < d or sz is last:
return '{} {}'.format(format(nbytes, '.1f').rstrip('0').rstrip('.'), sz)
@oprypin
oprypin / intset.c
Created March 22, 2015 11:41
Judy vs Nim IntSet iteration
#include <Judy.h>
#define indices_n 2000
Word_t indices[indices_n] = {15058, 4615, 6668, 19845, 10014, 15721, 2240, 2242, 631, 13511, 8475, 6887, 892, 4482, 8121, 14453, 4908, 17673, 5832, 2941, 5004, 6091, 8217, 10317, 9396, 5332, 2703, 7244, 13111, 15175, 8566, 16577, 11083, 17706, 4009, 19840, 9041, 16178, 12902, 2578, 15617, 17487, 5431, 18698, 2980, 6648, 19904, 12869, 14177, 2706, 13589, 1539, 2826, 2252, 19633, 14102, 19260, 12452, 1857, 4188, 13202, 1976, 10017, 4048, 3797, 7838, 11787, 12890, 9904, 17862, 15096, 5640, 1347, 6616, 9498, 15933, 13588, 14450, 2773, 13210, 12112, 1448, 2793, 7565, 1429, 8562, 510, 8059, 18845, 7602, 19739, 362, 18320, 14386, 6955, 10739, 8387, 15216, 456, 2042, 15969, 16558, 9596, 3598, 13373, 8904, 17604, 7732, 18004, 13635, 4626, 15346, 3456, 17926, 16743, 1101, 13777, 11257, 14685, 8030, 11892, 556, 19225, 2187, 18030, 7171, 12630, 13436, 13390, 7567, 8533, 18102, 16930, 4063, 452, 15028, 18198, 8103, 7002, 19811, 9833, 11289, 18303, 5631, 4767, 8337, 5920, 4558, 845
@oprypin
oprypin / sample.py
Created March 25, 2015 09:10
Memory efficiency benchmark
import sys
import random
import math
def sample1(randbelow, n, k):
# Array-based shuffle algorithm (used by Python)
pool = list(range(n))
for i in range(n, n-k, -1):
j = randbelow(i)
@oprypin
oprypin / option.nim
Last active August 29, 2015 14:18
Option[T]
type
Nullable = concept x
isNil(x) is bool
NilOption*[T] = distinct T
CompositeOption*[T] = object
has: bool
val: T
import macros
macro doThis(s: static[string]): stmt =
let n = parseStmt(s)
quote do:
template actuallyDoIt: stmt =
`n`
template exec*(s: string) {.immediate.} =
doThis(s)
%if false:
echo 5
echo "a"
echo "lol"
for i in 0..10:
%if true:
echo 6
echo 8
@oprypin
oprypin / csfml.diff
Last active August 29, 2015 14:20
API changes between SFML 2.2 and 2.3
git diff CSFML-2.2 CSFML-master | grep -vP '^([^-+]|. *//)' | sed -r 's/^[-+]{3} .+\///g' | uniq
SoundBuffer.h
-CSFML_AUDIO_API sfSoundBuffer* sfSoundBuffer_createFromSamples(const sfInt16* samples, size_t sampleCount, unsigned int channelCount, unsigned int sampleRate);
+CSFML_AUDIO_API sfSoundBuffer* sfSoundBuffer_createFromSamples(const sfInt16* samples, sfUint64 sampleCount, unsigned int channelCount, unsigned int sampleRate);
-CSFML_AUDIO_API size_t sfSoundBuffer_getSampleCount(const sfSoundBuffer* soundBuffer);
+CSFML_AUDIO_API sfUint64 sfSoundBuffer_getSampleCount(const sfSoundBuffer* soundBuffer);
Audio.h
+#include <SFML/Audio/SoundStatus.h>
+#include <SFML/Audio/SoundStream.h>