Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isSliceOf to check the origin of a slice #6147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

wilzbach
Copy link
Member

@wilzbach wilzbach commented Feb 9, 2018

This is has come up before:

tl;dr: of the previous discussion:

  • overlap exists, but is confusing to use
  • there were no "real world" use cases / huge interest
    • @dcarp provided some and I have run into similar uses when working with allocators (it's analog to owns)
    • sameHead and sameTail already exists
    • @trusted doesn't work for one-liners

CC @nordlow @dcarp

@dlang-bot
Copy link
Contributor

dlang-bot commented Feb 9, 2018

Thanks for your pull request, @wilzbach!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#6147"

@JackStouffer
Copy link
Member

std.exception.doesPointTo?

@wilzbach
Copy link
Member Author

wilzbach commented Feb 9, 2018

std.exception.doesPointTo?

Sorry for not mentioning that one. It's pretty useless (and in the wrong module):

If target is a pointer, a dynamic array or a class, then these functions will only check if source points to target, not what target references.

Convince yourself: https://run.dlang.io/is/NoZVWq

@wilzbach
Copy link
Member Author

wilzbach commented Feb 9, 2018

Argh, the spuriously failing std.regex due to its high memory consumption is getting more and more annoying.

See also: https://issues.dlang.org/show_bug.cgi?id=18411

Copy link
Contributor

@nordlow nordlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @wilzbach for making ideas become real work.

std/array.d Outdated Show resolved Hide resolved
std/array.d Outdated Show resolved Hide resolved
@JackStouffer
Copy link
Member

Any reason to not use the &whole[0] style rather than whole.ptr?

@wilzbach
Copy link
Member Author

wilzbach commented Apr 6, 2018

Any reason to not use the &whole[0] style rather than whole.ptr?

Yes - isSliceOf should work for empty arrays too, e.g.

assert(null.isSliceOf(null));

@wilzbach wilzbach added the @andralex Approval from Andrei is required label Apr 6, 2018
@JackStouffer
Copy link
Member

null isn't a slice of itself because null isn't anything. Nothing can't be a part of nothing.

@wilzbach
Copy link
Member Author

wilzbach commented Apr 6, 2018

Nothing can't be a part of nothing.

So you would treat null like FP's NaN where the default value is poison and yields false? Interesting thought!
The problem here is that null is null, so I thought it makes sense that isSliceOf matches this behavior.

@JackStouffer
Copy link
Member

If I had to be pedantic, I would say the nothing is the same as nothing but nothing is not a memory view of nothing.

Copy link
Member

@MetaLang MetaLang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's too bad to say that null.isSliceOf(null) is true.

@@ -0,0 +1,18 @@
std.array.sliceOf has been added

$(REF sliceOf, std,array) provides a `@safe` way to query
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a safe way to query what?

Copy link
Member

@JackStouffer JackStouffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null thing isn't a blocking issue for me. I approve baring that changelog nitpick.

@n8sh
Copy link
Member

n8sh commented Apr 10, 2018

Tangential to this pull request:

overlap exists, but is confusing to use

Maybe the documentation needs improvement then, because if (a.overlap(b)) should be crystal clear!

@thewilsonator
Copy link
Contributor

Ping @andralex

@andralex
Copy link
Member

This API could be better. As it is, it returns very little information i.e. "is this range overlapping this other range in any position?" with no further information on position.

The same information can be trivially fetched using overlap in one of two ways:

small.overlap(large) is small

or

small.overlap(large).length == small.length

I like the first one more for generality, and the second more for efficiency. Given the rarity of such a query, I think we don't need a new function for it.

BUT! All is not lost. I propose to convert this PR into a documentation PR. The definition and use of isSliceOf can be put in a documentation unittest. That way we match google queries like "dlang isSliceOf" and similar. Win-win-win!

@andralex andralex removed the @andralex Approval from Andrei is required label Dec 19, 2018
Copy link
Member

@andralex andralex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify I am asking for changes.

@thewilsonator
Copy link
Contributor

IMO your suggestion fall foul of

overlap exists, but is confusing to use

If I saw small.overlap(large) is small it would take me a good while to figure out what that meant.

As it is, it returns very little information i.e. "is this range overlapping this other range in any position?" with no further information on position.

c.f. canFind(/find), so what? The index is trivially whole.ptr - part.ptr but you'd just use part.ptr.

Given the rarity of such a query, I think we don't need a new function for it.

@dcarp provided some and I have run into similar uses when working with allocators (it's analog to owns)

The existence of one-liners on phobos exist precisely to a) stop people reinventing the wheel, and b) use a common name: this fit both of those.

Please reconsider.

@andralex
Copy link
Member

The existence of one-liners on phobos exist precisely to a) stop people reinventing the wheel, and b) use a common name: this fit both of those.

(c) must be in frequent use, this doesn't pass that test

@andralex
Copy link
Member

If I saw small.overlap(large) is small it would take me a good while to figure out what that meant.

"The overlap of small and large is the same as small itself, i.e. large subsumes small." For somebody in a place in life where they need overlap, this is not quite the leap.

@thewilsonator
Copy link
Contributor

For somebody in a place in life where they need overlap, this is not quite the leap.

I agree, but for those not in need of or familiar with overlap, small.overlap(large) is small is obtuse.

@andralex
Copy link
Member

@thewilsonator wouldn't disagree. I still think this should be relegated to a documentation example. Thanks.

@RazvanN7
Copy link
Collaborator

@andralex I have put isSliceOf into a documented unittest and have used overlap instead of pointer arithmetics. However, I had to delete a few tests regardind the behavior in the presence of null. However, I think this is fine since the point is to provide a use case for overlap.

@LightBender
Copy link
Contributor

@thewilsonator you wanted this one.

@LightBender LightBender added the Salvage This PR needs work, but we want to keep it and it can be salvaged. label Oct 27, 2024
@LightBender
Copy link
Contributor

@thewilsonator you wanted this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Salvage This PR needs work, but we want to keep it and it can be salvaged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.