Is it safe to assume that all overlapping slices share the same base array?
On 21 May 2013 13:01, minux <minu...@gmail.com> wrote:Agreed. In that case, the check in math/big might be wrong too of course.
> The question is what does the OP actually try to do?
> because in the most general case where slices are created using SliceHeader
> and
> package unsafe, i believe we must resort to use unsafe to detect overlaps
> efficiently.
> (simply consider the case that I reduced capacity of a slice that otherwise
> will
> overlap with another slice)
If we *did* add it to the language, how would you like it to look?
What do you think about my suggestions above?
Do you see a particular use case for allowing a pointer to an arrayOn 22 May 2013 17:34, minux <minu...@gmail.com> wrote:
> how about we first try a library solution at this problem?
> something like this in the runtime package (or reflect), just your 2nd
> proposal changed to a package function.
>
> // Overlapping returns true iff object a and b is aliasing each other.
> // a and b should be slice or pointer to array, or it panics.
> func Overlapping(a, b interface{}) bool
here? I'd prefer to allow slices only (trivially derivable from array pointers).
> i think future compiler optimization could make this as efficient as new> compiler intrinsic (to perserve correct stack traces on panics, the compilerI would really prefer a compiler built in so that we get static type
> might not be able to inline it if it can panic, we can change so that it
> returns false if a or b is of the wrong type).
checking (and I'm guessing that converting to interface{}
will incur some overhead too, even if the values don't escape).
On Thu, May 23, 2013 at 2:34 AM, minux <minu...@gmail.com> wrote:I don't know if we'd want to do this, but if we did, I don't think a
> // Overlapping returns true iff object a and b is aliasing each other.
> // a and b should be slice or pointer to array, or it panics.
> func Overlapping(a, b interface{}) bool
bool return is enough. For example, if we wanted to be able to
implement the built-in copy function, we'd also need to know whether
the overlapping slices required a forward copy or a backwards copy.
I have a vague memory of wanting something like this for the
2-dimensional equivalent of copy in image/draw, but I've long since
paged out the details.
On Thu, May 23, 2013 at 9:10 AM, Nigel Tao <nige...@golang.org> wrote:On Thu, May 23, 2013 at 2:34 AM, minux <minu...@gmail.com> wrote:I don't know if we'd want to do this, but if we did, I don't think a
> // Overlapping returns true iff object a and b is aliasing each other.
> // a and b should be slice or pointer to array, or it panics.
> func Overlapping(a, b interface{}) bool
bool return is enough. For example, if we wanted to be able to
implement the built-in copy function, we'd also need to know whether
the overlapping slices required a forward copy or a backwards copy.Yeah, that's a new use case. However, I can't think of a way to design thealiasing/overlapping interface to accommodate both use cases.