-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
RFC : Implementation of rol! and ror! #9822
Conversation
Cc: @carlobaldassi |
These definitions do the job, however the usual definitions of in-place mutating operators take advantage of the fact that you can use them to avoid allocating memory. The versions as written in this PR instead offer a very small advantage over writing So I think we'd rather have functions However, out of curiosity: @amitjamadagni, do you have a need for these functions? I don't think I've ever seen them come up in code, and they are not defined for anything but BitArrays. (A minor comment on the code is that the In any case, tests and documentation are also always needed whenever adding functionality. |
@carlobaldassi I had no direct requirement of function. I was going through some of the TODO tasks and this was one of them. |
Upon thinking about it a little more, I think we'd need 2 signatures. The first one would be the in-place one, The second one would be The current Actually, one potential problem with this is that even if Also, the actual implementation of the in-place version can be quite tricky, as it should cycle through the chunks jumping around and deal with situations of potential overlap. Take the simplest situation, in which we want to rotate a BitArray
i.e. cover the whole array with cycles of step The general case where the rotation parameter and the length are not a multiple of 64 is more complicated and should use (note I didn't test the above code) |
@carlobaldassi Thanks for the inputs on this. I will work on it and send in some commits with my understanding. |
@carlobaldassi Here is an implementation I have attempted. Please do let me know your thoughts on it. |
The What is this about:
? |
@carlobaldassi thanks. I will work on the inputs. My apologies for the comment. I had a doubt but it stands clarified. |
@carlobaldassi I have been looking at the failing tests. Should they be based on the idea of how splice! is being tested ?? |
@@ -1272,29 +1272,45 @@ end | |||
(>>)(B::BitVector, i::Int32) = B >>> i | |||
(>>)(B::BitVector, i::Integer) = B >>> i | |||
|
|||
function rol!(dest::BitVector, src::BitVector, i::Integer) | |||
if length(dest) == length(src) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check should throw an error in case it's false, rather then continuing silently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more readable if you write it as length(dest) == length(src) || trhrow(...)
and then de-indent the following lines.
@carlobaldassi could you please review this. The test cases seem to work. Thanks. |
@@ -637,13 +637,29 @@ BitArrays | |||
|
|||
Performs a bitwise not operation on B. See :ref:`~ operator <~>`. | |||
|
|||
.. function:: rol!(dest::BitArray{1},src::BitArray{1},i::Integer) -> BitArray{1} | |||
|
|||
Performs a left rotation operation on dest. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not very clear, even if t's self-evident from the argument names the description should state that the rotation is performed on src
and put into dest
.
Also a very minor nitpick: please put a space between the function arguments after the commas - here and below.
@amitjamadagni Are you planning to do an attempt at the non-copying in-place version? If not, just add a |
@carlobaldassi seems a time out on the build, therefore failing. Anyways a review on this would be helpful. |
@carlobaldassi done. |
It would be nice to squash all the commits. |
commit 28bcfc0 Author: amitjamadagni <[email protected]> Date: Wed Jan 28 16:31:08 2015 +0530 Further edits. commit 7bbafd9 Author: amitjamadagni <[email protected]> Date: Wed Jan 28 01:17:16 2015 +0530 RFC : Test cases added. Docs edited. commit 587ee84 Author: amitjamadagni <[email protected]> Date: Thu Jan 22 02:46:28 2015 +0530 Edits. commit 69e19e1 Author: amitjamadagni <[email protected]> Date: Wed Jan 21 02:06:35 2015 +0530 Docs added. WIP : Further cases, tests. commit 8397aac Author: amitjamadagni <[email protected]> Date: Wed Jan 21 01:32:16 2015 +0530 WIP : Further Edits. commit c84cb6e Author: amitjamadagni <[email protected]> Date: Sun Jan 18 01:22:16 2015 +0530 RFC : Implementation of rol! and ror! Sqaushed : RFC :: Implementation of rol! and ror!.
@ViralBShah done. |
@carlobaldassi : any comments on the commit would be really helpful. |
Sorry for the delay, I've been away with no internet connection. |
RFC : Implementation of rol! and ror!
@carlobaldassi : Thanks for all the help and also for the merge. |
I just noticed these functions. As @carlobaldassi mentioned, it would be better if these truly operated in-place for I don't know if you are aware of this, but there is actually a relatively simple trick (which I learned from @matteo-frigo) for cyclically shifting an array
This requires two passes over the array. Theoretically, you can do it in a single pass by chasing the cycles of the permutation, but in practice the two-pass trick is far more efficient, both because chasing the cycles involves a lot of auxiliary computation and because Might be nice to apply this trick define |
(Similarly, one could define |
(See also Knuth Vol. 1 3rd ed., Section 1.3.3 Exercises 34, 35, and 36. The trick is in exercise 36.) |
@stevengj good idea, I'll get to it soon-ish (if no one beats me to it). |
I am new to the community. I have worked on the addition of rol! and ror!. It would be really helpful to hear some comments from the community. Thanks.