/3.x/WBFSe3/Wbfs/WbfsUsage.cs

# · C# · 194 lines · 121 code · 29 blank · 44 comment · 16 complexity · 5e22e512c43d19271145736aecf63c2d MD5 · raw file

  1. //-----------------------------------------------------------------------------------------------------------
  2. // WBFSSync Project by Omega Frost
  3. // http://wbfssync.codeplex.com/
  4. //
  5. // WBFSSync is Licensed under the terms of the
  6. // Microsoft Reciprocal License (Ms-RL)
  7. //-----------------------------------------------------------------------------------------------------------
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. namespace WBFSe3.Wbfs
  13. {
  14. public class WbfsUsage
  15. {
  16. //------------------------------------------------------------------------------------------
  17. // Structs
  18. //------------------------------------------------------------------------------------------
  19. public struct REGION
  20. {
  21. public int Position;
  22. public bool Value;
  23. }
  24. //------------------------------------------------------------------------------------------
  25. // Variables
  26. //------------------------------------------------------------------------------------------
  27. private REGION[] rRegions;
  28. private int iCount;
  29. //------------------------------------------------------------------------------------------
  30. // Properties
  31. //------------------------------------------------------------------------------------------
  32. public Boolean this[int index]
  33. {
  34. get
  35. {
  36. int i = 0;
  37. while (index >= rRegions[i + 1].Position) i++;
  38. return rRegions[i].Value;
  39. }
  40. }
  41. public int Count
  42. {
  43. get
  44. {
  45. return iCount;
  46. }
  47. }
  48. //------------------------------------------------------------------------------------------
  49. // Routines
  50. //------------------------------------------------------------------------------------------
  51. //------------------------------------------------------------------------------------------
  52. // Constructor
  53. //------------------------------------------------------------------------------------------
  54. private WbfsUsage(WbfsUsage f)
  55. {
  56. rRegions = new REGION[f.rRegions.Length];
  57. f.rRegions.CopyTo(rRegions, 0);
  58. iCount = f.iCount;
  59. }
  60. //------------------------------------------------------------------------------------------
  61. // Constructor
  62. //------------------------------------------------------------------------------------------
  63. public WbfsUsage(Boolean[] bits)
  64. {
  65. iCount = bits.Length;
  66. // Conta o número de regiões
  67. int i, count = 1;
  68. for (i = 1; i < bits.Length; i++)
  69. {
  70. if (bits[i - 1] != bits[i])
  71. count++;
  72. }
  73. // Cria as regiões
  74. rRegions = new REGION[count + 1];
  75. // Carrega as regiões
  76. bool state = !bits[0];
  77. int j = 0;
  78. for (i = 0; i < bits.Length; i++)
  79. {
  80. if (state != bits[i])
  81. {
  82. state = bits[i];
  83. rRegions[j].Position = i;
  84. rRegions[j].Value = state;
  85. j++;
  86. }
  87. }
  88. rRegions[count].Position = i;
  89. rRegions[count].Value = false;
  90. }
  91. //------------------------------------------------------------------------------------------
  92. // Duplicates the object
  93. //------------------------------------------------------------------------------------------
  94. public WbfsUsage Duplicate()
  95. {
  96. return new WbfsUsage(this);
  97. }
  98. //------------------------------------------------------------------------------------------
  99. // Copy the regions
  100. //------------------------------------------------------------------------------------------
  101. public REGION[] GetRegions()
  102. {
  103. REGION[] r = new REGION[rRegions.Length];
  104. rRegions.CopyTo(r, 0);
  105. return r;
  106. }
  107. //------------------------------------------------------------------------------------------
  108. // Count used sectors
  109. //------------------------------------------------------------------------------------------
  110. public int CountOnes()
  111. {
  112. int used = 0;
  113. int i;
  114. for (i = 0; i < rRegions.Length - 1; i++)
  115. {
  116. if (rRegions[i].Value)
  117. {
  118. used += rRegions[i + 1].Position -
  119. rRegions[i].Position;
  120. }
  121. }
  122. if (rRegions[i].Value)
  123. {
  124. used += iCount - rRegions[i].Position;
  125. }
  126. return used;
  127. }
  128. //------------------------------------------------------------------------------------------
  129. // Count free sectors
  130. //------------------------------------------------------------------------------------------
  131. public int CountZeroes()
  132. {
  133. int free = 0;
  134. int i;
  135. for (i = 0; i < rRegions.Length - 1; i++)
  136. {
  137. if (!rRegions[i].Value)
  138. {
  139. free += rRegions[i + 1].Position -
  140. rRegions[i].Position;
  141. }
  142. }
  143. if (!rRegions[i].Value)
  144. {
  145. free += iCount - rRegions[i].Position;
  146. }
  147. return free;
  148. }
  149. //------------------------------------------------------------------------------------------
  150. // Get the last used sector
  151. //------------------------------------------------------------------------------------------
  152. public int GetLastOne()
  153. {
  154. if (rRegions[rRegions.Length - 1].Value)
  155. return iCount - 1;
  156. for (int i = rRegions.Length - 2; i >= 0; i--)
  157. {
  158. if (rRegions[i].Value)
  159. return rRegions[i + 1].Position - 1;
  160. }
  161. //Error
  162. return -1;
  163. }
  164. }
  165. }