I have a list of linear ranges which represent one big range:
X'
100 200 300 400 500 600 700 | 900 (X)
|----------|----------|----------|--------+----------|
0 | 100 (Y)
Y'
X consists of the following ranges (even and round numbers are just examples for ease of comprehension, they could be anything, no proportions here at all):
- From 100 to 200
- From 300 to 400
- From 500 to 600
- From 700 to 900
On the flip side, Y has just one range:
- From 0 to 100
Both X and Y are of the same length, just different units. Let’s say one is dollars and another is percents (or any other similarly unrelated units). So Y’0 == X’100 and Y’100 == X’900.
Given any point in Y, what is equivalent point in X and vise-versa, given a point in X – what is it in Y?
Is this a typical math problem? Does it have a name?
How many ranges do you have? Is it acceptable that the algorithm is O(number of ranges)?
If so, below is the description of the algorithm. Let me explain it on your (original) example.
1) What you’re doing to do is to map the value X in range A (100-800) to the value Y in continous range B (0-399) (as the total number of elements in your range is 400). Then it’s easy to change position in B to percents, I will omit this part.
2) Create a list of records, where each records represents one range mapping.
In your case, you will get the following list:
3) When you need to map a number X from A to B, you iterate the list to find first record with start_in_a <= X.Then your value Y is
4) The algorithm is symmettric, you just iterate the list to find the first record with start_in_b <= Y, and then
Note 1. For error checking purposes, you might keep the range size in RangeRecord, as well.
Note 2. If O(number of ranges) is not good enough, keep the records as a tree instead of a list. You will need O(log(number of ranges)) operations then,