REM  ****************************************************************
rem  this file contains the source code of quasi-standard functions
rem  which are actually provided by macros under Schema2.
rem
rem  The first section is the code for functions which are deprecated
rem  under Schema2. Don't use them unless you must!
rem

macro INTEV(val)
=SETSHEETINFO( WHOLESHEET(""), 2, val)
ENDMACRO

MACRO ANGLE(val)
=SETSHEETINFO( WHOLESHEET(""), 0, val)
ENDMACRO

MACRO ECR(val)
=SETSHEETINFO( WHOLESHEET(""), 1, val)
ENDMACRO

MACRO SECURITY(val)
=SETSHEETINFO( WHOLESHEET(""), 5, val)
ENDMACRO

MACRO CELLNAMECONV(val)
=SETSHEETINFO( WHOLESHEET(""), 15, val)
ENDMACRO

MACRO SAVE(filename)
=SAVEFILE( WHOLESHEET(""), filename, 0xC26)
ENDMACRO

MACRO YESORNO(prompt)
=QUERY( prompt, "NO", "YES")
ENDMACRO

MACRO CREATE
=SHEETNAME(CREATESHEET("",0,0,0))
ENDMACRO

rem **********************************************************
rem   code for quasi-standard function match
rem **********************************************************

macro match (n,a(),b())
local af,as,bf,bs,aiscol,biscol,elements,k
if iserror(a) or iserror (b) or iserror (n) then
     = "A parameter is an error"
endif
as = first(a)
af = second(a)
bf = first(b)
bs = second(b)
                  rem check parameters
                  rem a must be one-dimensional
if af > 1 and as > 1 then
      = "Second parameter is not a vector"
endif

if af=1 then
   aiscol = 0
   elements = as
endif
if as=1 then
    aiscol = 1
    elements = af
endif
for k=0 to elements-1
    if n = (if aiscol then a(0,k) else a(k,0) endif) then goto found
next k
= "Value not found"
found:
   if bf=1 and bs=1 then =k
   if bf > 1 and bs > 1 then
        ="Third parameter is not a vector"
   endif
   if bf = 1 then
      if bs <> elements then ="Parameter sizes do not match"
      = b(0,k)
   endif
   if bs = 1 then
       if bf <> elements then ="Parameter sizes do not match"
       = b(k,0)
   endif
endmacro
 
rem  *******************************************************
rem  code for quasi-function search.
rem  The function uses a binary chop, and the result really
rem  is undefined if the values are not in order.
rem  *******************************************************
macro search(n,a(),b(),flag)
local af,as,bf,bs
local aiscol,biscol,elements
local bottom,top,mid
if iserror(a) or iserror (b) or iserror (n) then
     = "A parameter is an error"
endif
as = first(a)
af = second(a)
bf = first(b)
bs = second(b)
                  rem check parameters
                  rem a must be one-dimensional
if af > 1 and as > 1 then
      = "Second parameter is not a vector"
endif

if af=1 then
   aiscol = 0
   elements = as
endif
if as=1 then
    aiscol = 1
    elements = af
endif
bottom = 0
top = elements-1
while top >= bottom
    mid = (top+bottom)/2
    if (if aiscol then a(0,mid) else a(mid,0) endif)= n then goto target
    if (if aiscol then a(0,mid) else a(mid,0) endif)>= n then
       top=mid-1
    else
        bottom=mid+1
    endif
endwhile
if flag > 0 then mid=bottom else mid = top
target:
if mid<0 or mid >= elements then
   = "Value not found"
endif

   if bf=1 and bs=1 then =mid
   if bf > 1 and bs > 1 then
        ="Third parameter is not a vector"
   endif
   if bf = 1 then
      if bs <> elements then ="Parameter sizes do not match"
      = b(0,mid)
   endif
   if bs = 1 then
       if bf <> elements then ="Parameter sizes do not match"
       = b(mid,0)
   endif
endmacro
 
