Aggregates.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. namespace FastReport.AdvMatrix
  5. {
  6. /// <summary>
  7. /// Hold the list of registered aggregate functions.
  8. /// </summary>
  9. public static class Aggregates
  10. {
  11. private static Dictionary<string, Type> registeredAggregates = new Dictionary<string, Type>();
  12. private static List<string> names = new List<string>();
  13. /// <summary>
  14. /// Gets names of aggregates registered.
  15. /// </summary>
  16. public static string[] Names
  17. {
  18. get { return names.ToArray(); }
  19. }
  20. /// <summary>
  21. /// Registers the aggregate function.
  22. /// </summary>
  23. /// <param name="name">The function name.</param>
  24. /// <param name="aggregateType">The type of aggregate class.</param>
  25. public static void Register(string name, Type aggregateType)
  26. {
  27. if (!aggregateType.IsSubclassOf(typeof(AggregateBase)))
  28. throw new Exception("The 'aggregateType' parameter must be of AggregateBase type.");
  29. if (!registeredAggregates.ContainsKey(name))
  30. {
  31. registeredAggregates[name] = aggregateType;
  32. names.Add(name);
  33. }
  34. }
  35. /// <summary>
  36. /// Returns an aggregate with specified name.
  37. /// </summary>
  38. /// <param name="name">The name of aggregate function.</param>
  39. /// <returns>The aggregate class type.</returns>
  40. public static Type Find(string name)
  41. {
  42. Type aggregateType;
  43. if (registeredAggregates.TryGetValue(name, out aggregateType))
  44. return aggregateType;
  45. return null;
  46. }
  47. static Aggregates()
  48. {
  49. Register("Sum", typeof(SumAggregate));
  50. Register("Avg", typeof(AvgAggregate));
  51. Register("Min", typeof(MinAggregate));
  52. Register("Max", typeof(MaxAggregate));
  53. Register("Count", typeof(CountAggregate));
  54. Register("CountDistinct", typeof(CountDistinctAggregate));
  55. Register("StDev", typeof(StDevAggregate));
  56. Register("StDevP", typeof(StDevPAggregate));
  57. Register("Var", typeof(VarAggregate));
  58. Register("VarP", typeof(VarPAggregate));
  59. Register("First", typeof(FirstAggregate));
  60. Register("Last", typeof(LastAggregate));
  61. Register("ValuesList", typeof(ValuesListAggregate));
  62. }
  63. }
  64. /// <summary>
  65. /// Represents base class for AdvMatrixObject aggregates.
  66. /// </summary>
  67. public abstract class AggregateBase
  68. {
  69. /// <summary>
  70. /// Adds a value to aggregate.
  71. /// </summary>
  72. /// <param name="value">The value.</param>
  73. public abstract void AddValue(object value);
  74. /// <summary>
  75. /// Gets aggregate value.
  76. /// </summary>
  77. /// <returns>Aggregate value.</returns>
  78. public abstract object GetValue();
  79. /// <summary>
  80. /// Merges value from another, similar aggregate.
  81. /// </summary>
  82. /// <param name="aggr">Aggregate to merge value from.</param>
  83. public abstract void Merge(AggregateBase aggr);
  84. }
  85. /// <summary>
  86. /// Represents the "Sum" aggregate.
  87. /// </summary>
  88. public class SumAggregate : AggregateBase
  89. {
  90. private object value;
  91. /// <inheritdoc/>
  92. public override void AddValue(object value)
  93. {
  94. if (this.value == null)
  95. this.value = value;
  96. else
  97. this.value = (dynamic)this.value + (dynamic)value;
  98. }
  99. /// <inheritdoc/>
  100. public override object GetValue()
  101. {
  102. return value;
  103. }
  104. /// <inheritdoc/>
  105. public override void Merge(AggregateBase aggr)
  106. {
  107. SumAggregate a = aggr as SumAggregate;
  108. if (a != null)
  109. {
  110. AddValue(a.value);
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// Represents the "Avg" aggregate.
  116. /// </summary>
  117. public class AvgAggregate : AggregateBase
  118. {
  119. private object value;
  120. private int count;
  121. /// <inheritdoc/>
  122. public override void AddValue(object value)
  123. {
  124. if (this.value == null)
  125. this.value = value;
  126. else
  127. this.value = (dynamic)this.value + (dynamic)value;
  128. count++;
  129. }
  130. /// <inheritdoc/>
  131. public override object GetValue()
  132. {
  133. if (count == 0)
  134. return null;
  135. if (value is float || value is double || value is decimal)
  136. return (dynamic)value / count;
  137. return (dynamic)value / (double)count;
  138. }
  139. /// <inheritdoc/>
  140. public override void Merge(AggregateBase aggr)
  141. {
  142. AvgAggregate a = aggr as AvgAggregate;
  143. if (a != null && a.value != null)
  144. {
  145. value = (dynamic)value + (dynamic)a.value;
  146. count += a.count;
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// Represents the "Min" aggregate.
  152. /// </summary>
  153. public class MinAggregate : AggregateBase
  154. {
  155. private object value;
  156. /// <inheritdoc/>
  157. public override void AddValue(object value)
  158. {
  159. if (this.value == null)
  160. this.value = value;
  161. else
  162. {
  163. if ((dynamic)value < (dynamic)this.value)
  164. this.value = value;
  165. }
  166. }
  167. /// <inheritdoc/>
  168. public override object GetValue()
  169. {
  170. return value;
  171. }
  172. /// <inheritdoc/>
  173. public override void Merge(AggregateBase aggr)
  174. {
  175. MinAggregate a = aggr as MinAggregate;
  176. if (a != null)
  177. {
  178. AddValue(a.value);
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// Represents the "Max" aggregate.
  184. /// </summary>
  185. public class MaxAggregate : AggregateBase
  186. {
  187. private object value;
  188. /// <inheritdoc/>
  189. public override void AddValue(object value)
  190. {
  191. if (this.value == null)
  192. this.value = value;
  193. else
  194. {
  195. if ((dynamic)value > (dynamic)this.value)
  196. this.value = value;
  197. }
  198. }
  199. /// <inheritdoc/>
  200. public override object GetValue()
  201. {
  202. return value;
  203. }
  204. /// <inheritdoc/>
  205. public override void Merge(AggregateBase aggr)
  206. {
  207. MaxAggregate a = aggr as MaxAggregate;
  208. if (a != null)
  209. {
  210. AddValue(a.value);
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// Represents the "Count" aggregate.
  216. /// </summary>
  217. public class CountAggregate : AggregateBase
  218. {
  219. private int count;
  220. /// <inheritdoc/>
  221. public override void AddValue(object value)
  222. {
  223. count++;
  224. }
  225. /// <inheritdoc/>
  226. public override object GetValue()
  227. {
  228. return count;
  229. }
  230. /// <inheritdoc/>
  231. public override void Merge(AggregateBase aggr)
  232. {
  233. CountAggregate a = aggr as CountAggregate;
  234. if (a != null)
  235. {
  236. count += a.count;
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// Represents the "CountDistinct" aggregate.
  242. /// </summary>
  243. public class CountDistinctAggregate : AggregateBase
  244. {
  245. private Hashtable values = new Hashtable();
  246. /// <inheritdoc/>
  247. public override void AddValue(object value)
  248. {
  249. values[value] = 1;
  250. }
  251. /// <inheritdoc/>
  252. public override object GetValue()
  253. {
  254. return values.Keys.Count;
  255. }
  256. /// <inheritdoc/>
  257. public override void Merge(AggregateBase aggr)
  258. {
  259. CountDistinctAggregate a = aggr as CountDistinctAggregate;
  260. if (a != null)
  261. {
  262. foreach (DictionaryEntry e in a.values)
  263. {
  264. values[e.Key] = 1;
  265. }
  266. }
  267. }
  268. }
  269. /// <summary>
  270. /// Represents the "Var" aggregate.
  271. /// </summary>
  272. public class VarAggregate : AggregateBase
  273. {
  274. private List<dynamic> values = new List<dynamic>();
  275. /// <inheritdoc/>
  276. public override void AddValue(object value)
  277. {
  278. values.Add(value);
  279. }
  280. /// <inheritdoc/>
  281. public override object GetValue()
  282. {
  283. if (values.Count < 2)
  284. return null;
  285. // calculate average
  286. double average = 0;
  287. foreach (dynamic v in values)
  288. {
  289. average += (double)v;
  290. }
  291. average = average / values.Count;
  292. // sum each difference
  293. double diff = 0;
  294. foreach (dynamic v in values)
  295. {
  296. diff += Math.Pow((double)v - average, 2);
  297. }
  298. return diff / (values.Count - 1);
  299. }
  300. /// <inheritdoc/>
  301. public override void Merge(AggregateBase aggr)
  302. {
  303. VarAggregate a = aggr as VarAggregate;
  304. if (a != null)
  305. {
  306. values.AddRange(a.values);
  307. }
  308. }
  309. }
  310. /// <summary>
  311. /// Represents the "VarP" aggregate.
  312. /// </summary>
  313. public class VarPAggregate : AggregateBase
  314. {
  315. private List<dynamic> values = new List<dynamic>();
  316. /// <inheritdoc/>
  317. public override void AddValue(object value)
  318. {
  319. values.Add(value);
  320. }
  321. /// <inheritdoc/>
  322. public override object GetValue()
  323. {
  324. if (values.Count == 0)
  325. return null;
  326. // calculate average
  327. double average = 0;
  328. foreach (dynamic v in values)
  329. {
  330. average += (double)v;
  331. }
  332. average = average / values.Count;
  333. // sum each difference
  334. double diff = 0;
  335. foreach (dynamic v in values)
  336. {
  337. diff += Math.Pow((double)v - average, 2);
  338. }
  339. return diff / values.Count;
  340. }
  341. /// <inheritdoc/>
  342. public override void Merge(AggregateBase aggr)
  343. {
  344. VarPAggregate a = aggr as VarPAggregate;
  345. if (a != null)
  346. {
  347. values.AddRange(a.values);
  348. }
  349. }
  350. }
  351. /// <summary>
  352. /// Represents the "StDev" aggregate.
  353. /// </summary>
  354. public class StDevAggregate : VarAggregate
  355. {
  356. /// <inheritdoc/>
  357. public override object GetValue()
  358. {
  359. return Math.Sqrt((double)base.GetValue());
  360. }
  361. }
  362. /// <summary>
  363. /// Represents the "StDevP" aggregate.
  364. /// </summary>
  365. public class StDevPAggregate : VarPAggregate
  366. {
  367. /// <inheritdoc/>
  368. public override object GetValue()
  369. {
  370. return Math.Sqrt((double)base.GetValue());
  371. }
  372. }
  373. /// <summary>
  374. /// Represents the "First" aggregate.
  375. /// </summary>
  376. public class FirstAggregate : AggregateBase
  377. {
  378. private object value;
  379. /// <inheritdoc/>
  380. public override void AddValue(object value)
  381. {
  382. if (this.value == null)
  383. this.value = value;
  384. }
  385. /// <inheritdoc/>
  386. public override object GetValue()
  387. {
  388. return value;
  389. }
  390. /// <inheritdoc/>
  391. public override void Merge(AggregateBase aggr)
  392. {
  393. FirstAggregate a = aggr as FirstAggregate;
  394. if (a != null)
  395. {
  396. AddValue(a.value);
  397. }
  398. }
  399. }
  400. /// <summary>
  401. /// Represents the "Last" aggregate.
  402. /// </summary>
  403. public class LastAggregate : AggregateBase
  404. {
  405. private object value;
  406. /// <inheritdoc/>
  407. public override void AddValue(object value)
  408. {
  409. this.value = value;
  410. }
  411. /// <inheritdoc/>
  412. public override object GetValue()
  413. {
  414. return value;
  415. }
  416. /// <inheritdoc/>
  417. public override void Merge(AggregateBase aggr)
  418. {
  419. LastAggregate a = aggr as LastAggregate;
  420. if (a != null)
  421. {
  422. AddValue(a.value);
  423. }
  424. }
  425. }
  426. /// <summary>
  427. /// Represents the "ValuesList" aggregate.
  428. /// </summary>
  429. public class ValuesListAggregate : UserAggregate
  430. {
  431. }
  432. /// <summary>
  433. /// Represents the "User" aggregate.
  434. /// </summary>
  435. public class UserAggregate : AggregateBase
  436. {
  437. private List<dynamic> values = new List<dynamic>();
  438. /// <inheritdoc/>
  439. public override void AddValue(object value)
  440. {
  441. values.Add(value);
  442. }
  443. /// <inheritdoc/>
  444. public override object GetValue()
  445. {
  446. return values;
  447. }
  448. /// <inheritdoc/>
  449. public override void Merge(AggregateBase aggr)
  450. {
  451. UserAggregate a = aggr as UserAggregate;
  452. if (a != null)
  453. {
  454. values.AddRange(a.values);
  455. }
  456. }
  457. }
  458. }