A class to represent a range of long values. Start is inclusive, End is exclusive.
Inheritance: IComparable
Exemplo n.º 1
0
 public void GetSimpleBufferRangeTest()
 {
     this.Cache.RangeCollection.AddRange(0, 500);
     LongRange range_actual = this.Cache.GetNextBufferRange(0);
     LongRange range_expected = new LongRange(500, this.RemoteFile.Size);
     Assert.AreEqual(0, range_actual.CompareTo(range_expected));
 }
Exemplo n.º 2
0
        public void GetBufferRangeTest()
        {
            this.Cache.RangeCollection.AddRange(0, 500);
            this.Cache.RangeCollection.AddRange(750, 1500);
            this.Cache.RangeCollection.AddRange(14323, 15630);

            LongRange range_actual;
            LongRange range_expected;

            range_actual = this.Cache.GetNextBufferRange(500);
            range_expected = new LongRange(500, 750);
            Assert.AreEqual(0, range_actual.CompareTo(range_expected));

            range_actual = this.Cache.GetNextBufferRange(560);
            range_expected = new LongRange(560, 750);
            Assert.AreEqual(0, range_actual.CompareTo(range_expected));

            range_actual = this.Cache.GetNextBufferRange(1120);
            range_expected = new LongRange(1500, 14323);
            Assert.AreEqual(0, range_actual.CompareTo(range_expected));

            range_actual = this.Cache.GetNextBufferRange(14859);
            range_expected = new LongRange(15630, this.RemoteFile.Size);
            Assert.AreEqual(0, range_actual.CompareTo(range_expected));

            range_actual = this.Cache.GetNextBufferRange(20000);
            range_expected = new LongRange(20000, this.RemoteFile.Size);
            Assert.AreEqual(0, range_actual.CompareTo(range_expected));
        }
Exemplo n.º 3
0
        public LongRange GetNextBufferRange(long offset)
        {
            if (offset >= this.PutioFile.Size)
                return null;

            lock (this.RangeCollection)
            {
                LongRange BufferThis = new LongRange(0, this.PutioFile.Size);
                int index = this.RangeCollection.BinaryIndexSearch(offset);
                if (index < 0)
                {
                    BufferThis.Start = offset;
                    index = this.RangeCollection.Bisect(offset);
                    if (index < this.RangeCollection.RangeSet.Count)
                        BufferThis.End = this.RangeCollection.RangeSet.ElementAt(index).Start;
                    else
                        BufferThis.End = this.PutioFile.Size;
                }
                else
                {
                    return this.GetNextBufferRange(this.RangeCollection.RangeSet.ElementAt(index).End);
                }

                return BufferThis;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Return how much is buffered after offset.
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public long CacheRange(long offset)
        {
            LongRange r = this.RangeCollection.BinarySearch(offset);

            if (r == null)
            {
                return(0);
            }
            else
            {
                return(r.End - offset);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///  Check if this object intersects with the given range.
        ///  Start positions are inclusive and End positions are
        ///  exclusive.
        /// </summary>
        /// <param name="range"></param>
        /// <returns></returns>
        public Boolean Intersects(LongRange range)
        {
            if (this.Start == range.Start || this.End == range.End)
            {
                return(true);
            }

            if (this.Start < range.Start)
            {
                return(this.End > range.Start);
            }
            else
            {
                return(this.Start < range.End);
            }
        }
Exemplo n.º 6
0
 /// <summary>
 ///  Check if this objects comes RIGHT AFTER or RIGHT
 ///  BEFORE the given range. That is. this.Start is equal
 ///  to range.End or vice versa.
 /// </summary>
 /// <param name="range"></param>
 /// <returns></returns>
 public Boolean IsConsequent(LongRange range)
 {
     return(this.End == range.Start || this.Start == range.End);
 }
Exemplo n.º 7
0
        /// <summary>
        ///  Try to merge two Ranges. Do nothing if they are
        ///  not consecutive or intersecting ranges. Return true
        ///  if merged, false otherwise.
        /// </summary>
        /// <param name="range"></param>
        public Boolean TryMerge(LongRange range)
        {
            if (this.Intersects(range) || this.IsConsequent(range))
            {
                this.Merge(range);
                return true;
            }

            return false;
        }
Exemplo n.º 8
0
 /// <summary>
 ///  Merge this LongRange with a one that intersects it.
 /// </summary>
 /// <param name="range"></param>
 public void Merge(LongRange range)
 {
     if (!this.Intersects(range) && !this.IsConsequent(range))
         throw new Exception("Can't merge two LongRange objects that don't intersect");
     this.Start = Math.Min(this.Start, range.Start);
     this.End = Math.Max(this.End, range.End);
 }
Exemplo n.º 9
0
 /// <summary>
 ///  Check if this objects comes RIGHT AFTER or RIGHT
 ///  BEFORE the given range. That is. this.Start is equal
 ///  to range.End or vice versa.
 /// </summary>
 /// <param name="range"></param>
 /// <returns></returns>
 public Boolean IsConsequent(LongRange range)
 {
     return this.End == range.Start || this.Start == range.End;
 }
Exemplo n.º 10
0
        /// <summary>
        ///  Check if this object intersects with the given range.
        ///  Start positions are inclusive and End positions are
        ///  exclusive.
        /// </summary>
        /// <param name="range"></param>
        /// <returns></returns>
        public Boolean Intersects(LongRange range)
        {
            if (this.Start == range.Start || this.End == range.End)
                return true;

            if (this.Start < range.Start)
                return this.End > range.Start;
            else
                return this.Start < range.End;
        }
Exemplo n.º 11
0
 public Boolean IsClosetoHandle(LongRange range, long position)
 {
     if (this.Handle.BufferPosition < range.Start)
         return false;
     return this.Handle.BufferPosition <= (position + Constants.CHUNK_TOLERANCE);
 }
Exemplo n.º 12
0
 public void RangeConstructorTest()
 {
     LongRange range = new LongRange(697080622, 697080622 + 1);
     range.End = 697080622;
 }