2002-06-24 Andrew Birkett <adb@tardis.ed.ac.uk>
authorMiguel de Icaza <miguel@gnome.org>
Thu, 4 Jul 2002 20:27:32 +0000 (20:27 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Thu, 4 Jul 2002 20:27:32 +0000 (20:27 -0000)
* BitVector32.cs: Implemented 'set' section indexer.  Check for
overly large sections. Factored out some helper methods.

svn path=/trunk/mcs/; revision=5593

mcs/class/System/System.Collections.Specialized/BitVector32.cs
mcs/class/System/System.Collections.Specialized/ChangeLog

index 85cf502f6a5e26ac9a9260715f31e19c10371e19..2ff52d876eb40cfa97aecad8d734cff8758b303a 100644 (file)
@@ -4,6 +4,7 @@
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
 //   Lawrence Pit (loz@cable.a2000.nl)
+//   Andrew Birkett (adb@tardis.ed.ac.uk)
 //
 // (C) Ximian, Inc.  http://www.ximian.com
 //
@@ -128,25 +129,15 @@ namespace System.Collections.Specialized {
                        if (maxValue < 1)
                                throw new ArgumentException ("maxValue");
                        
-                       int newmask = (int) maxValue;
-                       int mask = 0x8000;
-                       while ((newmask & mask) == 0)
-                               mask >>= 1;
-                       while (mask > 0) {
-                               newmask |= mask;
-                               mask >>= 1;
-                       }
+                       int bit = HighestSetBit(maxValue) + 1;
+                       int mask = (1 << bit) - 1;
+                       int offset = previous.Offset + NumberOfSetBits (previous.Mask);
 
-                       short count = 0;
-                       int prev = previous.Mask;
-                       mask = 0x8000;
-                       while (mask > 0) {
-                               if ((prev & mask) != 0)
-                                       count++;
-                               mask >>= 1;
+                       if (offset + NumberOfSetBits (mask) > 32) {
+                               throw new ArgumentException ("Sections cannot exceed 32 bits in total");
                        }
 
-                       return new Section ((short) newmask, (short) (previous.Offset + count));
+                       return new Section ((short) mask, (short) offset);
                }
                
                public override bool Equals (object o)
@@ -179,5 +170,30 @@ namespace System.Collections.Specialized {
                        b.Append ('}');
                        return b.ToString ();
                }
+
+               // Private utilities
+               private static int NumberOfSetBits (int i) 
+               {
+                       int count = 0;
+                       for (int bit = 0; bit < 32; bit++) {
+                               int mask = 1 << bit;
+                               if ((i & mask) != 0) 
+                                       count++;
+                       }
+                       return count;
+               }
+
+               private static int HighestSetBit (int i) 
+               {
+                       for (int bit = 31; bit >= 0; bit--) {
+                               int mask = 1 << bit;
+                               if ((mask & i) != 0) {
+                                       return bit;
+                               }
+                       }
+
+                       return -1;
+               }
+
        }
 }
index 82aaab0ac0e42875a954a10eca5ee592e1c62557..ef8ba4ca343928f8dbfba95e219041b6d7e25ac2 100755 (executable)
@@ -1,3 +1,8 @@
+2002-06-24  Andrew Birkett <adb@tardis.ed.ac.uk>
+
+       * BitVector32.cs: Implemented 'set' section indexer.  Check for
+       overly large sections.  Factored out some helper methods. 
+
 2002-05-11  Lawrence Pit <loz@cable.a2000.nl>
 
        * NameValueCollection.AsStringArray: fixed ArgumentNullException bug.