Author: alanmc
Date: 2006-11-05 08:03:51 -0500 (Sun, 05 Nov 2006)
New Revision: 67372

Modified:
   trunk/bitsharp/src/MonoTorrent.Client/BitField.cs
   trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
Log:
Updated BitField to keep a count of the number of bits that are true. This 
means i can calculate progress with one division and one multiplication, 
instead of a 1000+ item loop. Much much faster ;) Also allows for easy checking 
for when i should enable end-game mode

Modified: trunk/bitsharp/src/MonoTorrent.Client/BitField.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/BitField.cs   2006-11-05 12:55:35 UTC 
(rev 67371)
+++ trunk/bitsharp/src/MonoTorrent.Client/BitField.cs   2006-11-05 13:03:51 UTC 
(rev 67372)
@@ -59,6 +59,16 @@
             get { return this.array; }
         }
         private int[] array;
+
+
+        /// <summary>
+        /// Returns the number of elements in the array which are "true" (i.e. 
the bit is set to 1)
+        /// </summary>
+        public int TrueCount
+        {
+            get { return this.trueCount; }
+        }
+        private int trueCount;
         #endregion
 
 
@@ -211,9 +221,18 @@
                 throw new ArgumentOutOfRangeException("index");
 
             if (value)
+            {
+                if ((this.array[index >> 5] & (1 << (index & 31))) == 0)// If 
it's not already true
+                    trueCount++;                                        // 
Increase true count
                 this.array[index >> 5] |= (1 << (index & 31));
+
+            }
             else
+            {
+                if ((this.array[index >> 5] & (1 << (index & 31))) != 0)// If 
it's not already false
+                    trueCount--;                                        // 
Decrease true count
                 this.array[index >> 5] &= ~(1 << (index & 31));
+            }
         }
 
 
@@ -235,6 +254,8 @@
             int end = ((int)((this.length + 31) / 32)) * 32;
             for (int i = this.length; i < end; ++i)
                 this.array[i >> 5] &= ~(1 << (i & 31));
+
+            this.trueCount = this.length;
         }
         
 
@@ -354,14 +375,20 @@
 #warning Check the remaining bits in the last byte to make sure they're 0. use 
the length parameter
         internal void FromArray(byte[] buffer, int offset, int length)
         {
+            bool temp;
             if (buffer == null)
                 throw new ArgumentNullException("buffer");
 
+            this.trueCount = 0;
             byte p = 128;
 
             for (int i = 0; i < this.length; i++)
             {
-                this[i] = ((buffer[offset] & p) != 0);
+                temp = ((buffer[offset] & p) != 0);
+                if (temp)
+                    this.trueCount++;
+
+                this[i] = temp;
                 p >>= 1;
 
                 if (p != 0)
@@ -374,7 +401,11 @@
 
         internal void FromArray(int[] array)
         {
+            this.trueCount = 0;
             this.array = array;
+            for (int i = 0; i < this.length; i++)
+                if (this.Get(i))
+                    trueCount++;
         }
 
         /// <summary>

Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs    
2006-11-05 12:55:35 UTC (rev 67371)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs    
2006-11-05 13:03:51 UTC (rev 67372)
@@ -796,13 +796,7 @@
         /// </summary>
         public double Progress()
         {
-            double complete = 0;
-            for (int i = 0; i < this.PieceManager.MyBitField.Length; i++)
-                if (this.PieceManager.MyBitField[i])
-                    complete++;
-
-            complete -= this.pieceManager.CurrentRequestCount();
-            return (complete * 100.0 / this.PieceManager.MyBitField.Length);
+            return ((this.pieceManager.MyBitField.TrueCount * 100.0) / 
this.pieceManager.MyBitField.Length);
         }
 
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to