public EnumerableHelper(FrameData frame)
 {
     m_enumerator = frame.Points.GetEnumerator();
     IsValid = true;
     Read();
 }
        /// <summary>
        /// Rounds the frame to the nearest level of specified tolerance.
        /// </summary>
        /// <param name="origional">the frame to round</param>
        /// <param name="tolerance">the timespan to round on.</param>
        /// <returns>A new frame that is rounded.</returns>
        public static SortedList<DateTime, FrameData> RoundToTolerance(this SortedList<DateTime, FrameData> origional, TimeSpan tolerance)
        {
            SortedList<DateTime, FrameData> results = new SortedList<DateTime, FrameData>();

            SortedList<DateTime, List<FrameData>> buckets = new SortedList<DateTime, List<FrameData>>();

            foreach (var items in origional)
            {
                DateTime roundedDate = items.Key.Round(tolerance);
                List<FrameData> frames;
                if (!buckets.TryGetValue(roundedDate, out frames))
                {
                    frames = new List<FrameData>();
                    buckets.Add(roundedDate, frames);
                }
                frames.Add(items.Value);
            }

            foreach (var bucket in buckets)
            {
                if (bucket.Value.Count == 1)
                {
                    results.Add(bucket.Key, bucket.Value[0]);
                }
                else
                {
                    int count = bucket.Value.Sum(x => x.Points.Count);
                    List<ulong> keys = new List<ulong>(count);
                    List<HistorianValueStruct> values = new List<HistorianValueStruct>(count);

                    FrameData tempFrame = new FrameData();
                    tempFrame.Points = new SortedList<ulong, HistorianValueStruct>();

                    var allFrames = new List<EnumerableHelper>();

                    foreach (var frame in bucket.Value)
                    {
                        allFrames.Add(new EnumerableHelper(frame));
                    }

                    while (true)
                    {
                        EnumerableHelper lowestKey = null;

                        foreach (var item in allFrames)
                            lowestKey = Min(lowestKey, item);

                        if (lowestKey == null)
                            break;

                        keys.Add(lowestKey.PointId);
                        values.Add(lowestKey.Value);

                        //tempFrame.Points.Add(lowestKey.PointId, lowestKey.Value);
                        lowestKey.Read();
                    }
                    tempFrame.Points = SortedListConstructor.Create(keys, values);
                    results.Add(bucket.Key, tempFrame);
                }
            }
            return results;
        }