Int32Storage.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //------------------------------------------------------------------------------
  2. // <copyright file="Int32Storage.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. // <owner current="true" primary="false">Microsoft</owner>
  7. // <owner current="false" primary="false">Microsoft</owner>
  8. //------------------------------------------------------------------------------
  9. namespace System.Data.Common {
  10. using System;
  11. using System.Xml;
  12. using System.Data.SqlTypes;
  13. using System.Collections;
  14. internal sealed class Int32Storage : DataStorage {
  15. private const Int32 defaultValue = 0; // Convert.ToInt32(null)
  16. private Int32[] values;
  17. internal Int32Storage(DataColumn column)
  18. : base(column, typeof(Int32), defaultValue, StorageType.Int32) {
  19. }
  20. override public Object Aggregate(int[] records, AggregateType kind) {
  21. bool hasData = false;
  22. try {
  23. switch (kind) {
  24. case AggregateType.Sum:
  25. Int64 sum = 0;
  26. foreach (int record in records) {
  27. if (HasValue(record)) {
  28. checked { sum += values[record];}
  29. hasData = true;
  30. }
  31. }
  32. if (hasData) {
  33. return sum;
  34. }
  35. return NullValue;
  36. case AggregateType.Mean:
  37. Int64 meanSum = 0;
  38. int meanCount = 0;
  39. foreach (int record in records) {
  40. if (HasValue(record)) {
  41. checked { meanSum += (Int64)values[record];}
  42. meanCount++;
  43. hasData = true;
  44. }
  45. }
  46. if (hasData) {
  47. Int32 mean;
  48. checked {mean = (Int32)(meanSum / meanCount);}
  49. return mean;
  50. }
  51. return NullValue;
  52. case AggregateType.Var:
  53. case AggregateType.StDev:
  54. int count = 0;
  55. double var = 0.0f;
  56. double prec = 0.0f;
  57. double dsum = 0.0f;
  58. double sqrsum = 0.0f;
  59. foreach (int record in records) {
  60. if (HasValue(record)) {
  61. dsum += (double)values[record];
  62. sqrsum += (double)values[record]*(double)values[record];
  63. count++;
  64. }
  65. }
  66. if (count > 1) {
  67. var = ((double)count * sqrsum - (dsum * dsum));
  68. prec = var / (dsum * dsum);
  69. // we are dealing with the risk of a cancellation error
  70. // double is guaranteed only for 15 digits so a difference
  71. // with a result less than 1e-15 should be considered as zero
  72. if ((prec < 1e-15) || (var <0))
  73. var = 0;
  74. else
  75. var = var / (count * (count -1));
  76. if (kind == AggregateType.StDev) {
  77. return Math.Sqrt(var);
  78. }
  79. return var;
  80. }
  81. return NullValue;
  82. case AggregateType.Min:
  83. Int32 min = Int32.MaxValue;
  84. for (int i = 0; i < records.Length; i++) {
  85. int record = records[i];
  86. if (HasValue(record)) {
  87. min=Math.Min(values[record], min);
  88. hasData = true;
  89. }
  90. }
  91. if (hasData) {
  92. return min;
  93. }
  94. return NullValue;
  95. case AggregateType.Max:
  96. Int32 max = Int32.MinValue;
  97. for (int i = 0; i < records.Length; i++) {
  98. int record = records[i];
  99. if (HasValue(record)) {
  100. max=Math.Max(values[record], max);
  101. hasData = true;
  102. }
  103. }
  104. if (hasData) {
  105. return max;
  106. }
  107. return NullValue;
  108. case AggregateType.First:
  109. if (records.Length > 0) {
  110. return values[records[0]];
  111. }
  112. return null;
  113. case AggregateType.Count:
  114. count = 0;
  115. for (int i = 0; i < records.Length; i++) {
  116. if (HasValue(records[i])) {
  117. count++;
  118. }
  119. }
  120. return count;
  121. }
  122. }
  123. catch (OverflowException) {
  124. throw ExprException.Overflow(typeof(Int32));
  125. }
  126. throw ExceptionBuilder.AggregateException(kind, DataType);
  127. }
  128. override public int Compare(int recordNo1, int recordNo2) {
  129. Int32 valueNo1 = values[recordNo1];
  130. Int32 valueNo2 = values[recordNo2];
  131. if (valueNo1 == defaultValue || valueNo2 == defaultValue) {
  132. int bitCheck = CompareBits(recordNo1, recordNo2);
  133. if (0 != bitCheck) {
  134. return bitCheck;
  135. }
  136. }
  137. //return valueNo1.CompareTo(valueNo2);
  138. return(valueNo1 < valueNo2 ? -1 : (valueNo1 > valueNo2 ? 1 : 0)); // similar to Int32.CompareTo(Int32)
  139. }
  140. public override int CompareValueTo(int recordNo, object value) {
  141. System.Diagnostics.Debug.Assert(0 <= recordNo, "Invalid record");
  142. System.Diagnostics.Debug.Assert(null != value, "null value");
  143. if (NullValue == value) {
  144. return (HasValue(recordNo) ? 1 : 0);
  145. }
  146. Int32 valueNo1 = values[recordNo];
  147. if ((defaultValue == valueNo1) && !HasValue(recordNo)) {
  148. return -1;
  149. }
  150. return valueNo1.CompareTo((Int32)value);
  151. //return(valueNo1 < valueNo2 ? -1 : (valueNo1 > valueNo2 ? 1 : 0)); // similar to Int32.CompareTo(Int32)
  152. }
  153. public override object ConvertValue(object value) {
  154. if (NullValue != value) {
  155. if (null != value) {
  156. value = ((IConvertible)value).ToInt32(FormatProvider);
  157. }
  158. else {
  159. value = NullValue;
  160. }
  161. }
  162. return value;
  163. }
  164. override public void Copy(int recordNo1, int recordNo2) {
  165. CopyBits(recordNo1, recordNo2);
  166. values[recordNo2] = values[recordNo1];
  167. }
  168. override public Object Get(int record) {
  169. Int32 value = values[record];
  170. if (value != Int32Storage.defaultValue) {
  171. return value;
  172. }
  173. return GetBits(record);
  174. }
  175. override public void Set(int record, Object value) {
  176. System.Diagnostics.Debug.Assert(null != value, "null value");
  177. if (NullValue == value) {
  178. values[record] = defaultValue;
  179. SetNullBit(record, true);
  180. }
  181. else {
  182. values[record] = ((IConvertible)value).ToInt32(FormatProvider);
  183. SetNullBit(record, false); // WebData 105962
  184. }
  185. }
  186. override public void SetCapacity(int capacity) {
  187. Int32[] newValues = new Int32[capacity];
  188. if (null != values) {
  189. Array.Copy(values, 0, newValues, 0, Math.Min(capacity, values.Length));
  190. }
  191. values = newValues;
  192. base.SetCapacity(capacity);
  193. }
  194. override public object ConvertXmlToObject(string s) {
  195. return XmlConvert.ToInt32(s);
  196. }
  197. override public string ConvertObjectToXml(object value) {
  198. return XmlConvert.ToString((Int32)value);
  199. }
  200. override protected object GetEmptyStorage(int recordCount) {
  201. return new Int32[recordCount];
  202. }
  203. override protected void CopyValue(int record, object store, BitArray nullbits, int storeIndex) {
  204. Int32[] typedStore = (Int32[]) store;
  205. typedStore[storeIndex] = values[record];
  206. nullbits.Set(storeIndex, !HasValue(record));
  207. }
  208. override protected void SetStorage(object store, BitArray nullbits) {
  209. values = (Int32[]) store;
  210. SetNullStorage(nullbits);
  211. }
  212. }
  213. }