Squares

General FreeBASIC programming questions.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Jan 21, 2020 2:17

@Richard

I've got another compression.. Can you tell me if it can be undone??

for a as longint = 1 to len( bits ) step 4

n1 = mid( bits , a , 4 )
v1 = val( "&B" + n1 )

bits1+= right( "00" + bin( v1 mod 3 ) , 2 )
bits2+= right( "00" + bin( v1 \ 4 ) , 2 )

next

It mods by 3 and divides by 4 , how would you undo it? mod 4 expands..

It compresses by 90% after 40 loops of 100,000 bytes input.
Richard
Posts: 3030
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Postby Richard » Jan 21, 2020 2:54

If it uses a fixed set of rules, independent of the data, then it is theoretically impossible to reverse it in all cases.
You have another hash function.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Yah-Zip ( 6 bit )

Postby albert » Jan 21, 2020 17:57

@Richard

I got it this time....

"bits" is the input , converted to binary

n1 = mid( bits , a , 6 )

v1 = val( "&B" + n1 )

v2 = v1 mod 2
v3 = v1 mod 3
v4 = v1 mod 11

n2 = bin( v2 ) + "1" + bin( v3 ) + right( "0000" + bin( v4 ) , 4 )

output
bits1+= chr( val( "&B" + n2 ) )

You know the last 4 bits is v4 , then you have v3 = a 1-0 , 1-1 or a 1-10 , then the leading v2 bit ( 0 or 1 )..


Compresses 100,000 bytes in to 97% after 100 loops... takes 6 - 8 seconds..

Code: Select all


Declare Function   compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string


Namespace Zlibrary

#inclib "zlib"
Extern "C"
    Declare Function compressBound(Byval sourceLen As Ulong) As Ulong
    Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
    Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
End Extern

Function getpassedinfo(text As String,Byref passed_length As Integer) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function


'=================   UNPACK ===============
Function unpack(file As String) As String
    Dim As Integer passed_length
    Dim As String text=getpassedinfo(file,passed_length)
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength =passed_length
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr  destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=uncompress(destination,@destinationlength, source, stringlength)
    If mistake<>0 Then Print "There was an error":Sleep:End
    Dim As String uncompressed
    uncompressed=String(destinationlength,0)
    For i As Integer = 0 To destinationlength- 1
        uncompressed[i]=(destination[i])
    Next
    Deallocate destination
    Return uncompressed
End Function

'===================  PACK ============
Function pack(file As String) As String
    Dim As String text=file
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength = compressBound(stringlength)
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=compress(destination, @destinationlength, source, stringlength)
    If mistake <>0 Then Print "There was an error"
    Dim As String compressed
    compressed=String(destinationlength,0)
    For n As Integer=0 To destinationlength-1
        compressed[n]=destination[n]
    Next n
    compressed=stringlength &"|"+compressed
    Deallocate destination
    Return compressed
End Function

End Namespace


'==================================================================
'==================================================================
'test zipper
'==================================================================
'==================================================================
screen 19

Dim Shared As String s

Randomize

s=""
dim as string check=""
dim as string compare=""
dim as longint length = 0
dim as double compression = 0
dim as longint loops = 0

dim as double time1 , time2

time1 = timer
do
   
    loops+=1
   
    'one time run , create initial string
    if loops = 1 then
        For n As Long = 1 To 100000
            s+=chr(Int(Rnd*256))'+48
        Next
        compare =  s
        length = len(s)
    else
        'modify compression to make further compression possible
       
        s = compress_loop(s)
   
    end if
    check = s
    compression = (100 - ( 100 / ( length / len(check) ) ))
   
    Print "original string"
    Print Len(s)
    Print
   
    Dim As String compressed=Zlibrary.pack(s)
    s = compressed
   
    Print "packed string "
    Print Len(compressed)
    Print
   
    Dim As String uncompressed=Zlibrary.unpack(compressed)
   
    Print "Retrieve"
    Print Len(uncompressed)
    Print
    'Print "compression ratio  "; 100 - ( 100 / ( Len(s) / len(compressed) ) ) ; "%"
    Print "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
    Print Iif(uncompressed=check,"OK","ERROR")
    Print "-------------------------------"
   
    'sleep 1000
   
    'if loops > 2 and (100 - ( 100 / ( length / len(s) ) )) < compression then exit do
   
    print "press esc to exit."
    print
    print "press a key for next compression." ; " loops = " ; loops ; " out of 100."
    'sleep
   
    if inkey = chr(27) then exit do
   
loop until loops = 100

time2 = timer

print
print  "Compress time = " ; time2 - time1
print
print "Press a key to decompress."
sleep

s = str(loops) + "_" + s ' save as an output file...

'==================================================================
'decompress
'==================================================================
dim as longint dec = instr(1,s,"_")
dim as longint count = val(left(s,dec-1))
dim as string comp = mid(s,dec+1)
dim as string val1
dim as string outs
for a as longint = count to 2 step -1
    s = Zlibrary.unpack(comp)
    outs = decompress_loop(s)
    comp = outs
next

comp = Zlibrary.unpack(comp)

print
print "input = "; length , "output = " ; len(comp) , "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
print
if comp = compare then print "Decompression successful." else print "ERROR"
print
print
Print "!!~~Done~~!!"

Sleep
end
'===============================================================================
'============================,===================================================
'begin functions
'===============================================================================
'================='==============================================================
Function compress_loop( chrs as string ) as string
   
    print "c inp = " ; len(chrs) ' , chrs
   
    dim as string bits = ""
    dim as string zeros = string( 8 , "0" )
    dim as string n1
    dim as ubyte ptr ubp = cptr( ubyte ptr , strptr( chrs ) )
    for a as longint = 1 to len( chrs ) step 1
        n1 = zeros + bin( *ubp ) : ubp+= 1
        n1 = right( n1 , 8 )
        bits+= n1
    next
   
    print "c bit = " ; len( bits ) ', bits
   
    dim as string bits1 = ""
    dim as longint v1 , v2 , v3 , v4
    dim as string n2
    for a as longint = 1 to len( bits ) step 6
       
        n1 = mid( bits , a , 6 )
       
        v1 = val( "&B" + n1 )
       
        v2 = v1 mod 2
        v3 = v1 mod 3
        v4 = v1 mod 11
       
        n2 = bin( v2 ) + "1" + bin( v3 ) + right( "0000" + bin( v4 ) , 4 )
       
        bits1+= chr( val( "&B" + n2 ) )
       
        'UNCOMMENT TO SEE VALUES.
        'print
        'print n1
        'print n2
        'sleep
        'if inkey = " " then end
       
    next
   
    print "c out = " ; len( bits1 ) ', bits1
     
    dim as string final = bits1
    'for a as longint = 1 to len( bits1 ) step 8
    '    final+= chr( val( "&B" + mid( bits1 , a , 8 ) ) )
    'next
   
    print "c fin = " ; len(final)
   
    return final
   
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
   
    print
    print "d inp = " ; len( chrs )
   
    return chrs
   
end function



Also works with : n2 = bin( v4 ) + "1" + bin( v2 ) + bin( v3 )

Has to end with:
100
101
1010
110
111
1110
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Jan 22, 2020 3:19

While trying to write the decompression , i noticed a problem..

n2 = bin( v2 ) + "1" + bin( v3 ) + right( "0000" + bin( v4 ) , 4 )

v2 = 0 or 1
v3 = 0 or 1 or 10

010
011
0110 <---- equals

110 <--- equals
111
1110

Got to come up with another formula.....
Muttonhead
Posts: 130
Joined: May 28, 2009 20:07

Re: Squares

Postby Muttonhead » Jan 22, 2020 19:01

Is there any chance of knowing what this is about?
I don't want this to sound rude or presumptuous.
I once tried to follow this in "fast forward" and, admittedly, I did not quite succeed.
So, Albert, can you tell me in summary, whats your goal?
Sincerely, Mutton.
Richard
Posts: 3030
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Squares

Postby Richard » Jan 23, 2020 7:50

Muttonhead wrote:Is there any chance of knowing what this is about?
In the earlier “Circles” thread, amongst other things, Squaring The Circle was investigated. But that thread kept going round in circles.
This “Squares” thread better describes the participants than the geometrical figure. Squares is a spectator sport. We mostly watch as Albert searches for the Holy Grail of information processing, that is; spectacular compression, with a perfect resurrection. Mathematicians have proven that a mathematician cannot do it. Albert is uncontaminated by mathematics, so we can only trust in Albert's faith and persistence to find the ultimate prize.
badidea
Posts: 2149
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Squares

Postby badidea » Jan 23, 2020 18:57

I was looking up the definition of a square person. Answers.com gave me "anyone times themselves equals 1 square person". That sounds like circular reasoning to me. Albert keeps cutting corners resulting in an endless loop.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Yah-Zip ( 6 bit )

Postby albert » Jan 24, 2020 1:48

I think i got it this time....

n1 = mid( bits , a , 6 )

v1 = val( "&B" + n1 )

v2 = ( v1 mod 2 ) + 2
v3 = ( v1 mod 3 ) + 1
v4 = ( v1 mod 11 ) + 5

'V2 = 2 BITS 10 or 11
'V3 = 1 OR 2 BITS 1 10 11
'V4 = 3 OR 4 BITS 5 to 15
n3 = bin( v2 ) + bin( v3 ) + bin( v4 )

bits1+= chr( val( "&B" + n3 ) )

Compresses 10,000 bytes in to 71% after 100 loops : takes 1.3 seconds...
Compresses 100,000 bytes in to 80% after 100 loops : takes 11 seconds...
Compresses 1,000,000 bytes in to 80% after 100 loops : takes 168 seconds...

Code: Select all


Declare Function   compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string


Namespace Zlibrary

#inclib "zlib"
Extern "C"
    Declare Function compressBound(Byval sourceLen As Ulong) As Ulong
    Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
    Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
End Extern

Function getpassedinfo(text As String,Byref passed_length As Integer) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function


'=================   UNPACK ===============
Function unpack(file As String) As String
    Dim As Integer passed_length
    Dim As String text=getpassedinfo(file,passed_length)
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength =passed_length
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr  destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=uncompress(destination,@destinationlength, source, stringlength)
    If mistake<>0 Then Print "There was an error":Sleep:End
    Dim As String uncompressed
    uncompressed=String(destinationlength,0)
    For i As Integer = 0 To destinationlength- 1
        uncompressed[i]=(destination[i])
    Next
    Deallocate destination
    Return uncompressed
End Function

'===================  PACK ============
Function pack(file As String) As String
    Dim As String text=file
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength = compressBound(stringlength)
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=compress(destination, @destinationlength, source, stringlength)
    If mistake <>0 Then Print "There was an error"
    Dim As String compressed
    compressed=String(destinationlength,0)
    For n As Integer=0 To destinationlength-1
        compressed[n]=destination[n]
    Next n
    compressed=stringlength &"|"+compressed
    Deallocate destination
    Return compressed
End Function

End Namespace


'==================================================================
'==================================================================
'test zipper
'==================================================================
'==================================================================
screen 19

Dim Shared As String s

Randomize

s=""
dim as string check=""
dim as string compare=""
dim as longint length = 0
dim as double compression = 0
dim as longint loops = 0

dim as double time1 , time2

time1 = timer
do
   
    loops+=1
   
    'one time run , create initial string
    if loops = 1 then
        For n As Long = 1 To 100000
            s+=chr(Int(Rnd*256))'+48
        Next
        compare =  s
        length = len(s)
    else
        'modify compression to make further compression possible
       
        s = compress_loop(s)
   
    end if
    check = s
    compression = (100 - ( 100 / ( length / len(check) ) ))
   
    Print "original string"
    Print Len(s)
    Print
   
    Dim As String compressed=Zlibrary.pack(s)
    s = compressed
   
    Print "packed string "
    Print Len(compressed)
    Print
   
    Dim As String uncompressed=Zlibrary.unpack(compressed)
   
    Print "Retrieve"
    Print Len(uncompressed)
    Print
    'Print "compression ratio  "; 100 - ( 100 / ( Len(s) / len(compressed) ) ) ; "%"
    Print "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
    Print Iif(uncompressed=check,"OK","ERROR")
    Print "-------------------------------"
   
    'sleep 1000
   
    'if loops > 2 and (100 - ( 100 / ( length / len(s) ) )) < compression then exit do
   
    print "press esc to exit."
    print
    print "press a key for next compression." ; " loops = " ; loops ; " out of 100."
    'sleep
   
    if inkey = chr(27) then exit do
   
loop until loops = 100

time2 = timer

print
print  "Compress time = " ; time2 - time1
print
print "Press a key to decompress."
sleep

s = str(loops) + "_" + s ' save as an output file...

'==================================================================
'decompress
'==================================================================
dim as longint dec = instr(1,s,"_")
dim as longint count = val(left(s,dec-1))
dim as string comp = mid(s,dec+1)
dim as string val1
dim as string outs
for a as longint = count to 2 step -1
    s = Zlibrary.unpack(comp)
    outs = decompress_loop(s)
    comp = outs
next

comp = Zlibrary.unpack(comp)

print
print "input = "; length , "output = " ; len(comp) , "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
print
if comp = compare then print "Decompression successful." else print "ERROR"
print
print
Print "!!~~Done~~!!"

Sleep
end
'===============================================================================
'============================,===================================================
'begin functions
'===============================================================================
'================='==============================================================
Function compress_loop( chrs as string ) as string
   
    print "c inp = " ; len(chrs) ' , chrs
   
    dim as string bits = ""
    dim as string zeros = string( 8 , "0" )
    dim as string n1
    dim as ubyte ptr ubp = cptr( ubyte ptr , strptr( chrs ) )
    for a as longint = 1 to len( chrs ) step 1
        n1 = zeros + bin( *ubp ) : ubp+= 1
        n1 = right( n1 , 8 )
        bits+= n1
    next
   
    print "c bit = " ; len( bits ) ', bits
   
    dim as string bits1 = ""
    dim as longint v1 , v2 , v3 , v4
    dim as string n2 , n3 , n4
    for a as longint = 1 to len( bits ) step 6
       
        n1 = mid( bits , a , 6 )
       
        v1 = val( "&B" + n1 )
       
        v2 = ( v1 mod 2 ) + 2
        v3 = ( v1 mod 3 ) + 1
        v4 = ( v1 mod 11 ) + 5
       
        'V2 = 2 BITS 10 or 11
        'V3 = 1 OR 2 BITS  1 10 11
        'V4 = 3 OR 4 BITS 5 to 15
        n3 = bin( v2 ) + bin( v3 ) + bin( v4 )
       
        bits1+= chr( val( "&B" + n3 ) )
       
        'UNCOMMENT TO SEE VALUES.
        'print
        'print n1
        'print n3
        'sleep
        'if inkey = " " then end
       
    next
   
    print "c out = " ; len( bits1 ) ', bits1
     
    dim as string final = bits1
    'for a as longint = 1 to len( bits1 ) step 8
    '    final+= chr( val( "&B" + mid( bits1 , a , 8 ) ) )
    'next
   
    print "c fin = " ; len(final)
   
    return final
   
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
   
    print
    print "d inp = " ; len( chrs )
   
    return chrs
   
end function



Just got to figure out how to undo the mods....
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Yah-Zip ( 6 bit )

Postby albert » Jan 24, 2020 18:21

I got it corrected...

n1 = mid( bits , a , 6 )

v1 = val( "&B" + n1 )

v2 = ( v1 mod 2 ) + 1
v3 = ( v1 mod 3 ) + 1
v4 = ( v1 mod 11 ) + 5

n3 = bin( v2 ) + bin( v3 ) + bin( v4 )

bits1+= chr( val( "&B" + n3 ) )

Code: Select all


Declare Function   compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string


Namespace Zlibrary

#inclib "zlib"
Extern "C"
    Declare Function compressBound(Byval sourceLen As Ulong) As Ulong
    Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
    Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
End Extern

Function getpassedinfo(text As String,Byref passed_length As Integer) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function


'=================   UNPACK ===============
Function unpack(file As String) As String
    Dim As Integer passed_length
    Dim As String text=getpassedinfo(file,passed_length)
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength =passed_length
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr  destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=uncompress(destination,@destinationlength, source, stringlength)
    If mistake<>0 Then Print "There was an error":Sleep:End
    Dim As String uncompressed
    uncompressed=String(destinationlength,0)
    For i As Integer = 0 To destinationlength- 1
        uncompressed[i]=(destination[i])
    Next
    Deallocate destination
    Return uncompressed
End Function

'===================  PACK ============
Function pack(file As String) As String
    Dim As String text=file
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength = compressBound(stringlength)
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=compress(destination, @destinationlength, source, stringlength)
    If mistake <>0 Then Print "There was an error"
    Dim As String compressed
    compressed=String(destinationlength,0)
    For n As Integer=0 To destinationlength-1
        compressed[n]=destination[n]
    Next n
    compressed=stringlength &"|"+compressed
    Deallocate destination
    Return compressed
End Function

End Namespace


'==================================================================
'==================================================================
'test zipper
'==================================================================
'==================================================================
screen 19

Dim Shared As String s

Randomize

s=""
dim as string check=""
dim as string compare=""
dim as longint length = 0
dim as double compression = 0
dim as longint loops = 0

dim as double time1 , time2

time1 = timer
do
   
    loops+=1
   
    'one time run , create initial string
    if loops = 1 then
        For n As Long = 1 To 100000
            s+=chr(Int(Rnd*256))'+48
        Next
        compare =  s
        length = len(s)
    else
        'modify compression to make further compression possible
       
        s = compress_loop(s)
   
    end if
    check = s
    compression = (100 - ( 100 / ( length / len(check) ) ))
   
    Print "original string"
    Print Len(s)
    Print
   
    Dim As String compressed=Zlibrary.pack(s)
    s = compressed
   
    Print "packed string "
    Print Len(compressed)
    Print
   
    Dim As String uncompressed=Zlibrary.unpack(compressed)
   
    Print "Retrieve"
    Print Len(uncompressed)
    Print
    'Print "compression ratio  "; 100 - ( 100 / ( Len(s) / len(compressed) ) ) ; "%"
    Print "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
    Print Iif(uncompressed=check,"OK","ERROR")
    Print "-------------------------------"
   
    'sleep 1000
   
    'if loops > 2 and (100 - ( 100 / ( length / len(s) ) )) < compression then exit do
   
    print "press esc to exit."
    print
    print "press a key for next compression." ; " loops = " ; loops ; " out of 100."
    'sleep
   
    if inkey = chr(27) then exit do
   
loop until loops = 100

time2 = timer

print
print  "Compress time = " ; time2 - time1
print
print "Press a key to decompress."
sleep

s = str(loops) + "_" + s ' save as an output file...

'==================================================================
'decompress
'==================================================================
dim as longint dec = instr(1,s,"_")
dim as longint count = val(left(s,dec-1))
dim as string comp = mid(s,dec+1)
dim as string val1
dim as string outs
for a as longint = count to 2 step -1
    s = Zlibrary.unpack(comp)
    outs = decompress_loop(s)
    comp = outs
next

comp = Zlibrary.unpack(comp)

print
print "input = "; length , "output = " ; len(comp) , "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
print
if comp = compare then print "Decompression successful." else print "ERROR"
print
print
Print "!!~~Done~~!!"

Sleep
end
'===============================================================================
'============================,===================================================
'begin functions
'===============================================================================
'================='==============================================================
Function compress_loop( chrs as string ) as string
   
    print "c inp = " ; len(chrs) ' , chrs
   
    dim as string bits = ""
    dim as string zeros = string( 8 , "0" )
    dim as string n1
    dim as ubyte ptr ubp = cptr( ubyte ptr , strptr( chrs ) )
    for a as longint = 1 to len( chrs ) step 1
        n1 = zeros + bin( *ubp ) : ubp+= 1
        n1 = right( n1 , 8 )
        bits+= n1
    next
   
    print "c bit = " ; len( bits ) ', bits
   
    dim as string bits1 = ""
    dim as longint v1 , v2 , v3 , v4
    dim as string n2 , n3 , n4
    for a as longint = 1 to len( bits ) step 6
       
        n1 = mid( bits , a , 6 )
       
        v1 = val( "&B" + n1 )
       
        v2 = ( v1 mod 2 ) + 1
        v3 = ( v1 mod 3 ) + 1
        v4 = ( v1 mod 11 ) + 5
       
        n3 = bin( v2 ) + bin( v3 ) + bin( v4 )
       
        bits1+= chr( val( "&B" + n3 ) )
       
        'UNCOMMENT TO SEE VALUES.
        'print
        'print n1
        'print n3
        'sleep
        'if inkey = " " then end
       
    next
   
    print "c out = " ; len( bits1 ) ', bits1
     
    dim as string final = bits1
    'for a as longint = 1 to len( bits1 ) step 8
    '    final+= chr( val( "&B" + mid( bits1 , a , 8 ) ) )
    'next
   
    print "c fin = " ; len(final)
   
    return final
   
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
   
    print
    print "d inp = " ; len( chrs )
   
    return chrs
   
end function



( !!~~ EDITED ~~!! )
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Yah-Zip ( 6 bit )

Postby albert » Jan 25, 2020 0:50

( !!~~ COMPRESSION SUCCESS ~~!! )

This time it can be decompressed... I thought maybe to call it "Moderator"

n1 = mid( bits , a , 6 )

v1 = val( "&B" + n1 )

v2 = ( v1 mod 2 )
v3 = ( v1 mod 3 ) + 4
v4 = ( v1 mod 11 ) '+ 4

n3 = bin( v2 ) + bin( v3 ) + bin( v4 )

bits1+= chr( val( "&B" + n3 ) )

v2 has to be 0 or 1
v3 has to be 100 , 101 , 110
v4 has to 1 to 4 bits

So:
If it starts with 100 or 101 or 110 then you know v2 has to = 0
if it starts with a 1100 or 1101 or 1110 then you know v2 has to = 1

Then v4 is the rest... ( 1 to 4 bits. )

The only thing is , it only compresses 100,000 bytes in by 48% after 100 loops : And it takes 20 seconds...

Code: Select all


Declare Function   compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string


Namespace Zlibrary

#inclib "zlib"
Extern "C"
    Declare Function compressBound(Byval sourceLen As Ulong) As Ulong
    Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
    Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
End Extern

Function getpassedinfo(text As String,Byref passed_length As Integer) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function


'=================   UNPACK ===============
Function unpack(file As String) As String
    Dim As Integer passed_length
    Dim As String text=getpassedinfo(file,passed_length)
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength =passed_length
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr  destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=uncompress(destination,@destinationlength, source, stringlength)
    If mistake<>0 Then Print "There was an error":Sleep:End
    Dim As String uncompressed
    uncompressed=String(destinationlength,0)
    For i As Integer = 0 To destinationlength- 1
        uncompressed[i]=(destination[i])
    Next
    Deallocate destination
    Return uncompressed
End Function

'===================  PACK ============
Function pack(file As String) As String
    Dim As String text=file
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength = compressBound(stringlength)
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=compress(destination, @destinationlength, source, stringlength)
    If mistake <>0 Then Print "There was an error"
    Dim As String compressed
    compressed=String(destinationlength,0)
    For n As Integer=0 To destinationlength-1
        compressed[n]=destination[n]
    Next n
    compressed=stringlength &"|"+compressed
    Deallocate destination
    Return compressed
End Function

End Namespace


'==================================================================
'==================================================================
'test zipper
'==================================================================
'==================================================================
screen 19

Dim Shared As String s

Randomize

s=""
dim as string check=""
dim as string compare=""
dim as longint length = 0
dim as double compression = 0
dim as longint loops = 0

dim as double time1 , time2

time1 = timer
do
   
    loops+=1
   
    'one time run , create initial string
    if loops = 1 then
        For n As Long = 1 To 100000
            s+=chr(Int(Rnd*256))'+48
        Next
        compare =  s
        length = len(s)
    else
        'modify compression to make further compression possible
       
        s = compress_loop(s)
   
    end if
    check = s
    compression = (100 - ( 100 / ( length / len(check) ) ))
   
    Print "original string"
    Print Len(s)
    Print
   
    Dim As String compressed=Zlibrary.pack(s)
    s = compressed
   
    Print "packed string "
    Print Len(compressed)
    Print
   
    Dim As String uncompressed=Zlibrary.unpack(compressed)
   
    Print "Retrieve"
    Print Len(uncompressed)
    Print
    'Print "compression ratio  "; 100 - ( 100 / ( Len(s) / len(compressed) ) ) ; "%"
    Print "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
    Print Iif(uncompressed=check,"OK","ERROR")
    Print "-------------------------------"
   
    'sleep 1000
   
    'if loops > 2 and (100 - ( 100 / ( length / len(s) ) )) < compression then exit do
   
    print "press esc to exit."
    print
    print "press a key for next compression." ; " loops = " ; loops ; " out of 100."
    'sleep
   
    if inkey = chr(27) then exit do
   
loop until loops = 100

time2 = timer

print
print  "Compress time = " ; time2 - time1
print
print "Press a key to decompress."
sleep

s = str(loops) + "_" + s ' save as an output file...

'==================================================================
'decompress
'==================================================================
dim as longint dec = instr(1,s,"_")
dim as longint count = val(left(s,dec-1))
dim as string comp = mid(s,dec+1)
dim as string val1
dim as string outs
for a as longint = count to 2 step -1
    s = Zlibrary.unpack(comp)
    outs = decompress_loop(s)
    comp = outs
next

comp = Zlibrary.unpack(comp)

print
print "input = "; length , "output = " ; len(comp) , "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
print
if comp = compare then print "Decompression successful." else print "ERROR"
print
print
Print "!!~~Done~~!!"

Sleep
end
'===============================================================================
'============================,===================================================
'begin functions
'===============================================================================
'================='==============================================================
Function compress_loop( chrs as string ) as string
   
    print "c inp = " ; len(chrs) ' , chrs
   
    dim as string bits = ""
    dim as string zeros = string( 8 , "0" )
    dim as string n1
    dim as ubyte ptr ubp = cptr( ubyte ptr , strptr( chrs ) )
    for a as longint = 1 to len( chrs ) step 1
        n1 = zeros + bin( *ubp ) : ubp+= 1
        n1 = right( n1 , 8 )
        bits+= n1
    next
   
    print "c bit = " ; len( bits ) ', bits
   
    dim as string bits1 = ""
    dim as longint v1 , v2 , v3 , v4
    dim as string n2 , n3 , n4
    for a as longint = 1 to len( bits ) step 6
       
        n1 = mid( bits , a , 6 )
       
        v1 = val( "&B" + n1 )
       
        v2 = ( v1 mod 2 )
        v3 = ( v1 mod 3 ) + 4
        v4 = ( v1 mod 11 ) '+ 4
       
        n3 = bin( v2 ) + bin( v3 ) + bin( v4 )
       
        bits1+= chr( val( "&B" + n3 ) )
       
        'UNCOMMENT TO SEE VALUES.
        'print
        'print n1
        'print n3
        'sleep
        'if inkey = " " then end
       
    next
   
    print "c out = " ; len( bits1 ) ', bits1
     
    dim as string final = bits1
    'for a as longint = 1 to len( bits1 ) step 8
    '    final+= chr( val( "&B" + mid( bits1 , a , 8 ) ) )
    'next
   
    print "c fin = " ; len(final)
   
    return final
   
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
   
    print
    print "d inp = " ; len( chrs )
   
    return chrs
   
end function

Last edited by albert on Jan 25, 2020 2:21, edited 1 time in total.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Jan 25, 2020 1:47

1,000,000 bytes in takes 250 seconds for 100 loops.... Compresses the same 48% as 100,000 bytes in..

Got to figure out how to speed it up.....

Any suggestions??
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Yah-Zip ( 6 bit )

Postby albert » Jan 25, 2020 18:18

( !!~~ COMPRESSION SUCCESS ~~!! )

I modified it a little..

Now it gets 51% compression for 100,000 bytes in , and takes 20 seconds..

n1 = mid( bits , a , 6 )

v1 = val( "&B" + n1 )

v2 = ( v1 mod 2 )
v3 = ( v1 mod 3 ) + 2
v4 = ( v1 mod 11 )

n3 = bin( v2 ) + bin( v3 ) + bin( v4 )

If it starts with a 10 or 11 or 100 then you know v2 = 0
If it starts with a 110 or 111 or 1100 then you know v2 = 1

The rest is v4
If it ends with 100 ( common ) , then you know v4 = 0 and v3 = 10

Code: Select all


Declare Function   compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string


Namespace Zlibrary

#inclib "zlib"
Extern "C"
    Declare Function compressBound(Byval sourceLen As Ulong) As Ulong
    Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
    Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
End Extern

Function getpassedinfo(text As String,Byref passed_length As Integer) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function


'=================   UNPACK ===============
Function unpack(file As String) As String
    Dim As Integer passed_length
    Dim As String text=getpassedinfo(file,passed_length)
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength =passed_length
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr  destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=uncompress(destination,@destinationlength, source, stringlength)
    If mistake<>0 Then Print "There was an error":Sleep:End
    Dim As String uncompressed
    uncompressed=String(destinationlength,0)
    For i As Integer = 0 To destinationlength- 1
        uncompressed[i]=(destination[i])
    Next
    Deallocate destination
    Return uncompressed
End Function

'===================  PACK ============
Function pack(file As String) As String
    Dim As String text=file
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength = compressBound(stringlength)
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=compress(destination, @destinationlength, source, stringlength)
    If mistake <>0 Then Print "There was an error"
    Dim As String compressed
    compressed=String(destinationlength,0)
    For n As Integer=0 To destinationlength-1
        compressed[n]=destination[n]
    Next n
    compressed=stringlength &"|"+compressed
    Deallocate destination
    Return compressed
End Function

End Namespace


'==================================================================
'==================================================================
'test zipper
'==================================================================
'==================================================================
screen 19

Dim Shared As String s

Randomize

s=""
dim as string check=""
dim as string compare=""
dim as longint length = 0
dim as double compression = 0
dim as longint loops = 0

dim as double time1 , time2

time1 = timer
do
   
    loops+=1
   
    'one time run , create initial string
    if loops = 1 then
        For n As Long = 1 To 100000
            s+=chr(Int(Rnd*256))'+48
        Next
        compare =  s
        length = len(s)
    else
        'modify compression to make further compression possible
       
        s = compress_loop(s)
   
    end if
    check = s
    compression = (100 - ( 100 / ( length / len(check) ) ))
   
    Print "original string"
    Print Len(s)
    Print
   
    Dim As String compressed=Zlibrary.pack(s)
    s = compressed
   
    Print "packed string "
    Print Len(compressed)
    Print
   
    Dim As String uncompressed=Zlibrary.unpack(compressed)
   
    Print "Retrieve"
    Print Len(uncompressed)
    Print
    'Print "compression ratio  "; 100 - ( 100 / ( Len(s) / len(compressed) ) ) ; "%"
    Print "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
    Print Iif(uncompressed=check,"OK","ERROR")
    Print "-------------------------------"
   
    'sleep 1000
   
    'if loops > 2 and (100 - ( 100 / ( length / len(s) ) )) < compression then exit do
   
    print "press esc to exit."
    print
    print "press a key for next compression." ; " loops = " ; loops ; " out of 100."
    'sleep
   
    if inkey = chr(27) then exit do
   
loop until loops = 100

time2 = timer

print
print  "Compress time = " ; time2 - time1
print
print "Press a key to decompress."
sleep

s = str(loops) + "_" + s ' save as an output file...

'==================================================================
'decompress
'==================================================================
dim as longint dec = instr(1,s,"_")
dim as longint count = val(left(s,dec-1))
dim as string comp = mid(s,dec+1)
dim as string val1
dim as string outs
for a as longint = count to 2 step -1
    s = Zlibrary.unpack(comp)
    outs = decompress_loop(s)
    comp = outs
next

comp = Zlibrary.unpack(comp)

print
print "input = "; length , "output = " ; len(comp) , "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
print
if comp = compare then print "Decompression successful." else print "ERROR"
print
print
Print "!!~~Done~~!!"

Sleep
end
'===============================================================================
'============================,===================================================
'begin functions
'===============================================================================
'================='==============================================================
Function compress_loop( chrs as string ) as string
   
    print "c inp = " ; len(chrs) ' , chrs
   
    dim as string bits = ""
    dim as string zeros = string( 8 , "0" )
    dim as string n1
    dim as ubyte ptr ubp = cptr( ubyte ptr , strptr( chrs ) )
    for a as longint = 1 to len( chrs ) step 1
        n1 = zeros + bin( *ubp ) : ubp+= 1
        n1 = right( n1 , 8 )
        bits+= n1
    next
   
    print "c bit = " ; len( bits ) ', bits
   
    dim as string bits1 = ""
    dim as longint v1 , v2 , v3 , v4
    dim as string n2 , n3 , n4
    for a as longint = 1 to len( bits ) step 6
       
        n1 = mid( bits , a , 6 )
       
        v1 = val( "&B" + n1 )
       
        v2 = ( v1 mod 2 )
        v3 = ( v1 mod 3 ) + 2
        v4 = ( v1 mod 11 )
       
        n3 = bin( v2 ) + bin( v3 ) + bin( v4 )
       
        bits1+= chr( val( "&B" + n3 ) )
       
        'UNCOMMENT TO SEE VALUES.
        'print
        'print n1
        'print n3
        'sleep
        'if inkey = " " then end
       
    next
   
    print "c out = " ; len( bits1 ) ', bits1
     
    dim as string final = bits1
    'for a as longint = 1 to len( bits1 ) step 8
    '    final+= chr( val( "&B" + mid( bits1 , a , 8 ) ) )
    'next
   
    print "c fin = " ; len(final)
   
    return final
   
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
   
    print
    print "d inp = " ; len( chrs )
   
    return chrs
   
end function



( !!~~ EDITED ~~!! )
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Yah-Zip ( 6 bit )

Postby albert » Jan 25, 2020 19:38

( !!~~ COMPRESSION SUCCESS ~~!! )

I modified it again,

Now it gets 66% compression for 100,000 bytes in and takes 23 seconds..
Now it gets 66% compression for 1,000,000 bytes in and takes 250 seconds.. Got to figure how to speed it up...

n1 = mid( bits , a , 6 )

v1 = val( "&B" + n1 )

v2 = ( v1 mod 2 ) + 1
v3 = ( v1 mod 3 )
v4 = ( v1 mod 11 )

n3 = bin( v2 ) + right("0000" + bin( v4 ) , 4 ) + bin( v3 )

bits1+= chr( val( "&B" + n3 ) )

Code: Select all


Declare Function   compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string


Namespace Zlibrary

#inclib "zlib"
Extern "C"
    Declare Function compressBound(Byval sourceLen As Ulong) As Ulong
    Declare Function uncompress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
    Declare Function compress(Byval dest As Ubyte Ptr, Byval destLen As Uinteger Ptr, Byval source As  Ubyte Ptr, Byval sourceLen As Ulong) As Long
End Extern

Function getpassedinfo(text As String,Byref passed_length As Integer) As String
    Dim As String var1,var2
    Dim As Integer pst
    #macro splice(stri,char,var1,var2)
    pst=Instr(stri,char)
    var1="":var2=""
    If pst<>0 Then
        var1=Mid(stri,1,pst-1)
        var2=Mid(stri,pst+1)
    Else
        var1=stri
    End If
    #endmacro
    splice(text,"|",var1,var2)
    text=var2
    passed_length=Valint(var1)
    Return text
End Function


'=================   UNPACK ===============
Function unpack(file As String) As String
    Dim As Integer passed_length
    Dim As String text=getpassedinfo(file,passed_length)
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength =passed_length
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr  destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=uncompress(destination,@destinationlength, source, stringlength)
    If mistake<>0 Then Print "There was an error":Sleep:End
    Dim As String uncompressed
    uncompressed=String(destinationlength,0)
    For i As Integer = 0 To destinationlength- 1
        uncompressed[i]=(destination[i])
    Next
    Deallocate destination
    Return uncompressed
End Function

'===================  PACK ============
Function pack(file As String) As String
    Dim As String text=file
    Dim As Integer stringlength,destinationlength
    stringlength=Len(text)
    destinationlength = compressBound(stringlength)
    Dim As Ubyte Ptr source
    Dim As Ubyte Ptr destination =Callocate(destinationlength,1)
    source=@text[0]
    Var mistake=compress(destination, @destinationlength, source, stringlength)
    If mistake <>0 Then Print "There was an error"
    Dim As String compressed
    compressed=String(destinationlength,0)
    For n As Integer=0 To destinationlength-1
        compressed[n]=destination[n]
    Next n
    compressed=stringlength &"|"+compressed
    Deallocate destination
    Return compressed
End Function

End Namespace


'==================================================================
'==================================================================
'test zipper
'==================================================================
'==================================================================
screen 19

Dim Shared As String s

Randomize

s=""
dim as string check=""
dim as string compare=""
dim as longint length = 0
dim as double compression = 0
dim as longint loops = 0

dim as double time1 , time2

time1 = timer
do
   
    loops+=1
   
    'one time run , create initial string
    if loops = 1 then
        For n As Long = 1 To 100000
            s+=chr(Int(Rnd*256))'+48
        Next
        compare =  s
        length = len(s)
    else
        'modify compression to make further compression possible
       
        s = compress_loop(s)
   
    end if
    check = s
    compression = (100 - ( 100 / ( length / len(check) ) ))
   
    Print "original string"
    Print Len(s)
    Print
   
    Dim As String compressed=Zlibrary.pack(s)
    s = compressed
   
    Print "packed string "
    Print Len(compressed)
    Print
   
    Dim As String uncompressed=Zlibrary.unpack(compressed)
   
    Print "Retrieve"
    Print Len(uncompressed)
    Print
    'Print "compression ratio  "; 100 - ( 100 / ( Len(s) / len(compressed) ) ) ; "%"
    Print "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
    Print Iif(uncompressed=check,"OK","ERROR")
    Print "-------------------------------"
   
    'sleep 1000
   
    'if loops > 2 and (100 - ( 100 / ( length / len(s) ) )) < compression then exit do
   
    print "press esc to exit."
    print
    print "press a key for next compression." ; " loops = " ; loops ; " out of 100."
    'sleep
   
    if inkey = chr(27) then exit do
   
loop until loops = 100

time2 = timer

print
print  "Compress time = " ; time2 - time1
print
print "Press a key to decompress."
sleep

s = str(loops) + "_" + s ' save as an output file...

'==================================================================
'decompress
'==================================================================
dim as longint dec = instr(1,s,"_")
dim as longint count = val(left(s,dec-1))
dim as string comp = mid(s,dec+1)
dim as string val1
dim as string outs
for a as longint = count to 2 step -1
    s = Zlibrary.unpack(comp)
    outs = decompress_loop(s)
    comp = outs
next

comp = Zlibrary.unpack(comp)

print
print "input = "; length , "output = " ; len(comp) , "compression ratio  "; 100 - ( 100 / ( length / len(s) ) ) ; "%"
print
if comp = compare then print "Decompression successful." else print "ERROR"
print
print
Print "!!~~Done~~!!"

Sleep
end
'===============================================================================
'============================,===================================================
'begin functions
'===============================================================================
'================='==============================================================
Function compress_loop( chrs as string ) as string
   
    print "c inp = " ; len(chrs) ' , chrs
   
    dim as string bits = ""
    dim as string zeros = string( 8 , "0" )
    dim as string n1
    dim as ubyte ptr ubp = cptr( ubyte ptr , strptr( chrs ) )
    for a as longint = 1 to len( chrs ) step 1
        n1 = zeros + bin( *ubp ) : ubp+= 1
        n1 = right( n1 , 8 )
        bits+= n1
    next
   
    print "c bit = " ; len( bits ) ', bits
   
    dim as string bits1 = ""
    dim as longint v1 , v2 , v3 , v4
    dim as string n2 , n3 , n4
    for a as longint = 1 to len( bits ) step 6
       
        n1 = mid( bits , a , 6 )
       
        v1 = val( "&B" + n1 )
       
        v2 = ( v1 mod 2 ) + 1
        v3 = ( v1 mod 3 )
        v4 = ( v1 mod 11 )
       
        n3 = bin( v2 ) + right("0000" + bin( v4 ) , 4 ) + bin( v3 )
       
        bits1+= chr( val( "&B" + n3 ) )
       
        'UNCOMMENT TO SEE VALUES.
        'print
        'print n1
        'print n3
        'sleep
        'if inkey = " " then end
       
    next
   
    print "c out = " ; len( bits1 ) ', bits1
     
    dim as string final = bits1
    'for a as longint = 1 to len( bits1 ) step 8
    '    final+= chr( val( "&B" + mid( bits1 , a , 8 ) ) )
    'next
   
    print "c fin = " ; len(final)
   
    return final
   
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
   
    print
    print "d inp = " ; len( chrs )
   
    return chrs
   
end function

albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Yah-Zip ( Test Bed )

Postby albert » Jan 25, 2020 20:14

v2 = ( v1 mod 2 ) + 1
v3 = ( v1 mod 3 )
v4 = ( v1 mod 11 )

v2 = 1 , v4 = 0 to 10 , v3 = 0 to 2 = 33 combos
v2 = 10 , v4 = 0 to 10 , v3 = 0 to 2 = 33 combos

A total of 66 combos to hard wire for the decompression...

========================================================================================
Here's the decompression.... Sometimes it's decompressing okay and sometimes not... Got a problem somewhere..
Here's the "Test-Bed" , where i write the decompression...

Code: Select all



Declare Function      compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string

screen 19
'=====================================================================
'=====================================================================
'start program
'=====================================================================
'=====================================================================
dim as double time1 , time2 , time3 , time4
do
   
    randomize
   
    dim as string s = ""
    For n As Long = 1 To 8
        s+=chr(Int(Rnd*256))
    Next
   
    time1=timer
    'begin compress
        dim as string comp = s
            'do
            '    dim as longint chk = len(comp) - 1
            '    comp = compress_loop(comp)
            '    if len(comp) >= chk then exit do
            '    if inkey = chr( 27 ) then end
            'loop
            for a as longint = 1 to 1 step 1
                comp = compress_loop(comp)
            next
    'end compress
    time2 = timer
   
    time3=timer
    'begin decompress
        dim as string final_out = comp
        for a as longint = 1 to 1 step 1
            final_out = decompress_loop(final_out)
        next
    'end decompress
    time4 = timer
   
   'sleep
   
    print string(99,"=")
    print "inp = " ; (s)
    print string(99,"=")
    print "out = " ; (final_out)
    print
    print "compress time   = "; time2-time1
    print "decompress time = "; time4-time3
    print
   
    if s = final_out then print "Decompressed OK" else print "Decompression failed."
    print string(99,"=")
   
    sleep
   
loop until inkey = chr(27)

sleep
end
'===============================================================================
'===============================================================================
'compress
'===============================================================================
'===============================================================================
Function compress_loop( chrs as string ) as string
   
    print "c inp = " ; len(chrs) ' , chrs
   
    dim as string bits = ""
    dim as string zeros = string( 8 , "0" )
    dim as string n1
    dim as ubyte ptr ubp = cptr( ubyte ptr , strptr( chrs ) )
    for a as longint = 1 to len( chrs ) step 1
        n1 = zeros + bin( *ubp ) : ubp+= 1
        n1 = right( n1 , 8 )
        bits+= n1
    next
   
    print "c bit = " ; len( bits ) , bits
   
    dim as ubyte count
    dim as string str1
    dim as ubyte dec1
    do
        str1 = str( len( bits ) / 6 )
        dec1 = instr( 1 , str1 , "." )
        if dec1 <> 0 then bits+= "0" : count+= 1
    loop until dec1 = 0
   
    dim as string bits1 = ""
    dim as ubyte v1 , v2 , v3 , v4
    dim as string n2 , n3 , n4
    for a as longint = 1 to len( bits ) step 6
       
        n1 = mid( bits , a , 6 )
       
        v1 = val( "&B" + n1 )
       
        v2 = ( v1 mod 2 ) + 1
        v3 = ( v1 mod 3 )
        v4 = ( v1 mod 11 )
       
        n3 = bin( v2 ) + right( "0000" + bin( v4 ) , 4 ) + bin( v3 )
       
        bits1+= chr( val( "&B" + n3 ) )
       
        'UNCOMMENT TO SEE VALUES.
        'print
        'print n1
        'print n3
        'sleep
        'if inkey = " " then end
       
    next
   
    print "c out = " ; len( bits1 ) ', bits1
     
    dim as string final = bits1
    'for a as longint = 1 to len( bits1 ) step 8
    '    final+= chr( val( "&B" + mid( bits1 , a , 8 ) ) )
    'next
   
    final = chr( count ) + final
   
    print "c fin = " ; len(final)
   
    return final
   
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
   
    print
    print "d inp = " ; len( chrs )
   
    dim as ubyte count = asc( left( chrs , 1 ) ) : chrs = mid( chrs , 2 )
   
    dim as string bits = ""
    dim as ubyte v1 , v2 , v3 , v4
    dim as string n1
    dim as ubyte ptr ubp = cptr( ubyte ptr , strptr( chrs ) )
    for a as longint = 1 to len( chrs ) step 1
       
        n1 = bin( *ubp ) : ubp+= 1
       
        if n1 = "1" + "0000" + "0" then v2 = 0 : v4 = 0 : v3 = 0
        if n1 = "1" + "0001" + "0" then v2 = 0 : v4 = 1 : v3 = 0
        if n1 = "1" + "0010" + "0" then v2 = 0 : v4 = 2 : v3 = 0
        if n1 = "1" + "0011" + "0" then v2 = 0 : v4 = 3 : v3 = 0
        if n1 = "1" + "0100" + "0" then v2 = 0 : v4 = 4 : v3 = 0
        if n1 = "1" + "0101" + "0" then v2 = 0 : v4 = 5 : v3 = 0
        if n1 = "1" + "0110" + "0" then v2 = 0 : v4 = 6 : v3 = 0
        if n1 = "1" + "0111" + "0" then v2 = 0 : v4 = 7 : v3 = 0
        if n1 = "1" + "1000" + "0" then v2 = 0 : v4 = 8 : v3 = 0
        if n1 = "1" + "1001" + "0" then v2 = 0 : v4 = 9 : v3 = 0
        if n1 = "1" + "1010" + "0" then v2 = 0 : v4 = 10 : v3 = 0

        if n1 = "1" + "0000" + "1" then v2 = 0 : v4 = 0 : v3 = 1
        if n1 = "1" + "0001" + "1" then v2 = 0 : v4 = 1 : v3 = 1
        if n1 = "1" + "0010" + "1" then v2 = 0 : v4 = 2 : v3 = 1
        if n1 = "1" + "0011" + "1" then v2 = 0 : v4 = 3 : v3 = 1
        if n1 = "1" + "0100" + "1" then v2 = 0 : v4 = 4 : v3 = 1
        if n1 = "1" + "0101" + "1" then v2 = 0 : v4 = 5 : v3 = 1
        if n1 = "1" + "0110" + "1" then v2 = 0 : v4 = 6 : v3 = 1
        if n1 = "1" + "0111" + "1" then v2 = 0 : v4 = 7 : v3 = 1
        if n1 = "1" + "1000" + "1" then v2 = 0 : v4 = 8 : v3 = 1
        if n1 = "1" + "1001" + "1" then v2 = 0 : v4 = 9 : v3 = 1
        if n1 = "1" + "1010" + "1" then v2 = 0 : v4 = 10 : v3 = 1

        if n1 = "1" + "0000" + "10" then v2 = 0 : v4 = 0 : v3 = 2
        if n1 = "1" + "0001" + "10" then v2 = 0 : v4 = 1 : v3 = 2
        if n1 = "1" + "0010" + "10" then v2 = 0 : v4 = 2 : v3 = 2
        if n1 = "1" + "0011" + "10" then v2 = 0 : v4 = 3 : v3 = 2
        if n1 = "1" + "0100" + "10" then v2 = 0 : v4 = 4 : v3 = 2
        if n1 = "1" + "0101" + "10" then v2 = 0 : v4 = 5 : v3 = 2
        if n1 = "1" + "0110" + "10" then v2 = 0 : v4 = 6 : v3 = 2
        if n1 = "1" + "0111" + "10" then v2 = 0 : v4 = 7 : v3 = 2
        if n1 = "1" + "1000" + "10" then v2 = 0 : v4 = 8 : v3 = 2
        if n1 = "1" + "1001" + "10" then v2 = 0 : v4 = 9 : v3 = 2
        if n1 = "1" + "1010" + "10" then v2 = 0 : v4 = 10 : v3 = 2

'===============================================

        if n1 = "10" + "0000" + "0" then v2 = 1 : v4 = 0 : v3 = 0
        if n1 = "10" + "0001" + "0" then v2 = 1 : v4 = 1 : v3 = 0
        if n1 = "10" + "0010" + "0" then v2 = 1 : v4 = 2 : v3 = 0
        if n1 = "10" + "0011" + "0" then v2 = 1 : v4 = 3 : v3 = 0
        if n1 = "10" + "0100" + "0" then v2 = 1 : v4 = 4 : v3 = 0
        if n1 = "10" + "0101" + "0" then v2 = 1 : v4 = 5 : v3 = 0
        if n1 = "10" + "0110" + "0" then v2 = 1 : v4 = 6 : v3 = 0
        if n1 = "10" + "0111" + "0" then v2 = 1 : v4 = 7 : v3 = 0
        if n1 = "10" + "1000" + "0" then v2 = 1 : v4 = 8 : v3 = 0
        if n1 = "10" + "1001" + "0" then v2 = 1 : v4 = 9 : v3 = 0
        if n1 = "10" + "1010" + "0" then v2 = 1 : v4 = 10 : v3 = 0

        if n1 = "10" + "0000" + "1" then v2 = 1 : v4 = 0 : v3 = 1
        if n1 = "10" + "0001" + "1" then v2 = 1 : v4 = 1 : v3 = 1
        if n1 = "10" + "0010" + "1" then v2 = 1 : v4 = 2 : v3 = 1
        if n1 = "10" + "0011" + "1" then v2 = 1 : v4 = 3 : v3 = 1
        if n1 = "10" + "0100" + "1" then v2 = 1 : v4 = 4 : v3 = 1
        if n1 = "10" + "0101" + "1" then v2 = 1 : v4 = 5 : v3 = 1
        if n1 = "10" + "0110" + "1" then v2 = 1 : v4 = 6 : v3 = 1
        if n1 = "10" + "0111" + "1" then v2 = 1 : v4 = 7 : v3 = 1
        if n1 = "10" + "1000" + "1" then v2 = 1 : v4 = 8 : v3 = 1
        if n1 = "10" + "1001" + "1" then v2 = 1 : v4 = 9 : v3 = 1
        if n1 = "10" + "1010" + "1" then v2 = 1 : v4 = 10 : v3 = 1

        if n1 = "10" + "0000" + "10" then v2 = 1 : v4 = 0 : v3 = 2
        if n1 = "10" + "0001" + "10" then v2 = 1 : v4 = 1 : v3 = 2
        if n1 = "10" + "0010" + "10" then v2 = 1 : v4 = 2 : v3 = 2
        if n1 = "10" + "0011" + "10" then v2 = 1 : v4 = 3 : v3 = 2
        if n1 = "10" + "0100" + "10" then v2 = 1 : v4 = 4 : v3 = 2
        if n1 = "10" + "0101" + "10" then v2 = 1 : v4 = 5 : v3 = 2
        if n1 = "10" + "0110" + "10" then v2 = 1 : v4 = 6 : v3 = 2
        if n1 = "10" + "0111" + "10" then v2 = 1 : v4 = 7 : v3 = 2
        if n1 = "10" + "1000" + "10" then v2 = 1 : v4 = 8 : v3 = 2
        if n1 = "10" + "1001" + "10" then v2 = 1 : v4 = 9 : v3 = 2
        if n1 = "10" + "1010" + "10" then v2 = 1 : v4 = 10 : v3 = 2
           
        dim as ubyte m2 , m3 , m4 , value
        for b as longint = 0 to 255
            m2 = b mod 2
            m3 = b mod 3
            m4 = b mod 11
            if m2 = v2 and m3 = v3 and m4 = v4 then value = b : exit for
        next
       
        bits+= right( "000000" + bin( value ) , 6 )
       
    next
    bits = left( bits , len( bits ) - count )
   
    print "d bit = " ; len( bits ) , bits
   
    dim as string final = ""
    for a as longint = 1 to len( bits ) step 8
        final+= chr( val( "&B" + mid( bits , a , 8 ) ) )
    next
   
    return final
   
end function



The only thing i can think of is ; mod 2 , mod 3 , mod 11 , doesn't get all 64 values correct..???
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: Squares

Postby albert » Jan 25, 2020 23:19

Can someone look it over , and see where the error is?
Maybe 1 string , is equaling another???

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests