Skip to content

Conversation

Infiltrator
Copy link
Contributor


$(P If overlapping is required, first make a duplicate, and then
copy into the original source. Keep in mind the performance hit
hit this would entail.
Copy link
Member

Choose a reason for hiding this comment

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

"hit" is duplicated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch.

)

---------
s[0..2] = s[1..3].dup;
Copy link

Choose a reason for hiding this comment

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

Shouldn't we have some library functionality with which we can effectively do s[0] = s[1]; s[1] = s[2]; with a single call?

Copy link
Member

Choose a reason for hiding this comment

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

remove?

Copy link

Choose a reason for hiding this comment

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

I think it's copy :

import std.algorithm;

void main()
{
    {
        int[] s = [1, 2, 3, 4, 5];
        s[1..3].copy(s[0..2]);
        assert(s == [2, 3, 3, 4, 5]);
    }
    {
        int[] s = [1, 2, 3, 4, 5];
        s[0..2] = s[1..3].dup;
        assert(s == [2, 3, 3, 4, 5]);
    }
}

This should avoid .dup's memory allocation.

Copy link
Member

Choose a reason for hiding this comment

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

Nice. Remove would do this:

        int[] s = [1, 2, 3, 4, 5];
        s = s.remove(0);
        assert(s == [2, 3, 4, 5]);

@dnadlinger
Copy link
Contributor

Yep, encouraging the use of dup here is a bad idea, as it is suicide performance-wise. I'd just make it say something along the lines of »use an element-wise copy«, maybe with a reference to std.algorithm.copy in parens.

@andralex
Copy link
Member

Anyhow @Infiltrator could you please amend the example to avoid dup?

@quickfur
Copy link
Member

ping
Any updates since last comments?

@ghost
Copy link

ghost commented Sep 11, 2014

Looks like not. Can anyone else take this over?

@ntrel
Copy link
Contributor

ntrel commented Oct 24, 2014

Can anyone else take this over?

See #680.

@Infiltrator Infiltrator deleted the patch-4 branch December 3, 2015 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants