Beispiel #1
0
        public bool IsNextTo(IntInterval newInterval)
        {
            bool newIsLefter  = newInterval.Last + 1 < First;
            bool newIsRighter = newInterval.First > Last + 1;

            return(!(newIsLefter || newIsRighter));
        }
        protected BoundsRelation ParseBoundsRelation(IntInterval bounds)
        {
            var result = new BoundsRelation();

            if (arrows.Count == 0 || bounds.IsEmpty)
            {
                result.ReplaceIndex = 0;
                return(result);
            }

            int i     = 0;
            int count = arrows.Count;

            IntIntervalRelation relation;

            // Skip 'Less*'
            while (LookAhead(i, bounds) == IntIntervalRelation.Less)
            {
                ++i;
            }

            // 'OverlapFirst?'
            if (LookAhead(i, bounds) == IntIntervalRelation.OverlapFirst)
            {
                result.FirstOverlapIndex = i++;
            }

            result.ReplaceIndex = i;

            while (true)
            {
                relation = LookAhead(i, bounds);
                switch (relation)
                {
                case IntIntervalRelation.Undefined:
                case IntIntervalRelation.Greater:
                    return(result);

                // 'OverlapLast?'
                case IntIntervalRelation.OverlapLast:
                    result.LastOverlapIndex = i;
                    ++i;
                    return(result);

                case IntIntervalRelation.Equal:
                case IntIntervalRelation.Contains:
                    result.ContainsIndex = i;
                    return(result);

                case IntIntervalRelation.Contained:
                    result.ContainedIndexes.Add(i);
                    ++i;
                    continue;

                default:
                    Debug.Fail("Internal error");
                    break;
                }
            }
        }
Beispiel #3
0
 public IntIntervalRelation RelationTo(IntInterval other)
 {
     if (this < other)
     {
         return(IntIntervalRelation.Less);
     }
     else if (other < this)
     {
         return(IntIntervalRelation.Greater);
     }
     else if (this == other)
     {
         return(IntIntervalRelation.Equal);
     }
     else if (this.Contains(other))
     {
         return(IntIntervalRelation.Contains);
     }
     else if (other.Contains(this))
     {
         return(IntIntervalRelation.Contained);
     }
     else if (this.Contains(other.First))
     {
         return(IntIntervalRelation.OverlapFirst);
     }
     else if (other.Contains(this.First))
     {
         return(IntIntervalRelation.OverlapLast);
     }
     else
     {
         throw new InvalidOperationException("Internal error");
     }
 }
        public IEnumerable <IntArrow <TAttr> > Enumerate(IntInterval bounds)
        {
            if (arrows.Count == 0 || bounds.IsEmpty)
            {
                yield break;
            }

            int i     = 0;
            int count = arrows.Count;

            while (i != count && bounds.First > arrows[i].Key.Last)
            {
                ++i;
            }

            while (i != count && bounds.Contains(arrows[i].Key))
            {
                yield return(arrows[i]);

                ++i;
            }

            if (i != count)
            {
                var intersection = arrows[i].Key * bounds;
                if (!intersection.IsEmpty)
                {
                    yield return(new IntArrow <TAttr>(intersection, arrows[i].Value));
                }
            }
        }
        public Decision Build(
            IIntMap <int> outcomeArrows,
            IntInterval possibleBounds,
            IIntFrequency frequency)
        {
            defaultAction = outcomeArrows.DefaultValue;
            this.actionIdToDecision[defaultAction] = new ActionDecision(defaultAction);

            Decision result;

            if (TryBuildElementaryTree(outcomeArrows, out result))
            {
                return(result);
            }

            // TODO: Alternatively to averaging probability, each test can
            // be split if probability varies significantly within test range
            DecisionTest[] tests
                = outcomeArrows
                  .EnumerateCoverage(possibleBounds)
                  .Select(
                      a => new DecisionTest(a.Key, a.Value, frequency.Average(a.Key))
                      )
                  .ToArray();

            result = GenTree(new ArraySlice <DecisionTest>(tests));
            return(result);
        }
Beispiel #6
0
        public override void Remove(Int value)
        {
            if (bounds.Contains(value))
            {
                int i = FindInterval(value);
                if (i >= 0)
                {
                    var interval = intervals[i];
                    if (interval.First == interval.Last)
                    {
                        intervals.RemoveAt(i);
                    }
                    else if (interval.First == value)
                    {
                        intervals[i] = new IntInterval(value + 1, interval.Last);
                    }
                    else if (interval.Last == value)
                    {
                        intervals[i] = new IntInterval(interval.First, value - 1);
                    }
                    else
                    {
                        intervals[i] = new IntInterval(value + 1, interval.Last);
                        intervals.Insert(i, new IntInterval(interval.First, value - 1));
                    }

                    bounds = bounds - value;
                }
            }
        }
        public void TestBalanced()
        {
            var frequency = new MutableIntFrequency();
            frequency.DefaultValue = 0.0000001;
            frequency.Set(new IntArrow<double>(1, 520.0));
            frequency.Set(new IntArrow<double>(2, 49, 3.0));
            frequency.Set(new IntArrow<double>(50, 236.0));
            frequency.Set(new IntArrow<double>(51, 100, 2.0));

            const int DefaultValue = -1;
            var elementToAction = new MutableIntMap<int>();
            elementToAction.DefaultValue = DefaultValue;
            elementToAction.Set(new IntArrow<int>(1, 1));
            elementToAction.Set(new IntArrow<int>(2, 49, 2));
            elementToAction.Set(new IntArrow<int>(50, 3));
            elementToAction.Set(new IntArrow<int>(51, 100, 4));

            var target = new DecisionTreeBuilder(platformInfo);
            var bounds = new IntInterval(int.MinValue, int.MaxValue);
            var node = target.Build(elementToAction, bounds, frequency);
            PrintProgram(node, target.DefaultActionDecision);

            Assert.AreEqual(-1, node.Decide(int.MinValue) );
            Assert.AreEqual(-1, node.Decide(0) );
            Assert.AreEqual(1, node.Decide(1) );
            Assert.AreEqual(2, node.Decide(2) );
            Assert.AreEqual(2, node.Decide(49) );
            Assert.AreEqual(3, node.Decide(50) );
            Assert.AreEqual(4, node.Decide(51) );
            Assert.AreEqual(4, node.Decide(100) );
            Assert.AreEqual(-1, node.Decide(200) );
            Assert.AreEqual(-1, node.Decide(bounds.Last) );
        }
        public Decision Build(
            IIntMap<int>  outcomeArrows,
            IntInterval   possibleBounds,
            IIntFrequency frequency)
        {
            defaultAction = outcomeArrows.DefaultValue;
            this.actionIdToDecision[defaultAction] = new ActionDecision(defaultAction);

            Decision result;

            if (TryBuildElementaryTree(outcomeArrows, out result))
            {
                return result;
            }

            // TODO: Alternatively to averaging probability, each test can
            // be split if probability varies significantly within test range
            DecisionTest[] tests
                = outcomeArrows
                .EnumerateCoverage(possibleBounds)
                .Select(
                    a => new DecisionTest(a.Key, a.Value, frequency.Average(a.Key))
                    )
                    .ToArray();

            result = GenTree(new ArraySlice<DecisionTest>(tests));
            return result;
        }
        public EmitSyntax Build(EmitSyntax emit)
        {
            int rows = table.RowCount;
            int columns = table.ColumnCount;

            Ref<Labels> END = emit.Labels.Generate().GetRef();
            Ref<Labels>[] labels = new Ref<Labels>[rows];
            for (int i = 0; i != rows; ++i)
            {
                labels[i] = emit.Labels.Generate().GetRef();
            }

            emit
                .Do(LdRow)
                .Switch(labels)
                .Ldc_I4(-1)
                .Br(END)
                ;

            var columnRange = new IntInterval(0, columns - 1);
            var columnFrequency = new UniformIntFrequency(columnRange);
            for (int i = 0; i != rows; ++i)
            {
                var switchEmitter = SwitchGenerator.Sparse(table.GetRow(i), columnRange, columnFrequency);

                emit.Label(labels[i].Def);
                switchEmitter.Build(emit, LdCol, SwitchGenerator.LdValueAction(END));
            }

            return emit .Label(END.Def);
        }
Beispiel #10
0
        /// <summary>
        /// Intersection
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static IntInterval operator *(IntInterval x, IntInterval y)
        {
            var result = new IntInterval(
                (x.First > y.First) ? x.First : y.First,
                (x.Last < y.Last) ? x.Last : y.Last);

            return(result);
        }
Beispiel #11
0
        public IntInterval Union(IntInterval other)
        {
            var result = new IntInterval(
                Math.Min(First, other.First),
                Math.Max(Last, other.Last));

            return(result);
        }
 public static SwitchGenerator Sparse(
     IIntMap<int>  arrows,
     IntInterval   possibleBounds,
     IIntFrequency frequency = null)
 {
     var result = new SparseSwitchGenerator(arrows, possibleBounds, frequency);
     return result;
 }
Beispiel #13
0
        public override void RemoveAll(IntSet other)
        {
            var complemented = (IntervalIntSetBase)other.Complement(this);

            this.intervals = complemented.intervals;
            this.bounds    = complemented.bounds;
            this.hash      = complemented.hash;
        }
 public static SwitchGenerator Sparse(
     IntArrow<int>[] intervalToValue,
     int defaultValue,
     IntInterval   possibleBounds,
     IIntFrequency frequency = null)
 {
     var result = new SparseSwitchGenerator(intervalToValue, defaultValue, possibleBounds, frequency);
     return result;
 }
Beispiel #15
0
        private IntIntervalRelation LookAhead(int i, IntInterval bounds)
        {
            if (i == arrows.Count)
            {
                return(IntIntervalRelation.Undefined);
            }

            return(arrows[i].Key.RelationTo(bounds));
        }
Beispiel #16
0
        public IEnumerable <IntArrow <TAttr> > EnumerateCoverage(IntInterval bounds)
        {
            var relation = ParseBoundsRelation(bounds);

            if (relation.ContainsIndex >= 0)
            {
                yield return(new IntArrow <TAttr>(bounds, arrows[relation.ContainsIndex].Value));

                yield break;
            }

            int prevIndex = bounds.First;

            if (relation.FirstOverlapIndex >= 0)
            {
                var arrow        = arrows[relation.FirstOverlapIndex];
                var intersection = arrow.Key * bounds;
                yield return(new IntArrow <TAttr>(intersection, arrow.Value));

                prevIndex = intersection.Last + 1;
            }

            foreach (int i in relation.ContainedIndexes)
            {
                var arrow = arrows[i];

                if (prevIndex < arrow.Key.First)
                {
                    yield return(new IntArrow <TAttr>(prevIndex, arrow.Key.First - 1, DefaultValue));
                }

                yield return(arrow);

                prevIndex = arrow.Key.Last + 1;
            }

            if (relation.LastOverlapIndex >= 0)
            {
                var arrow = arrows[relation.LastOverlapIndex];

                if (prevIndex < arrow.Key.First)
                {
                    yield return(new IntArrow <TAttr>(prevIndex, arrow.Key.First - 1, DefaultValue));
                }

                var intersection = arrow.Key * bounds;
                yield return(new IntArrow <TAttr>(intersection, arrow.Value));
            }
            else
            {
                if (prevIndex <= bounds.Last)
                {
                    yield return(new IntArrow <TAttr>(prevIndex, bounds.Last, DefaultValue));
                }
            }
        }
Beispiel #17
0
        public IntInterval After(IntInterval bounds)
        {
            if (bounds.Last < First)
            {
                return(this);
            }

            // Maybe empty
            return(new IntInterval(bounds.Last + 1, Last));
        }
Beispiel #18
0
        protected void UpdateHashAndBounds()
        {
            bounds = new IntInterval(setType.MaxValue, setType.MinValue);
            foreach (var interval in intervals)
            {
                bounds = bounds.Union(interval);
            }

            UpdateHash();
        }
Beispiel #19
0
        public override void Add(IntInterval interval)
        {
            int first = interval.First;
            int last  = interval.Last;

            while (first <= last)
            {
                Add(first++);
            }
        }
        public double Average(IntInterval interval)
        {
            var intersection = arrow * interval;
            if (intersection.IsEmpty)
            {
                return 0;
            }

            return intersection.Key.LongSize / (double)arrow.Key.LongSize / interval.LongSize;
        }
 public SparseSwitchGenerator(
     IntArrow<int>[] intArrows,
     int defaultValue,
     IntInterval possibleBounds,
     IIntFrequency frequency = null)
     : this(new MutableIntMap<int>(intArrows, defaultValue), 
         possibleBounds,
         frequency)
 {
 }
Beispiel #22
0
        public IntInterval Before(IntInterval bounds)
        {
            if (Last < bounds.First)
            {
                return(this);
            }

            // Maybe empty
            return(new IntInterval(First, bounds.First - 1));
        }
 public SparseSwitchGenerator(
     IIntMap<int>  intMap,
     IntInterval   possibleBounds,
     IIntFrequency frequency)
 {
     this.strategy = new InlineFirstDTStrategy(this);
     this.intMap = intMap;
     this.possibleBounds = possibleBounds;
     this.frequency = frequency ?? new UniformIntFrequency(possibleBounds);
 }
        public double Sum(IntInterval interval)
        {
            var intersection = arrow * interval;
            if (intersection.IsEmpty)
            {
                return 0;
            }

            double result = intersection.Key.LongSize / (double)arrow.Key.LongSize / interval.LongSize;
            return result;
        }
Beispiel #25
0
        public double Average(IntInterval interval)
        {
            var intersection = arrow * interval;

            if (intersection.IsEmpty)
            {
                return(0);
            }

            return(intersection.Key.LongSize / (double)arrow.Key.LongSize / interval.LongSize);
        }
Beispiel #26
0
        public double Sum(IntInterval interval)
        {
            var intersection = arrow * interval;

            if (intersection.IsEmpty)
            {
                return(0);
            }

            double result = intersection.Key.LongSize / (double)arrow.Key.LongSize / interval.LongSize;

            return(result);
        }
        public void TestElementaryChecks()
        {
            const int DefaultValue = -100;
            var elementToAction = new MutableIntMap<int>();
            elementToAction.DefaultValue = DefaultValue;
            var bounds = new IntInterval(0, 9);
            var frequency = new UniformIntFrequency(bounds);
            elementToAction.Set(new IntArrow<int>(2, 100));
            elementToAction.Set(new IntArrow<int>(5, 200));

            var target = new DecisionTreeBuilder(platformInfo);
            var node = target.Build(elementToAction, bounds, frequency);
            PrintProgram(node, target.DefaultActionDecision);
        }
Beispiel #28
0
        public void Clear(IntInterval bounds)
        {
            var relation = ParseBoundsRelation(bounds);

            if (relation.ContainsIndex >= 0)
            {
                var before = arrows[relation.ContainsIndex].Before(bounds);
                var after  = arrows[relation.ContainsIndex].After(bounds);
                if (!before.Key.IsEmpty)
                {
                    arrows[relation.ContainsIndex] = before;
                }

                if (!after.Key.IsEmpty)
                {
                    if (before.IsEmpty)
                    {
                        arrows[relation.ContainsIndex] = after;
                    }
                    else
                    {
                        arrows.Insert(relation.ContainsIndex + 1, after);
                    }
                }

                return;
            }

            if (relation.FirstOverlapIndex >= 0)
            {
                arrows[relation.FirstOverlapIndex]
                    = arrows[relation.FirstOverlapIndex].Before(bounds);
            }

            if (relation.LastOverlapIndex >= 0)
            {
                arrows[relation.LastOverlapIndex]
                    = arrows[relation.LastOverlapIndex].After(bounds);
            }

            int shift = 0;

            foreach (int containedIndex in relation.ContainedIndexes)
            {
                arrows.RemoveAt(containedIndex + shift);
                --shift;
            }
        }
        public IEnumerable<IntArrow<double>> EnumerateCoverage(IntInterval bounds)
        {
            var before = arrow.Before(bounds);
            if (!before.IsEmpty)
            {
                yield return before;
            }

            var intersection = arrow * bounds;
            if (!intersection.IsEmpty)
            {
                yield return intersection;
            }

            var after = arrow.After(bounds);
            if (!after.IsEmpty)
            {
                yield return after;
            }
        }
Beispiel #30
0
        public override Int PopAny()
        {
            if (intervals.Count == 0)
            {
                throw new InvalidOperationException("Unable to pop element from the empty set");
            }

            int index    = intervals.Count - 1;
            var interval = intervals[index];

            if (interval.First == interval.Last)
            {
                intervals.RemoveAt(index);
                return(interval.First);
            }

            Int result = interval.Last;

            intervals[index] = new IntInterval(interval.First, interval.Last - 1);
            return(result);
        }
Beispiel #31
0
        // TODO: Performance in set differences
        private void Remove(IntInterval otherInterval)
        {
            // TODO: BOUNDS update at the end
            if (bounds.Intersects(otherInterval))
            {
                bounds = IntInterval.Empty;

                bool notDone = false;
                for (int i = 0; i != intervals.Count && notDone; ++i)
                {
                    var interval = intervals[i];
                    var rel      = interval.RelationTo(otherInterval);
                    switch (rel)
                    {
                    case IntIntervalRelation.Contained:
                        intervals.RemoveAt(i);
                        continue;

                    case IntIntervalRelation.Contains:
                        intervals[i] = interval.Before(otherInterval);
                        intervals.Insert(i + 1, interval.After(otherInterval));
                        return;

                    case IntIntervalRelation.OverlapFirst:
                        intervals[i] = interval.Before(otherInterval);
                        ++i;
                        continue;

                    case IntIntervalRelation.OverlapLast:
                        intervals[i] = interval.After(otherInterval);
                        return;

                    default:
                        ++i;
                        continue;
                    }
                }
            }
        }
Beispiel #32
0
        public IEnumerable <IntArrow <double> > EnumerateCoverage(IntInterval bounds)
        {
            var before = arrow.Before(bounds);

            if (!before.IsEmpty)
            {
                yield return(before);
            }

            var intersection = arrow * bounds;

            if (!intersection.IsEmpty)
            {
                yield return(intersection);
            }

            var after = arrow.After(bounds);

            if (!after.IsEmpty)
            {
                yield return(after);
            }
        }
Beispiel #33
0
        public IntInterval After(IntInterval bounds)
        {
            if (bounds.Last < First)
            {
                return this;
            }

            // Maybe empty
            return new IntInterval(bounds.Last + 1, Last);
        }
Beispiel #34
0
 public bool Disjoined(IntInterval other)
 {
     return(this < other || other < this);
 }
 public SparseSwitchGenerator(IIntMap<int> intMap, IntInterval possibleBounds)
     : this(intMap, possibleBounds, null)
 {
 }
        public void TestSameActionUnification()
        {
            var frequency = new UniformIntFrequency(new IntInterval(-100, 100));

            const int DefaultValue = -100;
            var elementToAction = new MutableIntMap<int>();
            elementToAction.DefaultValue = DefaultValue;
            elementToAction.Set(new IntArrow<int>(1, 1));
            elementToAction.Set(new IntArrow<int>(3, 49, 1));
            elementToAction.Set(new IntArrow<int>(51, 1));
            elementToAction.Set(new IntArrow<int>(54, 100, 1));

            var target = new DecisionTreeBuilder(platformInfo);
            var bounds = new IntInterval(0, 1000);
            var node = target.Build(elementToAction, bounds, frequency);
            PrintProgram(node);
        }
Beispiel #37
0
        public void Set(IntArrow <TAttr> newArrow)
        {
#if false
            if (newArrow.Value.Equals(DefaultValue))
            {
                Clear(newArrow.Key);
                return;
            }
#endif

            IntInterval bounds   = newArrow.Key;
            var         relation = ParseBoundsRelation(bounds);

            if (relation.ContainsIndex >= 0)
            {
                int i = relation.ContainsIndex;

                var oldArrow = arrows[i];

                if (oldArrow.Value.Equals(newArrow.Value))
                {
                    return;
                }

                var before = oldArrow.Before(bounds);
                if (!before.IsEmpty)
                {
                    arrows.Insert(i, before);
                    ++i;
                }

                arrows[i] = newArrow;
                ++i;

                var after = oldArrow.After(bounds);
                if (!after.IsEmpty)
                {
                    arrows.Insert(i, after);
                }

                return;
            }

            if (relation.FirstOverlapIndex >= 0)
            {
                int i = relation.FirstOverlapIndex;
                arrows[i] = arrows[i].Before(bounds);
            }

            int shift = 0;

            Debug.Assert(relation.ReplaceIndex >= 0);
            arrows.Insert(relation.ReplaceIndex, newArrow);
            ++shift;

            foreach (int containedIndex in relation.ContainedIndexes)
            {
                arrows.RemoveAt(containedIndex + shift);
                --shift;
            }

            if (relation.LastOverlapIndex >= 0)
            {
                arrows[relation.LastOverlapIndex + shift]
                    = arrows[relation.LastOverlapIndex + shift].After(bounds);
            }
        }
Beispiel #38
0
 public bool IsNextTo(IntInterval newInterval)
 {
     bool newIsLefter = newInterval.Last + 1 < First;
     bool newIsRighter = newInterval.First > Last + 1;
     return !(newIsLefter || newIsRighter);
 }
Beispiel #39
0
 public bool Disjoined(IntInterval other)
 {
     return this < other  || other < this;
 }
 public IEnumerable<IntArrow<double>> EnumerateCoverage(IntInterval bounds)
 {
     return data.EnumerateCoverage(bounds);
 }
 public double Sum(IntInterval interval)
 {
     return data.Sum(interval);
 }
        public void BuildBody(
            EmitSyntax emit, 
            LanguageData data,
            Ref<Args> tokenId,
            Ref<Args> oldValue,
            Ref<Args> newValue,
            Ref<Args> ctx,
            Ref<Args> lookbackStart)
        {
            var mergers = data.Grammar.Mergers;

            if (mergers.Count == 0)
            {
                emit
                    .Ldarg(oldValue)
                    .Ret();

                return;
            }

            var contextResolverCode = new ContextCode(
                                            emit,
                                            il => il.Ldarg(ctx),
                                            il => il.Ldarg(lookbackStart),
                                            data,
                                            data.Grammar.GlobalContextProvider,
                                            data.LocalParseContexts);

            IActionCode code = new MergeCode(emit, contextResolverCode)
            {
                LoadOldValue = il => il.Ldarg(oldValue),
                LoadNewValue = il => il.Ldarg(newValue),
            };

            var tokenToRuleIndex = new MutableIntMap<int>(
                                        mergers.Select(
                                            merger =>
                                                new IntArrow<int>(merger.Symbol.Index, merger.Index)));
            tokenToRuleIndex.DefaultValue = -1;

            var ids = mergers.Select(m => m.Symbol.Index);
            IntInterval possibleBounds = new IntInterval(ids.Min(), ids.Max());
            var switchGenerator = SwitchGenerator.Sparse(tokenToRuleIndex, possibleBounds);
            switchGenerator.Build(
                emit,
                il => il.Ldarg(tokenId),
                (il, value) =>
                {
                    if (value < 0)
                    {
                        emit
                            .Ldarg(newValue)
                            .Ret();
                    }
                    else
                    {
                        var merger = mergers[value];
                        var binding = merger.Joint.The<CilMerger>();
                        code = code
                            .Do(binding.Context.Load)
                            .Do(binding.ActionBuilder)
                            .Emit(il2 => il2.Ret())
                            ;
                    }
                });

            emit
                .Ret();
        }
Beispiel #43
0
 public bool Intersects(IntInterval other)
 {
     return(!(this < other || other < this));
 }
Beispiel #44
0
        public IntInterval Before(IntInterval bounds)
        {
            if (Last < bounds.First)
            {
                return this;
            }

            // Maybe empty
            return new IntInterval(First, bounds.First - 1);
        }
Beispiel #45
0
 public bool Contains(IntInterval other)
 {
     return other.First >= First && other.Last <= Last;
 }
Beispiel #46
0
 public IntInterval Union(IntInterval other)
 {
     var result = new IntInterval(
         Math.Min(First, other.First),
         Math.Max(Last, other.Last));
     return result;
 }
Beispiel #47
0
 public bool Intersects(IntInterval other)
 {
     return !(this < other || other < this);
 }
        public void WhenBoundsAreInsideSingleArrowEnumerateCoverage()
        {
            var target = new MutableIntMap<double>();
            target.DefaultValue = D;
            target.Set(new IntArrow<double>(0, 5, X));
            target.Set(new IntArrow<double>(9, 9, Y));
            target.Set(new IntArrow<double>(11, 14, Z));
            target.Set(new IntArrow<double>(20, 25, Y));

            var bounds = new IntInterval(21, 24);

            var expected = new[]
            {
                new IntArrow<double>(21, 24, Y),
            };

            var got = target.EnumerateCoverage(bounds).ToArray();

            Assert.AreEqual(expected, got);
        }
Beispiel #49
0
 public IntIntervalRelation RelationTo(IntInterval other)
 {
     if (this < other)
     {
         return IntIntervalRelation.Less;
     }
     else if (other < this)
     {
         return IntIntervalRelation.Greater;
     }
     else if (this == other)
     {
         return IntIntervalRelation.Equal;
     }
     else if (this.Contains(other))
     {
         return IntIntervalRelation.Contains;
     }
     else if (other.Contains(this))
     {
         return IntIntervalRelation.Contained;
     }
     else if (this.Contains(other.First))
     {
         return IntIntervalRelation.OverlapFirst;
     }
     else if (other.Contains(this.First))
     {
         return IntIntervalRelation.OverlapLast;
     }
     else
     {
         throw new InvalidOperationException("Internal error");
     }
 }
 public UniformIntFrequency(IntInterval bounds)
 {
     double elementProbability = bounds.IsEmpty ? 0 : (1.0 / bounds.LongSize);
     this.arrow = new IntArrow<double>(bounds, elementProbability);
 }
        public void WhenBoundsCoverMultipleArrowsEnumerateCoverage()
        {
            var target = new MutableIntMap<double>();
            target.DefaultValue = D;
            target.Set(new IntArrow<double>(0, 5, X));
            target.Set(new IntArrow<double>(9, 9, Y));
            target.Set(new IntArrow<double>(11, 14, Z));
            target.Set(new IntArrow<double>(20, 25, Y));

            var bounds = new IntInterval(3, 22);

            var expected = new[]
            {
                new IntArrow<double>(3, 5, X),
                new IntArrow<double>(6, 8, D),
                new IntArrow<double>(9, 9, Y),
                new IntArrow<double>(10, 10, D),
                new IntArrow<double>(11, 14, Z),
                new IntArrow<double>(15, 19, D),
                new IntArrow<double>(20, 22, Y),
            };

            var got = target.EnumerateCoverage(bounds).ToArray();

            Assert.AreEqual(expected, got);
        }
Beispiel #52
0
 public IEnumerable <IntArrow <double> > EnumerateCoverage(IntInterval bounds)
 {
     return(data.EnumerateCoverage(bounds));
 }
Beispiel #53
0
 public abstract void Add(IntInterval interval);
Beispiel #54
0
        public override IntSet Intersect(IntSet other0)
        {
            IntervalIntSetBase other = (IntervalIntSetBase)other0;

            if (bounds.Intersects(other.bounds))
            {
                var result = new IntervalIntSet(setType);

                var otherIntervals = other.intervals;
                int myCount = intervals.Count;
                int theirCount = otherIntervals.Count;
                int i = 0, j = 0;

                while (i != myCount && j != theirCount)
                {
                    IntInterval mine   = intervals[i];
                    IntInterval theirs = otherIntervals[j];

                    switch (mine.RelationTo(theirs))
                    {
                    case IntIntervalRelation.Less:
                    {
                        ++i;
                        break;
                    }

                    case IntIntervalRelation.Greater:
                    {
                        ++j;
                        break;
                    }

                    case IntIntervalRelation.Equal:
                    case IntIntervalRelation.Contains:
                    {
                        result.Add(theirs);
                        ++j;
                        break;
                    }

                    case IntIntervalRelation.Contained:
                    {
                        result.Add(mine);
                        ++i;
                        break;
                    }

                    case IntIntervalRelation.OverlapFirst:
                    {
                        result.Add(mine * theirs);
                        ++i;
                        break;
                    }

                    case IntIntervalRelation.OverlapLast:
                    {
                        result.Add(mine * theirs);
                        ++j;
                        break;
                    }

                    default:
                        throw new InvalidOperationException("Internal error");
                    }
                }

                result.UpdateHash();
                return(result);
            }

            return(setType.Empty);
        }
Beispiel #55
0
        public void set_from_intervals_array_contains_only_element_in_these_ranges(IntInterval[] intervals)
        {
            var target = IntSet.Ranges(intervals);
            foreach (var interval in intervals)
            {
                Assert.IsTrue(target.Contains(interval.First));
                Assert.IsTrue(target.Contains(interval.Last));
                int middle = (int)(((long)interval.First + interval.Last) / 2);
                Assert.IsTrue(target.Contains(middle));
            }

            foreach (int value in ExceptValues(intervals))
            {
                Assert.IsFalse(target.Contains(value));
            }
        }
Beispiel #56
0
 private IEnumerable<int> ExceptValues(IntInterval[] intervals)
 {
     foreach (var value in Values)
     {
         if (!intervals.Any(r => value >= r.First && value <= r.Last))
         {
             yield return value;
         }
     }
 }
Beispiel #57
0
 public bool Contains(IntInterval other)
 {
     return(other.First >= First && other.Last <= Last);
 }
Beispiel #58
0
 /// <summary>
 /// Intersection
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <returns></returns>
 public static IntInterval operator *(IntInterval x, IntInterval y)
 {
     var result = new IntInterval(
         (x.First > y.First) ? x.First : y.First,
          (x.Last < y.Last) ? x.Last : y.Last);
     return result;
 }
Beispiel #59
0
        public override void Add(IntInterval newInterval)
        {
            if (newInterval.First > newInterval.Last)
            {
                return;
            }

            bool changed   = false;
            var  oldBounds = bounds;

            bounds = bounds.Union(newInterval);

            if (oldBounds.IsNextTo(newInterval))
            {
                bool done = false;
                for (int i = 0; i != intervals.Count; ++i)
                {
                    IntInterval interval = intervals[i];
                    if (interval == newInterval)
                    {
                        // already in set
                        done = true;
                        break;
                    }

                    if (interval.IsNextTo(newInterval))
                    {
                        changed = !interval.Contains(newInterval);

                        var bigger = interval.Union(newInterval);
                        intervals[i] = bigger;

                        for (int j = i + 1; j != intervals.Count;)
                        {
                            interval = intervals[j];
                            if (bigger.IsNextTo(interval))
                            {
                                bigger       = bigger.Union(interval);
                                intervals[i] = bigger;
                                intervals.RemoveAt(j);
                            }
                            else
                            {
                                ++j;
                            }
                        }

                        done = true;
                        break;
                    }

                    if (newInterval < interval)
                    {
                        intervals.Insert(i, newInterval);
                        done    = true;
                        changed = true;
                        break;
                    }
                }

                if (!done)
                {
                    intervals.Insert(0, newInterval);
                    changed = true;
                }
            }
            else if (newInterval < oldBounds)
            {
                intervals.Insert(0, newInterval);
                changed = true;
            }
            else
            {
                intervals.Add(newInterval);
                changed = true;
            }

            if (changed)
            {
                UpdateHash();
            }
        }
Beispiel #60
0
 public double Sum(IntInterval interval)
 {
     return(data.Sum(interval));
 }