cool. almost twise as fast as my first suggestion. collective coding makes miracles
fn lastbit7 bits =
(
pivot = 1
_bits = copy bits
while (p = (pivot + _bits.count)/2) > pivot do
(
bb = #{p+1.._bits.count} * _bits
if bb.isEmpty then _bits.count = p
else
(
_bits = bb
pivot = p
)
)
p+1
)
bitarray is a pointer type value. so you can change it in the function body (as well as array, struct, point2,3,4).
in the code above i make a copy of the original bitarray to solve the issue
I really can’t reproduce this behavior. In fact, I think that is the reason why you can send a variable by reference using ‘&’.
That works for me without any corruption of the array:
(
fn lastbit7 bits =
(
pivot = 1
while (p = (pivot + bits.count)/2) > pivot do
(
bb = #{p+1..bits.count} * bits
if bb.isEmpty then bits.count = p
else
(
bits = bb
pivot = p
)
)
p+1
)
aaa = #{}
for k=1 to 50 by 2 do append aaa k
format "Before: aaa= %
" aaa
gc()
t1=timestamp()
hf = heapfree
i = lastbit7 aaa
format "lastbit7 Time: %sec. Mem: % lastBit: %
" ((timestamp()-t1)/1000 as float) (hf-heapfree) i
format "After: aaa= %
" aaa
)
do you want another bits tasks?
ok.
#1 find first available bit in a bitarray.
for example, in #{1…3,5} the first available is 4
#2 find index of bit in list of set bits
for example, in #{2,4,5} bit 4 has index 2
First try:
(
fn firstAvailableBit bits =
(
count = 1
for i in bits while i == count do
count += 1
count
)
aaa= #{1..5, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49}
i = firstAvailableBit aaa
format "First available bit = %
" i
)
First try:
(
fn findIndexOfBit bitValue bits =
(
fn compareFn val index valArray: =
(
arrayVal = valArray[index]
case of (
(val < arrayVal): -1
(val > arrayVal): 1
default: 0
)
)
bits_array = bits as array
bits_index = #{1..bits_array.count} as array
index = bsearch bitValue bits_index compareFn valArray:bits_array
)
aaa= #{ 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 100..120}
bitValue = 100
index = findIndexOfBit bitValue aaa
format "Index of bit % = %
" bitValue index
)
Second try:
(
fn firstAvailableBit bits =
(
fn firstBit arr =
(
local b
for n in arr while (b = n; off) do ()
b
)
firstBit = firstBit (-bits)
)
aaa= #{1..1000000, 1000002}
i = firstAvailableBit aaa
format "First available bit = %
" i
)
Yes.
For aaa= #{1…1000000, 1000002} and bitValue = 1000002, time and memory are:
Time: 0.349sec. Mem: 111.946.296L
It’s fast, but memory consuming.
Second try:
(
fn findIndexOfBit bitValue bits =
(
if not bits[bitValue] do return undefined
index = (bits - #{(bitValue + 1)..bits.count}).numberSet
)
aaa= #{1..1000000, 1000002}
bitValue = 1000002
gc()
t1=timestamp()
hf = heapfree
index = findIndexOfBit bitValue aaa
format "Time: %sec. Mem: % IndexOfBit %: %
" ((timestamp()-t1)/1000 as float) (hf-heapfree) bitValue index
)
Time: 0.0sec. Mem: 248L IndexOfBit 1000002: 1000001
but it corrupts original (bits) array as i mentioned above
wait a sec… i’m not sure this is correct:
pivot = bits.count/2
while (p = (pivot + bits.count)/2) > pivot do