UInt32Storage.cs 9.2 KB

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