OSDN Git Service

Added Bits#getAndClear, Bits#getAndSet and Bits#containsAll
authorhneuer <hanno.neuer@gmail.com>
Mon, 7 Oct 2013 08:03:32 +0000 (10:03 +0200)
committerhneuer <hanno.neuer@gmail.com>
Mon, 7 Oct 2013 08:03:32 +0000 (10:03 +0200)
Also made some variables local in other methods.

CHANGES
gdx/src/com/badlogic/gdx/utils/Bits.java

diff --git a/CHANGES b/CHANGES
index 46ff0e4..4ad3721 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -85,6 +85,7 @@
 - added render-hooks beginRender() and endRender() to BatchTiledMapRenderer
 - Added panStop to GestureListener interface.
 - ScissorStack#calculateScissors changed to take viewport, enabling it to work with glViewport.
+- Added Bits#getAndClear, Bits#getAndSet and Bits#containsAll
 
 [0.9.8]
 - see http://www.badlogicgames.com/wordpress/?p=2791
index f4a0f16..7ea009f 100644 (file)
@@ -29,6 +29,30 @@ public class Bits {
                return (bits[word] & (1L << (index & 0x3F))) != 0L;\r
        }\r
 \r
+       /** Returns the bit at the given index and clears it in one go.\r
+        * @param index the index of the bit\r
+        * @return whether the bit was set before invocation\r
+        * @throws ArrayIndexOutOfBoundsException if index < 0 */\r
+       public boolean getAndClear (int index) {\r
+               final int word = index >>> 6;\r
+               if (word >= bits.length) return false;\r
+               long oldBits = bits[word];\r
+               bits[word] &= ~(1L << (index & 0x3F));\r
+               return bits[word] != oldBits;\r
+       }\r
+\r
+       /** Returns the bit at the given index and sets it in one go.\r
+        * @param index the index of the bit\r
+        * @return whether the bit was set before invocation\r
+        * @throws ArrayIndexOutOfBoundsException if index < 0 */\r
+       public boolean getAndSet (int index) {\r
+               final int word = index >>> 6;\r
+               checkCapacity(word);\r
+               long oldBits = bits[word];\r
+               bits[word] |= 1L << (index & 0x3F);\r
+               return bits[word] == oldBits;\r
+       }\r
+\r
        /** @param index the index of the bit to set\r
         * @throws ArrayIndexOutOfBoundsException if index < 0 */\r
        public void set (int index) {\r
@@ -62,6 +86,7 @@ public class Bits {
 \r
        /** Clears the entire bitset */\r
        public void clear () {\r
+               long[] bits = this.bits;\r
                int length = bits.length;\r
                for (int i = 0; i < length; i++) {\r
                        bits[i] = 0L;\r
@@ -78,10 +103,12 @@ public class Bits {
         * \r
         * @return the logical size of this bitset */\r
        public int length () {\r
+               long[] bits = this.bits;\r
                for (int word = bits.length - 1; word >= 0; --word) {\r
-                       if (bits[word] != 0) {\r
+                       long bitsAtWord = bits[word]; \r
+                       if (bitsAtWord != 0) {\r
                                for (int bit = 63; bit >= 0; --bit) {\r
-                                       if ((bits[word] & (1L << (bit & 0x3F))) != 0L) {\r
+                                       if ((bitsAtWord & (1L << (bit & 0x3F))) != 0L) {\r
                                                return (word << 6) + bit;\r
                                        }\r
                                }\r
@@ -92,6 +119,7 @@ public class Bits {
 \r
        /** @return true if this bitset contains no bits that are set to true */\r
        public boolean isEmpty () {\r
+               long[] bits = this.bits;\r
                int length = bits.length;\r
                for (int i = 0; i < length; i++) {\r
                        if (bits[i] != 0L) {\r
@@ -104,17 +132,21 @@ public class Bits {
        /** Returns the index of the first bit that is set to true that occurs on or after the specified starting index. If no such bit\r
         * exists then -1 is returned. */\r
        public int nextSetBit (int fromIndex) {\r
+               long[] bits = this.bits;\r
                int word = fromIndex >>> 6;\r
-               if (word >= bits.length) return -1;\r
+               int bitsLength = bits.length;\r
+               if (word >= bitsLength) return -1;\r
+               long bitsAtWord = bits[word];\r
                for (int i = fromIndex & 0x3f; i < 64; i++) {\r
-                       if ((bits[word] & (1L << (i & 0x3F))) != 0L) {\r
+                       if ((bitsAtWord & (1L << (i & 0x3F))) != 0L) {\r
                                return (word << 6) + i;\r
                        }\r
                }\r
-               for (word++; word < bits.length; word++) {\r
+               for (word++; word < bitsLength; word++) {\r
                        if (word != 0) {\r
+                               bitsAtWord = bits[word];\r
                                for (int i = 0; i < 64; i++) {\r
-                                       if ((bits[word] & (1L << (i & 0x3F))) != 0L) {\r
+                                       if ((bitsAtWord & (1L << (i & 0x3F))) != 0L) {\r
                                                return (word << 6) + i;\r
                                        }\r
                                }\r
@@ -126,19 +158,23 @@ public class Bits {
        /** Returns the index of the first bit that is set to false that occurs on or after the specified starting index. If no such bit\r
         * exists then -1 is returned. */\r
        public int nextClearBit (int fromIndex) {\r
+               long[] bits = this.bits;\r
                int word = fromIndex >>> 6;\r
-               if (word >= bits.length) return -1;\r
+               int bitsLength = bits.length;\r
+               if (word >= bitsLength) return -1;\r
+               long bitsAtWord = bits[word];\r
                for (int i = fromIndex & 0x3f; i < 64; i++) {\r
-                       if ((bits[word] & (1L << (i & 0x3F))) == 0L) {\r
+                       if ((bitsAtWord & (1L << (i & 0x3F))) == 0L) {\r
                                return (word << 6) + i;\r
                        }\r
                }\r
-               for (word++; word < bits.length; word++) {\r
+               for (word++; word < bitsLength; word++) {\r
                        if (word == 0) {\r
                                return word << 6;\r
                        }\r
+                       bitsAtWord = bits[word];\r
                        for (int i = 0; i < 64; i++) {\r
-                               if ((bits[word] & (1L << (i & 0x3F))) == 0L) {\r
+                               if ((bitsAtWord & (1L << (i & 0x3F))) == 0L) {\r
                                        return (word << 6) + i;\r
                                }\r
                        }\r
@@ -194,12 +230,37 @@ public class Bits {
         * @return boolean indicating whether this bit set intersects the specified bit set\r
         */\r
        public boolean intersects (Bits other) {\r
-               for (int i = 0, j = bits.length, k = other.bits.length; i < j && i < k; i++) {\r
-                       if ((bits[i] & other.bits[i]) != 0) {\r
+               long[] bits = this.bits;\r
+               long[] otherBits = other.bits;\r
+               for (int i = Math.min(bits.length, otherBits.length) - 1; i >= 0; i--) {\r
+                       if ((bits[i] & otherBits[i]) != 0) {\r
                                return true;\r
                        }\r
                }\r
                return false;\r
        }\r
 \r
+       /** Returns true if this bit set is a super set of the specified set, i.e. it has all bits set to true that are also set to true\r
+        * in the specified BitSet.\r
+        * \r
+        * @param other a bit set\r
+        * @return boolean indicating whether this bit set is a super set of the specified set */\r
+       public boolean containsAll (Bits other) {\r
+               long[] bits = this.bits;\r
+               long[] otherBits = other.bits;\r
+               int otherBitsLength = otherBits.length;\r
+               int bitsLength = bits.length;\r
+\r
+               for (int i = bitsLength; i < otherBitsLength; i++) {\r
+                       if (otherBits[i] != 0) {\r
+                               return false;\r
+                       }\r
+               }\r
+               for (int i = Math.min(bitsLength, otherBitsLength) - 1; i >= 0; i--) {\r
+                       if ((bits[i] & otherBits[i]) != otherBits[i]) {\r
+                               return false;\r
+                       }\r
+               }\r
+               return true;\r
+       }\r
 }
\ No newline at end of file