Re: Fast lookup of ranges
- From: Thad Smith <ThadSmith@xxxxxxx>
- Date: Sat, 25 Nov 2006 02:00:52 -0700
satya wrote:
hi,
I have a peculiar (atleast to me) problem before my hand. There is a
big table with numeric ranges in each row. For e.g,
row 1 (1000,1050)
row 2 (4046,5683)
...
etc.
Now, given a number like 4210, I need to be able to say as fast as
possible, which row this number falls under. ( In this case row #2
because it falls in that range). The input is guaranteed to contain
numbers that MUST fall under some row in the table.
The rows are always tuples, and the first element is always less than
the second one. Further, there is NO overlap between the (ranges in
the) rows.
The following will work well for most distributions.
Let P and Q be the minimum and maximum number contained in all ranges, giving a range of R=Q-P+1 values. Chose an array length N, the larger the faster we can search. Compute a scale factor S=ceil(R/N). Fill each entry r[i] with the range number which contains the values P+i*S through P+(i+1)*S-1. If those numbers fall within multiple ranges, you can link the ranges.
Given a number x, r[i] gives the first range number to check. Advance through the ranges, starting with range r[i] until a range match is found or the range exceeds x.
The larger the array, the faster you get an answer.
--
Thad
.
- Follow-Ups:
- Re: Fast lookup of ranges
- From: Thad Smith
- Re: Fast lookup of ranges
- References:
- Fast lookup of ranges
- From: satya
- Fast lookup of ranges
- Prev by Date: Re: Algoritim - Create Unique Integer from String
- Next by Date: Re: New non-cryptograhic hash function(s)
- Previous by thread: Fast lookup of ranges
- Next by thread: Re: Fast lookup of ranges
- Index(es):
Relevant Pages
|