UInt64Storage.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //------------------------------------------------------------------------------
  2. // <copyright file="UInt64Storage.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 UInt64Storage : DataStorage {
  15. private const UInt64 defaultValue = UInt64.MinValue;
  16. private UInt64[] values;
  17. public UInt64Storage(DataColumn column)
  18. : base(column, typeof(UInt64), defaultValue, StorageType.UInt64) {
  19. }
  20. override public Object Aggregate(int[] records, AggregateType kind) {
  21. bool hasData = false;
  22. try {
  23. switch (kind) {
  24. case AggregateType.Sum:
  25. UInt64 sum = defaultValue;
  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. Decimal meanSum = (Decimal)defaultValue;
  38. int meanCount = 0;
  39. foreach (int record in records) {
  40. if (HasValue(record)) {
  41. checked { meanSum += (Decimal)values[record];}
  42. meanCount++;
  43. hasData = true;
  44. }
  45. }
  46. if (hasData) {
  47. UInt64 mean;
  48. checked {mean = (UInt64)(Decimal)(meanSum / (Decimal)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. UInt64 min = UInt64.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. UInt64 max = UInt64.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. return base.Aggregate(records, kind);
  115. }
  116. }
  117. catch (OverflowException) {
  118. throw ExprException.Overflow(typeof(UInt64));
  119. }
  120. throw ExceptionBuilder.AggregateException(kind, DataType);
  121. }
  122. override public int Compare(int recordNo1, int recordNo2) {
  123. UInt64 valueNo1 = values[recordNo1];
  124. UInt64 valueNo2 = values[recordNo2];
  125. if (valueNo1.Equals(defaultValue) || valueNo2.Equals(defaultValue)) {
  126. int bitCheck = CompareBits(recordNo1, recordNo2);
  127. if (0 != bitCheck)
  128. return bitCheck;
  129. }
  130. //return valueNo1.CompareTo(valueNo2);
  131. return(valueNo1 < valueNo2 ? -1 : (valueNo1 > valueNo2 ? 1 : 0)); // similar to UInt64.CompareTo(UInt64)
  132. }
  133. public override int CompareValueTo(int recordNo, object value) {
  134. System.Diagnostics.Debug.Assert(0 <= recordNo, "Invalid record");
  135. System.Diagnostics.Debug.Assert(null != value, "null value");
  136. if (NullValue == value) {
  137. return (HasValue(recordNo) ? 1 : 0);
  138. }
  139. UInt64 valueNo1 = values[recordNo];
  140. if ((defaultValue == valueNo1) && !HasValue(recordNo)) {
  141. return -1;
  142. }
  143. return valueNo1.CompareTo((UInt64)value);
  144. //return(valueNo1 < valueNo2 ? -1 : (valueNo1 > valueNo2 ? 1 : 0)); // similar to UInt64.CompareTo(UInt64)
  145. }
  146. public override object ConvertValue(object value) {
  147. if (NullValue != value) {
  148. if (null != value) {
  149. value = ((IConvertible)value).ToUInt64(FormatProvider);
  150. }
  151. else {
  152. value = NullValue;
  153. }
  154. }
  155. return value;
  156. }
  157. override public void Copy(int recordNo1, int recordNo2) {
  158. CopyBits(recordNo1, recordNo2);
  159. values[recordNo2] = values[recordNo1];
  160. }
  161. override public Object Get(int record) {
  162. UInt64 value = values[record];
  163. if (!value.Equals(defaultValue)) {
  164. return value;
  165. }
  166. return GetBits(record);
  167. }
  168. override public void Set(int record, Object value) {
  169. System.Diagnostics.Debug.Assert(null != value, "null value");
  170. if (NullValue == value) {
  171. values[record] = defaultValue;
  172. SetNullBit(record, true);
  173. }
  174. else {
  175. values[record] = ((IConvertible)value).ToUInt64(FormatProvider);
  176. SetNullBit(record, false);
  177. }
  178. }
  179. override public void SetCapacity(int capacity) {
  180. UInt64[] newValues = new UInt64[capacity];
  181. if (null != values) {
  182. Array.Copy(values, 0, newValues, 0, Math.Min(capacity, values.Length));
  183. }
  184. values = newValues;
  185. base.SetCapacity(capacity);
  186. }
  187. override public object ConvertXmlToObject(string s) {
  188. return XmlConvert.ToUInt64(s);
  189. }
  190. override public string ConvertObjectToXml(object value) {
  191. return XmlConvert.ToString((UInt64)value);
  192. }
  193. override protected object GetEmptyStorage(int recordCount) {
  194. return new UInt64[recordCount];
  195. }
  196. override protected void CopyValue(int record, object store, BitArray nullbits, int storeIndex) {
  197. UInt64[] typedStore = (UInt64[]) store;
  198. typedStore[storeIndex] = values[record];
  199. nullbits.Set(storeIndex, !HasValue(record));
  200. }
  201. override protected void SetStorage(object store, BitArray nullbits) {
  202. values = (UInt64[]) store;
  203. SetNullStorage(nullbits);
  204. }
  205. }
  206. }