Int64Storage.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //------------------------------------------------------------------------------
  2. // <copyright file="Int64Storage.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 Int64Storage : DataStorage {
  15. private const Int64 defaultValue = 0;
  16. private Int64[] values;
  17. internal Int64Storage(DataColumn column)
  18. : base(column, typeof(Int64), defaultValue, StorageType.Int64) {
  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 = 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. Int64 mean;
  48. checked {mean = (Int64)(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. Int64 min = Int64.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. Int64 max = Int64.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(Int64));
  119. }
  120. throw ExceptionBuilder.AggregateException(kind, DataType);
  121. }
  122. override public int Compare(int recordNo1, int recordNo2) {
  123. Int64 valueNo1 = values[recordNo1];
  124. Int64 valueNo2 = values[recordNo2];
  125. if (valueNo1 == defaultValue || valueNo2 == defaultValue) {
  126. int bitCheck = CompareBits(recordNo1, recordNo2);
  127. if (0 != bitCheck) {
  128. return bitCheck;
  129. }
  130. }
  131. //return valueNo1.CompareTo(valueNo2);
  132. return(valueNo1 < valueNo2 ? -1 : (valueNo1 > valueNo2 ? 1 : 0)); // similar to Int64.CompareTo(Int64)
  133. }
  134. public override int CompareValueTo(int recordNo, object value) {
  135. System.Diagnostics.Debug.Assert(0 <= recordNo, "Invalid record");
  136. System.Diagnostics.Debug.Assert(null != value, "null value");
  137. if (NullValue == value) {
  138. return (HasValue(recordNo) ? 1 : 0);
  139. }
  140. Int64 valueNo1 = values[recordNo];
  141. if ((defaultValue == valueNo1) && !HasValue(recordNo)) {
  142. return -1;
  143. }
  144. return valueNo1.CompareTo((Int64)value);
  145. //return(valueNo1 < valueNo2 ? -1 : (valueNo1 > valueNo2 ? 1 : 0)); // similar to Int64.CompareTo(Int64)
  146. }
  147. public override object ConvertValue(object value) {
  148. if (NullValue != value) {
  149. if (null != value) {
  150. value = ((IConvertible)value).ToInt64(FormatProvider);
  151. }
  152. else {
  153. value = NullValue;
  154. }
  155. }
  156. return value;
  157. }
  158. override public void Copy(int recordNo1, int recordNo2) {
  159. CopyBits(recordNo1, recordNo2);
  160. values[recordNo2] = values[recordNo1];
  161. }
  162. override public Object Get(int record) {
  163. Int64 value = values[record];
  164. if (value != defaultValue) {
  165. return value;
  166. }
  167. return GetBits(record);
  168. }
  169. override public void Set(int record, Object value) {
  170. System.Diagnostics.Debug.Assert(null != value, "null value");
  171. if (NullValue == value) {
  172. values[record] = defaultValue;
  173. SetNullBit(record, true);
  174. }
  175. else {
  176. values[record] = ((IConvertible)value).ToInt64(FormatProvider);
  177. SetNullBit(record, false);
  178. }
  179. }
  180. override public void SetCapacity(int capacity) {
  181. Int64[] newValues = new Int64[capacity];
  182. if (null != values) {
  183. Array.Copy(values, 0, newValues, 0, Math.Min(capacity, values.Length));
  184. }
  185. values = newValues;
  186. base.SetCapacity(capacity);
  187. }
  188. override public object ConvertXmlToObject(string s) {
  189. return XmlConvert.ToInt64(s);
  190. }
  191. override public string ConvertObjectToXml(object value) {
  192. return XmlConvert.ToString((Int64)value);
  193. }
  194. override protected object GetEmptyStorage(int recordCount) {
  195. return new Int64[recordCount];
  196. }
  197. override protected void CopyValue(int record, object store, BitArray nullbits, int storeIndex) {
  198. Int64[] typedStore = (Int64[]) store;
  199. typedStore[storeIndex] = values[record];
  200. nullbits.Set(storeIndex, !HasValue(record));
  201. }
  202. override protected void SetStorage(object store, BitArray nullbits) {
  203. values = (Int64[]) store;
  204. SetNullStorage(nullbits);
  205. }
  206. }
  207. }