Re: boolean sequence
- Posted by Juergen Luethje <j.lue at gmx.de> Sep 05, 2004
- 474 views
Tone Škoda wrote: > yes i already looked at your lib and exactly those functions you mentioned. i > will > make a wrapper lib out of it then. Sorry for the delay in replying, I'm currently rather busy ... In case you're not alredy done in the meantime, the following functions might be useful for you:
include misc.e -- for pretty_print() include bit.e ------------------------------------------------------------------------ -- utility functions function ceil (object x) -- the opposite of floor() -- x may be an atom or a sequence return -floor(-x) end function ------------------------------------------------------------------------ -- types type sequence_of_int_32bit (object x) -- sequence consisting of 32-bit integers (signed or unsigned) object t if atom(x) then return 0 end if for i = 1 to length(x) do t = x[i] if (not atom(t)) or (t != floor(t)) or (t < -#80000000) or (#FFFFFFFF < t) then return 0 end if end for return 1 end type type nonnegative_int (object x) if integer(x) then return x >= 0 end if return 0 end type ------------------------------------------------------------------------ -- * The following functions let us treat sequences consisting of an -- arbitrary number of 32-bit integers as huge "bit arrays". * global function bs_set (sequence_of_int_32bit s, nonnegative_int number) integer n n = ceil((number+1)/32) if n > length(s) then return -1 -- error end if s[n] = c_func( BIT_SET, {s[n], remainder(number,32)} ) return s end function global function bs_reset (sequence_of_int_32bit s, nonnegative_int number) integer n n = ceil((number+1)/32) if n > length(s) then return -1 -- error end if s[n] = c_func( BIT_RESET, {s[n], remainder(number,32)} ) return s end function global function bs_test (sequence_of_int_32bit s, nonnegative_int number) integer n n = ceil((number+1)/32) if n > length(s) then return -1 -- error end if return c_func( BIT_TEST, {s[n], remainder(number,32)} ) end function global function bs_scan_forward (sequence_of_int_32bit s) integer n for i = 1 to length(s) do n = c_func( BIT_SCAN_FORWARD, {s[i]} ) if n != -1 then return (i-1)*32 + n end if end for return -1 end function global function bs_scan_reverse (sequence_of_int_32bit s) integer n for i = length(s) to 1 by -1 do n = c_func( BIT_SCAN_REVERSE, {s[i]} ) if n != -1 then return (i-1)*32 + n end if end for return -1 end function ------------------------------------------------------------------------ -- * Demo * -- object s sequence_of_int_32bit pattern sequence p nonnegative_int numEls integer fn fn = open("bs.txt", "w") numEls = 3 pattern = repeat(0, numEls) p = "0" for i = 1 to numEls-1 do p &= ",0" end for for bitNo = 0 to numEls*32 do s = bs_set(pattern, bitNo) printf(fn, "s = bs_set({%s}, %d)", {p, bitNo}) puts(fn, "\n-> s = ") if sequence(s) then pretty_print(fn, s, {0,3,1,78,"#%08x","#%08x"}) if bs_test(s, bitNo) = 0 then puts(fn, "* Error *\n") end if printf(fn, "-> bs_scan_forward(s) = %d\n", {bs_scan_forward(s)}) printf(fn, "-> bs_scan_reverse(s) = %d\n", {bs_scan_reverse(s)}) else printf(fn, "%d\n", {s}) end if puts(fn, "\n") end for puts(fn, "---------------------------------------------\n\n") pattern = repeat(#FFFFFFFF, numEls) p = "#FFFFFFFF" for i = 1 to numEls-1 do p &= ",#FFFFFFFF" end for for bitNo = 0 to numEls*32 do s = bs_reset(pattern, bitNo) printf(fn, "s = bs_reset({%s}, %d)", {p, bitNo}) puts(fn, "\n-> s = ") if sequence(s) then pretty_print(fn, s, {0,3,1,78,"#%08x","#%08x"}) if bs_test(s, bitNo) = 1 then puts(fn, "* Error *\n") end if printf(fn, "-> bs_scan_forward(s) = %d\n", {bs_scan_forward(s)}) printf(fn, "-> bs_scan_reverse(s) = %d\n", {bs_scan_reverse(s)}) else printf(fn, "%d\n", {s}) end if puts(fn, "\n") end for close(fn) puts(1, "finished")
Any suggestions for improvement and other comments are appreciated. Regards, Juergen