Browse Source

Interval improvements

pull/728/head
Daniel Grunwald 11 years ago
parent
commit
32b10e4fc3
  1. 1
      ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 2
      ICSharpCode.Decompiler/Tests/Util/IntervalTests.cs
  3. 2
      ICSharpCode.Decompiler/Tests/Util/LongSetTests.cs
  4. 183
      ICSharpCode.Decompiler/Util/Interval.cs

1
ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj

@ -111,6 +111,7 @@ @@ -111,6 +111,7 @@
<Compile Include="TestCases\Switch.cs" />
<Compile Include="TestCases\ValueTypeCall.cs" />
<Compile Include="TestRunner.cs" />
<Compile Include="Util\IntervalTests.cs" />
<Compile Include="Util\LongSetTests.cs" />
<Compile Include="CustomAttributes\CustomAttributeTests.cs" />
<Compile Include="CustomAttributes\S_AssemblyCustomAttribute.cs" />

2
ICSharpCode.Decompiler/Tests/Util/IntervalTests.cs

@ -22,7 +22,7 @@ using ICSharpCode.Decompiler.IL; @@ -22,7 +22,7 @@ using ICSharpCode.Decompiler.IL;
using NUnit.Framework;
using ICSharpCode.Decompiler.Tests.Helpers;
namespace ICSharpCode.Decompiler.Tests.ILTransforms
namespace ICSharpCode.Decompiler.Tests.Util
{
public class IntervalTests
{

2
ICSharpCode.Decompiler/Tests/Util/LongSetTests.cs

@ -20,7 +20,7 @@ using System; @@ -20,7 +20,7 @@ using System;
using System.Collections.Immutable;
using NUnit.Framework;
namespace ICSharpCode.Decompiler.Tests
namespace ICSharpCode.Decompiler.Tests.Util
{
[TestFixture]
public class LongSetTests

183
ICSharpCode.Decompiler/Util/Interval.cs

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -8,9 +9,14 @@ using System.Threading.Tasks; @@ -8,9 +9,14 @@ using System.Threading.Tasks;
namespace ICSharpCode.Decompiler
{
/// <summary>
/// Represents a half-open interval.
/// Represents a half-closed interval.
/// The start position is inclusive; but the end position is exclusive.
/// </summary>
/// <remarks>
/// Start &lt;= unchecked(End - 1): normal interval
/// Start == End: empty interval
/// Special case: Start == End == int.MinValue: interval containing all integers, not an empty interval!
/// </remarks>
public struct Interval : IEquatable<Interval>
{
/// <summary>
@ -21,24 +27,73 @@ namespace ICSharpCode.Decompiler @@ -21,24 +27,73 @@ namespace ICSharpCode.Decompiler
/// <summary>
/// Gets the exclusive end of the interval.
/// </summary>
/// <remarks>
/// Note that an End of int.MinValue is a special case, and stands
/// for an actual End of int.MaxValue+1.
/// If possible, prefer using InclusiveEnd for comparisons, as that does not have an overflow problem.
/// </remarks>
public readonly int End;
/// <summary>
/// Creates a new interval.
/// </summary>
/// <param name="start">Start position (inclusive)</param>
/// <param name="end">End position (exclusive).
/// Note that it is possible to create an interval that includes int.MaxValue
/// by using end==int.MaxValue+1==int.MinValue.</param>
public Interval(int start, int end)
{
if (start > end && end != int.MinValue)
if (!(start <= unchecked(end - 1) || start == end))
throw new ArgumentException("The end must be after the start", "end");
this.Start = start;
this.End = end;
}
/// <summary>
/// Gets the inclusive end of the interval. (End - 1)
/// For empty intervals, this returns Start - 1.
/// </summary>
/// <remarks>
/// Because there is no empty interval at int.MinValue,
/// (Start==End==int.MinValue is a special case referring to [int.MinValue..int.MaxValue]),
/// integer overflow is not a problem here.
/// </remarks>
public int InclusiveEnd {
get {
return unchecked(End - 1);
}
}
public bool IsEmpty {
get {
return Start > InclusiveEnd;
}
}
public bool Contains(int val)
{
// Use 'val <= End-1' instead of 'val < End' to allow intervals to include int.MaxValue.
return Start <= val && val <= unchecked(End - 1);
// Use 'val <= InclusiveEnd' instead of 'val < End' to allow intervals to include int.MaxValue.
return Start <= val && val <= InclusiveEnd;
}
/// <summary>
/// Calculates the intersection between this interval and the other interval.
/// </summary>
public Interval Intersect(Interval other)
{
int start = Math.Max(this.Start, other.Start);
int inclusiveEnd = Math.Min(this.InclusiveEnd, other.InclusiveEnd);
if (start <= inclusiveEnd)
return new Interval(start, unchecked(inclusiveEnd + 1));
else
return default(Interval);
}
public override string ToString()
{
if (End == int.MinValue)
return string.Format("[{0}..int.MaxValue]", Start);
else
return string.Format("[{0}..{1})", Start, End);
}
@ -55,28 +110,30 @@ namespace ICSharpCode.Decompiler @@ -55,28 +110,30 @@ namespace ICSharpCode.Decompiler
public override int GetHashCode()
{
int hashCode = 0;
unchecked {
hashCode += 1000000007 * Start.GetHashCode();
hashCode += 1000000009 * End.GetHashCode();
return Start ^ End ^ (End << 7);
}
return hashCode;
}
public static bool operator ==(Interval lhs, Interval rhs) {
public static bool operator ==(Interval lhs, Interval rhs)
{
return lhs.Equals(rhs);
}
public static bool operator !=(Interval lhs, Interval rhs) {
public static bool operator !=(Interval lhs, Interval rhs)
{
return !(lhs == rhs);
}
#endregion
}
/// <summary>
/// Represents a half-open interval.
/// Represents a half-closed interval.
/// The start position is inclusive; but the end position is exclusive.
/// </summary>
/// <remarks>
/// Start &lt;= unchecked(End - 1): normal interval
/// Start == End: empty interval
/// Special case: Start == End == int.MinValue: interval containing all integers, not an empty interval!
/// </remarks>
public struct LongInterval : IEquatable<LongInterval>
{
/// <summary>
@ -87,33 +144,101 @@ namespace ICSharpCode.Decompiler @@ -87,33 +144,101 @@ namespace ICSharpCode.Decompiler
/// <summary>
/// Gets the exclusive end of the interval.
/// </summary>
/// <remarks>
/// Note that an End of long.MinValue is a special case, and stands
/// for an actual End of long.MaxValue+1.
/// If possible, prefer using InclusiveEnd for comparisons, as that does not have an overflow problem.
/// </remarks>
public readonly long End;
/// <summary>
/// Creates a new interval.
/// </summary>
/// <param name="start">Start position (inclusive)</param>
/// <param name="end">End position (exclusive).
/// Note that it is possible to create an interval that includes int.MaxValue
/// by using end==int.MaxValue+1==int.MinValue.</param>
public LongInterval(long start, long end)
{
if (start > end && end != long.MinValue)
if (!(start <= unchecked(end - 1) || start == end))
throw new ArgumentException("The end must be after the start", "end");
this.Start = start;
this.End = end;
}
/// <summary>
/// Gets the inclusive end of the interval. (End - 1)
/// For empty intervals, this returns Start - 1.
/// </summary>
/// <remarks>
/// Because there is no empty interval at int.MinValue,
/// (Start==End==int.MinValue is a special case referring to [int.MinValue..int.MaxValue]),
/// integer overflow is not a problem here.
/// </remarks>
public long InclusiveEnd {
get {
return unchecked(End - 1);
}
}
public bool IsEmpty {
get {
return Start > InclusiveEnd;
}
}
public bool Contains(long val)
{
// Use 'val <= End-1' instead of 'val < End' to allow intervals to include int.MaxValue.
return Start <= val && val <= unchecked(End - 1);
// Use 'val <= InclusiveEnd' instead of 'val < End' to allow intervals to include int.MaxValue.
return Start <= val && val <= InclusiveEnd;
}
/// <summary>
/// Calculates the intersection between this interval and the other interval.
/// </summary>
public LongInterval Intersect(LongInterval other)
{
long start = Math.Max(this.Start, other.Start);
long inclusiveEnd = Math.Min(this.InclusiveEnd, other.InclusiveEnd);
if (start <= inclusiveEnd)
return new LongInterval(start, unchecked(inclusiveEnd + 1));
else
return default(LongInterval);
}
/// <summary>
/// Returns an enumerator over all values in this interval.
/// </summary>
public IEnumerable<long> Range()
{
if (End == long.MinValue) {
long i = Start;
while (true) {
yield return i;
if (i == long.MaxValue)
break;
i++;
}
} else {
for (long i = Start; i < End; i++)
yield return i;
}
}
public override string ToString()
{
if (End == long.MinValue)
return string.Format("[{0}..long.MaxValue]", Start);
else
return string.Format("[{0}..{1})", Start, End);
}
#region Equals and GetHashCode implementation
public override bool Equals(object obj)
{
return (obj is LongInterval) && Equals((LongInterval)obj);
}
public bool Equals(LongInterval other)
{
return this.Start == other.Start && this.End == other.End;
@ -121,13 +246,7 @@ namespace ICSharpCode.Decompiler @@ -121,13 +246,7 @@ namespace ICSharpCode.Decompiler
public override int GetHashCode()
{
int hashCode = 0;
unchecked
{
hashCode += 1000000007 * Start.GetHashCode();
hashCode += 1000000009 * End.GetHashCode();
}
return hashCode;
return (Start ^ End ^ (End << 7)).GetHashCode();
}
public static bool operator ==(LongInterval lhs, LongInterval rhs)
@ -140,12 +259,6 @@ namespace ICSharpCode.Decompiler @@ -140,12 +259,6 @@ namespace ICSharpCode.Decompiler
return !(lhs == rhs);
}
#endregion
public IEnumerable<long> Range()
{
for (long i = Start; i < End; i++)
yield return i;
}
}
/// <summary>

Loading…
Cancel
Save