Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dialac dev #16

Open
wants to merge 2 commits into
base: add-system-sorting
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guessing you can delete this? git rm is the command you want.

74 changes: 74 additions & 0 deletions src/System.Sorting/src/InsertionSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace System.Sorting
{
public class InsertionSorting : Sorting
{
public InsertionSorting() { }
internal override void SortInternal<Tkey>(Tkey[] array, int index, int length, IComparer<Tkey> comparer)
{

//Cheking the input parameters conditions.
Contract.Requires(array != null);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use Debug.Assert instead, as that's more in line with what we do other places in CoreFX.

Contract.Requires(index >= 0);
Contract.Requires(length >= 0);
Contract.Requires(length <= array.Length);
Contract.Requires(length + index <= array.Length);

int i, j;
int high = index + length - 1;
Tkey t;
for (i = index; i < high; i++)
{
j = i;
t = array[i + 1];
while (j >= index && comparer.Compare(t, array[j]) < 0)
{
array[j + 1] = array[j];
j--;
}
array[j + 1] = t;
}
}


internal override void SortInternal<TKey, TValue>(TKey[] keys, TValue[] items, int index, int length, IComparer<TKey> comparer)
{
Contract.Requires(keys != null);
Contract.Requires(items != null);
Contract.Requires(comparer != null);
Contract.Requires(index >= 0);
Contract.Requires(length >= 0);
Contract.Requires(length <= keys.Length);
Contract.Requires(length + index <= keys.Length);
Contract.Requires(length + index <= items.Length);

if (length < 2)
return;

int i, j;
int high = length + index - 1;
TKey t;
TValue tValue;

for (i = index; i < high; i++)
{
j = i;
t = keys[i + 1];
tValue = (items != null) ? items[i + 1] : default(TValue);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above you Contract.Required that items was not null, but now here you do a test again. Is one of these branches dead code or is the above assert incorrect?

while (j >= index && comparer.Compare(t, keys[j]) < 0)
{
keys[j + 1] = keys[j];
if (items != null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar feedback about this guard.

items[j + 1] = items[j];
j--;
}
keys[j + 1] = t;
if (items != null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again.

items[j + 1] = tValue;
}
}
}
}
20 changes: 20 additions & 0 deletions src/System.Sorting/src/IntroSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;

namespace System.Sorting
{
public class IntroSorting : Sorting
{
public IntroSorting()
{ }
internal override void SortInternal<TKey>(TKey[] array, int index, int length, IComparer<TKey> comparer)
{
//It will call the current clr of Array.Sort Implementation; Just a wrap
Array.Sort<TKey>(array, index, length, comparer);
}
internal override void SortInternal<TKey, TValue>(TKey[] keys, TValue[] items, int index, int length, IComparer<TKey> comparer)
{
//It will call the current clr of Array.Sort Implementation; Just a wrap
Array.Sort<TKey, TValue>(keys, items, index, length, comparer);
}
}
}
223 changes: 223 additions & 0 deletions src/System.Sorting/src/Sort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics.Contracts;
using System.Text;

namespace System.Sorting
{

/// <summary>
/// Base class that defines the signature methods for possible derived classes
/// which will be implementing diffrent types of Sorting Algorithms.
/// </summary>
public abstract class Sorting
{

public Sorting ()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to remove this empty .ctor.

{ }

#region Methods of array of keys only

/// <summary>
/// Sorts the elements of an array.
/// </summary>
/// <param name="array">array to be sorted</param>
public virtual void Sort<T>(T[] array) //where T : IComparable
{
if (array == null)
throw new ArgumentNullException("array");
Sort<T>(array, array.GetLowerBound(0), array.Length, null);
}


/// <summary>
/// Sorts the elements in a section of an array. The sort compares the
/// elements to each other using the given IComparer interface. If
/// comparer is null, the elements are compared to each other using
/// the IComparable interface, which in that case must be implemented
/// by all elements in the given section of the array.
/// </summary>
/// <param name="array"></param>
/// <param name="comparer"></param>
public virtual void Sort<T> (T[] array, IComparer<T> comparer) //where T : IComparable
{
if (array == null)
throw new ArgumentNullException("array");
Sort<T>(array, array.GetLowerBound(0), array.Length, comparer);
}


/// <summary>
/// Sorts the elements in a section of an array. The sort compares the
/// elements to each other using the IComparable interface, which
/// must be implemented by all elements in the given section of the array.
/// </summary>
/// <param name="array"></param>
/// <param name="index"></param>
/// <param name="length"></param>
public virtual void Sort<T>(T[] array, int index, int length) //where T : IComparable
{
if (array == null)
throw new ArgumentNullException("array");
this.Sort<T>(array, index, length, null);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Consider removing the "this" prefix.

}


/// <summary>
/// Sorts the elements in a section of an array. The sort compares the
/// elements to each other using the given IComparer interface. If
/// comparer is null, the elements are compared to each other using
/// the IComparable interface, which in that case must be implemented
/// by all elements in the given section of the array.
/// </summary>
/// <param name="array"></param>
/// <param name="index"></param>
/// <param name="length"></param>
/// <param name="comparer"></param>
public void Sort<T> (T[] array, int index, int length, IComparer<T> comparer) //where T : IComparable
{
if (array == null)
throw new ArgumentNullException("array");
if (index < 0 || length < 0)
//throw new ArgumentOutOfRangeException((length < 0 ? "length" : "index"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
throw new ArgumentOutOfRangeException((length < 0 ? "length" : "index"), "ArgumentOutOfRange_NeedNonNegNum");
if (array.Length - index < length)
//throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
throw new ArgumentException("Argument_InvalidOffLen");

if (length < 2)
return;

//Delegating the implementation to the derived classes which will implement different types of sorting algorithms.
SortInternal<T>(array, index, length, comparer);

}


/// <summary>
/// Sorts the elements in a section of an array. The sort compares the
/// elements to each other using the given IComparer interface. If
/// comparer is null, the elements are compared to each other using
/// the IComparable interface, which in that case must be implemented
/// by all elements in the given section of the array.
/// </summary>
/// <param name="array"></param>
/// <param name="index"></param>
/// <param name="length"></param>
/// <param name="comparer"></param>
internal virtual void SortInternal<T>(T[] array, int index, int length, IComparer<T> comparer) //where T : IComparable
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if this was protected and abstract. It being abstract will require it to be implemented.

The convention we follow for methods were you have to implement them but not do argument checking is to suffix the method with the word "Core", so I would also rename this to SortCore.

{
// Each type of derived sorting Class will implement a different type of sorting algorithm
}

#endregion

#region Methods of arrays of keys and values pair

/// <summary>
/// Sorts the elements of two arrays based on the keys in the first array.
/// Elements in the keys array specify the sort keys for
/// corresponding elements in the items array. The sort compares the
/// keys to each other using the IComparable interface, which must be
/// implemented by all elements of the keys array.
/// </summary>
/// <param name="array">array to be sorted</param>
public virtual void Sort<TKey, TValue>(TKey[] keys, TValue[] items) //where TKey : IComparable
{
if (keys == null)
throw new ArgumentNullException("keys");
Sort<TKey, TValue>(keys, items, keys.GetLowerBound(0), keys.Length, null);
}

/// <summary>
/// Sorts the elements of two arrays based on the keys in the first array.
/// Elements in the keys array specify the sort keys for
/// corresponding elements in the items array. The sort compares the
/// keys to each other using the given IComparer interface. If
/// comparer is null, the elements are compared to each other using
/// the IComparable interface, which in that case must be implemented
/// by all elements of the keys array
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="keys"></param>
/// <param name="items"></param>
/// <param name="comparer"></param>
public virtual void Sort<TKey, TValue>(TKey[] keys, TValue[] items, IComparer<TKey> comparer) //where TKey : IComparable
{
if (keys == null)
throw new ArgumentNullException("keys");
Sort<TKey>(keys, keys.GetLowerBound(0), keys.Length, comparer);
}


/// <summary>
/// Sorts the elements in a section of two arrays based on the keys in the
/// first array. Elements in the keys array specify the sort keys for
/// corresponding elements in the items array. The sort compares the
/// keys to each other using the given IComparer interface. If
/// comparer is null, the elements are compared to each other using
/// the IComparable interface, which in that case must be implemented
/// by all elements of the given section of the keys array.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="keys"></param>
/// <param name="items"></param>
/// <param name="index"></param>
/// <param name="length"></param>
public virtual void Sort<TKey, TValue>(TKey[] keys, TValue[] items, int index, int length) //where TKey : IComparable
{
if (keys == null)
throw new ArgumentNullException("keys");
Sort<TKey, TValue>(keys, items, index, length, null);
}

/// <summary>
/// Sorts the elements in a section of two arrays based on the keys in the
/// first array. Elements in the keys array specify the sort keys for
/// corresponding elements in the items array. The sort compares the
/// keys to each other using the given IComparer interface. If
/// comparer is null, the elements are compared to each other using
/// the IComparable interface, which in that case must be implemented
/// by all elements of the given section of the keys array.
/// </summary>
/// <param name="keys"></param>
/// <param name="items"></param>
/// <param name="index"></param>
/// <param name="length"></param>
/// <param name="comparer"></param>
public void Sort <TKey, TValue>(TKey[] keys, TValue[] items, int index, int length, IComparer<TKey> comparer) //where TKey : IComparable
{
if (keys == null)
throw new ArgumentNullException("keys");
if (index < 0 || length < 0)
//throw new ArgumentOutOfRangeException((length < 0 ? "length" : "index"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
throw new ArgumentOutOfRangeException((length < 0 ? "length" : "index"), "ArgumentOutOfRange_NeedNonNegNum");
if (keys.Length - index < length || (items != null && index > items.Length - length))
//throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
throw new ArgumentException("Argument_InvalidOffLen");

if (length < 2)
return;

if (items == null)
{
Sort<TKey>(keys, index, length, comparer);
return;
}

SortInternal(keys, items, index, length, comparer);
}

internal virtual void SortInternal<T, TValue>(T[] keys, TValue[] items, int index, int length, IComparer<T> comparer) //where T : IComparable
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same feedback about visibility and abstractness.

{
// Each type of derived sorting Class will implement a different type of sorting algorithm
}
#endregion

}


}
3 changes: 3 additions & 0 deletions src/System.Sorting/src/System.Sorting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
<!-- Compiled Source Files -->
<ItemGroup>
<Compile Include="InsertionSort.cs" />
<Compile Include="IntroSort.cs" />
<Compile Include="Sort.cs" />
</ItemGroup>
<!-- Resource files -->
<ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions src/System.Sorting/tests/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of this file? Was it to make things easier to test while you were developing? I think we should remove it since in general we just use XUnit to run the tests.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Sorting.Tests
{
class Program
{
public static void Main (String[] args)
{
SortTests.TestSort();

}
}
}
Loading