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

Fixes in Cycle #1149

Closed
wants to merge 7 commits into from
Closed

Fixes in Cycle #1149

wants to merge 7 commits into from

Conversation

monarchdodra
Copy link
Collaborator

Fixes and improves a couple of points in cycle:

  1. Overflow protection: After being iterated on for more than size_t.max, the index overflow+modulo broke the correct flow of the cicular buffer.
  2. improved a couple of safe/nothrow signatures
  3. Added support for unicode with static arrays
  4. Added a special case for 1 element array circular buffer. This is actually important, as things like recurrence tends to use them...

The changes mostly shuffles and/or changes the indentation of the code, Github's diff is very bad with this. It may be worth looking into each diff individually to see what changed, and what was just moved for each edit.

@@ -286,7 +286,7 @@ module std.range;
public import std.array;
import core.bitop, core.exception;
import std.algorithm, std.conv, std.exception, std.functional,
std.traits, std.typecons, std.typetuple, std.string;
std.traits, std.typecons, std.typetuple, std.string, std.utf;
Copy link
Member

Choose a reason for hiding this comment

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

order alphabetically

@monarchdodra
Copy link
Collaborator Author

Rebased. Added fix for:
#1149
Issue 10845 - std.range.Cycle broken for reference type forward ranges

@Poita
Copy link
Contributor

Poita commented Aug 18, 2013

LGTM, but could you make the std.utf import local to the functions that need it? Having std.range depend on a whole new module to serve two functions seems unnecessary.

@monarchdodra
Copy link
Collaborator Author

LGTM, but could you make the std.utf import local to the functions that need it? Having std.range depend on a whole new module to serve two functions seems unnecessary.

Done. Also, added 10845 unittest.

@@ -3842,7 +3853,7 @@ struct Cycle(Range)

static if (is(typeof((cast(const R)_original)[0])) &&
is(typeof((cast(const R)_original).length)))
{
{
Copy link
Member

Choose a reason for hiding this comment

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

Wrong indentation here.

@dnadlinger
Copy link
Member

LGTM otherwise. Could you fix that indentation change and rebase so we can finally get this pulled? 6 months is a ridiculously long period of time for something like this to remain open…

@monarchdodra
Copy link
Collaborator Author

LGTM otherwise. Could you fix that indentation change and rebase so we can finally get this pulled? 6 months is a ridiculously long period of time for something like this to remain open…

Ping.

@property dchar front() const
{
import std.utf : decode;
size_t copy = _index;
Copy link

Choose a reason for hiding this comment

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

You're only using this variable once, maybe you should just do:

return decode(_ptr[0 .. R.length], _index); 

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The point is that decode takes the index by reference, and increases it to the index after the decoded char. So copy is necessary to not modify _index. That said, instead of re-inventing the whell, I might as well just write:

return _ptr[_index .. $].front;

Copy link

Choose a reason for hiding this comment

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

Ah ref arguments. I think there was actually a pull by Dennis recently which introduced a getRef template or something of that sort.

@ghost
Copy link

ghost commented Oct 22, 2013

Apart from the comments, this looks good and almost ready to be merged (A squash will be necessary later).

The problem is that if the cycle is iterated upon for more than size_t.max, the index makes a jump and becomes out of sync, given the modulo approach to indexing. This is especially relevant, since cycle is supposed to be used for circular buffers, which can and are iterated upon for potentially MASSIVE amounts of data.

The new approach is to always bind _index between 0 and range.length, so that _index never actually overflows.

This has the added advantage of shifting the modulo cost from front to popFront(), which is more efficient.

Tweaked a few other things.
@monarchdodra
Copy link
Collaborator Author

This pull has gone stale. Closing for re-write.

@monarchdodra monarchdodra deleted the cycle branch March 12, 2014 22:38
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.

4 participants