[Roam-commit] SF.net SVN: roam: [90] Trunk/Source
Status: Alpha
Brought to you by:
jonfroehlich
|
From: <jon...@us...> - 2007-08-31 14:46:52
|
Revision: 90
http://roam.svn.sourceforge.net/roam/?rev=90&view=rev
Author: jonfroehlich
Date: 2007-08-26 20:36:45 -0700 (Sun, 26 Aug 2007)
Log Message:
-----------
Added SummaryStatistics which is a container class storing stats like mean, median, mode, variance, std dev, etc.
updated math utils to support stats of ICollection<int> in addition to ICollection<long> and ICollection<double>... I wish there was a way to do this in a OO manner through a INumber interface or something.
Modified Paths:
--------------
Trunk/Source/Roam.Common/Roam.Common.csproj
Trunk/Source/Roam.Common/Utils/MathUtils.cs
Trunk/Source/Roam.Test/Roam.Test.csproj
Trunk/Source/Roam.Test/Sandbox/RegisterHotKeyTest.cs
Trunk/Source/Roam.Test/Utils/KeySnifferTest.Designer.cs
Trunk/Source/Roam.Test/Utils/KeySnifferTest.cs
Trunk/Source/Roam.suo
Added Paths:
-----------
Trunk/Source/Roam.Common/Utils/SummaryStatistics.cs
Modified: Trunk/Source/Roam.Common/Roam.Common.csproj
===================================================================
--- Trunk/Source/Roam.Common/Roam.Common.csproj 2007-07-31 08:23:27 UTC (rev 89)
+++ Trunk/Source/Roam.Common/Roam.Common.csproj 2007-08-27 03:36:45 UTC (rev 90)
@@ -79,6 +79,7 @@
<Compile Include="Utils\EnumUtils.cs" />
<Compile Include="Utils\ErrorUtils.cs" />
<Compile Include="Utils\ImagingUtils.cs" />
+ <Compile Include="Utils\SummaryStatistics.cs" />
<Compile Include="Utils\Vector2.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResourceNotFoundException.cs" />
Modified: Trunk/Source/Roam.Common/Utils/MathUtils.cs
===================================================================
--- Trunk/Source/Roam.Common/Utils/MathUtils.cs 2007-07-31 08:23:27 UTC (rev 89)
+++ Trunk/Source/Roam.Common/Utils/MathUtils.cs 2007-08-27 03:36:45 UTC (rev 90)
@@ -16,6 +16,7 @@
using System.Collections.Generic;
using System.Text;
using System.Drawing;
+using System.Collections;
namespace Roam.Common.Utils
{
@@ -28,43 +29,51 @@
/// <author>Jon Froehlich</author>
public static class MathUtils
{
- public static double GetEuclideanDistance(Point ptStart, Point ptEnd)
+ public static double Variance(ICollection<double> values)
{
- int xDiff = ptEnd.X - ptStart.X;
- int yDiff = ptEnd.Y - ptStart.Y;
- return Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
- }
+ if (values.Count <= 0) { throw new ArgumentException("The values.Length must be greater than 0", "values"); }
+ else if (values.Count == 1) { return 0; }
- /// <summary>
- /// Gets the angle in radians between these two points and the Vector (1,0)
- /// </summary>
- /// <param name="ptStart"></param>
- /// <param name="ptEnd"></param>
- /// <returns></returns>
- public static double GetAngle(Point ptStart, Point ptEnd)
- {
- Vector2 v = new Vector2(1,0);
- Vector2 w = new Vector2(ptEnd.X - ptStart.X, ptEnd.Y - ptStart.Y);
- double angleRad = v.GetAngle(w);
-
- if(w.X < 0 && w.Y > 0 || w.X >=0 && w.Y >= 0){
- angleRad = (System.Math.PI * 2) - angleRad;
- }
+ //see http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
+ int n = 0;
+ double mean = 0;
+ double S = 0;
+ double delta = 0;
- return angleRad;
+ foreach (double x in values)
+ {
+ n = n + 1;
+ delta = x - mean;
+ mean = mean + delta / (double)n;
+ S = S + delta * (x - mean);
+ }
+
+ return S / (n - 1);
}
- public static double DegreesToRadians(double degrees)
+ public static double Variance(ICollection<long> values)
{
- return degrees * System.Math.PI / 180.0;
- }
+ if (values.Count <= 0) { throw new ArgumentException("The values.Length must be greater than 0", "values"); }
+ else if (values.Count == 1) { return 0; }
- public static double RadiansToDegrees(double radians)
- {
- return radians * 180.0 / System.Math.PI;
+ //see http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
+ int n = 0;
+ double mean = 0;
+ double S = 0;
+ double delta = 0;
+
+ foreach (long x in values)
+ {
+ n = n + 1;
+ delta = x - mean;
+ mean = mean + delta / (double)n;
+ S = S + delta * (x - mean);
+ }
+
+ return S / (n - 1);
}
- public static double Variance(ICollection<double> values)
+ public static double Variance(ICollection<int> values)
{
if (values.Count <= 0) { throw new ArgumentException("The values.Length must be greater than 0", "values"); }
else if (values.Count == 1) { return 0; }
@@ -75,11 +84,11 @@
double S = 0;
double delta = 0;
- foreach (double x in values)
+ foreach (int x in values)
{
n = n + 1;
delta = x - mean;
- mean = mean + delta / (float)n;
+ mean = mean + delta / (double)n;
S = S + delta * (x - mean);
}
@@ -96,12 +105,41 @@
/// <returns></returns>
public static double StandardDeviation(ICollection<double> values)
{
- double variance = Variance(values);
//the standard deviation is the square root of the variance
//http://en.wikipedia.org/wiki/Standard_deviation
- return Math.Sqrt(variance);
+ return Math.Sqrt(Variance(values));
}
+ /// <summary>
+ /// To understand standard deviation, keep in mind that variance is the average of the squared
+ /// differences between data points and the mean. Variance is tabulated in units squared.
+ /// Standard deviation, being the square root of that quantity, therefore measures the spread
+ /// of data about the mean, measured in the same units as the data.
+ /// </summary>
+ /// <param name="values"></param>
+ /// <returns></returns>
+ public static double StandardDeviation(ICollection<long> values)
+ {
+ //the standard deviation is the square root of the variance
+ //http://en.wikipedia.org/wiki/Standard_deviation
+ return Math.Sqrt(Variance(values));
+ }
+
+ /// <summary>
+ /// To understand standard deviation, keep in mind that variance is the average of the squared
+ /// differences between data points and the mean. Variance is tabulated in units squared.
+ /// Standard deviation, being the square root of that quantity, therefore measures the spread
+ /// of data about the mean, measured in the same units as the data.
+ /// </summary>
+ /// <param name="values"></param>
+ /// <returns></returns>
+ public static double StandardDeviation(ICollection<int> values)
+ {
+ //the standard deviation is the square root of the variance
+ //http://en.wikipedia.org/wiki/Standard_deviation
+ return Math.Sqrt(Variance(values));
+ }
+
public static Pair<double, double> MinMax(ICollection<double> values)
{
double max = double.MinValue;
@@ -114,16 +152,78 @@
return new Pair<double, double>(min, max);
}
+ public static Pair<long, long> MinMax(ICollection<long> values)
+ {
+ long max = long.MinValue;
+ long min = long.MaxValue;
+ foreach (long l in values)
+ {
+ if (l > max) { max = l; }
+ if (l < min) { min = l; }
+ }
+ return new Pair<long, long>(min, max);
+ }
+
+ public static Pair<int, int> MinMax(ICollection<int> values)
+ {
+ int max = int.MinValue;
+ int min = int.MaxValue;
+ foreach (int l in values)
+ {
+ if (l > max) { max = l; }
+ if (l < min) { min = l; }
+ }
+ return new Pair<int, int>(min, max);
+ }
+
+ public static double Range(ICollection<double> values)
+ {
+ Pair<double, double> minMax = MinMax(values);
+ return minMax.Element2 - minMax.Element1;
+ }
+
+ public static double Range(ICollection<long> values)
+ {
+ Pair<long, long> minMax = MinMax(values);
+ return minMax.Element2 - minMax.Element1;
+ }
+
+ public static double Range(ICollection<int> values)
+ {
+ Pair<int, int> minMax = MinMax(values);
+ return minMax.Element2 - minMax.Element1;
+ }
+
+ public static double Minimum(ICollection<double> values)
+ {
+ return MinMax(values).Element1;
+ }
+
public static double Maximum(ICollection<double> values)
{
return MinMax(values).Element2;
}
- public static double Minimum(ICollection<double> values)
+ public static double Minimum(ICollection<long> values)
{
return MinMax(values).Element1;
}
+ public static double Maximum(ICollection<long> values)
+ {
+ return MinMax(values).Element2;
+ }
+
+ public static double Minimum(ICollection<int> values)
+ {
+ return MinMax(values).Element1;
+ }
+
+ public static double Maximum(ICollection<int> values)
+ {
+ return MinMax(values).Element2;
+ }
+
public static double Mode(ICollection<double> values)
{
Dictionary<double, int> dictCount = new Dictionary<double, int>();
@@ -133,13 +233,13 @@
else { dictCount[d] = 1; }
}
- int max = int.MinValue;
+ int maxCount = int.MinValue;
double mode = double.NaN;
foreach (KeyValuePair<double, int> kvp in dictCount)
{
- if (kvp.Value > max)
+ if (kvp.Value > maxCount)
{
- max = kvp.Value;
+ maxCount = kvp.Value;
mode = kvp.Key;
}
}
@@ -147,6 +247,52 @@
return mode;
}
+ public static long Mode(ICollection<long> values)
+ {
+ Dictionary<long, int> dictCount = new Dictionary<long, int>();
+ foreach (long l in values)
+ {
+ if (dictCount.ContainsKey(l)) { dictCount[l] = dictCount[l] + 1; }
+ else { dictCount[l] = 1; }
+ }
+
+ int maxCount = int.MinValue;
+ long mode = long.MinValue;
+ foreach (KeyValuePair<long, int> kvp in dictCount)
+ {
+ if (kvp.Value > maxCount)
+ {
+ maxCount = kvp.Value;
+ mode = kvp.Key;
+ }
+ }
+
+ return mode;
+ }
+
+ public static int Mode(ICollection<int> values)
+ {
+ Dictionary<int, int> dictCount = new Dictionary<int, int>();
+ foreach (int i in values)
+ {
+ if (dictCount.ContainsKey(i)) { dictCount[i] = dictCount[i] + 1; }
+ else { dictCount[i] = 1; }
+ }
+
+ int maxCount = int.MinValue;
+ int mode = int.MinValue;
+ foreach (KeyValuePair<int, int> kvp in dictCount)
+ {
+ if (kvp.Value > maxCount)
+ {
+ maxCount = kvp.Value;
+ mode = kvp.Key;
+ }
+ }
+
+ return mode;
+ }
+
public static double Mean(ICollection<double> values)
{
if (values.Count <= 0) { throw new ArgumentException("The values.Length must be greater than 0", "values"); }
@@ -161,7 +307,7 @@
public static double Mean(ICollection<int> values)
{
if (values.Count <= 0) { throw new ArgumentException("The values.Length must be greater than 0", "values"); }
- int total = 0;
+ long total = 0;
foreach (int i in values)
{
total += i;
@@ -169,6 +315,18 @@
return total / (double)values.Count;
}
+ public static double Mean(ICollection<long> values)
+ {
+ if (values.Count <= 0) { throw new ArgumentException("The values.Length must be greater than 0", "values"); }
+
+ long total = 0;
+ foreach (long l in values)
+ {
+ total = checked(total + l);
+ }
+ return total / (double)values.Count;
+ }
+
public static double Median(ICollection<double> values)
{
if (values.Count <= 0) { throw new ArgumentException("The values.Length must be greater than 0", "values"); }
@@ -186,5 +344,332 @@
return doubles[(int)System.Math.Floor(doubles.Length / 2f)];
}
}
+
+ public static double Median(ICollection<long> values)
+ {
+ if (values.Count <= 0) { throw new ArgumentException("The values.Length must be greater than 0", "values"); }
+
+ long[] longs = new long[values.Count];
+ values.CopyTo(longs, 0);
+ Array.Sort(longs);
+ if (longs.Length % 2 == 0)
+ {
+ int i = (int)(longs.Length / 2f);
+ return (longs[i - 1] + longs[i]) / 2f;
+ }
+ else
+ {
+ return longs[(int)System.Math.Floor(longs.Length / 2f)];
+ }
+ }
+
+ public static double Median(ICollection<int> values)
+ {
+ if (values.Count <= 0) { throw new ArgumentException("The values.Length must be greater than 0", "values"); }
+
+ int[] ints = new int[values.Count];
+ values.CopyTo(ints, 0);
+ Array.Sort(ints);
+ if (ints.Length % 2 == 0)
+ {
+ int i = (int)(ints.Length / 2f);
+ return (ints[i - 1] + ints[i]) / 2f;
+ }
+ else
+ {
+ return ints[(int)System.Math.Floor(ints.Length / 2f)];
+ }
+ }
+
+ private static double Sum(ICollection<double> values)
+ {
+ double sum = 0;
+ foreach (double v in values) { checked { sum += v; } }
+ return sum;
+ }
+
+ private static long Sum(ICollection<long> values)
+ {
+ long sum = 0;
+ foreach (long l in values) { checked { sum += l; } }
+ return sum;
+ }
+
+ private static long Sum(ICollection<int> values)
+ {
+ long sum = 0;
+ foreach (long l in values) { checked { sum += l; } }
+ return sum;
+ }
+
+ public static double EuclideanDistance(double x1, double y1, double x2, double y2)
+ {
+ double xDiff = x2 - x1;
+ double yDiff = y2 - y1;
+ return Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
+ }
+
+ public static double EuclideanDistance(long x1, long y1, long x2, long y2)
+ {
+ long xDiff = x2 - x1;
+ long yDiff = y2 - y1;
+ return Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
+ }
+
+ public static double EuclideanDistance(int x1, int y1, int x2, int y2)
+ {
+ int xDiff = x2 - x1;
+ int yDiff = y2 - y1;
+ return Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
+ }
+
+ public static double DegreesToRadians(double degrees)
+ {
+ return Math.PI * degrees / 180.0;
+ }
+
+ public static double RadiansToDegrees(double radians)
+ {
+ //radians = (PI * degrees) / 180
+ //degrees = (180 * radians)/PI
+ return (180 * radians) / Math.PI;
+ }
+
+ public static SummaryStatistics GetSummaryStatistics(ICollection<double> values, SummaryStatisticsFlags flags)
+ {
+ if (values.Count <= 0) { return new SummaryStatistics(); }
+
+ SummaryStatistics summaryStatistics = new SummaryStatistics(values.Count);
+
+ if ((flags & SummaryStatisticsFlags.Mean) == SummaryStatisticsFlags.Mean)
+ {
+ summaryStatistics.Mean = Mean(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Median) == SummaryStatisticsFlags.Median)
+ {
+ summaryStatistics.Median = Median(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Mode) == SummaryStatisticsFlags.Mode)
+ {
+ summaryStatistics.Mode = Mode(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Sum) == SummaryStatisticsFlags.Sum)
+ {
+ summaryStatistics.Sum = Sum(values);
+ }
+
+ Pair<double, double> minMaxPair = null;
+ if ((flags & SummaryStatisticsFlags.Range) == SummaryStatisticsFlags.Range)
+ {
+ minMaxPair = MinMax(values);
+ summaryStatistics.Range = minMaxPair.Element2 - minMaxPair.Element1;
+ }
+
+ if ((flags & SummaryStatisticsFlags.Minimum) == SummaryStatisticsFlags.Minimum)
+ {
+ if (summaryStatistics.IsValid(SummaryStatisticsFlags.Range)) { summaryStatistics.Minimum = minMaxPair.Element1; }
+ else { summaryStatistics.Minimum = Minimum(values); }
+ }
+
+ if ((flags & SummaryStatisticsFlags.Maximum) == SummaryStatisticsFlags.Maximum)
+ {
+ if (summaryStatistics.IsValid(SummaryStatisticsFlags.Range)) { summaryStatistics.Maximum = minMaxPair.Element2; }
+ else { summaryStatistics.Maximum = Maximum(values); }
+ }
+
+ if ((flags & SummaryStatisticsFlags.First) == SummaryStatisticsFlags.First)
+ {
+ double first = double.NaN;
+ if (values is IList<double>) { first = ((IList<double>)values)[0]; }
+ else { foreach (double val in values) { first = val; break; } }
+ summaryStatistics.First = first;
+ }
+
+ if ((flags & SummaryStatisticsFlags.Last) == SummaryStatisticsFlags.Last)
+ {
+ double last = double.NaN;
+ if (values is IList<double>) { last = ((IList<double>)values)[values.Count - 1]; }
+ else { foreach (double val in values) { last = val; } }
+ summaryStatistics.Last = last;
+ }
+
+ if ((flags & SummaryStatisticsFlags.StandardDeviation) == SummaryStatisticsFlags.StandardDeviation)
+ {
+ summaryStatistics.StandardDeviation = StandardDeviation(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Variance) == SummaryStatisticsFlags.Variance)
+ {
+ summaryStatistics.Variance = Variance(values);
+ }
+
+ return summaryStatistics;
+ }
+
+ public static SummaryStatistics GetSummaryStatistics(ICollection<double> values)
+ {
+ return GetSummaryStatistics(values, SummaryStatisticsFlags.All);
+ }
+
+ public static SummaryStatistics GetSummaryStatistics(ICollection<long> values, SummaryStatisticsFlags flags)
+ {
+ if (values.Count <= 0) { return new SummaryStatistics(); }
+
+ SummaryStatistics summaryStatistics = new SummaryStatistics(values.Count);
+
+ if ((flags & SummaryStatisticsFlags.Mean) == SummaryStatisticsFlags.Mean)
+ {
+ summaryStatistics.Mean = Mean(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Median) == SummaryStatisticsFlags.Median)
+ {
+ summaryStatistics.Median = Median(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Mode) == SummaryStatisticsFlags.Mode)
+ {
+ summaryStatistics.Mode = Mode(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Sum) == SummaryStatisticsFlags.Sum)
+ {
+ summaryStatistics.Sum = Sum(values);
+ }
+
+ Pair<long, long> minMaxPair = null;
+ if ((flags & SummaryStatisticsFlags.Range) == SummaryStatisticsFlags.Range)
+ {
+ minMaxPair = MinMax(values);
+ summaryStatistics.Range = minMaxPair.Element2 - minMaxPair.Element1;
+ }
+
+ if ((flags & SummaryStatisticsFlags.Minimum) == SummaryStatisticsFlags.Minimum)
+ {
+ if (summaryStatistics.IsValid(SummaryStatisticsFlags.Range)) { summaryStatistics.Minimum = minMaxPair.Element1; }
+ else { summaryStatistics.Minimum = Minimum(values); }
+ }
+
+ if ((flags & SummaryStatisticsFlags.Maximum) == SummaryStatisticsFlags.Maximum)
+ {
+ if (summaryStatistics.IsValid(SummaryStatisticsFlags.Range)) { summaryStatistics.Maximum = minMaxPair.Element2; }
+ else { summaryStatistics.Maximum = Maximum(values); }
+ }
+
+ if ((flags & SummaryStatisticsFlags.First) == SummaryStatisticsFlags.First)
+ {
+ long first = long.MinValue;
+ if (values is IList<long>) { first = ((IList<long>)values)[0]; }
+ else { foreach (long val in values) { first = val; break; } }
+ summaryStatistics.First = first;
+ }
+
+ if ((flags & SummaryStatisticsFlags.Last) == SummaryStatisticsFlags.Last)
+ {
+ long last = long.MinValue;
+ if (values is IList<long>) { last = ((IList<long>)values)[values.Count - 1]; }
+ else { foreach (long val in values) { last = val; } }
+ summaryStatistics.Last = last;
+ }
+
+ if ((flags & SummaryStatisticsFlags.StandardDeviation) == SummaryStatisticsFlags.StandardDeviation)
+ {
+ summaryStatistics.StandardDeviation = StandardDeviation(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Variance) == SummaryStatisticsFlags.Variance)
+ {
+ summaryStatistics.Variance = Variance(values);
+ }
+
+ return summaryStatistics;
+ }
+
+ public static SummaryStatistics GetSummaryStatistics(ICollection<long> values)
+ {
+ return GetSummaryStatistics(values, SummaryStatisticsFlags.All);
+ }
+
+ public static SummaryStatistics GetSummaryStatistics(ICollection<int> values, SummaryStatisticsFlags flags)
+ {
+ if (values.Count <= 0) { return new SummaryStatistics(); }
+
+ SummaryStatistics summaryStatistics = new SummaryStatistics(values.Count);
+
+ if ((flags & SummaryStatisticsFlags.Mean) == SummaryStatisticsFlags.Mean)
+ {
+ summaryStatistics.Mean = Mean(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Median) == SummaryStatisticsFlags.Median)
+ {
+ summaryStatistics.Median = Median(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Mode) == SummaryStatisticsFlags.Mode)
+ {
+ summaryStatistics.Mode = Mode(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Sum) == SummaryStatisticsFlags.Sum)
+ {
+ summaryStatistics.Sum = Sum(values);
+ }
+
+ Pair<int, int> minMaxPair = null;
+ if ((flags & SummaryStatisticsFlags.Range) == SummaryStatisticsFlags.Range)
+ {
+ minMaxPair = MinMax(values);
+ summaryStatistics.Range = minMaxPair.Element2 - minMaxPair.Element1;
+ }
+
+ if ((flags & SummaryStatisticsFlags.Minimum) == SummaryStatisticsFlags.Minimum)
+ {
+ if (summaryStatistics.IsValid(SummaryStatisticsFlags.Range)) { summaryStatistics.Minimum = minMaxPair.Element1; }
+ else { summaryStatistics.Minimum = Minimum(values); }
+ }
+
+ if ((flags & SummaryStatisticsFlags.Maximum) == SummaryStatisticsFlags.Maximum)
+ {
+ if (summaryStatistics.IsValid(SummaryStatisticsFlags.Range)) { summaryStatistics.Maximum = minMaxPair.Element2; }
+ else { summaryStatistics.Maximum = Maximum(values); }
+ }
+
+ if ((flags & SummaryStatisticsFlags.First) == SummaryStatisticsFlags.First)
+ {
+ int first = int.MinValue;
+ if (values is IList<int>) { first = ((IList<int>)values)[0]; }
+ else { foreach (int val in values) { first = val; break; } }
+ summaryStatistics.First = first;
+ }
+
+ if ((flags & SummaryStatisticsFlags.Last) == SummaryStatisticsFlags.Last)
+ {
+ int last = int.MinValue;
+ if (values is IList<int>) { last = ((IList<int>)values)[values.Count - 1]; }
+ else { foreach (int val in values) { last = val; } }
+ summaryStatistics.Last = last;
+ }
+
+ if ((flags & SummaryStatisticsFlags.StandardDeviation) == SummaryStatisticsFlags.StandardDeviation)
+ {
+ summaryStatistics.StandardDeviation = StandardDeviation(values);
+ }
+
+ if ((flags & SummaryStatisticsFlags.Variance) == SummaryStatisticsFlags.Variance)
+ {
+ summaryStatistics.Variance = Variance(values);
+ }
+
+ return summaryStatistics;
+ }
+
+ public static SummaryStatistics GetSummaryStatistics(ICollection<int> values)
+ {
+ return GetSummaryStatistics(values, SummaryStatisticsFlags.All);
+ }
}
}
Added: Trunk/Source/Roam.Common/Utils/SummaryStatistics.cs
===================================================================
--- Trunk/Source/Roam.Common/Utils/SummaryStatistics.cs (rev 0)
+++ Trunk/Source/Roam.Common/Utils/SummaryStatistics.cs 2007-08-27 03:36:45 UTC (rev 90)
@@ -0,0 +1,256 @@
+#region Copyright & License
+//
+// Copyright (c) 2005 - 2007 The Regents of the University of Washington, Intel
+// Research Seattle, and the Roam project. All rights reserved.
+//
+// This software is open-source under the BSD license. You may obtain a copy
+// of this license at
+//
+// http://roam.sourceforge.net/license.txt
+//
+// Alternatively, please see "license.txt" which should be included in
+// this source code distribution.
+//
+#endregion Copyright & License
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Roam.Common.Utils
+{
+ /// <summary>
+ ///
+ /// </summary>
+ /// <remarks>
+ ///
+ /// </remarks>
+ /// <author>Jon Froehlich</author>
+ public class SummaryStatistics
+ {
+ #region Fields
+ private SummaryStatisticsFlags _flagsCurrentlySet = SummaryStatisticsFlags.None;
+ //private double _mean;
+ //private double _median;
+ //private double _sum;
+ //private double _minimum;
+ //private double _maximum;
+ //private double _range;
+ //private double _first;
+ //private double _last;
+ //private double _stdDev;
+ //private double _variance;
+ //private double _mode;
+ private Dictionary<SummaryStatisticsFlags, double> _mapStats = new Dictionary<SummaryStatisticsFlags, double>();
+ //private int _count = int.MinValue;
+ #endregion Fields
+
+ #region Constructors
+ public SummaryStatistics() { }
+ public SummaryStatistics(int cases) { Cases = cases; }
+ public SummaryStatistics(IDictionary<SummaryStatisticsFlags, double> mapStats)
+ {
+ Set(mapStats);
+ }
+ #endregion Constructors
+
+ #region Properties
+ public double this[SummaryStatisticsFlags flag]
+ {
+ get
+ {
+ if(_mapStats.ContainsKey(flag))
+ {
+ return _mapStats[flag];
+ }
+ throw new ArgumentException(string.Format("We cannot return a value for the flag={0}", flag));
+ //switch (flag)
+ //{
+ // case SummaryStatisticsFlags.Mean:
+ // return Mean;
+ // case SummaryStatisticsFlags.Median:
+ // return Median;
+ // case SummaryStatisticsFlags.Sum:
+ // return Sum;
+ // case SummaryStatisticsFlags.Minimum:
+ // return Minimum;
+ // case SummaryStatisticsFlags.Maximum:
+ // return Maximum;
+ // case SummaryStatisticsFlags.Range:
+ // return Range;
+ // case SummaryStatisticsFlags.First:
+ // return First;
+ // case SummaryStatisticsFlags.Last:
+ // return Last;
+ // case SummaryStatisticsFlags.StandardDeviation:
+ // return StandardDeviation;
+ // case SummaryStatisticsFlags.Variance:
+ // return Variance;
+ // case SummaryStatisticsFlags.Mode:
+ // return Mode;
+ // case
+ //}
+
+ //throw new ArgumentException(string.Format("We cannot return a value for the flag={0}", flag));
+ }
+ set
+ {
+ switch (flag)
+ {
+ case SummaryStatisticsFlags.Mean:
+ Mean = value;
+ break;
+ case SummaryStatisticsFlags.Median:
+ Median = value;
+ break;
+ case SummaryStatisticsFlags.Sum:
+ Sum = value;
+ break;
+ case SummaryStatisticsFlags.Minimum:
+ Minimum = value;
+ break;
+ case SummaryStatisticsFlags.Maximum:
+ Maximum = value;
+ break;
+ case SummaryStatisticsFlags.Range:
+ Range = value;
+ break;
+ case SummaryStatisticsFlags.First:
+ First = value;
+ break;
+ case SummaryStatisticsFlags.Last:
+ Last = value;
+ break;
+ case SummaryStatisticsFlags.StandardDeviation:
+ StandardDeviation = value;
+ break;
+ case SummaryStatisticsFlags.Variance:
+ Variance = value;
+ break;
+ case SummaryStatisticsFlags.Mode:
+ Mode = value;
+ break;
+ case SummaryStatisticsFlags.Cases:
+ Cases = (int)value;
+ break;
+ default:
+ throw new ArgumentException(string.Format("We cannot set a value for the flag={0}", flag));
+ break;
+ }
+ }
+ }
+
+ public double Mean
+ {
+ get { if(_mapStats.ContainsKey(SummaryStatisticsFlags.Mean)) { return _mapStats[SummaryStatisticsFlags.Mean]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.Mean] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.Mean; }
+ }
+
+ public double Median
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.Median)) { return _mapStats[SummaryStatisticsFlags.Median]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.Median] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.Median; }
+ }
+
+ public double Sum
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.Sum)) { return _mapStats[SummaryStatisticsFlags.Sum]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.Sum] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.Sum; }
+ }
+
+ public double Minimum
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.Minimum)) { return _mapStats[SummaryStatisticsFlags.Minimum]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.Minimum] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.Minimum; }
+ }
+
+ public double Maximum
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.Maximum)) { return _mapStats[SummaryStatisticsFlags.Maximum]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.Maximum] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.Maximum; }
+ }
+
+ public double Range
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.Range)) { return _mapStats[SummaryStatisticsFlags.Range]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.Range] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.Range; }
+ }
+
+ public double First
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.First)) { return _mapStats[SummaryStatisticsFlags.First]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.First] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.First; }
+ }
+
+ public double Last
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.Last)) { return _mapStats[SummaryStatisticsFlags.Last]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.Last] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.Last; }
+ }
+
+ public double StandardDeviation
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.StandardDeviation)) { return _mapStats[SummaryStatisticsFlags.StandardDeviation]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.StandardDeviation] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.StandardDeviation; }
+ }
+
+ public double Variance
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.Variance)) { return _mapStats[SummaryStatisticsFlags.Variance]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.Variance] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.Variance; }
+ }
+
+ public double Mode
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.Mode)) { return _mapStats[SummaryStatisticsFlags.Mode]; } return double.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.Mode] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.Mode; }
+ }
+
+ public int Cases
+ {
+ get { if (_mapStats.ContainsKey(SummaryStatisticsFlags.Cases)) { return (int)_mapStats[SummaryStatisticsFlags.Cases]; } return int.MinValue; }
+ set { _mapStats[SummaryStatisticsFlags.Cases] = value; _flagsCurrentlySet |= SummaryStatisticsFlags.Cases; }
+ }
+
+ #endregion Properties
+
+ #region Methods
+ public bool IsValid(SummaryStatisticsFlags flags)
+ {
+ return (_flagsCurrentlySet & flags) != 0;
+ }
+
+ public IDictionary<SummaryStatisticsFlags, double> GetStatistics()
+ {
+ Dictionary<SummaryStatisticsFlags, double> d = new Dictionary<SummaryStatisticsFlags, double>(_mapStats);
+ return d;
+ }
+
+ private void Set(IDictionary<SummaryStatisticsFlags, double> mapStats)
+ {
+ foreach (KeyValuePair<SummaryStatisticsFlags, double> kvp in mapStats)
+ {
+ this[kvp.Key] = kvp.Value;
+ }
+ }
+ #endregion Methods
+ }
+
+ [Flags]
+ public enum SummaryStatisticsFlags
+ {
+ None = 0x000,
+ Mean = 0x0001,
+ Median = 0x002,
+ Sum = 0x004,
+ Minimum = 0x008,
+ Maximum = 0x010,
+ Range = 0x020,
+ First = 0x040,
+ Last = 0x080,
+ StandardDeviation = 0x100,
+ Variance = 0x200,
+ Mode = 0x400,
+ Cases = 0x800,
+ All = Mean | Median | Sum | Minimum | Maximum | Range | First | Last | StandardDeviation | Variance | Mode | Cases
+ }
+}
Modified: Trunk/Source/Roam.Test/Roam.Test.csproj
===================================================================
--- Trunk/Source/Roam.Test/Roam.Test.csproj 2007-07-31 08:23:27 UTC (rev 89)
+++ Trunk/Source/Roam.Test/Roam.Test.csproj 2007-08-27 03:36:45 UTC (rev 90)
@@ -81,12 +81,6 @@
<DependentUpon>EnumWindowsTest.cs</DependentUpon>
</Compile>
<Compile Include="Sandbox\PhoneButtonManager.cs" />
- <Compile Include="Sandbox\Camera.cs">
- <SubType>Component</SubType>
- </Compile>
- <Compile Include="Sandbox\Camera.Designer.cs">
- <DependentUpon>Camera.cs</DependentUpon>
- </Compile>
<Compile Include="Sandbox\ColorsTest.cs">
<SubType>Form</SubType>
</Compile>
@@ -212,10 +206,6 @@
<SubType>Designer</SubType>
<DependentUpon>AlphaBlendTest.cs</DependentUpon>
</EmbeddedResource>
- <EmbeddedResource Include="UI\Controls\AudioRecorderTest.resx">
- <SubType>Designer</SubType>
- <DependentUpon>AudioRecorderTest.cs</DependentUpon>
- </EmbeddedResource>
<EmbeddedResource Include="UI\Controls\CalendarTest.resx">
<SubType>Designer</SubType>
<DependentUpon>CalendarTest.cs</DependentUpon>
@@ -498,12 +488,6 @@
<Compile Include="UI\Controls\AlphaBlendTest.Designer.cs">
<DependentUpon>AlphaBlendTest.cs</DependentUpon>
</Compile>
- <Compile Include="UI\Controls\AudioRecorderTest.cs">
- <SubType>Form</SubType>
- </Compile>
- <Compile Include="UI\Controls\AudioRecorderTest.Designer.cs">
- <DependentUpon>AudioRecorderTest.cs</DependentUpon>
- </Compile>
<Compile Include="UI\Controls\CalendarTest.cs">
<SubType>Form</SubType>
</Compile>
@@ -553,7 +537,7 @@
<DependentUpon>TextBoxFilteredListTest.cs</DependentUpon>
</Compile>
<Compile Include="UI\Controls\FontTest.cs">
- <SubType>Form</SubType>
+ <SubType>Component</SubType>
</Compile>
<Compile Include="UI\Controls\FontTest.Designer.cs">
<DependentUpon>FontTest.cs</DependentUpon>
@@ -691,7 +675,7 @@
<DependentUpon>SystemColorTest.cs</DependentUpon>
</Compile>
<Compile Include="UI\Controls\TabControlTest.cs">
- <SubType>Component</SubType>
+ <SubType>Form</SubType>
</Compile>
<Compile Include="UI\Controls\TabControlTest.Designer.cs">
<DependentUpon>TabControlTest.cs</DependentUpon>
Modified: Trunk/Source/Roam.Test/Sandbox/RegisterHotKeyTest.cs
===================================================================
--- Trunk/Source/Roam.Test/Sandbox/RegisterHotKeyTest.cs 2007-07-31 08:23:27 UTC (rev 89)
+++ Trunk/Source/Roam.Test/Sandbox/RegisterHotKeyTest.cs 2007-08-27 03:36:45 UTC (rev 90)
@@ -7,6 +7,7 @@
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;
using System.Runtime.InteropServices;
+using Roam.Common.Utils;
namespace Roam.Test.Sandbox
{
@@ -63,6 +64,7 @@
{
//register to listen for home key
RegisterHotKey(this.Hwnd, VK_LWIN, 8, VK_LWIN);
+
}
~MessageWin()
Modified: Trunk/Source/Roam.Test/Utils/KeySnifferTest.Designer.cs
===================================================================
--- Trunk/Source/Roam.Test/Utils/KeySnifferTest.Designer.cs 2007-07-31 08:23:27 UTC (rev 89)
+++ Trunk/Source/Roam.Test/Utils/KeySnifferTest.Designer.cs 2007-08-27 03:36:45 UTC (rev 90)
@@ -33,8 +33,9 @@
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this._menuItemStartStop = new System.Windows.Forms.MenuItem();
+ this._menuItemTrap = new System.Windows.Forms.MenuItem();
this._scrollableLabel = new Roam.UI.Controls.ScrollableLabel();
- this._menuItemTrap = new System.Windows.Forms.MenuItem();
+ this.menuItem3 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// mainMenu1
@@ -51,6 +52,7 @@
//
this.menuItem2.MenuItems.Add(this._menuItemStartStop);
this.menuItem2.MenuItems.Add(this._menuItemTrap);
+ this.menuItem2.MenuItems.Add(this.menuItem3);
this.menuItem2.Text = "Menu";
//
// _menuItemStartStop
@@ -58,6 +60,11 @@
this._menuItemStartStop.Text = "Start";
this._menuItemStartStop.Click += new System.EventHandler(this._menuItemStartStop_Click);
//
+ // _menuItemTrap
+ //
+ this._menuItemTrap.Text = "Trap";
+ this._menuItemTrap.Click += new System.EventHandler(this._menuItemTrap_Click_1);
+ //
// _scrollableLabel
//
this._scrollableLabel.Dock = System.Windows.Forms.DockStyle.Fill;
@@ -67,9 +74,10 @@
this._scrollableLabel.Size = new System.Drawing.Size(240, 266);
this._scrollableLabel.TabIndex = 3;
//
- // _menuItemTrap
+ // menuItem3
//
- this._menuItemTrap.Text = "Trap";
+ this.menuItem3.Text = "Clear";
+ this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// KeySnifferTest
//
@@ -92,5 +100,6 @@
private System.Windows.Forms.MenuItem _menuItemStartStop;
private Roam.UI.Controls.ScrollableLabel _scrollableLabel;
private System.Windows.Forms.MenuItem _menuItemTrap;
+ private System.Windows.Forms.MenuItem menuItem3;
}
}
\ No newline at end of file
Modified: Trunk/Source/Roam.Test/Utils/KeySnifferTest.cs
===================================================================
--- Trunk/Source/Roam.Test/Utils/KeySnifferTest.cs 2007-07-31 08:23:27 UTC (rev 89)
+++ Trunk/Source/Roam.Test/Utils/KeySnifferTest.cs 2007-08-27 03:36:45 UTC (rev 90)
@@ -61,6 +61,7 @@
private void menuItem1_Click(object sender, EventArgs e)
{
+ _keySniffer.Stop();
this.Close();
}
@@ -78,9 +79,14 @@
}
}
- private void _menuItemTrap_Click(object sender, EventArgs e)
+ private void _menuItemTrap_Click_1(object sender, EventArgs e)
{
_menuItemTrap.Checked = !_menuItemTrap.Checked;
}
+
+ private void menuItem3_Click(object sender, EventArgs e)
+ {
+ _scrollableLabel.Clear();
+ }
}
}
\ No newline at end of file
Modified: Trunk/Source/Roam.suo
===================================================================
(Binary files differ)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|