任何使用BWT(Burrows-Wheeler变换)的东西都应该能够很好地压缩它。
我的快速Python测试:
>>> import gzip
>>> import lzma
>>> import zlib
>>> import bz2
>>> import time
>>> dLen = 16
>>> inputData = '\n'.join('{:0{}b}'.format(x, dLen) for x in range(2**dLen))
>>> inputData[:100]
'0000000000000000\n0000000000000001\n0000000000000010\n0000000000000011\n0000000000000100\n000000000000010'
>>> inputData[-100:]
'111111111111010\n1111111111111011\n1111111111111100\n1111111111111101\n1111111111111110\n1111111111111111'
>>> def bwt(text):
N = len(text)
text2 = text * 2
class K:
def __init__(self, i):
self.i = i
def __lt__(a, b):
i, j = a.i, b.i
for k in range(N): # use `range()` in Python 3
if text2[i+k] < text2[j+k]:
return True
elif text2[i+k] > text2[j+k]:
return False
return False # they're equal
inorder = sorted(range(N), key=K)
return "".join(text2[i+N-1] for i in inorder)
>>> class nothing:
def compress(x):
return x
>>> class bwt_c:
def compress(x):
return bwt(x.decode('latin_1')).encode('latin_1')
>>> for first in ('bwt_c', 'nothing', 'lzma', 'zlib', 'gzip', 'bz2'):
fTime = -time.process_time()
fOut = eval(first).compress(inputData)
fTime += time.process_time()
print(first, fTime)
for second in ('nothing', 'lzma', 'zlib', 'gzip', 'bz2'):
print(first, second, end=' ')
sTime = -time.process_time()
sOut = eval(second).compress(fOut)
sTime += time.process_time()
print(fTime + sTime, len(sOut))
bwt_c 53.76768319200005
bwt_c nothing 53.767727423000224 1114111
bwt_c lzma 53.83853460699993 2344
bwt_c zlib 53.7767307470001 5930
bwt_c gzip 53.782549449000044 4024
bwt_c bz2 54.15730512699997 237
nothing 6.357100005516259e-05
nothing nothing 0.0001084830000763759 1114111
nothing lzma 0.6671195740000258 27264
nothing zlib 0.05987233699988792 118206
nothing gzip 2.307255977000068 147743
nothing bz2 0.07741139000017938 187906
lzma 0.6767229399999906
lzma nothing 0.6767684639999061 27264
lzma lzma 0.6843232409999018 27324
lzma zlib 0.6774435929999072 27280
lzma gzip 0.6774431810001715 27292
lzma bz2 0.6821310499999527 27741
zlib 0.05984937799985346
zlib nothing 0.05989508399989063 118206
zlib lzma 0.09543156799986718 22800
zlib zlib 0.06264000899977873 24854
zlib gzip 0.0639041649999399 24611
zlib bz2 0.07275534999985211 21283
gzip 2.303239570000187
gzip nothing 2.303286251000145 147743
gzip lzma 2.309592880000082 1312
gzip zlib 2.3042639900002087 2088
gzip gzip 2.304663197000309 1996
gzip bz2 2.344431411000187 1670
bz2 0.07537686600016968
bz2 nothing 0.07542737000017041 187906
bz2 lzma 0.11371452700018381 22940
bz2 zlib 0.0785322910001014 24719
bz2 gzip 0.07945505000020603 24605
bz2 bz2 0.09332576600013454 27138
(这里的数字是“ first_compressor second_compressor time_taken bytes_out”)
(BWT从这里拍摄)
这仍然是“不仅是几个字节”,但比仅使用gzip更好。例如,对于16位输入,BWT + bz2从1114111减少到237个字节。
遗憾的是,对于许多应用程序而言,BWT太慢且占用大量内存。尤其是考虑到这是Python的幼稚实现-在我的机器上,我在2 ** 20之前用完RAM。
使用Pypy,我可以运行完整的2 ** 20输入,并使用BWT和bz2将其压缩为2611字节。但是要花费3分钟以上的时间,并使用超过4GB的RAM。
同样不幸的是,这种方法仍然是O(2 ^ n)输出空间,至少会出现在曲线拟合1..20中。