Ejemplo n.º 1
0
        public static RangeCollection GetInstance(int singleItem)
        {
            RangeCollection range = new RangeCollection();

            range.Add(singleItem);
            return(range);
        }
Ejemplo n.º 2
0
        public RangeCollection Clone()
        {
            RangeCollection clone = new RangeCollection();

            clone._startItems   = new List <int>(_startItems);
            clone._itemToLength = new SortedDictionary <int, int>(_itemToLength);
            return(clone);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses strings of the form -10--5,-2-10,12-12 . Spaces are allowed, no other characters are.
        /// If mergeOverlappingRanges, then, for example, 2-3,4-5 is represented
        /// as 2-5. Otherwise, they're maintained as separate ranges. The only difference is in the behavior of the ToString() call.
        /// By extension, this will change how a RangeCollection is parsed into a RangeCollectionCollection using the latter's
        /// GetInstance(RangeCollection) initializer.
        /// </summary>
        /// <param name="ranges"></param>
        /// <returns></returns>
        public static RangeCollection Parse(string ranges)
        {
            ranges = ranges.Trim();
            RangeCollection aRangeCollection = GetInstance();

            aRangeCollection.AddRanges(ranges.Split(','));

            return(aRangeCollection);
        }
        static public void Test()
        {
            RangeCollection aRangeCollection = RangeCollection.GetInstance();

            aRangeCollection.Add(0);
            SpecialFunctions.CheckCondition("0-0" == aRangeCollection.ToString());
            aRangeCollection.Add(1);
            SpecialFunctions.CheckCondition("0-1" == aRangeCollection.ToString());
            aRangeCollection.Add(4);
            SpecialFunctions.CheckCondition("0-1,4-4" == aRangeCollection.ToString());
            aRangeCollection.Add(5);
            SpecialFunctions.CheckCondition("0-1,4-5" == aRangeCollection.ToString());
            aRangeCollection.Add(7);
            SpecialFunctions.CheckCondition("0-1,4-5,7-7" == aRangeCollection.ToString());
            aRangeCollection.Add(2);
            SpecialFunctions.CheckCondition("0-2,4-5,7-7" == aRangeCollection.ToString());
            aRangeCollection.Add(3);
            SpecialFunctions.CheckCondition("0-5,7-7" == aRangeCollection.ToString());
            aRangeCollection.Add(6);
            SpecialFunctions.CheckCondition("0-7" == aRangeCollection.ToString());
            aRangeCollection.Add(-10);
            SpecialFunctions.CheckCondition("-10--10,0-7" == aRangeCollection.ToString());
            aRangeCollection.Add(-5);
            SpecialFunctions.CheckCondition("-10--10,-5--5,0-7" == aRangeCollection.ToString());

            aRangeCollection = RangeCollection.Parse("1-5,7-12,13-14");

            Console.WriteLine(aRangeCollection);
            Console.WriteLine(aRangeCollection.Contains(3));
            Console.WriteLine(aRangeCollection.Contains(12));
            Console.WriteLine(aRangeCollection.Contains(13));
            Console.WriteLine(aRangeCollection.Contains(6));

            aRangeCollection = RangeCollection.Parse("-10--5,-1-14");

            Console.WriteLine(aRangeCollection);
            Console.WriteLine(aRangeCollection.Contains(-12));
            Console.WriteLine(aRangeCollection.Contains(-10));
            Console.WriteLine(aRangeCollection.Contains(-7));
            Console.WriteLine(aRangeCollection.Contains(-5));
            Console.WriteLine(aRangeCollection.Contains(-4));
            Console.WriteLine(aRangeCollection.Contains(0));
            Console.WriteLine(aRangeCollection.Contains(1));
            Console.WriteLine(aRangeCollection.Contains(-2));

            Console.WriteLine("Count: " + aRangeCollection.Count());
            Console.WriteLine("Count -5 to 2: " + aRangeCollection.Count(-5, 2));

            RangeCollectionCollection rcc = RangeCollectionCollection.GetInstance(aRangeCollection);

            Console.WriteLine(rcc);
            Console.WriteLine(rcc.GetContainingRangeCollection(-12));
            Console.WriteLine(rcc.GetContainingRangeCollection(-10));
            Console.WriteLine(rcc.GetContainingRangeCollection(-5));
            Console.WriteLine(rcc.GetContainingRangeCollection(3));
            Console.WriteLine(rcc.GetContainingRangeCollection(15));
        }
Ejemplo n.º 5
0
        public bool TryAddRangeCollection(RangeCollection rangeCollection)
        {
            bool allNew = true;

            foreach (KeyValuePair <int, int> startAndLast in rangeCollection.Collection)
            {
                allNew &= TryAdd(startAndLast.Key, startAndLast.Value - startAndLast.Key + 1);
            }
            return(allNew);
        }
Ejemplo n.º 6
0
 public static RangeCollection GetInstance(RangeCollection rangeCollection)
 {
     return(rangeCollection.Clone());
 }
Ejemplo n.º 7
0
        static public void Test()
        {
            RangeCollection aRangeCollection = RangeCollection.GetInstance();

            aRangeCollection.Add(0);
            SpecialFunctions.CheckCondition("0" == aRangeCollection.ToString());
            aRangeCollection.Add(1);
            SpecialFunctions.CheckCondition("0-1" == aRangeCollection.ToString());
            aRangeCollection.Add(4);
            SpecialFunctions.CheckCondition("0-1,4" == aRangeCollection.ToString());
            aRangeCollection.Add(5);
            SpecialFunctions.CheckCondition("0-1,4-5" == aRangeCollection.ToString());
            aRangeCollection.Add(7);
            SpecialFunctions.CheckCondition("0-1,4-5,7" == aRangeCollection.ToString());
            aRangeCollection.Add(2);
            SpecialFunctions.CheckCondition("0-2,4-5,7" == aRangeCollection.ToString());
            aRangeCollection.Add(3);
            SpecialFunctions.CheckCondition("0-5,7" == aRangeCollection.ToString());
            aRangeCollection.Add(6);
            SpecialFunctions.CheckCondition("0-7" == aRangeCollection.ToString());
            aRangeCollection.Add(-10);
            SpecialFunctions.CheckCondition("-10,0-7" == aRangeCollection.ToString());
            aRangeCollection.Add(-5);
            SpecialFunctions.CheckCondition("-10,-5,0-7" == aRangeCollection.ToString());

            string range = "-10--5,-3,-2-1,1-5,7-12,13-15,14-16,20-25,22-23";

            aRangeCollection = RangeCollection.Parse(range);
            Console.WriteLine(range);
            Console.WriteLine(aRangeCollection);
            //Console.WriteLine(aRangeCollection.Contains(3));
            //Console.WriteLine(aRangeCollection.Contains(12));
            //Console.WriteLine(aRangeCollection.Contains(13));
            //Console.WriteLine(aRangeCollection.Contains(6));

            range            = "1-5,0,4-10,-10--5,-12--3,15-20,12-21,-13";
            aRangeCollection = RangeCollection.Parse(range);

            Console.WriteLine(range);
            Console.WriteLine(aRangeCollection);
            //Console.WriteLine(aRangeCollection.Contains(-12));
            //Console.WriteLine(aRangeCollection.Contains(-10));
            //Console.WriteLine(aRangeCollection.Contains(-7));
            //Console.WriteLine(aRangeCollection.Contains(-5));
            //Console.WriteLine(aRangeCollection.Contains(-4));
            //Console.WriteLine(aRangeCollection.Contains(0));
            //Console.WriteLine(aRangeCollection.Contains(1));
            //Console.WriteLine(aRangeCollection.Contains(-2));

            Console.WriteLine("Count: " + aRangeCollection.Count());
            Console.WriteLine("Count -5 to 2: " + aRangeCollection.Count(-5, 2));

            RangeCollectionCollection rcc = RangeCollectionCollection.GetInstance(aRangeCollection);

            Console.WriteLine(rcc);
            Console.WriteLine(rcc.GetContainingRangeCollection(-12));
            Console.WriteLine(rcc.GetContainingRangeCollection(-10));
            Console.WriteLine(rcc.GetContainingRangeCollection(-5));
            Console.WriteLine(rcc.GetContainingRangeCollection(3));
            Console.WriteLine(rcc.GetContainingRangeCollection(15));
        }
Ejemplo n.º 8
0
 public void AddRangeCollection(RangeCollection rangeCollection)
 {
     TryAddRangeCollection(rangeCollection);
 }
        public static RangeCollection Parse(string ranges, bool mergeOverlappingRanges)
        {
            RangeCollection aRangeCollection = GetInstance();

            int lastBegin = int.MaxValue;
            int lastEnd   = int.MinValue;

            string[] contiguousRanges = ranges.Split(',');
            foreach (string r in contiguousRanges)
            {
                string range = r;   // only do this cuz I need to be able to reassign it later and you can't with foreach iterators.

                bool beginIsNegative = false;
                bool endIsNegative   = false;

                if (range[0] == '-')
                {
                    beginIsNegative = true;
                    range           = range.Substring(1);
                }
                if (range.IndexOf("--") > 0)
                {
                    endIsNegative = true;
                    range         = range.Replace("--", "-");
                }

                string[] rangeBeginAndEnd = range.Split('-');

                int begin = int.Parse(rangeBeginAndEnd[0].Trim());
                if (beginIsNegative)
                {
                    begin *= -1;
                }

                int end;
                if (rangeBeginAndEnd.Length == 1)
                {
                    end = begin;
                    SpecialFunctions.CheckCondition(!endIsNegative, "Ill-formed Range. " + ranges);
                }
                else
                {
                    SpecialFunctions.CheckCondition(rangeBeginAndEnd.Length == 2, "Ill-formed Range. " + ranges);
                    end = int.Parse(rangeBeginAndEnd[1].Trim());
                    if (endIsNegative)
                    {
                        end *= -1;
                    }
                }

                SpecialFunctions.CheckCondition(end > lastEnd, "Ill-formed Range. " + ranges);
                SpecialFunctions.CheckCondition(end >= begin, range + " is not a valid range. End is before begin!");

                if (begin > lastEnd + 1 || !mergeOverlappingRanges) // this is the beginning of a new range
                {
                    aRangeCollection.StartItems.Add(begin);
                    aRangeCollection.ItemToLength.Add(begin, end - begin + 1);
                }
                else // we're really just extending the previous range
                {
                    aRangeCollection.ItemToLength[lastBegin] = end - lastBegin + 1;
                }

                lastBegin = begin;
                lastEnd   = end;
            }

            return(aRangeCollection);
        }