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

ncave's JIT and corlib updates #6

Open
wants to merge 1 commit into
base: hackathon-state
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
bin
obj
Debug
Release
*.suo
*.user
*.nupkg
Expand Down
17 changes: 15 additions & 2 deletions src/DNA/corlib/System.Collections.Generic/Comparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
using System.Collections;

namespace System.Collections.Generic {
public abstract class Comparer<T> : IComparer<T>,IComparer {
public abstract class Comparer<T> : IComparer<T>, IComparer {

private sealed class DefaultComparer : Comparer<T> {

Expand All @@ -45,7 +45,6 @@ public override int Compare(T x, T y) {
}
throw new ArgumentException("Does not implement IComparable");
}

}

private sealed class DefaultComparerValueType : Comparer<T> {
Expand All @@ -61,7 +60,21 @@ public override int Compare(T x, T y) {
}
throw new ArgumentException("Does not implement IComparable");
}
}

private sealed class ComparisonComparer : Comparer<T> {
private readonly Comparison<T> comparison;
public ComparisonComparer(Comparison<T> comparison) {
this.comparison = comparison;
}
public override int Compare(T x, T y) {
return comparison(x, y);
}
}

public static Comparer<T> Create(Comparison<T> comparison) {
if (comparison == null) throw new ArgumentNullException("comparison");
return new ComparisonComparer(comparison);
}

static Comparer() {
Expand Down
33 changes: 33 additions & 0 deletions src/DNA/corlib/System.Collections.Generic/IReadOnlyCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// IReadOnlyCollection.cs
//
// Authors:
// Marek Safar <[email protected]>
//
// Copyright (C) 2012 Xamarin, Inc (http://www.xamarin.com)
//
// 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.
//

namespace System.Collections.Generic {
public interface IReadOnlyCollection<T> : IEnumerable<T> {
int Count { get; }
}
}
16 changes: 16 additions & 0 deletions src/DNA/corlib/System.Collections.Generic/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ public void InsertRange(int index, IEnumerable<T> collection) {
}
}

// public void Sort() {
// Array.Sort(this.items, 0, this.size);
// }

public void Sort(Comparison<T> comparison) {
Array.Sort(this.items, 0, this.size, comparison);
}

public void Sort(IComparer<T> comparer) {
Array.Sort(this.items, 0, this.size, comparer);
}

public void Sort(int index, int count, IComparer<T> comparer) {
Array.Sort(this.items, index, count, comparer);
}

public T[] ToArray() {
T[] array = new T[this.size];
Array.Copy(this.items, array, this.size);
Expand Down
245 changes: 245 additions & 0 deletions src/DNA/corlib/System.Collections.Generic/Queue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace System.Collections.Generic {
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable {
T[] _array;
int _head;
int _tail;
int _size;
int _version;

private const int INITIAL_SIZE = 16;

public Queue() {
}

public Queue(int count) {
if (count < 0)
throw new ArgumentOutOfRangeException("count");

_array = new T[count];
}

public Queue(IEnumerable<T> collection) {
if (collection == null)
throw new ArgumentNullException("collection");

foreach (T t in collection)
Enqueue(t);
}

public void Clear() {
if (_array != null)
Array.Clear(_array, 0, _array.Length);

_head = _tail = _size = 0;
_version++;
}

public bool Contains(T item) {
if (item == null) {
foreach (T t in this)
if (t == null)
return true;
} else {
foreach (T t in this)
if (item.Equals(t))
return true;
}

return false;
}

public void CopyTo(T[] array, int idx) {
if (array == null)
throw new ArgumentNullException();

if ((uint)idx > (uint)array.Length)
throw new ArgumentOutOfRangeException();

if (array.Length - idx < _size)
throw new ArgumentOutOfRangeException();

if (_size == 0)
return;

int contents_length = _array.Length;
int length_from_head = contents_length - _head;

Array.Copy(_array, _head, array, idx, Math.Min(_size, length_from_head));
if (_size > length_from_head)
Array.Copy(_array, 0, array,
idx + length_from_head,
_size - length_from_head);

}

void ICollection.CopyTo(Array array, int idx) {
if (array == null)
throw new ArgumentNullException();

if ((uint)idx < (uint)array.Length)
throw new ArgumentOutOfRangeException();

if (array.Length - idx < _size)
throw new ArgumentOutOfRangeException();

if (_size == 0)
return;

try {
int contents_length = _array.Length;
int length_from_head = contents_length - _head;

Array.Copy(_array, _head, array, idx, Math.Min(_size, length_from_head));
if (_size > length_from_head)
Array.Copy(_array, 0, array,
idx + length_from_head,
_size - length_from_head);
} catch (ArrayTypeMismatchException) {
throw new ArgumentException();
}
}

public T Dequeue() {
T ret = Peek();

// clear stuff out to make the GC happy
_array[_head] = default(T);

if (++_head == _array.Length)
_head = 0;
_size--;
_version++;

return ret;
}

public T Peek() {
if (_size == 0)
throw new InvalidOperationException();

return _array[_head];
}

public void Enqueue(T item) {
if (_array == null || _size == _array.Length)
SetCapacity(Math.Max(_size * 2, 4));

_array[_tail] = item;

if (++_tail == _array.Length)
_tail = 0;

_size++;
_version++;
}

public T[] ToArray() {
T[] t = new T[_size];
CopyTo(t, 0);
return t;
}

public void TrimExcess() {
if (_array != null && (_size < _array.Length * 0.9))
SetCapacity(_size);
}

void SetCapacity(int new_size) {
if (_array != null && new_size == _array.Length)
return;

if (new_size < _size)
throw new InvalidOperationException("shouldnt happen");

T[] new_data = new T[new_size];
if (_size > 0)
CopyTo(new_data, 0);

_array = new_data;
_tail = _size;
_head = 0;
_version++;
}

public int Count {
get { return _size; }
}

bool ICollection.IsSynchronized {
get { return false; }
}

object ICollection.SyncRoot {
get { return this; }
}

public Enumerator GetEnumerator() {
return new Enumerator(this);
}

IEnumerator<T> IEnumerable<T>.GetEnumerator() {
return GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}

public struct Enumerator : IEnumerator<T>, IEnumerator, IDisposable {
const int NOT_STARTED = -2;

// this MUST be -1, because we depend on it in move next.
// we just decr the _size, so, 0 - 1 == FINISHED
const int FINISHED = -1;

Queue<T> q;
int idx;
int ver;

internal Enumerator(Queue<T> q) {
this.q = q;
idx = NOT_STARTED;
ver = q._version;
}

public void Dispose() {
idx = NOT_STARTED;
}

public bool MoveNext() {
if (ver != q._version)
throw new InvalidOperationException();

if (idx == NOT_STARTED)
idx = q._size;

return idx != FINISHED && --idx != FINISHED;
}

public T Current {
get {
if (idx < 0)
throw new InvalidOperationException();

return q._array[(q._size - 1 - idx + q._head) % q._array.Length];
}
}

void IEnumerator.Reset() {
if (ver != q._version)
throw new InvalidOperationException();

idx = NOT_STARTED;
}

object IEnumerator.Current {
get { return Current; }
}

}
}
}
Loading