You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by "Uwe Schindler (JIRA)" <ji...@apache.org> on 2011/09/22 15:40:28 UTC

[jira] [Created] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
---------------------------------------------------------------------------------------------------

                 Key: LUCENE-3449
                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
             Project: Lucene - Java
          Issue Type: Improvement
          Components: core/other
    Affects Versions: 3.4, 4.0
            Reporter: Uwe Schindler
            Priority: Minor


The usage pattern for nextSetBit/prevSetBit is the following:

{code:java}
for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
 // operate on index i here
}
{code}

The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).

If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:

{code:java}
for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
 // operate on index i here
}
{code}

We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.

The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.


--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Issue Comment Edited] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Dawid Weiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13114025#comment-13114025 ] 

Dawid Weiss edited comment on LUCENE-3449 at 9/24/11 5:55 PM:
--------------------------------------------------------------

Eh... I shouldn't be throwing suggestions not backed up by patches... ;)) I'm working on something else tonight, but I'll add it to my queue. If anybody (Uwe, Uwe, Uwe! :)) wants to give it a go, go ahead.

      was (Author: dweiss):
    Eh... I shouldn't be throwing suggestions not backed up by patches... ;)) I'm working on something else tonight, but I'll add it to my queue. If anybody (Uwe, Uwe, Uwe! :)) wants to give it a take, go ahead.
  
> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Uwe Schindler (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13112820#comment-13112820 ] 

Uwe Schindler commented on LUCENE-3449:
---------------------------------------

I agree with both of you, so we should first check if it really slows anything down.

bq. I agree with Uwe that the idiom is frequently used and if Lucene's bitset behaves differently, this may lead to hard-to-trace issues.

This was exactly my problem in LUCENE-3448. The bug in the code was only found because the testcase had the last bit set in the filter. I will for now change the code in 3448 to the pattern suggested by Mike.

The overflow is less problematic as MAX_VALUE is not a valid docId (it's NO_MORE_DOCS for DISIs).

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Yonik Seeley (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13112894#comment-13112894 ] 

Yonik Seeley commented on LUCENE-3449:
--------------------------------------

One could allocate an extra bit on construction (round up if the last word is full) to avoid the extra if statement.

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Updated] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Uwe Schindler (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Uwe Schindler updated LUCENE-3449:
----------------------------------

    Attachment: LUCENE-3449.patch

Patch also fixing the ugly test code.

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Michael McCandless (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13113957#comment-13113957 ] 

Michael McCandless commented on LUCENE-3449:
--------------------------------------------

bq. Nomatter what, this loop needs 2 ifs per cycle

Duh, I was wrong about this!

We just need to change the sentinel value returned by nextSetBit when
there is no next set bit, from -1 to MAX_INT.  In fact we did this for
DISI.nextDoc, for the same reason (saves an if per cycle).

Then you just rotate the loop:

{noformat}
  if (bits.length() != 0) {
    int bit = bits.nextSetBit(0);
    final int limit = bits.length()-1;
    while (bit < limit) {
      // ...do something with bit...
      bit = bits.nextSetBit(1+bit);
    }

    if (bit == bits.length()-1) {
      // ...do something with bit...
    }
  }
{noformat}

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Dawid Weiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13114025#comment-13114025 ] 

Dawid Weiss commented on LUCENE-3449:
-------------------------------------

Eh... I shouldn't be throwing suggestions not backed up by patches... ;) I'm working on something else tonight, but I'll add it to my queue. If anybody (Uwe, Uwe, Uwe! :) wants to give it a take, go ahead.

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Dawid Weiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13113184#comment-13113184 ] 

Dawid Weiss commented on LUCENE-3449:
-------------------------------------

Exactly. Overallocate a bit and allow reaching the +1. I agree "repeating the same mistakes" is not a good idea, but in this case it may be misleading because the API is already mirroring juc.BitSet (naming convention), so the contract comes as an assumption. I'm not that fond of juc API myself (vide a simplified container API in HPPC), so my opinion has nothing to do with supporting juc.

bq. Well... I doubt this is easily measured empirically?

Exactly. I think the difference will be minor, if statistically significant at all (when running inside a larger program and not in a microbenchmark).

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Dawid Weiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13113961#comment-13113961 ] 

Dawid Weiss commented on LUCENE-3449:
-------------------------------------

Sorry, but my gut feeling says no to this loop logic. It just seems strangely complicated. If adherence to BitSet is not an issue why not:

{code}
for (int i = bs.firstSetBit(); i >= 0; i = bs.nextSetBitAfter(i)) {
{code}

this seems clearer on method naming, has a single if... and I think could be implemented nearly identically to what's already in the code. We can run microbenchmarks for fun and see what comes out better an by what margin. 

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Uwe Schindler (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13114055#comment-13114055 ] 

Uwe Schindler commented on LUCENE-3449:
---------------------------------------

bq (Uwe, Uwe, Uwe! :))

Yes, but today is/was freetime :-)

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Issue Comment Edited] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Yonik Seeley (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13112894#comment-13112894 ] 

Yonik Seeley edited comment on LUCENE-3449 at 9/22/11 8:48 PM:
---------------------------------------------------------------

We could allocate an extra bit on construction (round up if the last word is full) to avoid the extra if statement.

      was (Author: yseeley@gmail.com):
    One could allocate an extra bit on construction (round up if the last word is full) to avoid the extra if statement.
  
> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Issue Comment Edited] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Uwe Schindler (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13114055#comment-13114055 ] 

Uwe Schindler edited comment on LUCENE-3449 at 9/24/11 7:45 PM:
----------------------------------------------------------------

bq. (Uwe, Uwe, Uwe! :))

Yes, but today is/was freetime :-)

      was (Author: thetaphi):
    bq (Uwe, Uwe, Uwe! :))

Yes, but today is/was freetime :-)
  
> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Michael McCandless (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13112885#comment-13112885 ] 

Michael McCandless commented on LUCENE-3449:
--------------------------------------------

bq. Can you check if this actually slows down things, Mike? 

Well... I doubt this is easily measured empirically?

But I don't think measuring it is necessary to make a decision here,
since it's an if statement that must find its way down to the CPU.  It
can only add unecessary cost for those callers that will never pass an
out-of-bounds index.

I also don't like the API inconsistency that this would introduce to
FixedBitSet: currently all methods require index be in-bounds.  This
is one of the improvements over OpenBitSet, which had some methods
requiring in-bounds and some not.

I don't think we should in any way feel compelled to replicate the
mistakes of Java's BitSet just because it's a common idiom / some
named pattern / taught in textbooks.  Rather, it's Lucene that is
using this bit set impl, and so we should do what's best for Lucene.


> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Dawid Weiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13114057#comment-13114057 ] 

Dawid Weiss commented on LUCENE-3449:
-------------------------------------

I was just teasing you, enjoy your weekend :)

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Michael McCandless (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13113988#comment-13113988 ] 

Michael McCandless commented on LUCENE-3449:
--------------------------------------------

{quote}
If adherence to BitSet is not an issue why not:

for (int i = bs.firstSetBit(); i >= 0; i = bs.nextSetBitAfter(i)) {
this seems clearer on method naming, has a single if... and I think could be implemented nearly identically to what's already in the code. We can run microbenchmarks for fun and see what comes out better an by what margin.
{quote}

Ooh I love that!

If in fact we can achieve such clean code (above), a clean API ( all
methods require in-bounds index), and not incur added cost in
nextSetBitAfter (vs the nextSetBit we have today) then I agree this
would be the best of all worlds.

I think we should give the sentinal a name (eg FBS.END)?  Then the end
condition can be {{i != FBS.END}}.


> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Michael McCandless (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13113371#comment-13113371 ] 

Michael McCandless commented on LUCENE-3449:
--------------------------------------------


bq. We could allocate an extra bit on construction (round up if the last word is full) to avoid the extra if statement.

+1 for adding a bit guard at the end; that's a great idea!

But this only works for the nextSetBit case right?  So I don't think
prevSetBit should accept -1.

Let's clearly jdoc that nextSetBit is an exception to the other
methods (ie it allows for exactly 1 (= length()) out-of-bounds index).

bq. but in this case it may be misleading because the API is already mirroring juc.BitSet (naming convention), so the contract comes as an assumption.

Well, still we shouldn't let BitSet's mistakes live on through the
method names it had chosen.  I mean at some point someone has to stand
up to BitSet's mistakes ;)  It may as well be us.

I suppose we could also change our names if necessary.

{quote}
bq. Well... I doubt this is easily measured empirically?

Exactly. I think the difference will be minor, if statistically significant at all (when running inside a larger program and not in a microbenchmark).
{quote}

Yet the cost is non-zero.

Why add a some cost, any cost, even if it's small, when you don't have
to?

Over time such minor slowdowns can eventually add up, and only by
being vigilant to changes in Lucene's hotspots can we all keep Lucene
fast and lean.

This logic only applies to potential "hotspots", like these methods.
For non-hotpots I fully agree that convenience, readability,
convention, etc., are more important.  But "down low", performance
trumps these other concerns.

Different rules apply down low.  Just like quantum mechanics!


> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Issue Comment Edited] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Dawid Weiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13114025#comment-13114025 ] 

Dawid Weiss edited comment on LUCENE-3449 at 9/24/11 5:54 PM:
--------------------------------------------------------------

Eh... I shouldn't be throwing suggestions not backed up by patches... ;)) I'm working on something else tonight, but I'll add it to my queue. If anybody (Uwe, Uwe, Uwe! :)) wants to give it a take, go ahead.

      was (Author: dweiss):
    Eh... I shouldn't be throwing suggestions not backed up by patches... ;) I'm working on something else tonight, but I'll add it to my queue. If anybody (Uwe, Uwe, Uwe! :) wants to give it a take, go ahead.
  
> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Dawid Weiss (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13112809#comment-13112809 ] 

Dawid Weiss commented on LUCENE-3449:
-------------------------------------

Can you check if this actually slows down things, Mike? I agree with Uwe that the idiom is frequently used and if Lucene's bitset behaves differently, this may lead to hard-to-trace issues. On the other hand the idiom _is_ screwed up because it overflows into negative index values fairly fast. A nicer one would be:

{code}
for (int i = bs.firstSetBit(); i >= 0; i = bs.nextSetBit(i)) {
{code}

But this would confuse people familiar with the contract of juc.BitSet#nextSetBit even more...

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Uwe Schindler (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13113373#comment-13113373 ] 

Uwe Schindler commented on LUCENE-3449:
---------------------------------------

bq. Let's clearly jdoc that nextSetBit is an exception to the other methods (ie it allows for exactly 1 (= length()) out-of-bounds index).

We must then always overallocate one bit (which is easy to do in the ctor).

> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


[jira] [Commented] (LUCENE-3449) Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book

Posted by "Michael McCandless (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LUCENE-3449?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13112766#comment-13112766 ] 

Michael McCandless commented on LUCENE-3449:
--------------------------------------------

I don't like that this adds an additional if inside next/prevSetBit
that for some cases is not needed (eg BlockJoinQuery's usage).

I think performance trumps convenience here for these low level APIs.

Why not use a simple while loop?

{noformat}
int idx = 0;
while (idx < length) {
  idx = bits.nextSetBit(idx);
  if (idx == -1) {
    break;
  }
  // do something with idx
  idx++;
}
{noformat}

Nomatter what, this loop needs 2 ifs per cycle; I just think that 2nd
if belongs in the loop itself and not inside next/prevSetBit, since
not all use cases require it.


> Fix FixedBitSet.nextSetBit/prevSetBit to support the common usage pattern in every programming book
> ---------------------------------------------------------------------------------------------------
>
>                 Key: LUCENE-3449
>                 URL: https://issues.apache.org/jira/browse/LUCENE-3449
>             Project: Lucene - Java
>          Issue Type: Improvement
>          Components: core/other
>    Affects Versions: 3.4, 4.0
>            Reporter: Uwe Schindler
>            Priority: Minor
>         Attachments: LUCENE-3449.patch
>
>
> The usage pattern for nextSetBit/prevSetBit is the following:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) {
>  // operate on index i here
> }
> {code}
> The problem is that the i+1 at the end can be bs.length(), but the code in nextSetBit does not allow this (same applies to prevSetBit(0)). The above usage pattern is in every programming book, so it should really be supported. The check has to be done in all cases (with the current impl in the calling code).
> If the check is done inside xxxSetBit() it can also be optimized to be only called seldom and not all the time, like in the ugly looking replacement, thats currently needed:
> {code:java}
> for(int i=bs.nextSetBit(0); i>=0; i=(i<bs.length()-1) ? bs.nextSetBit(i+1) : -1) {
>  // operate on index i here
> }
> {code}
> We should change this and allow out-of bounds indexes for those two methods (they already do some checks in that direction). Enforcing this with an assert is unuseable on the client side.
> The test code for FixedBitSet also uses this, horrible. Please support the common usage pattern for BitSets.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org