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