Skip to content

Instantly share code, notes, and snippets.

View KrzysztofCiba's full-sized avatar

Krzysztof Daniel Ciba KrzysztofCiba

View GitHub Profile
@KrzysztofCiba
KrzysztofCiba / color.py
Last active December 18, 2015 16:39
simple graph coloring
#!/usr/bin/env python
import random
import sys
class node( object ):
""" a vertex
"""
def __init__( self, key ):
""" c'tor """
self.key = key
class Node( object ):
"""
.. class:: Node
rbTree Node
"""
def __init__( self, key ):
""" c'tor """
self.left = None
__bc16b = [ 0 ] * (256*256)
for i in range(256*256):
__bc16b[i] = ( i & 1 ) + __bc16b[i/2]
def popcnt16b( arg ):
""" popcnt using 16bits loom up table """
return sum( [ __bc16b[( arg >> i ) & 65535 ] for i in range( 0, arg.bit_length(), 16 ) ] )
## bit count look up table in range 0..255
__bcLUT = [ 0 ] * 256
for i in range(256):
__bcLUT[i] = ( i & 1 ) + __bcLUT[i/2]
def popcntLUT( arg ):
""" popcnt using look up table """
assert type(arg) in ( int, long )
return sum( [ __bcLUT[ ( arg >> i ) & 255 ]
for i in range( 0, arg.bit_length(), 8 ) ] )
@KrzysztofCiba
KrzysztofCiba / gist:5717137
Last active December 18, 2015 03:19
popcnt benchmarks
# --- testBK.py
import sys
import random
from popcnt import popcntBK
up = 2**256
for i in range(1, 1001):
if not i % 10:
print ".",
sys.stdout.flush()
if not i % 100:
@KrzysztofCiba
KrzysztofCiba / popcnt.py
Last active December 18, 2015 02:58
python pop count
#!/bin/env python
""" :mod: popcnt
============
.. module: popcnt
:synopsis: couting bits set ("1") in int or long
.. moduleauthor:: Krzysztof.Ciba@NOSPAMgmail.com
"""
## bit count look up table in range 0..255
__bcLUT = [ 0 ] * 256
@KrzysztofCiba
KrzysztofCiba / perm.py
Last active October 11, 2021 09:05
Get nth permutation of a list, while permutations are created in a lexicographical order.
#!/bin/env python
"""
Solves a problem of getting permutation of a given index,
while you know that permutations are created in a lexicographical order.
As input you are specifying a list of elements and index of permutation you want to get.
"""
## imports
from math import factorial
## this one just for checking
from multiprocessing import Process, Manager
def sender( S ):
print "sending 256 MB"
S["foo"] = "."*256*1024*1024
if __name__ == '__main__':
m = Manager()
S = m.dict()
[Tue, 19 Feb 19:25 E1][cibak@localhost:~]> python pipeBuf.py
send 0 B
read 0 B
send 1024 B
read 1024 B
send 2048 B
read 2048 B
[...cut...]
read 59392 B
send 60416 B
from multiprocessing import Pipe
if __name__ == "__main__":
( pout, pin ) = Pipe(False)
for i in range(1024):
pin.send("."*i*1024)
print "send", i*1024, "B"
r = pout.recv()
print "read", len(r), "B"