## Squares

General FreeBASIC programming questions.
BasicCoder2
Posts: 3566
Joined: Jan 01, 2009 7:03
Location: Australia

### Redundancy

.
Last edited by BasicCoder2 on Feb 09, 2020 3:11, edited 1 time in total.
Richard
Posts: 3027
Joined: Jan 15, 2007 20:44
Location: Australia

### Re: Yah-Zip ( Test Bed )

albert wrote:( !!~~ COMPRESSION SUCCESS ~~!! )

Oh no, not again. How many times is that now ?
albert
Posts: 5713
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Richard

Sorry for the false wolf cry...

I tried 1 , 10 , 2 , 20 and it doesn't compress.

It was compressing a couple hundred bytes each loop , because of the 0 , 10 , 2 , 20 ( "2" - "0" happens every so often.. and confuses "20" )

Back to the drawing board!!!!
dodicat
Posts: 6559
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: Squares

I was having a cuppa today in a local garden centre when I noticed one of our Scottish Labour Party MSP'S doing likewise.
We don't often see Parliamentarians here, we are a bit remote.
I didn't trouble the fellow of course, but I emailed his office tonight regarding the threatened woodland.
Maybe he has a connection with these parts, and if so, and his alarm bells ring, I might not be completely alone.
(Maybe too many if's)
Good luck Albert . . . carry on.
albert
Posts: 5713
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Dodicat

Can this be resolved?? A new compression method..

===================================================
n1 = "00000000" + bin( *usp ) : usp+= 1
n1 = right( n1 , 8 )

v1 = val( "&B" + mid( n1 , 1 , 4 ) )
v2 = val( "&B" + mid( n1 , 5 , 4 ) )

if v1 < v2 then v3 = ( v2 - v1 )
if v1 > v2 then v3 = ( v1 - v2 )
if v1 = v2 then v3 = 0

bits1+= hex( v1 )
bits1+= hex( v3 )
==================================================
Got to resolve the v3 issue... v2 - v1 or v1 - v2 ??

Compresses 100,000 bytes in , to 1,200 bytes out , 98% : After 100 loops...

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 bits1 = ""
dim as string n1
dim as longint v1 , v2 , v3
dim as ubyte ptr usp = cptr( ubyte ptr , strptr( chrs ) )
for a as longint = 1 to len( chrs ) step 1

n1 = "00000000" + bin( *usp ) : usp+= 1
n1 = right( n1 , 8 )

v1 = val( "&B" + mid( n1 , 1 , 4 ) )
v2 = val( "&B" + mid( n1 , 5 , 4 ) )

if v1 < v2 then v3 = ( v2 - v1 )
if v1 > v2 then v3 = ( v1 - v2 )
if v1 = v2 then v3 = 0

bits1+= hex( v1 )
bits1+= hex( v3 )

'print n1 , v1 , v2 , v3
'sleep
'if inkey = " " then end

next

print "c out = " ; len( bits1 ) , bits1

dim as string final = ""
for a as longint = 1 to len( bits1 ) step 2
final+= chr( val( "&H" + mid( bits1 , a , 2 ) ) )
next

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 string bits = ""
for a as longint = 1 to len( chrs ) step 1
bits+= right( "00" + hex( chrs[ a - 1 ] ) , 2 )
next

print "d bit = " ; len( bits ) , bits

return chrs

end function

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

### Re: Squares

@Dodicat

I got it almost returning correct answers...

You got to adjust the val1 ( answer ) to equal "v2"

I was trying logic , to try to figure out when to add and when to subtract.. ( no luck!! )

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

print "n1" , "v1" , "v2" , "v3" , "answer"

dim as string bits1 = ""
dim as string n1
dim as longint v1 , v2 , v3
dim as ubyte ptr usp = cptr( ubyte ptr , strptr( chrs ) )
for a as longint = 1 to len( chrs ) step 1

n1 = "00000000" + bin( *usp ) : usp+= 1
n1 = right( n1 , 8 )

v1 = val( "&B" + mid( n1 , 1 , 4 ) )
v2 = val( "&B" + mid( n1 , 5 , 4 ) )

if v1 < v2 then v3 = abs( v1 - v2 )
if v1 > v2 then v3 = ( v1 - v2 )
if v1 = v2 then v3 = 0

bits1+= hex( v1 )
bits1+= hex( v3 )

dim as longint val1
for b as longint = 1  to 15 step 1
if v1 < b and abs( v1 - b ) = v3 then val1 = b : exit for
if v1 > b and ( v1 - b ) = v3 then val1 = b : exit for
next
if v3 = 0 then val1 = v1

print n1 , v1 , v2 , v3 , val1
sleep
if inkey = " " then end

next

print "c out = " ; len( bits1 ) , bits1

dim as string final = ""
for a as longint = 1 to len( bits1 ) step 2
final+= chr( val( "&H" + mid( bits1 , a , 2 ) ) )
next

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 string bits = ""
for a as longint = 1 to len( chrs ) step 1
bits+= right( "00" + hex( chrs[ a - 1 ] ) , 2 )
next

print "d bit = " ; len( bits ) , bits

return chrs

end function

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

### Re: Squares

@Dodicat

I found a flaw in my method....

if you have a 5 , 0 then the output would be 5 , 5
if you have a 5 , 10 then the output would be 5 , 5

So : i got to make it go by 6 bits , and add 1 to every number , so we can't have a 5 , 0
albert
Posts: 5713
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Dodicat

I converted it to 6 bits input... I got it returning correct , about 80% of the time....

==================================
v1 = val( "&B" + mid( n1 , 1 , 3 ) ) + 1
v2 = val( "&B" + mid( n1 , 4 , 3 ) ) + 1

if v1 < v2 then v3 = ( v2 - v1 )
if v1 > v2 then v3 = ( v2 )
if v1 = v2 then v3 = 0

bits1+= hex( v1 ) + hex( v3 )
==================================

Got to get "val1" to equal "v2"

Still compresses 100,000 bytes by 98% after 100 loops...Takes about 8 to 10 seconds

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 n1
dim as ubyte ptr usp = cptr( ubyte ptr , strptr( chrs ) )
for a as longint = 1 to len( chrs ) step 1
n1 = "00000000" + bin( *usp ) : usp+= 1
n1 = right( n1 , 8 )
bits+= n1
next

print "c bin = " ; len( bits ) , bits

print "  n1" , "v1" , "v2" , "v3" , "answer"
dim as string bits1 = ""
dim as string n2
dim as longint v1 , v2 , v3
for a as longint = 1 to len( bits ) step 6

n1 = mid( bits , a , 6 )

v1 = val( "&B" + mid( n1 , 1 , 3 ) ) + 1
v2 = val( "&B" + mid( n1 , 4 , 3 ) ) + 1

if v1 < v2 then v3 = ( v2 - v1 )
if v1 > v2 then v3 = ( v2 )
if v1 = v2 then v3 = 0

bits1+= hex( v1 ) + hex( v3 )

dim as longint val1

if v1 > v3 then val1 = v3 else val1 = v3 + v1

if v3 = 0 then val1 = v1

print n1 , v1 , v2 , v3 , val1
sleep
if inkey = " " then end

next

print "c out = " ; len( bits1 ) , bits1

dim as string final = ""
for a as longint = 1 to len( bits1 ) step 2
final+= chr( val( "&H" + mid( bits1 , a , 2 ) ) )
next

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 string bits = ""
for a as longint = 1 to len( chrs ) step 1
bits+= right( "00" + hex( chrs[ a - 1 ] ) , 2 )
next

print "d bit = " ; len( bits ) , bits

return chrs

end function

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

### Yah-Zip ( Test Bed )

@Dodicat

Getting closer!!!
It's returning correct in all cases , except for when v2 = 8

How would you handle the case of v2 = 8 ???

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 n1
dim as ubyte ptr usp = cptr( ubyte ptr , strptr( chrs ) )
for a as longint = 1 to len( chrs ) step 1
n1 = "00000000" + bin( *usp ) : usp+= 1
n1 = right( n1 , 8 )
bits+= n1
next

print "c bin = " ; len( bits ) , bits

print
print "  n1" , "v1" , "v2" , "v3" , "answer"
dim as string bits1 = ""
dim as string n2
dim as longint v1 , v2 , v3
for a as longint = 1 to len( bits ) step 6

n1 = mid( bits , a , 6 )

v1 = val( "&B" + mid( n1 , 1 , 3 ) ) + 1
v2 = val( "&B" + mid( n1 , 4 , 3 ) ) + 1

if v1 < v2 then v3 = v2 xor 8
if v1 > v2 then v3 = v2
if v1 = v2 then v3 = 0

bits1+= hex( v1 ) + hex( v3 )

dim as longint val1 = v3 xor 8

if v1 > v3 then val1 = v3

if v3 = 0 then val1 = v1

if val1 <> v2 then n2 = "X" else n2= "="

print n1 , v1 , v2 , v3 , val1 , n2
sleep
if inkey = " " then end

next

print "c out = " ; len( bits1 ) , bits1

dim as string final = ""
for a as longint = 1 to len( bits1 ) step 2
final+= chr( val( "&H" + mid( bits1 , a , 2 ) ) )
next

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 string bits = ""
for a as longint = 1 to len( chrs ) step 1
bits+= right( "00" + hex( chrs[ a - 1 ] ) , 2 )
next

print "d bit = " ; len( bits ) , bits

return chrs

end function

Here's your Zlib doing the compression... Compresses 87% after 100 loops... Takes ~16 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 n1
dim as ubyte ptr usp = cptr( ubyte ptr , strptr( chrs ) )
for a as longint = 1 to len( chrs ) step 1
n1 = "00000000" + bin( *usp ) : usp+= 1
n1 = right( n1 , 8 )
bits+= n1
next

print "c bin = " ; len( bits ) ', bits

dim as string bits1 = ""
dim as longint v1 , v2 , v3
for a as longint = 1 to len( bits ) step 6

n1 = mid( bits , a , 6 )

v1 = val( "&B" + mid( n1 , 1 , 3 ) ) + 1
v2 = val( "&B" + mid( n1 , 4 , 3 ) ) + 1

if v1 < v2 then v3 = v2 xor 8
if v1 > v2 then v3 = v2
if v1 = v2 then v3 = 0

bits1+= hex( v1 ) + hex( v3 )

next

print "c out = " ; len( bits1 ) ' , bits1

dim as string final = ""
for a as longint = 1 to len( bits1 ) step 2
final+= chr( val( "&H" + mid( bits1 , a , 2 ) ) )
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: 5713
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Dodicat

Never mind!!
It can't be resolved.. All you have is v1 and 0..

You can't tell if 0 is supposed to equal v1 or 8... you have to guess...and guessing between 2 values can't be resolved...

I got to figure out how the Space Aliens , compress data....
Posts: 2080
Joined: May 24, 2007 22:10
Location: The Netherlands

### Re: Squares

albert wrote:I got to figure out how the Space Aliens , compress data....

Why, maybe they have unlimited storage. Maybe to have invented time-travel, so they can just delete data and go back in time to retrieve it. The ultimate Wayback Machine.
dodicat
Posts: 6559
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: Squares

Data is a human concept.
For instance navigators use an ephemeris, the projected trajectories (positions of celestial bodies) including now satellites.
Also tables of tides, currents.
Each year new data is published, in book form or digital form of one kind or another.
We need the travel data because we are not designed for travel.
But the albatross, a much superior traveller than us, seems to need nothing.
Recently (a few hundred years) we started keeping records, hard copies on paper or vellum i.e. data
Now that era has finished and our records (data) is now stored on a magnetic or silicon medium, which has a very short lifespan.
So the moment we cease updating our data it will be all lost within a short time (50 years?), whether compressed or not.
So what form are these space aliens, more human like than albatross like?
I would really like to think they are more of of the bird brain variety.
Richard
Posts: 3027
Joined: Jan 15, 2007 20:44
Location: Australia

### Re: Squares

Albert wrote:I got to figure out how the Space Aliens , compress data....
That is an easy question. They store information on the event horizon surface of Black Holes as a hologram. As the information crosses the event horizon, it's passage is recorded in the event horizon surface by the strings that form the information. The surface has zero thickness, so one physical dimension is removed. In effect, as it has the dimension removed, the volume of information is compressed infinitely, to an area with zero volume. Everything consumed by a black hole is recorded in it's event horizon by the strings connected to the compressed information.
albert
Posts: 5713
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Dodicat

Check this one out!!!

Compresses 100,000 bytes in 98% , after 30 loops..

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 n1
dim as ubyte ptr usp = cptr( ubyte ptr , strptr( chrs ) )
for a as longint = 1 to len( chrs ) step 1
n1 = "00000000" + bin( *usp ) : usp+= 1
n1 = right( n1 , 8 )
bits+= n1
next

print "c bin = " ; len( bits ) , bits

dim as string bits1 = ""
dim as string bits2 = ""
for a as longint = 1 to len( bits ) step 2

n1 = mid( bits , a , 2 )

if n1 = "00" then bits1+= "0" : bits2+= "1"
if n1 = "01" then bits1+= "1" : bits2+= "1"
if n1 = "10" then bits2+= "0"
if n1 = "11" then bits2+= "10"

next

print "c out = " ; len( bits1 ) , bits1
print "c out = " ; len( bits2 ) , bits2

dim as string final = ""
for a as longint = 1 to len( bits1 ) step 8
final+= chr( val( "&B" + mid( bits1 , a , 8 ) ) )
next
final+= "END"
for a as longint = 1 to len( bits2 ) step 8
final+= chr( val( "&B" + mid( bits2 , 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: 5713
Joined: Sep 28, 2006 2:41
Location: California, USA

### Re: Squares

@Richard

The Alien Orbs , at the center , was a small 2 inch diameter sphere..
The sphere was like a Rubik's cube.
The section bands , were rotating around horizontally and vertically as it was talking...
They could speak in any Alien or Earth language..

Plus they had multi-colored lasers and could light up in any color...And output lasers in any color...
They were making rainbow circles on the ground..
And when i walked through , one of the rainbow circles , i turned and looked down and my footprints were glowing in a turquoise color..
They were heat-mapping my footprints and lasering them out in a color....

To do all they could do , would require data compression , to hold all that info in a small 2 inch sphere.... plus the batteries , motors and lasers...

?????