Skip to content

Commit

Permalink
Merge branch 'experimental'
Browse files Browse the repository at this point in the history
Conflicts:
	src/bdelta.cpp
	src/bdelta_python.cpp
	src/libbdelta.cpp
  • Loading branch information
jjwhitney committed Feb 17, 2012
2 parents 0293d8a + 4e65a82 commit fb0a916
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 276 deletions.
26 changes: 18 additions & 8 deletions setup.py
@@ -1,10 +1,20 @@
from distutils.core import setup, Extension
from Cython.Distutils import build_ext

ext_modules = [Extension(
"bdelta",
["src/bdelta.pyx", "src/libbdelta.cpp"],
define_macros=[('TOKEN_SIZE', '2')],
)]

setup(
name = 'BDelta',
version='0.3.0',
description='Python Bindings for BDelta',
author='John Whitney',
author_email='jjw@deltup.org',
url='http://deltup.org',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)

setup(name='bdelta_python',
version='0.2.3',
description='Python Bindings for BDelta',
author='John Whitney',
author_email='jjw@deltup.org',
url='http://deltup.org',
ext_modules=[Extension('bdelta_python', ['src/bdelta_python.cpp'])],
)
40 changes: 32 additions & 8 deletions src/bdelta.cpp
Expand Up @@ -25,6 +25,11 @@ void *f_read(void *f, void *buf, unsigned place, unsigned num) {
return buf;
}

void my_pass(BDelta_Instance *b, unsigned blocksize, unsigned minMatchSize, unsigned flags) {
bdelta_pass(b, blocksize, minMatchSize, 0, flags);
bdelta_clean_matches(b, BDELTA_REMOVE_OVERLAP);
}

int main(int argc, char **argv) {
try {
if (argc != 4) {
Expand All @@ -41,15 +46,34 @@ int main(int argc, char **argv) {
FILE *f1 = fopen(argv[1], "rb"),
*f2 = fopen(argv[2], "rb");

void *b = bdelta_init_alg(size, size2, f_read, f1, f2, 1);
BDelta_Instance *b = bdelta_init_alg(size, size2, f_read, f1, f2, 1);
int nummatches;
#ifdef CARELESSMATCH
const int MINSIZE = 16;
#else
const int MINSIZE = 8;
#endif
for (int i = 512; i >= MINSIZE; i /= 2)
nummatches = bdelta_pass(b, i);

// List of primes for reference. Taken from Wikipedia.
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 1-20 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
// 21-40 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173
// 41-60 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281
// 61-80 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
// 81-100 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541
// 101-120 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659
// 121-140 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809
// 141-160 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941
// 161-180 947 953 967 971 977 983 991 997

int seq[] = {503, 127, 31, 7, 5, 3, -31, 31, 7, 5, 3, -7, 2};
my_pass(b, 997, 1994, 0);
my_pass(b, 503, 1006, 0);
my_pass(b, 127, 254, 0);
my_pass(b, 31, 62, 0);
my_pass(b, 7, 14, 0);
my_pass(b, 5, 10, 0);
my_pass(b, 3, 6, 0);
my_pass(b, 13, 26, BDELTA_GLOBAL);
my_pass(b, 7, 14, 0);
my_pass(b, 5, 10, 0);

nummatches = bdelta_numMatches(b);

unsigned * copyloc1 = new unsigned[nummatches + 1];
unsigned * copyloc2 = new unsigned[nummatches + 1];
Expand Down
26 changes: 20 additions & 6 deletions src/bdelta.h
Expand Up @@ -17,23 +17,37 @@
extern "C" {
#endif // __cplusplus

typedef struct _BDelta_Instance BDelta_Instance;

// Callback function must return a pointer to the data requested.
// A "fill and forget" buffer is provided, but can be ignored, so
// long as the data persists throughout the life of bdelta_pass().
typedef void *(*bdelta_readCallback)(void *handle, void *buf, unsigned place, unsigned num);

void *bdelta_init_alg(unsigned data1_size, unsigned data2_size,
BDelta_Instance *bdelta_init_alg(unsigned data1_size, unsigned data2_size,
bdelta_readCallback cb, void *handle1, void *handle2,
unsigned tokenSize);
void bdelta_done_alg(void *instance);
void bdelta_done_alg(BDelta_Instance *b);

void bdelta_pass(BDelta_Instance *b, unsigned blockSize, unsigned minMatchSize, unsigned maxHoleSize, unsigned flags);

void bdelta_swap_inputs(BDelta_Instance *b);
void bdelta_clean_matches(BDelta_Instance *b, unsigned flags);

//returns the total number of matches found
unsigned bdelta_pass(void *instance, unsigned blocksize);
unsigned bdelta_numMatches(BDelta_Instance *b);

void bdelta_getMatch(void *instance, unsigned matchNum,
void bdelta_getMatch(BDelta_Instance *b, unsigned matchNum,
unsigned *p1, unsigned *p2, unsigned *num);

int bdelta_getError(void *instance);
int bdelta_getError(BDelta_Instance *b);
void bdelta_showMatches(BDelta_Instance *b);

// Flags for bdelta_pass()
#define BDELTA_GLOBAL 1
#define BDELTA_SIDES_ORDERED 2

// Flags for bdelta_clean_matches()
#define BDELTA_REMOVE_OVERLAP 1

enum BDELTA_RESULT {
BDELTA_OK = 0,
Expand Down
56 changes: 56 additions & 0 deletions src/bdelta.pyx
@@ -0,0 +1,56 @@
cdef extern from "bdelta.h":
ctypedef struct BDelta_Instance:
pass

ctypedef void *(*bdelta_readCallback)(void *handle, void *buf, unsigned place, unsigned num)
BDelta_Instance *bdelta_init_alg(unsigned data1_size, unsigned data2_size,
bdelta_readCallback cb, void *handle1, void *handle2,
unsigned tokenSize)
void bdelta_done_alg(BDelta_Instance *b)

void bdelta_pass(BDelta_Instance *b, unsigned blockSize, unsigned minMatchSize, unsigned maxHoleSize, unsigned flags)

void bdelta_swap_inputs(BDelta_Instance *b)
void bdelta_clean_matches(BDelta_Instance *b, unsigned flags)

unsigned bdelta_numMatches(BDelta_Instance *b)

void bdelta_getMatch(BDelta_Instance *b, unsigned matchNum,
unsigned *p1, unsigned *p2, unsigned *num)

int bdelta_getError(BDelta_Instance *b)
void bdelta_showMatches(BDelta_Instance *b)

cdef enum PassFlags:
BDELTA_GLOBAL,
BDELTA_SIDES_ORDERED
cdef enum CleanFlags:
BDELTA_REMOVE_OVERLAP

cdef void *readCallback(void *handle, void *buf, unsigned place, unsigned num):
cdef char *str = <bytes>handle
return str + ((place + 1) * 2);

cdef class BDelta:
cdef BDelta_Instance *_b
cdef bytes str1, str2

def __cinit__(self, str1, str2):
self.str1 = str1.encode('UTF-16')
self.str2 = str2.encode('UTF-16')
self._b = bdelta_init_alg(len(str1), len(str2), readCallback, <void*>self.str1, <void*>self.str2, 2)

def __dealloc__(self):
self.str1 = None
self.str2 = None
bdelta_done_alg(self._b)

def b_pass(self, blockSize, minMatchSize, maxHoleSize, globalScope = False, sidesOrdered = False):
bdelta_pass(self._b, blockSize, minMatchSize, maxHoleSize,
(BDELTA_GLOBAL if globalScope else 0) | (BDELTA_SIDES_ORDERED if sidesOrdered else 0))

def matches(self):
cdef unsigned p1, p2, num
for i in xrange(bdelta_numMatches(self._b)):
bdelta_getMatch(self._b, i, &p1, &p2, &num)
yield (int(p1), int(p2), int(num))
89 changes: 0 additions & 89 deletions src/bdelta_python.cpp

This file was deleted.

0 comments on commit fb0a916

Please sign in to comment.