Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LinSpace: improve length 0 and 1 corner cases.
The length zero and one cases for LinSpace are really weird – arguably they simply shouldn't be allowed at all. However, I think for usability we really need to support these. This change uses -1 and 0 as special values of the r.len field, encoding, somewhat counterintuitively, real lengths of 0 and 1, respectively. The reason for this is so that the element access computations for the length one case has r.len-1 == -1 and only flips sign, avoiding overflow when r.start is very large. In the length zero case, r.len-1 == -2, so we need to divide by -2 to get correct first and last values back. This is susceptible to overflow, but since these values are kind of nonsense in the zero case, this seems to be a preferrable situation. There are still cases where the (r.len-i)*r.start + (i-1)*r.stop can overflow for longer ranges, and it's unclear to me whether you can handle those correctly with the same LinSpace type. This plus the complexity of handling corner cases like lengths zero and one, makes me wonder if it's really a good idea to make this a type at all.
- Loading branch information