Author Topic: Instrev & Instrword & Lookup  (Read 2888 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Instrev & Instrword & Lookup
« on: June 03, 2014, 02:01:08 AM »
quick update: ( 18:08 02/06/2014 )
http://www.oxygenbasic.org/o2zips/Oxygen.zip

fixes a bug affecting byte array index expressions such as:  b[ i+lk ]
Also a further adjustment to case alternatives such as: case 45,46, 48 to 57



Reverse Instr

Code: [Select]
function instrev(sys i,string s,k) as sys
=========================================
sys a=asc k
if i=0 then i=len s
byte b at strptr s
do
  if i<1 then exit do
  if b[i]=a then
    if instr(i,s,k)=i then exit do
  end if
  i--
enddo
return i
end function


Whole word Instr

Code: [Select]
function instrword(sys i,string s,k) as sys
===========================================
sys  lk=len k
byte b at strptr s
if not k then return 0
do
  i=instr(i,s,k)
  if i=0 then
    return i
  elseif b[i+lk]<33
    if i=1 or b[i-1]<33
      return i
    end if
  end if
  i+=lk
end do
end function


Lookup

ops="
list   1,
let    2,
set    3,
get    4,
"

function lookup(string s,k) as sys
==================================
sys a
a=instrword(1,s,k)
if a then
  a+=len k
  return val mid s,a
end if
end function

op=lookup ops,"let"
« Last Edit: June 03, 2014, 08:08:48 AM by Charles Pegge »

Mike Lobanovsky

  • Guest
Re: Instrev & Instrword & Lookup
« Reply #1 on: June 03, 2014, 03:25:25 AM »
Hi Charles,

Quote
function instrev(sys i,string s,k) as sys
............................
sys i

Won't the sys i local variable shadow the sys i parameter so that the parameter will become effectively inaccessible?

Also, there seems to be a typo in op=lookip ops,"let".

Peter

  • Guest
Re: Instrev & Instrword & Lookup
« Reply #2 on: June 03, 2014, 03:39:35 AM »
Hi Charles,

The last Dll is slower than the older Dll's.

If I run this test source code with your last Dll, I get a time about 7 seconds!
If I run the  test source code with your Dll, Date 21.o3.2o14, then I get a time about 5 seconds!
Code: [Select]
include "sw.inc"
Window 320,240,1

int left_edge, right_edge, top_edge, bottom_edge, max_iter,the_char,
    x_step, y_step, y0, x0, x, y, i, x_x, y_y, temp,ty,count,accum
   
t1 = ticks 
     
While count <1545
  left_edge   = -420
  right_edge  =  300
  top_edge    =  300
  bottom_edge = -300
  x_step   = 7
  y_step   = 15
  max_iter = 200
  y0 = top_edge
  while y0 > bottom_edge
    x0 = left_edge;
    while x0 < right_edge
      y = 0
      x = 0
      the_char = 32
      x_x = 0
      y_y = 0
      i   = 0
      while i < max_iter and x_x + y_y <= 800
        x_x = (x * x) / 200
        y_y = (y * y) / 200
        iF x_x + y_y > 800 then
           the_char = 48 + i
           iF i > 9 then
              the_char = 64
           End iF
        Else
           temp = x_x - y_y + x0
           iF (x < 0 and y > 0) OR (x > 0 and y < 0) then
               y = (-1 * ((-1 * (x * y)) / 100)) + y0
           Else
               y = x * y / 100 + y0
           End iF
           x = temp
        End iF
i +=1
      wend
      accum += the_char
      x0 += x_step
    wend
    y0 -= y_step
  wend
  iF MOD(count,300)=0 THEN
     Text 0,ty,accum,0
     ty +=14
  End iF
  count +=1
Wend
t2 =(ticks()-t1)/1000 
Text 0,ty,accum,0
Text 0,ty+28,"Time: " + str(t2,4) + " Seconds",0
WaitKey
CloseWindow
 

.

Charles Pegge

  • Guest
Re: Instrev & Instrword & Lookup
« Reply #3 on: June 03, 2014, 04:40:32 AM »
Mike, thanks for correcting those items.

Peter, I'll check the asm for efficiency!

To get the concensus figures you will need do integer divisions: "\". there are three of them in this section:
Code: [Select]
        x_x = (x * x) \ 200
        y_y = (y * y) \ 200
        iF x_x + y_y > 800 then
           the_char = 48 + i
           iF i > 9 then
              the_char = 64
           End iF
        Else
           temp = x_x - y_y + x0
           #show iF (x < 0 and y > 0) OR (x > 0 and y < 0) then
               y = (-1 * ((-1 * (x * y)) \ 100)) + y0
           Else
               y = x * y \ 100 + y0
This makes it a bit faster too.
« Last Edit: June 03, 2014, 05:05:59 AM by Charles Pegge »

Peter

  • Guest
Re: Instrev & Instrword & Lookup
« Reply #4 on: June 03, 2014, 08:00:44 AM »
Code: [Select]
To get the concensus figures you will need do integer divisions: "\". there are three of them in this section:
yes I did.

.

Peter

  • Guest
Re: Instrev & Instrword & Lookup
« Reply #5 on: June 03, 2014, 08:03:30 AM »
Here's a select case example.
Code: [Select]
include "sw.inc"
Window 320,240,1

while Key(27)=0
Cls sw_white
Circle 110,50,60,1,0
Circle 180,50,60,1,0
Text 110,120,"Seconds =  " + TimeSecond,0
Select TimeSecond
Case 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,_
     32,34,36,38,40,42,44,46,48,50,52,54,56,58
     FillCircle 110,50,60,sw_bred
Case 1,3,5,7,9,11,12,15,17,19,21,23,25,27,29,_
     31,33,35,37,39,41,43,45,47,49,51,53,55,57,59
     FillCircle 180,50,60,sw_bblue    
End Select    
Sync()
wend
CloseWindow

Charles Pegge

  • Guest
Re: Instrev & Instrword & Lookup
« Reply #6 on: June 03, 2014, 10:44:15 AM »
I may be able to make better use of registers, instead of temp variables for holding boolean intermediates.