Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Efficient algorithm needed to split Set::IntSpan objects

by jethro (Monsignor)
on Sep 23, 2009 at 15:03 UTC ( [id://796992]=note: print w/replies, xml ) Need Help??


in reply to Efficient algorithm needed to split Set::IntSpan objects

Instead of splitting you could do a sort of accumulating using the set operations of Set::IntSpan:
First create a result-set 0 which would have (in the example) a span of 1,14. Adding set a would create a result-set 1 with span 3-11 and result-set 0 with 1-2,12-14. This is easily done with set operations available in Set::IntSpan.

For adding b you would create the union of result-set 0 and b, which is empty. Then you would create the union of result-set 1 and b, which is 5-8, subtract that from b and result-set 1, and add the range to result-set 2.

Before: XXXXXXXXXXXXXX = result-set 0 -------------- = result-set 1 -------------- = result-set 2 After adding a: XX---------XXX = result-set 0 --XXXXXXXXX--- = result-set 1 -------------- = result-set 2 After adding b: XX---------XXX = result-set 0 --XXX---XXX--- = result-set 1 -----XXX------ = result-set 2

Whenever you add another set, you look for the union of your set and all the already created result-sets: If there is a range common to the set and result-set X, that range will be subtracted from both sets and added to the result-set X+1, until your set is empty

Naturally this means if you have a value where all sets overlap, this will create lots of result-sets and make the algorithm slow again. If not it should be reasonable fast if set operations in Set::IntSpan are efficient as well

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://796992]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found