I have three kind of numerical ranges which are defined within some interval like:
1. countinous range (any value within specified interval)
2. periodical sequence (sequence start, step and steps count are specified)
3. a set of exact values (like 1, 3, 7 etc)
I need to union/intersect them (from 2 to N of different types) and get optimized results. Obviously, intersection of above will return result of one of types above and unioning them will result in 1 to M ranges of types above.
Example 1:
1st range is defined as continous range from 5 to 11 and 2nd is periodical sequence from 2 to 18 with step 2 (thus, 8 steps).
Intersection will return a periodic sequence from 6 to 10 with step 2.
Union will return three results: a periodic sequence from 2 to 4 with step 2, a continous range from 5 to 11 and a periodic sequence from 12 to 18 with step 2.
Example 2:
1st range is defined as periodic sequence 0 to 10 with step 2 and 2nd is periodical sequence from 1 to 7 with step 2 (thus, 3 steps).
Intersection will return null as they dont intersect.
Union will return two results: a periodic sequence from 1 to 8 with step 1 (note: optimized result) and an exact value of 10.
Hope i didnt make mistakes 🙂
Well, these operations over these kind of sequences shouldnt be too complex and i hope there is a library for things like here. Please advice any (will use in C#.NET).
Thanks!
UPDATE
Answering to “how do i think i use the library”. All three types above can be easily defined in programming language as:
1. Contionous: { decimal Start; decimal End; } where Start is the beginning of range and End is the end
2. Periodical: { decimal Start; decimal Step; int Count; } where Start is the beginning of sequence, Step is increment and Count is steps count
3. Set of exact numbers: { decimal[] Values; } where Values is array of some decimals.
When i make an intersection of any two ranges of any types listed above then i’ll certainly get result of one of that types, e.g. “continous” and “periodical” ranges intersection will result to “periodical”, “cont.” and “set of exact” will result to set of exact, “cont.” and “exact” will return exacts as well. Intersection of the same types will return result of the input types.
Union of 2 ranges is a bit more complex but will anyway return 2 to 3 ranges defined in types above as well.
Having intersection/unioning functions i’ll always be able run it over 2 to N ranges and will get results in the input types terms.
First, following other answers before: I don’t believe there’s a standard library for this: it looks very much as a special case.
Second, the problem / requirement is not stated sufficiently clear. I’ll follow the problem introduction and refer to the 3 different types as “set”, “periodic”, “continuous”.
Consider two sets, {1,4,5,6} and {4,5,6,8}. Their intersection is {4,5,6}. Do we have to label that as “periodic” because that description fits the case, or as “set” because it is an intersection of sets?
From this, more in general, do we need to change a “set”-label into “periodic” as soon as its contents IS periodic? After all, a “periodic” is a special case of a “set”.
Likewise, consider the union of a “periodic” {4,6,8} and a set {10,15,16}. Do we have to define the result as a periodic {4,6,8,10} plus a periodic{15,16} or rather as one set with all the values, or yet another variety?
And how about the degenerate cases: is {3} a “set”, a “periodic”, or even a “continuous”? What type is the intersection of “continuous”{1-4} and “set”{4,7,8}? and the intersection of continuous{1-4} and continous{4-7}?
And so forth: it has to be clear how any result – from union and/or intersection – must be labeled/described, whether – for instance – the intersection or union of two discrete types (non-continuous) is always ONE type (usually a set, possibly a periodic), or rather always a series of periodics, or …
Third, assuming that the above questions have been taken care of, I believe you may approach the implementation with the following guidelines:
consider only two “types”, namely “continuous” and “set”. The “periodic” is just a special form of a “set”, and at any moment we can, if and where appropriate, label a “set” as “periodic”.
define a common baseclass, and use appropriate derived (overloaded) methods for union, intersection, and whatever you may need, to produce – for instance – a list(of baseType) as result.
The case-by-case implementations are basically quite simple – assuming that my introductory questions have been answered. Wherever the initial problem appears somewhat “difficult”, it is only because the problem and specifications are not yet very well defined.