Untitled

 avatar
unknown
python
a year ago
1.6 kB
6
Indexable
import array

with open('hashes.bin','rb') as hf:
    filebuf = hf.read()

num_hashes = len(filebuf)//16
indx = array.array('L', range(num_hashes))

def hash_comp_val(p, v):
    if filebuf[indx[p]*16:(indx[p]+1)*16] < v:
        return -1
    elif filebuf[indx[p]*16:(indx[p]+1)*16] > v:
        return 1
    return 0

def hash_comp_hash(p, q):
    if filebuf[indx[p]*16:(indx[p]+1)*16] < filebuf[indx[q]*16:(indx[q]+1)*16]:
        return -1
    elif filebuf[indx[p]*16:(indx[p]+1)*16] > filebuf[indx[q]*16:(indx[q]+1)*16]:
        return 1
    return 0

def hash_swap(x, y):
    temp = indx[x]
    indx[x] = indx[y]
    indx[y] = temp

def simplesort(lo, hi):
    for i in range(hi-lo, 0, -1):
        for j in range(lo, lo+i):
            if hash_comp_hash(j, j+1) == 1:
                hash_swap(j, j+1)

def somesort(lo, hi):
    if lo <= (hi - 8):
        
        mid = (lo+hi)//2
        mid_val = filebuf[indx[mid]*16:(indx[mid]+1)*16]
        lt = lo
        ct = lo
        rt = hi
        
        while ct <= rt:
            if hash_comp_val(ct, mid_val) > 0:
                hash_swap(ct, rt)
                rt -= 1
            elif hash_comp_val(ct, mid_val) < 0:
                hash_swap(ct, lt)
                lt += 1
                ct += 1
            else:
                ct += 1
        
        # print (lo, lt, ct, rt, hi)
        somesort(lo, lt-1)
        somesort(rt+1, hi)
        return
    elif lo < hi:
        simplesort(lo, hi)
        return

somesort(0, num_hashes-1)

with open('hashes_s.bin','wb') as shf:
    for i in range(num_hashes):
        shf.write(filebuf[indx[i]*16:(indx[i]+1)*16])
Editor is loading...
Leave a Comment