Beispiel #1
0
        public void AddRange(char lo, char hi)
        {
            Interval i = new Interval(lo, hi);

            if (ignore)
            {
                if (upper_case_characters.Intersects(i))
                {
                    Interval i2;
                    if (i.low < upper_case_characters.low)
                    {
                        i2     = new Interval(upper_case_characters.low + 32, i.high + 32);
                        i.high = upper_case_characters.low - 1;
                    }
                    else
                    {
                        i2    = new Interval(i.low + 32, upper_case_characters.high + 32);
                        i.low = upper_case_characters.high + 1;
                    }
                    intervals.Add(i2);
                }
                else if (upper_case_characters.Contains(i))
                {
                    i.high += 32;
                    i.low  += 32;
                }
            }
            intervals.Add(i);
        }
Beispiel #2
0
 private void AddProfileToTimeline(IntervalCollection <BasalProfile> timeline, BasalProfile profile)
 {
     if (profile.Duration == 0)
     {
         timeline.Add(profile.Time, null, profile);
     }
     else
     {
         timeline.Add(profile.Time, profile.Time.AddMinutes(profile.Duration), profile);
     }
 }
        public void Contains()
        {
            var now = DateTime.UtcNow;

            var intervals = new IntervalCollection();

            intervals.Add(new Interval(now, TimeSpan.FromHours(1)));
            intervals.Add(new Interval(now.AddDays(10), TimeSpan.FromHours(1)));

            Assert.IsTrue(intervals.Contains(now));
            Assert.IsTrue(intervals.Contains(now.AddMinutes(10)));
            Assert.IsFalse(intervals.Contains(now.AddMinutes(-10)));
            Assert.IsFalse(intervals.Contains(now.AddDays(5)));
            Assert.IsFalse(intervals.Contains(now.AddDays(11)));
        }
        public void GetBetween_PerformanceTest()
        {
            const int itemCount    = 100000;
            const int requestCount = 10000;
            var       random       = new Random(42);

            var collection = new IntervalCollection <int>();

            {
                int start;
                int end;
                for (var i = 0; i < itemCount; i++)
                {
                    start = random.Next(1000);
                    end   = start + random.Next(1, 50);
                    collection.Add(new TestInterval(start, end));
                }
            }

            var stopwatch     = Stopwatch.StartNew();
            var selectedCount = 0;

            for (var i = 0; i < requestCount; i++)
            {
                var start = random.Next(1000);
                var end   = start + random.Next(50);

                var result = collection.GetBetween(start, end);
                selectedCount += result.Count;
            }

            stopwatch.Stop();

            Assert.Inconclusive($"Executed {nameof(collection.GetBetween)}() {requestCount} times to select {selectedCount} results from {itemCount} items in {stopwatch.Elapsed}");
        }
        /// <summary>
        /// Add a new operation which will only be performed when required.
        /// </summary>
        /// <param name = "operation">The operation to be done on all the objects in the given range, returning the updated object.</param>
        /// <param name = "range">The range on which to perform the operation.</param>
        public void AddOperation(Func <TObject, TObject> operation, IInterval <int> range)
        {
            if (range.Start < 0 || range.End >= Count)
            {
                throw new ArgumentException("The specified range lies outside of the available range.", nameof(range));
            }

            _pendingOperations.Add(range, operation);
        }
Beispiel #6
0
 private void AddTempbasalToTimeline(IntervalCollection <TempBasal> timeline, TempBasal tempBasal)
 {
     if (tempBasal.Duration == 0)
     {
         timeline.Crop(tempBasal.Time);
     }
     else
     {
         timeline.Add(tempBasal.Time, tempBasal.Time.AddMinutes(tempBasal.Duration), tempBasal);
     }
 }
        public void Intersections()
        {
            var now = DateTime.UtcNow;

            var intervals = new IntervalCollection();

            intervals.Add(new Interval(now, TimeSpan.FromHours(1)));
            intervals.Add(new Interval(now.AddDays(10), TimeSpan.FromHours(1)));

            Assert.IsTrue(intervals.GetIntersections().Count() == 0);

            intervals.Clear();
            intervals.Add(new Interval(now, TimeSpan.FromHours(2)));
            intervals.Add(new Interval(now.AddHours(-0.5), TimeSpan.FromHours(1)));

            var intersection = intervals.GetIntersections().Single();

            Assert.AreEqual(now, intersection.Start);
            Assert.AreEqual(TimeSpan.FromHours(0.5), intersection.Length);
        }
        public void Add_PerformanceTest()
        {
            const int itemCount = 100000;
            var       random    = new Random(42);

            var stopwatch = Stopwatch.StartNew();

            var collection = new IntervalCollection <int>();
            int start;
            int end;

            for (var i = 0; i < itemCount; i++)
            {
                start = random.Next(1000);
                end   = start + random.Next(1, 50);
                collection.Add(new TestInterval(start, end));
            }

            stopwatch.Stop();

            Assert.Inconclusive($"Added {itemCount} items in {stopwatch.Elapsed}");
        }
Beispiel #9
0
        public override AnchorInfo GetAnchorInfo(bool reverse)
        {
            int ptr;
            int width = GetFixedWidth();

            ArrayList          infos    = new ArrayList();
            IntervalCollection segments = new IntervalCollection();

            // accumulate segments
            ptr = 0;
            int count = Expressions.Count;

            for (int i = 0; i < count; ++i)
            {
                Expression e;
                if (reverse)
                {
                    e = Expressions [count - i - 1];
                }
                else
                {
                    e = Expressions [i];
                }

                AnchorInfo info = e.GetAnchorInfo(reverse);
                infos.Add(info);

                if (info.IsPosition)
                {
                    return(new AnchorInfo(this, ptr + info.Offset, width, info.Position));
                }

                if (info.IsSubstring)
                {
                    segments.Add(info.GetInterval(ptr));
                }

                if (info.IsUnknownWidth)
                {
                    break;
                }

                ptr += info.Width;
            }

            // normalize and find the longest segment
            segments.Normalize();

            Interval longest = Interval.Empty;

            foreach (Interval segment in segments)
            {
                if (segment.Size > longest.Size)
                {
                    longest = segment;
                }
            }

            if (longest.IsEmpty)
            {
                return(new AnchorInfo(this, width));
            }

            // now chain the substrings that made this segment together
            bool ignore    = false;
            int  n_strings = 0;

            ptr = 0;
            for (int i = 0; i < infos.Count; ++i)
            {
                AnchorInfo info = (AnchorInfo)infos [i];

                if (info.IsSubstring && longest.Contains(info.GetInterval(ptr)))
                {
                    ignore |= info.IgnoreCase;
                    infos [n_strings++] = info;
                }

                if (info.IsUnknownWidth)
                {
                    break;
                }

                ptr += info.Width;
            }

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < n_strings; ++i)
            {
                AnchorInfo info;
                if (reverse)
                {
                    info = (AnchorInfo)infos [n_strings - i - 1];
                }
                else
                {
                    info = (AnchorInfo)infos [i];
                }
                sb.Append(info.Substring);
            }

            if (sb.Length == longest.Size)
            {
                return(new AnchorInfo(this, longest.low, width, sb.ToString(), ignore));
            }
            // were the string segments overlapping?
            if (sb.Length > longest.Size)
            {
                Console.Error.WriteLine("overlapping?");
                return(new AnchorInfo(this, width));
            }
            throw new SystemException("Shouldn't happen");
        }
Beispiel #10
0
		public override AnchorInfo GetAnchorInfo (bool reverse)
		{
			int ptr;
			int width = GetFixedWidth ();

			ArrayList infos = new ArrayList ();
			IntervalCollection segments = new IntervalCollection ();

			// accumulate segments
			ptr = 0;
			int count = Expressions.Count;
			for (int i = 0; i < count; ++ i) {
				Expression e;
				if (reverse)
					e = Expressions [count - i - 1];
				else
					e = Expressions [i];

				AnchorInfo info = e.GetAnchorInfo (reverse);
				infos.Add (info);

				if (info.IsPosition)
					return new AnchorInfo (this, ptr + info.Offset, width, info.Position);

				if (info.IsSubstring)
					segments.Add (info.GetInterval (ptr));

				if (info.IsUnknownWidth)
					break;

				ptr += info.Width;
			}

			// normalize and find the longest segment
			segments.Normalize ();

			Interval longest = Interval.Empty;
			foreach (Interval segment in segments) {
				if (segment.Size > longest.Size)
					longest = segment;
			}

			if (longest.IsEmpty)
				return new AnchorInfo (this, width);

			// now chain the substrings that made this segment together
			bool ignore = false;
			int n_strings = 0;

			ptr = 0;
			for (int i = 0; i < infos.Count; ++i) {
				AnchorInfo info = (AnchorInfo) infos [i];

				if (info.IsSubstring && longest.Contains (info.GetInterval (ptr))) {
					ignore |= info.IgnoreCase;
					infos [n_strings ++] = info;
				}

				if (info.IsUnknownWidth)
					break;

				ptr += info.Width;
			}

			StringBuilder sb = new StringBuilder ();
			for (int i = 0; i < n_strings; ++i) {
				AnchorInfo info;
				if (reverse)
					info = (AnchorInfo) infos [n_strings - i - 1];
				else
					info = (AnchorInfo) infos [i];
				sb.Append (info.Substring);
			}

			if (sb.Length == longest.Size)
				return new AnchorInfo (this, longest.low, width, sb.ToString (), ignore);
			// were the string segments overlapping?
			if (sb.Length > longest.Size) {
				Console.Error.WriteLine ("overlapping?");
				return new AnchorInfo (this, width);
			}
			throw new SystemException ("Shouldn't happen");
		}
Beispiel #11
0
        public override AnchorInfo GetAnchorInfo(bool reverse)
        {
            int ptr;
            int width = GetFixedWidth();

            ArrayList          infos    = new ArrayList();
            IntervalCollection segments = new IntervalCollection();

            // accumulate segments

            ptr = 0;
            //foreach (Expression e in Expressions) {
            int count = Expressions.Count;

            for (int i = 0; i < count; ++i)
            {
                Expression e;
                if (reverse)
                {
                    e = Expressions [count - i - 1];
                }
                else
                {
                    e = Expressions [i];
                }

                AnchorInfo info = e.GetAnchorInfo(reverse);
                infos.Add(info);

                if (info.IsPosition)
                {
                    return(new AnchorInfo(this, ptr + info.Offset, width, info.Position));
                }

                if (info.IsSubstring)
                {
                    segments.Add(info.GetInterval(ptr));
                }

                if (info.IsUnknownWidth)
                {
                    break;
                }

                ptr += info.Width;
            }

            // normalize and find the longest segment

            segments.Normalize();

            Interval longest = Interval.Empty;

            foreach (Interval segment in segments)
            {
                if (segment.Size > longest.Size)
                {
                    longest = segment;
                }
            }

            // now chain the substrings that made this segment together

            if (!longest.IsEmpty)
            {
                string    str    = "";
                ArrayList strs   = new ArrayList();
                bool      ignore = false;

                ptr = 0;

                //foreach (AnchorInfo info in infos) {
                for (int i = 0; i < infos.Count; ++i)
                {
                    AnchorInfo info;

                    info = (AnchorInfo)infos[i];

                    if (info.IsSubstring && longest.Contains(info.GetInterval(ptr)))
                    {
                        //str += info.Substring;	// TODO mark subexpressions
                        strs.Add(info.Substring);
                        ignore |= info.IgnoreCase;
                    }


                    if (info.IsUnknownWidth)
                    {
                        break;
                    }

                    ptr += info.Width;
                }

                for (int i = 0; i < strs.Count; ++i)
                {
                    if (reverse)
                    {
                        str += strs [strs.Count - i - 1];
                    }
                    else
                    {
                        str += strs [i];
                    }
                }


                return(new AnchorInfo(this, longest.low, width, str, ignore));
            }

            return(new AnchorInfo(this, width));
        }
Beispiel #12
0
        public override AnchorInfo GetAnchorInfo(bool reverse)
        {
            int                fixedWidth         = base.GetFixedWidth();
            ArrayList          arrayList          = new ArrayList();
            IntervalCollection intervalCollection = new IntervalCollection();
            int                num   = 0;
            int                count = base.Expressions.Count;

            for (int i = 0; i < count; i++)
            {
                Expression expression;
                if (reverse)
                {
                    expression = base.Expressions[count - i - 1];
                }
                else
                {
                    expression = base.Expressions[i];
                }
                AnchorInfo anchorInfo = expression.GetAnchorInfo(reverse);
                arrayList.Add(anchorInfo);
                if (anchorInfo.IsPosition)
                {
                    return(new AnchorInfo(this, num + anchorInfo.Offset, fixedWidth, anchorInfo.Position));
                }
                if (anchorInfo.IsSubstring)
                {
                    intervalCollection.Add(anchorInfo.GetInterval(num));
                }
                if (anchorInfo.IsUnknownWidth)
                {
                    break;
                }
                num += anchorInfo.Width;
            }
            intervalCollection.Normalize();
            Interval interval = Interval.Empty;

            foreach (object obj in intervalCollection)
            {
                Interval interval2 = (Interval)obj;
                if (interval2.Size > interval.Size)
                {
                    interval = interval2;
                }
            }
            if (interval.IsEmpty)
            {
                return(new AnchorInfo(this, fixedWidth));
            }
            bool flag = false;
            int  num2 = 0;

            num = 0;
            for (int j = 0; j < arrayList.Count; j++)
            {
                AnchorInfo anchorInfo2 = (AnchorInfo)arrayList[j];
                if (anchorInfo2.IsSubstring && interval.Contains(anchorInfo2.GetInterval(num)))
                {
                    flag |= anchorInfo2.IgnoreCase;
                    arrayList[num2++] = anchorInfo2;
                }
                if (anchorInfo2.IsUnknownWidth)
                {
                    break;
                }
                num += anchorInfo2.Width;
            }
            StringBuilder stringBuilder = new StringBuilder();

            for (int k = 0; k < num2; k++)
            {
                AnchorInfo anchorInfo3;
                if (reverse)
                {
                    anchorInfo3 = (AnchorInfo)arrayList[num2 - k - 1];
                }
                else
                {
                    anchorInfo3 = (AnchorInfo)arrayList[k];
                }
                stringBuilder.Append(anchorInfo3.Substring);
            }
            if (stringBuilder.Length == interval.Size)
            {
                return(new AnchorInfo(this, interval.low, fixedWidth, stringBuilder.ToString(), flag));
            }
            if (stringBuilder.Length > interval.Size)
            {
                Console.Error.WriteLine("overlapping?");
                return(new AnchorInfo(this, fixedWidth));
            }
            throw new SystemException("Shouldn't happen");
        }
Beispiel #13
0
		public override AnchorInfo GetAnchorInfo (bool reverse) {
			int ptr;
			int width = GetFixedWidth ();

			ArrayList infos = new ArrayList ();
			IntervalCollection segments = new IntervalCollection ();

			// accumulate segments

			ptr = 0;
			//foreach (Expression e in Expressions) {
			int count = Expressions.Count;
			for (int i = 0; i < count; ++ i) {
				Expression e;
				if (reverse)
					e = Expressions [count - i - 1];
				else
					e = Expressions [i];		
				
				AnchorInfo info = e.GetAnchorInfo (reverse);
				infos.Add (info);

				if (info.IsPosition)
					return new AnchorInfo (this, ptr + info.Offset, width, info.Position);

				if (info.IsSubstring)
					segments.Add (info.GetInterval (ptr));

				if (info.IsUnknownWidth)
					break;

				ptr += info.Width;
			}

			// normalize and find the longest segment

			segments.Normalize ();

			Interval longest = Interval.Empty;
			foreach (Interval segment in segments) {
				if (segment.Size > longest.Size)
					longest = segment;
			}

			// now chain the substrings that made this segment together

			if (!longest.IsEmpty) {
				string str = "";
				ArrayList strs = new ArrayList();
				bool ignore = false;

				ptr = 0;
				
				//foreach (AnchorInfo info in infos) {
				for (int i = 0; i < infos.Count; ++ i) {
					AnchorInfo info;

					info = (AnchorInfo)infos[i];		
					
					if (info.IsSubstring && longest.Contains (info.GetInterval (ptr))) {
						//str += info.Substring;	// TODO mark subexpressions
						strs.Add(info.Substring);
						ignore |= info.IgnoreCase;
					}

					
					 	if (info.IsUnknownWidth)					 		
							break;
					
						ptr += info.Width;
				}	
					
				for (int i = 0; i< strs.Count; ++i)
				{
					if (reverse)
						str += strs [strs.Count - i - 1];
					else
						str += strs [i];
							
					
				}
			

				return new AnchorInfo (this, longest.low, width, str, ignore);
			}

			return new AnchorInfo (this, width);
		}