Browse Source

implement LongInterval and LongSet

pull/728/head
Siegfried Pammer 10 years ago
parent
commit
4f2c231d00
  1. 5
      ICSharpCode.Decompiler/CecilExtensions.cs
  2. 5
      ICSharpCode.Decompiler/Disassembler/DisassemblerHelpers.cs
  3. 1
      ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj
  4. 39
      ICSharpCode.Decompiler/Tests/LongSetTests.cs
  5. 104
      ICSharpCode.Decompiler/Util/Interval.cs

5
ICSharpCode.Decompiler/CecilExtensions.cs

@ -178,6 +178,11 @@ namespace ICSharpCode.Decompiler @@ -178,6 +178,11 @@ namespace ICSharpCode.Decompiler
return string.Format("IL_{0:x4}", offset);
}
public static string OffsetToString(long offset)
{
return string.Format("IL_{0:x4}", offset);
}
public static HashSet<MethodDefinition> GetAccessorMethods(this TypeDefinition type)
{
HashSet<MethodDefinition> accessorMethods = new HashSet<MethodDefinition>();

5
ICSharpCode.Decompiler/Disassembler/DisassemblerHelpers.cs

@ -50,6 +50,11 @@ namespace ICSharpCode.Decompiler.Disassembler @@ -50,6 +50,11 @@ namespace ICSharpCode.Decompiler.Disassembler
{
return string.Format("IL_{0:x4}", offset);
}
public static string OffsetToString(long offset)
{
return string.Format("IL_{0:x4}", offset);
}
public static void WriteOffsetReference(ITextOutput writer, Instruction instruction)
{

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

@ -102,6 +102,7 @@ @@ -102,6 +102,7 @@
<Compile Include="Helpers\TypeSystemHelper.cs" />
<Compile Include="ILTransforms\InliningTests.cs" />
<Compile Include="Loops.cs" />
<Compile Include="LongSetTests.cs" />
<Compile Include="StackToVariablesTests.cs" />
<Compile Include="TestCases\CompoundAssignment.cs" />
<Compile Include="TestCases\ControlFlow.cs" />

39
ICSharpCode.Decompiler/Tests/LongSetTests.cs

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Immutable;
using NUnit.Framework;
namespace ICSharpCode.Decompiler.Tests
{
[TestFixture]
public class LongSetTests
{
[Test]
public void UpperBound()
{
var longSet = new LongSet(new [] { new LongInterval(1, 5), new LongInterval(6, 7) }.ToImmutableArray());
Assert.AreEqual(0, longSet.upper_bound(0));
for (int i = 1; i <= 5; i++)
Assert.AreEqual(1, longSet.upper_bound(i));
for (int i = 6; i <= 10; i++)
Assert.AreEqual(2, longSet.upper_bound(i));
}
}
}

104
ICSharpCode.Decompiler/Util/Interval.cs

@ -74,13 +74,82 @@ namespace ICSharpCode.Decompiler @@ -74,13 +74,82 @@ namespace ICSharpCode.Decompiler
}
/// <summary>
/// An immutable set of integers, that is implemented as a list of intervals.
/// Represents a half-open interval.
/// The start position is inclusive; but the end position is exclusive.
/// </summary>
struct IntegerSet
public struct LongInterval : IEquatable<LongInterval>
{
public readonly ImmutableArray<Interval> Intervals;
/// <summary>
/// Gets the inclusive start of the interval.
/// </summary>
public readonly long Start;
/// <summary>
/// Gets the exclusive end of the interval.
/// </summary>
public readonly long End;
public LongInterval(long start, long end)
{
if (start > end && end != long.MinValue)
throw new ArgumentException("The end must be after the start", "end");
this.Start = start;
this.End = end;
}
public IntegerSet(ImmutableArray<Interval> intervals)
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);
}
public override string ToString()
{
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;
}
public override int GetHashCode()
{
int hashCode = 0;
unchecked
{
hashCode += 1000000007 * Start.GetHashCode();
hashCode += 1000000009 * End.GetHashCode();
}
return hashCode;
}
public static bool operator ==(LongInterval lhs, LongInterval rhs)
{
return lhs.Equals(rhs);
}
public static bool operator !=(LongInterval lhs, LongInterval rhs)
{
return !(lhs == rhs);
}
#endregion
}
/// <summary>
/// An immutable set of longs, that is implemented as a list of intervals.
/// </summary>
struct LongSet
{
public readonly ImmutableArray<LongInterval> Intervals;
public LongSet(ImmutableArray<LongInterval> intervals)
{
this.Intervals = intervals;
}
@ -90,14 +159,29 @@ namespace ICSharpCode.Decompiler @@ -90,14 +159,29 @@ namespace ICSharpCode.Decompiler
get { return Intervals.IsDefaultOrEmpty; }
}
public bool Contains(int val)
public bool Contains(long val)
{
int index = upper_bound(val);
return index > 0 && Intervals[index - 1].Contains(val);
}
internal int upper_bound(long val)
{
// TODO: use binary search
foreach (Interval v in Intervals) {
if (v.Start <= val && val <= v.End)
return true;
int min = 0, max = Intervals.Length - 1;
while (max >= min) {
int m = min + (max - min) / 2;
LongInterval i = Intervals[m];
if (val < i.Start) {
max = m - 1;
continue;
}
if (val > i.End) {
min = m + 1;
continue;
}
return m + 1;
}
return false;
return min;
}
public override string ToString()

Loading…
Cancel
Save