1. find_all() And More

a = { 0, 1, 2, 3, 0 } 
find_all( 0, a ) -- is { 1, 5 } 

Is there a lib function that will let me do a find for "anything but 0" such that it returns { 2, 3, 4 }?

I guess I could do:

a = { 0, 1, 2, 3, 0 } 
b = a = 0 
find_all( 0, b ) -- is now { 2, 3, 4 } 

I just want what's fastest. :)

new topic     » topic index » view message » categorize

2. Re: find_all() And More

sequence r = remove_all(0, { 0, 10, 0, 20, 0, 30, 0 }) 
r -- { 10, 20, 30 } 

Jeremy

new topic     » goto parent     » topic index » view message » categorize

3. Re: find_all() And More

jeremy said...
sequence r = remove_all(0, { 0, 10, 0, 20, 0, 30, 0 }) 
r -- { 10, 20, 30 } 

Jeremy

I need indexes, not values.

new topic     » goto parent     » topic index » view message » categorize

4. Re: find_all() And More

euphoric said...
a = { 0, 1, 2, 3, 0 } 
find_all( 0, a ) -- is { 1, 5 } 

Is there a lib function that will let me do a find for "anything but 0" such that it returns { 2, 3, 4 }?

I guess I could do:

a = { 0, 1, 2, 3, 0 } 
b = a = 0 
find_all( 0, b ) -- is now { 2, 3, 4 } 

I just want what's fastest. :)

Hello euphoric,

The first example is the fastest.

Don Cole

new topic     » goto parent     » topic index » view message » categorize

5. Re: find_all() And More

dcole said...
euphoric said...
a = { 0, 1, 2, 3, 0 } 
find_all( 0, a ) -- is { 1, 5 } 

Is there a lib function that will let me do a find for "anything but 0" such that it returns { 2, 3, 4 }?

I guess I could do:

a = { 0, 1, 2, 3, 0 } 
b = a = 0 
find_all( 0, b ) -- is now { 2, 3, 4 } 

I just want what's fastest. :)

Hello euphoric,

The first example is the fastest.

Don Cole

Those two examples above are not doing the same thing, so it's not apples to apples. :)

new topic     » goto parent     » topic index » view message » categorize

6. Re: find_all() And More

euphoric said...
a = { 0, 1, 2, 3, 0 } 
find_all( 0, a ) -- is { 1, 5 } 

Is there a lib function that will let me do a find for "anything but 0" such that it returns { 2, 3, 4 }?

I guess I could do:

a = { 0, 1, 2, 3, 0 } 
b = a = 0 
find_all( 0, b ) -- is now { 2, 3, 4 } 

I just want what's fastest. :)

I'd try something like this:

constant a = { 0, 1, 2, 3, 0 }  
 
function find_all_but( object needle, sequence haystack ) 
	integer ix = 1 
	integer jx 
	sequence found = {} 
	while jx with entry do 
		for i = ix to jx - 1 do 
			found &= i 
		end for 
		ix = jx + 1 
	entry 
		jx = find( needle, haystack, ix ) 
	end while 
	return found 
end function 
 
? find_all_but( 0, a ) 

Matt

new topic     » goto parent     » topic index » view message » categorize

7. Re: find_all() And More

Sorry I miss read your question.

Do you want the index to include the zeros?

Ex. {0,0,0,7,12,17}={3,4,5}

or exclude the zeros?

Ex.{0,0,0,7,12,17}={1,2,3}

Don Cole

new topic     » goto parent     » topic index » view message » categorize

8. Re: find_all() And More

mattlewis said...

I'd try something like this:

Here's a benchmarking program for it.

include std/sequence.e 
include std/console.e 
include std/search.e 
 
sequence a = { 0, 1, 2, 3, 0 }, b 
 
b = repeat_pattern( a, 500 ) 
  
function find_all_but( object needle, sequence haystack )  
	integer ix = 1  
	integer jx  
	sequence found = {}  
	while jx with entry do  
		for i = ix to jx - 1 do  
			found &= i  
		end for  
		ix = jx + 1  
	entry  
		jx = find( needle, haystack, ix )  
	end while  
	return found  
end function  
 
atom timer = 3 + time() 
sequence x 
atom c = 0 
 
while time() < timer do 
	c+=1 
	x = find_all_but( 0, a ) 
end while 
?c 
 
sequence g 
timer = 3+time() 
c=0 
g=a=0 -- here's the cheat? 
while time() < timer do 
	c+=1 
	x = find_all( 0, g ) 
end while 
?c 
 
wait_key() 

If I put g=a=0 INSIDE the loop, then your code is waaaaAAAAAyyy faster.

So, huh?

new topic     » goto parent     » topic index » view message » categorize

9. Re: find_all() And More

mattlewis said...

I'd try something like this:

Here's a tweak to Matt's code ...

constant a = { 0, 1, 2, 3, 0 }  
 
function find_all_but( object needle, sequence haystack ) 
	integer ix = 1 
	integer jx 
        integer kx = 0 
	while jx with entry do 
		for i = ix to jx - 1 do 
                        kx += 1 
			haystack[kx] = i 
		end for 
		ix = jx + 1 
	entry 
		jx = find( needle, haystack, ix ) 
	end while 
         
	for i = ix to length(haystack) do 
                kx += 1 
		haystack[kx] = i 
	end for 
	return haystack[1 .. kx] 
end function 
 
? find_all_but( 0, a ) 
new topic     » goto parent     » topic index » view message » categorize

10. Re: find_all() And More

DerekParnell said...
mattlewis said...

I'd try something like this:

Here's a tweak to Matt's code ...

D'oh! I always do that...

Matt


Forked into: find_all_not() and using a pattern sequence

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu