Add compatibility types to the v3.5 runtime (types introduced in .NET 4.0)

This commit is contained in:
Sam Harwell 2013-02-27 13:23:09 -06:00
parent 5d167ce21b
commit 4231313eb1
8 changed files with 2251 additions and 0 deletions

View File

@ -164,6 +164,13 @@
<Compile Include="Sharpen\BitSet.cs" />
<Compile Include="Sharpen\Checksum.cs" />
<Compile Include="Sharpen\Collections.cs" />
<Compile Include="Sharpen\Compat\CollectionDebuggerView`2.cs" />
<Compile Include="Sharpen\Compat\ConcurrentDictionary`2.cs" />
<Compile Include="Sharpen\Compat\IStructuralComparable.cs" />
<Compile Include="Sharpen\Compat\IStructuralEquatable.cs" />
<Compile Include="Sharpen\Compat\SplitOrderedList`2.cs" />
<Compile Include="Sharpen\Compat\Tuple.cs" />
<Compile Include="Sharpen\Compat\Tuples.cs" />
<Compile Include="Sharpen\CRC32.cs" />
<Compile Include="Sharpen\DictionaryExtensions.cs" />
<Compile Include="Sharpen\ListExtensions.cs" />

View File

@ -0,0 +1,79 @@
//
// CollectionDebuggerView.cs
//
// Authors:
// Marek Safar <marek.safar@gmail.com>
//
// Copyright (C) 2009 Novell, Inc (http://www.novell.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.
//
#if !NET_4_0
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Sharpen
{
//
// Custom debugger type proxy to display collections as arrays
//
internal sealed class CollectionDebuggerView<T>
{
readonly ICollection<T> c;
public CollectionDebuggerView (ICollection<T> col)
{
this.c = col;
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public T[] Items {
get {
var o = new T [c.Count];
c.CopyTo (o, 0);
return o;
}
}
}
internal sealed class CollectionDebuggerView<T, U>
{
readonly ICollection<KeyValuePair<T, U>> c;
public CollectionDebuggerView (ICollection<KeyValuePair<T, U>> col)
{
this.c = col;
}
[DebuggerBrowsable (DebuggerBrowsableState.RootHidden)]
public KeyValuePair<T, U>[] Items {
get {
var o = new KeyValuePair<T, U> [c.Count];
c.CopyTo (o, 0);
return o;
}
}
}
}
#endif

View File

@ -0,0 +1,465 @@
// ConcurrentDictionary.cs
//
// Copyright (c) 2009 Jérémie "Garuma" Laval
//
// 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.
//
//
#if !NET_4_0
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Diagnostics;
// declare the namespace so Sharpen-generated using declarations will not produce errors
namespace System.Collections.Concurrent
{
}
namespace Sharpen
{
[DebuggerDisplay ("Count={Count}")]
[DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>,
ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>,
IDictionary, ICollection, IEnumerable
{
IEqualityComparer<TKey> comparer;
SplitOrderedList<TKey, KeyValuePair<TKey, TValue>> internalDictionary;
public ConcurrentDictionary () : this (EqualityComparer<TKey>.Default)
{
}
public ConcurrentDictionary (IEnumerable<KeyValuePair<TKey, TValue>> collection)
: this (collection, EqualityComparer<TKey>.Default)
{
}
public ConcurrentDictionary (IEqualityComparer<TKey> comparer)
{
this.comparer = comparer;
this.internalDictionary = new SplitOrderedList<TKey, KeyValuePair<TKey, TValue>> (comparer);
}
public ConcurrentDictionary (IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
: this (comparer)
{
foreach (KeyValuePair<TKey, TValue> pair in collection)
Add (pair.Key, pair.Value);
}
// Parameters unused
public ConcurrentDictionary (int concurrencyLevel, int capacity)
: this (EqualityComparer<TKey>.Default)
{
}
public ConcurrentDictionary (int concurrencyLevel,
IEnumerable<KeyValuePair<TKey, TValue>> collection,
IEqualityComparer<TKey> comparer)
: this (collection, comparer)
{
}
// Parameters unused
public ConcurrentDictionary (int concurrencyLevel, int capacity, IEqualityComparer<TKey> comparer)
: this (comparer)
{
}
void CheckKey (TKey key)
{
if (key == null)
throw new ArgumentNullException ("key");
}
void Add (TKey key, TValue value)
{
while (!TryAdd (key, value));
}
void IDictionary<TKey, TValue>.Add (TKey key, TValue value)
{
Add (key, value);
}
public bool TryAdd (TKey key, TValue value)
{
CheckKey (key);
return internalDictionary.Insert (Hash (key), key, Make (key, value));
}
void ICollection<KeyValuePair<TKey,TValue>>.Add (KeyValuePair<TKey, TValue> pair)
{
Add (pair.Key, pair.Value);
}
public TValue AddOrUpdate (TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
{
CheckKey (key);
if (addValueFactory == null)
throw new ArgumentNullException ("addValueFactory");
if (updateValueFactory == null)
throw new ArgumentNullException ("updateValueFactory");
return internalDictionary.InsertOrUpdate (Hash (key),
key,
() => Make (key, addValueFactory (key)),
(e) => Make (key, updateValueFactory (key, e.Value))).Value;
}
public TValue AddOrUpdate (TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
{
return AddOrUpdate (key, (_) => addValue, updateValueFactory);
}
TValue AddOrUpdate (TKey key, TValue addValue, TValue updateValue)
{
CheckKey (key);
return internalDictionary.InsertOrUpdate (Hash (key),
key,
Make (key, addValue),
Make (key, updateValue)).Value;
}
TValue GetValue (TKey key)
{
TValue temp;
if (!TryGetValue (key, out temp))
throw new KeyNotFoundException (key.ToString ());
return temp;
}
public bool TryGetValue (TKey key, out TValue value)
{
CheckKey (key);
KeyValuePair<TKey, TValue> pair;
bool result = internalDictionary.Find (Hash (key), key, out pair);
value = pair.Value;
return result;
}
public bool TryUpdate (TKey key, TValue newValue, TValue comparisonValue)
{
CheckKey (key);
return internalDictionary.CompareExchange (Hash (key), key, Make (key, newValue), (e) => e.Value.Equals (comparisonValue));
}
public TValue this[TKey key] {
get {
return GetValue (key);
}
set {
AddOrUpdate (key, value, value);
}
}
public TValue GetOrAdd (TKey key, Func<TKey, TValue> valueFactory)
{
CheckKey (key);
return internalDictionary.InsertOrGet (Hash (key), key, Make (key, default(TValue)), () => Make (key, valueFactory (key))).Value;
}
public TValue GetOrAdd (TKey key, TValue value)
{
CheckKey (key);
return internalDictionary.InsertOrGet (Hash (key), key, Make (key, value), null).Value;
}
public bool TryRemove (TKey key, out TValue value)
{
CheckKey (key);
KeyValuePair<TKey, TValue> data;
bool result = internalDictionary.Delete (Hash (key), key, out data);
value = data.Value;
return result;
}
bool Remove (TKey key)
{
TValue dummy;
return TryRemove (key, out dummy);
}
bool IDictionary<TKey, TValue>.Remove (TKey key)
{
return Remove (key);
}
bool ICollection<KeyValuePair<TKey,TValue>>.Remove (KeyValuePair<TKey,TValue> pair)
{
return Remove (pair.Key);
}
public bool ContainsKey (TKey key)
{
CheckKey (key);
KeyValuePair<TKey, TValue> dummy;
return internalDictionary.Find (Hash (key), key, out dummy);
}
bool IDictionary.Contains (object key)
{
if (!(key is TKey))
return false;
return ContainsKey ((TKey)key);
}
void IDictionary.Remove (object key)
{
if (!(key is TKey))
return;
Remove ((TKey)key);
}
object IDictionary.this [object key]
{
get {
if (!(key is TKey))
throw new ArgumentException ("key isn't of correct type", "key");
return this[(TKey)key];
}
set {
if (!(key is TKey) || !(value is TValue))
throw new ArgumentException ("key or value aren't of correct type");
this[(TKey)key] = (TValue)value;
}
}
void IDictionary.Add (object key, object value)
{
if (!(key is TKey) || !(value is TValue))
throw new ArgumentException ("key or value aren't of correct type");
Add ((TKey)key, (TValue)value);
}
bool ICollection<KeyValuePair<TKey,TValue>>.Contains (KeyValuePair<TKey, TValue> pair)
{
return ContainsKey (pair.Key);
}
public KeyValuePair<TKey,TValue>[] ToArray ()
{
// This is most certainly not optimum but there is
// not a lot of possibilities
return new List<KeyValuePair<TKey,TValue>> (this).ToArray ();
}
public void Clear()
{
// Pronk
internalDictionary = new SplitOrderedList<TKey, KeyValuePair<TKey, TValue>> (comparer);
}
public int Count {
get {
return internalDictionary.Count;
}
}
public bool IsEmpty {
get {
return Count == 0;
}
}
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
get {
return false;
}
}
bool IDictionary.IsReadOnly {
get {
return false;
}
}
public ICollection<TKey> Keys {
get {
return GetPart<TKey> ((kvp) => kvp.Key);
}
}
public ICollection<TValue> Values {
get {
return GetPart<TValue> ((kvp) => kvp.Value);
}
}
ICollection IDictionary.Keys {
get {
return (ICollection)Keys;
}
}
ICollection IDictionary.Values {
get {
return (ICollection)Values;
}
}
ICollection<T> GetPart<T> (Func<KeyValuePair<TKey, TValue>, T> extractor)
{
List<T> temp = new List<T> ();
foreach (KeyValuePair<TKey, TValue> kvp in this)
temp.Add (extractor (kvp));
return temp.AsReadOnly ();
}
void ICollection.CopyTo (Array array, int startIndex)
{
KeyValuePair<TKey, TValue>[] arr = array as KeyValuePair<TKey, TValue>[];
if (arr == null)
return;
CopyTo (arr, startIndex, Count);
}
void CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex)
{
CopyTo (array, startIndex, Count);
}
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex)
{
CopyTo (array, startIndex);
}
void CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex, int num)
{
foreach (var kvp in this) {
array [startIndex++] = kvp;
if (--num <= 0)
return;
}
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator ()
{
return GetEnumeratorInternal ();
}
IEnumerator IEnumerable.GetEnumerator ()
{
return (IEnumerator)GetEnumeratorInternal ();
}
IEnumerator<KeyValuePair<TKey, TValue>> GetEnumeratorInternal ()
{
return internalDictionary.GetEnumerator ();
}
IDictionaryEnumerator IDictionary.GetEnumerator ()
{
return new ConcurrentDictionaryEnumerator (GetEnumeratorInternal ());
}
class ConcurrentDictionaryEnumerator : IDictionaryEnumerator
{
IEnumerator<KeyValuePair<TKey, TValue>> internalEnum;
public ConcurrentDictionaryEnumerator (IEnumerator<KeyValuePair<TKey, TValue>> internalEnum)
{
this.internalEnum = internalEnum;
}
public bool MoveNext ()
{
return internalEnum.MoveNext ();
}
public void Reset ()
{
internalEnum.Reset ();
}
public object Current {
get {
return Entry;
}
}
public DictionaryEntry Entry {
get {
KeyValuePair<TKey, TValue> current = internalEnum.Current;
return new DictionaryEntry (current.Key, current.Value);
}
}
public object Key {
get {
return internalEnum.Current.Key;
}
}
public object Value {
get {
return internalEnum.Current.Value;
}
}
}
object ICollection.SyncRoot {
get {
return this;
}
}
bool IDictionary.IsFixedSize {
get {
return false;
}
}
bool ICollection.IsSynchronized {
get { return true; }
}
static KeyValuePair<U, V> Make<U, V> (U key, V value)
{
return new KeyValuePair<U, V> (key, value);
}
uint Hash (TKey key)
{
return (uint)comparer.GetHashCode (key);
}
}
}
#endif

View File

@ -0,0 +1,41 @@
//
// IStructuralComparable.cs
//
// Authors:
// Zoltan Varga (vargaz@gmail.com)
//
// Copyright (C) 2009 Novell
//
// 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.
//
#if !NET_4_0
using System;
using System.Collections;
namespace Sharpen
{
public interface IStructuralComparable {
int CompareTo (object other, IComparer comparer);
}
}
#endif

View File

@ -0,0 +1,43 @@
//
// IStructuralEquatable.cs
//
// Authors:
// Zoltan Varga (vargaz@gmail.com)
//
// Copyright (C) 2009 Novell
//
// 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.
//
#if !NET_4_0
using System;
using System.Collections;
namespace Sharpen
{
public interface IStructuralEquatable {
bool Equals (object other, IEqualityComparer comparer);
int GetHashCode (IEqualityComparer comparer);
}
}
#endif

View File

@ -0,0 +1,506 @@
// SplitOrderedList.cs
//
// Copyright (c) 2010 Jérémie "Garuma" Laval
//
// 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.
//
//
#if !NET_4_0
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Sharpen
{
internal class SplitOrderedList<TKey, T>
{
class Node
{
public bool Marked;
public ulong Key;
public TKey SubKey;
public T Data;
public Node Next;
public Node Init (ulong key, TKey subKey, T data)
{
this.Key = key;
this.SubKey = subKey;
this.Data = data;
this.Marked = false;
this.Next = null;
return this;
}
// Used to create dummy node
public Node Init (ulong key)
{
this.Key = key;
this.Data = default (T);
this.Next = null;
this.Marked = false;
this.SubKey = default (TKey);
return this;
}
// Used to create marked node
public Node Init (Node wrapped)
{
this.Marked = true;
this.Next = wrapped;
this.Key = 0;
this.Data = default (T);
this.SubKey = default (TKey);
return this;
}
}
const int MaxLoad = 5;
const uint BucketSize = 512;
Node head;
Node tail;
Node[] buckets = new Node [BucketSize];
int count;
int size = 2;
SimpleRwLock slim = new SimpleRwLock ();
readonly IEqualityComparer<TKey> comparer;
public SplitOrderedList (IEqualityComparer<TKey> comparer)
{
this.comparer = comparer;
head = new Node ().Init (0);
tail = new Node ().Init (ulong.MaxValue);
head.Next = tail;
SetBucket (0, head);
}
public int Count {
get {
return count;
}
}
public T InsertOrUpdate (uint key, TKey subKey, Func<T> addGetter, Func<T, T> updateGetter)
{
Node current;
bool result = InsertInternal (key, subKey, default (T), addGetter, out current);
if (result)
return current.Data;
// FIXME: this should have a CAS-like behavior
return current.Data = updateGetter (current.Data);
}
public T InsertOrUpdate (uint key, TKey subKey, T addValue, T updateValue)
{
Node current;
if (InsertInternal (key, subKey, addValue, null, out current))
return current.Data;
// FIXME: this should have a CAS-like behavior
return current.Data = updateValue;
}
public bool Insert (uint key, TKey subKey, T data)
{
Node current;
return InsertInternal (key, subKey, data, null, out current);
}
public T InsertOrGet (uint key, TKey subKey, T data, Func<T> dataCreator)
{
Node current;
InsertInternal (key, subKey, data, dataCreator, out current);
return current.Data;
}
bool InsertInternal (uint key, TKey subKey, T data, Func<T> dataCreator, out Node current)
{
Node node = new Node ().Init (ComputeRegularKey (key), subKey, data);
uint b = key % (uint)size;
Node bucket;
if ((bucket = GetBucket (b)) == null)
bucket = InitializeBucket (b);
if (!ListInsert (node, bucket, out current, dataCreator))
return false;
int csize = size;
if (Interlocked.Increment (ref count) / csize > MaxLoad && (csize & 0x40000000) == 0)
Interlocked.CompareExchange (ref size, 2 * csize, csize);
current = node;
return true;
}
public bool Find (uint key, TKey subKey, out T data)
{
Node node;
uint b = key % (uint)size;
data = default (T);
Node bucket;
if ((bucket = GetBucket (b)) == null)
bucket = InitializeBucket (b);
if (!ListFind (ComputeRegularKey (key), subKey, bucket, out node))
return false;
data = node.Data;
return !node.Marked;
}
public bool CompareExchange (uint key, TKey subKey, T data, Func<T, bool> check)
{
Node node;
uint b = key % (uint)size;
Node bucket;
if ((bucket = GetBucket (b)) == null)
bucket = InitializeBucket (b);
if (!ListFind (ComputeRegularKey (key), subKey, bucket, out node))
return false;
if (!check (node.Data))
return false;
node.Data = data;
return true;
}
public bool Delete (uint key, TKey subKey, out T data)
{
uint b = key % (uint)size;
Node bucket;
if ((bucket = GetBucket (b)) == null)
bucket = InitializeBucket (b);
if (!ListDelete (bucket, ComputeRegularKey (key), subKey, out data))
return false;
Interlocked.Decrement (ref count);
return true;
}
public IEnumerator<T> GetEnumerator ()
{
Node node = head.Next;
while (node != tail) {
while (node.Marked || (node.Key & 1) == 0) {
node = node.Next;
if (node == tail)
yield break;
}
yield return node.Data;
node = node.Next;
}
}
Node InitializeBucket (uint b)
{
Node current;
uint parent = GetParent (b);
Node bucket;
if ((bucket = GetBucket (parent)) == null)
bucket = InitializeBucket (parent);
Node dummy = new Node ().Init (ComputeDummyKey (b));
if (!ListInsert (dummy, bucket, out current, null))
return current;
return SetBucket (b, dummy);
}
// Turn v's MSB off
static uint GetParent (uint v)
{
uint t, tt;
// Find MSB position in v
var pos = (tt = v >> 16) > 0 ?
(t = tt >> 8) > 0 ? 24 + logTable[t] : 16 + logTable[tt] :
(t = v >> 8) > 0 ? 8 + logTable[t] : logTable[v];
return (uint)(v & ~(1 << pos));
}
// Reverse integer bits and make sure LSB is set
static ulong ComputeRegularKey (uint key)
{
return ComputeDummyKey (key) | 1;
}
// Reverse integer bits
static ulong ComputeDummyKey (uint key)
{
return ((ulong)(((uint)reverseTable[key & 0xff] << 24) |
((uint)reverseTable[(key >> 8) & 0xff] << 16) |
((uint)reverseTable[(key >> 16) & 0xff] << 8) |
((uint)reverseTable[(key >> 24) & 0xff]))) << 1;
}
// Bucket storage is abstracted in a simple two-layer tree to avoid too much memory resize
Node GetBucket (uint index)
{
if (index >= buckets.Length)
return null;
return buckets[index];
}
Node SetBucket (uint index, Node node)
{
try {
slim.EnterReadLock ();
CheckSegment (index, true);
Interlocked.CompareExchange (ref buckets[index], node, null);
return buckets[index];
} finally {
slim.ExitReadLock ();
}
}
// When we run out of space for bucket storage, we use a lock-based array resize
void CheckSegment (uint segment, bool readLockTaken)
{
if (segment < buckets.Length)
return;
if (readLockTaken)
slim.ExitReadLock ();
try {
slim.EnterWriteLock ();
while (segment >= buckets.Length)
Array.Resize (ref buckets, buckets.Length * 2);
} finally {
slim.ExitWriteLock ();
}
if (readLockTaken)
slim.EnterReadLock ();
}
Node ListSearch (ulong key, TKey subKey, ref Node left, Node h)
{
Node leftNodeNext = null, rightNode = null;
do {
Node t = h;
Node tNext = t.Next;
do {
if (!tNext.Marked) {
left = t;
leftNodeNext = tNext;
}
t = tNext.Marked ? tNext.Next : tNext;
if (t == tail)
break;
tNext = t.Next;
} while (tNext.Marked || t.Key < key || (tNext.Key == key && !comparer.Equals (subKey, t.SubKey)));
rightNode = t;
if (leftNodeNext == rightNode) {
if (rightNode != tail && rightNode.Next.Marked)
continue;
else
return rightNode;
}
if (Interlocked.CompareExchange (ref left.Next, rightNode, leftNodeNext) == leftNodeNext) {
if (rightNode != tail && rightNode.Next.Marked)
continue;
else
return rightNode;
}
} while (true);
}
bool ListDelete (Node startPoint, ulong key, TKey subKey, out T data)
{
Node rightNode = null, rightNodeNext = null, leftNode = null;
data = default (T);
Node markedNode = null;
do {
rightNode = ListSearch (key, subKey, ref leftNode, startPoint);
if (rightNode == tail || rightNode.Key != key || !comparer.Equals (subKey, rightNode.SubKey))
return false;
data = rightNode.Data;
rightNodeNext = rightNode.Next;
if (!rightNodeNext.Marked) {
if (markedNode == null)
markedNode = new Node ();
markedNode.Init (rightNodeNext);
if (Interlocked.CompareExchange (ref rightNode.Next, markedNode, rightNodeNext) == rightNodeNext)
break;
}
} while (true);
if (Interlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNode)
ListSearch (rightNode.Key, subKey, ref leftNode, startPoint);
return true;
}
bool ListInsert (Node newNode, Node startPoint, out Node current, Func<T> dataCreator)
{
ulong key = newNode.Key;
Node rightNode = null, leftNode = null;
do {
rightNode = current = ListSearch (key, newNode.SubKey, ref leftNode, startPoint);
if (rightNode != tail && rightNode.Key == key && comparer.Equals (newNode.SubKey, rightNode.SubKey))
return false;
newNode.Next = rightNode;
if (dataCreator != null)
newNode.Data = dataCreator ();
if (Interlocked.CompareExchange (ref leftNode.Next, newNode, rightNode) == rightNode)
return true;
} while (true);
}
bool ListFind (ulong key, TKey subKey, Node startPoint, out Node data)
{
Node rightNode = null, leftNode = null;
data = null;
rightNode = ListSearch (key, subKey, ref leftNode, startPoint);
data = rightNode;
return rightNode != tail && rightNode.Key == key && comparer.Equals (subKey, rightNode.SubKey);
}
static readonly byte[] reverseTable = {
0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
};
static readonly byte[] logTable = {
0xFF, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
};
struct SimpleRwLock
{
const int RwWait = 1;
const int RwWrite = 2;
const int RwRead = 4;
int rwlock;
public void EnterReadLock ()
{
SpinWait sw = new SpinWait ();
do {
while ((rwlock & (RwWrite | RwWait)) > 0)
sw.SpinOnce ();
if ((Interlocked.Add (ref rwlock, RwRead) & (RwWait | RwWait)) == 0)
return;
Interlocked.Add (ref rwlock, -RwRead);
} while (true);
}
public void ExitReadLock ()
{
Interlocked.Add (ref rwlock, -RwRead);
}
public void EnterWriteLock ()
{
SpinWait sw = new SpinWait ();
do {
int state = rwlock;
if (state < RwWrite) {
if (Interlocked.CompareExchange (ref rwlock, RwWrite, state) == state)
return;
state = rwlock;
}
// We register our interest in taking the Write lock (if upgradeable it's already done)
while ((state & RwWait) == 0 && Interlocked.CompareExchange (ref rwlock, state | RwWait, state) != state)
state = rwlock;
// Before falling to sleep
while (rwlock > RwWait)
sw.SpinOnce ();
} while (true);
}
public void ExitWriteLock ()
{
Interlocked.Add (ref rwlock, -RwWrite);
}
}
}
internal struct SpinWait
{
// The number of step until SpinOnce yield on multicore machine
const int step = 10;
const int maxTime = 200;
static readonly bool isSingleCpu = (Environment.ProcessorCount == 1);
int ntime;
public void SpinOnce ()
{
ntime += 1;
if (isSingleCpu) {
// On a single-CPU system, spinning does no good
Thread.Sleep (0);
} else {
if (ntime % step == 0)
Thread.Sleep (0);
else
// Multi-CPU system might be hyper-threaded, let other thread run
Thread.SpinWait (Math.Min (ntime, maxTime) << 1);
}
}
}
}
#endif

View File

@ -0,0 +1,115 @@
//
// Tuple.cs
//
// Authors:
// Zoltan Varga (vargaz@gmail.com)
//
// Copyright (C) 2009 Novell
//
// 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.
//
#if !NET_4_0
using System;
namespace Sharpen
{
public static class Tuple
{
public static Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>> Create<T1, T2, T3, T4, T5, T6, T7, T8>
(
T1 item1,
T2 item2,
T3 item3,
T4 item4,
T5 item5,
T6 item6,
T7 item7,
T8 item8) {
return new Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>> (item1, item2, item3, item4, item5, item6, item7, new Tuple<T8> (item8));
}
public static Tuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>
(
T1 item1,
T2 item2,
T3 item3,
T4 item4,
T5 item5,
T6 item6,
T7 item7) {
return new Tuple<T1, T2, T3, T4, T5, T6, T7> (item1, item2, item3, item4, item5, item6, item7);
}
public static Tuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>
(
T1 item1,
T2 item2,
T3 item3,
T4 item4,
T5 item5,
T6 item6) {
return new Tuple<T1, T2, T3, T4, T5, T6> (item1, item2, item3, item4, item5, item6);
}
public static Tuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>
(
T1 item1,
T2 item2,
T3 item3,
T4 item4,
T5 item5) {
return new Tuple<T1, T2, T3, T4, T5> (item1, item2, item3, item4, item5);
}
public static Tuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>
(
T1 item1,
T2 item2,
T3 item3,
T4 item4) {
return new Tuple<T1, T2, T3, T4> (item1, item2, item3, item4);
}
public static Tuple<T1, T2, T3> Create<T1, T2, T3>
(
T1 item1,
T2 item2,
T3 item3) {
return new Tuple<T1, T2, T3> (item1, item2, item3);
}
public static Tuple<T1, T2> Create<T1, T2>
(
T1 item1,
T2 item2) {
return new Tuple<T1, T2> (item1, item2);
}
public static Tuple<T1> Create<T1>
(
T1 item1) {
return new Tuple<T1> (item1);
}
}
}
#endif

View File

@ -0,0 +1,995 @@
//
// Tuples.cs
//
// Authors:
// Zoltan Varga (vargaz@gmail.com)
// Marek Safar (marek.safar@gmail.com)
//
// Copyright (C) 2009 Novell
//
// 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.
//
#if !NET_4_0
using System;
using System.Collections;
using System.Collections.Generic;
namespace Sharpen
{
public partial class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>
{
public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest)
{
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
this.item4 = item4;
this.item5 = item5;
this.item6 = item6;
this.item7 = item7;
this.rest = rest;
bool ok = true;
if (!typeof (TRest).IsGenericType)
ok = false;
if (ok) {
Type t = typeof (TRest).GetGenericTypeDefinition ();
if (!(t == typeof (Tuple<>) || t == typeof (Tuple<,>) || t == typeof (Tuple<,,>) || t == typeof (Tuple<,,,>) || t == typeof (Tuple<,,,,>) || t == typeof (Tuple <,,,,,>) || t == typeof (Tuple<,,,,,,>) || t == typeof (Tuple<,,,,,,,>)))
ok = false;
}
if (!ok)
throw new ArgumentException ("rest", "The last element of an eight element tuple must be a Tuple.");
}
}
/* The rest is generated by the script at the bottom */
[Serializable]
public class Tuple<T1> : IStructuralEquatable, IStructuralComparable, IComparable
{
T1 item1;
public Tuple (T1 item1)
{
this.item1 = item1;
}
public T1 Item1 {
get { return item1; }
}
int IComparable.CompareTo (object obj)
{
return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo (object other, IComparer comparer)
{
var t = other as Tuple<T1>;
if (t == null) {
if (other == null) return 1;
throw new ArgumentException ("other");
}
return comparer.Compare (item1, t.item1);
}
public override bool Equals (object obj)
{
return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
}
bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
{
var t = other as Tuple<T1>;
if (t == null)
return false;
return comparer.Equals (item1, t.item1);
}
public override int GetHashCode ()
{
return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
}
int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
{
return comparer.GetHashCode (item1);
}
public override string ToString ()
{
return String.Format ("({0})", item1);
}
}
[Serializable]
public class Tuple<T1, T2> : IStructuralEquatable, IStructuralComparable, IComparable
{
T1 item1;
T2 item2;
public Tuple (T1 item1, T2 item2)
{
this.item1 = item1;
this.item2 = item2;
}
public T1 Item1 {
get { return item1; }
}
public T2 Item2 {
get { return item2; }
}
int IComparable.CompareTo (object obj)
{
return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo (object other, IComparer comparer)
{
var t = other as Tuple<T1, T2>;
if (t == null) {
if (other == null) return 1;
throw new ArgumentException ("other");
}
int res = comparer.Compare (item1, t.item1);
if (res != 0) return res;
return comparer.Compare (item2, t.item2);
}
public override bool Equals (object obj)
{
return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
}
bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
{
var t = other as Tuple<T1, T2>;
if (t == null)
return false;
return comparer.Equals (item1, t.item1) &&
comparer.Equals (item2, t.item2);
}
public override int GetHashCode ()
{
return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
}
int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
{
int h = comparer.GetHashCode (item1);
h = (h << 5) - h + comparer.GetHashCode (item2);
return h;
}
public override string ToString ()
{
return String.Format ("({0}, {1})", item1, item2);
}
}
[Serializable]
public class Tuple<T1, T2, T3> : IStructuralEquatable, IStructuralComparable, IComparable
{
T1 item1;
T2 item2;
T3 item3;
public Tuple (T1 item1, T2 item2, T3 item3)
{
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
}
public T1 Item1 {
get { return item1; }
}
public T2 Item2 {
get { return item2; }
}
public T3 Item3 {
get { return item3; }
}
int IComparable.CompareTo (object obj)
{
return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo (object other, IComparer comparer)
{
var t = other as Tuple<T1, T2, T3>;
if (t == null) {
if (other == null) return 1;
throw new ArgumentException ("other");
}
int res = comparer.Compare (item1, t.item1);
if (res != 0) return res;
res = comparer.Compare (item2, t.item2);
if (res != 0) return res;
return comparer.Compare (item3, t.item3);
}
public override bool Equals (object obj)
{
return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
}
bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
{
var t = other as Tuple<T1, T2, T3>;
if (t == null)
return false;
return comparer.Equals (item1, t.item1) &&
comparer.Equals (item2, t.item2) &&
comparer.Equals (item3, t.item3);
}
public override int GetHashCode ()
{
return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
}
int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
{
int h = comparer.GetHashCode (item1);
h = (h << 5) - h + comparer.GetHashCode (item2);
h = (h << 5) - h + comparer.GetHashCode (item3);
return h;
}
public override string ToString ()
{
return String.Format ("({0}, {1}, {2})", item1, item2, item3);
}
}
[Serializable]
public class Tuple<T1, T2, T3, T4> : IStructuralEquatable, IStructuralComparable, IComparable
{
T1 item1;
T2 item2;
T3 item3;
T4 item4;
public Tuple (T1 item1, T2 item2, T3 item3, T4 item4)
{
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
this.item4 = item4;
}
public T1 Item1 {
get { return item1; }
}
public T2 Item2 {
get { return item2; }
}
public T3 Item3 {
get { return item3; }
}
public T4 Item4 {
get { return item4; }
}
int IComparable.CompareTo (object obj)
{
return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo (object other, IComparer comparer)
{
var t = other as Tuple<T1, T2, T3, T4>;
if (t == null) {
if (other == null) return 1;
throw new ArgumentException ("other");
}
int res = comparer.Compare (item1, t.item1);
if (res != 0) return res;
res = comparer.Compare (item2, t.item2);
if (res != 0) return res;
res = comparer.Compare (item3, t.item3);
if (res != 0) return res;
return comparer.Compare (item4, t.item4);
}
public override bool Equals (object obj)
{
return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
}
bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
{
var t = other as Tuple<T1, T2, T3, T4>;
if (t == null)
return false;
return comparer.Equals (item1, t.item1) &&
comparer.Equals (item2, t.item2) &&
comparer.Equals (item3, t.item3) &&
comparer.Equals (item4, t.item4);
}
public override int GetHashCode ()
{
return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
}
int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
{
int h = comparer.GetHashCode (item1);
h = (h << 5) - h + comparer.GetHashCode (item2);
h = (h << 5) - h + comparer.GetHashCode (item3);
h = (h << 5) - h + comparer.GetHashCode (item4);
return h;
}
public override string ToString ()
{
return String.Format ("({0}, {1}, {2}, {3})", item1, item2, item3, item4);
}
}
[Serializable]
public class Tuple<T1, T2, T3, T4, T5> : IStructuralEquatable, IStructuralComparable, IComparable
{
T1 item1;
T2 item2;
T3 item3;
T4 item4;
T5 item5;
public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
{
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
this.item4 = item4;
this.item5 = item5;
}
public T1 Item1 {
get { return item1; }
}
public T2 Item2 {
get { return item2; }
}
public T3 Item3 {
get { return item3; }
}
public T4 Item4 {
get { return item4; }
}
public T5 Item5 {
get { return item5; }
}
int IComparable.CompareTo (object obj)
{
return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo (object other, IComparer comparer)
{
var t = other as Tuple<T1, T2, T3, T4, T5>;
if (t == null) {
if (other == null) return 1;
throw new ArgumentException ("other");
}
int res = comparer.Compare (item1, t.item1);
if (res != 0) return res;
res = comparer.Compare (item2, t.item2);
if (res != 0) return res;
res = comparer.Compare (item3, t.item3);
if (res != 0) return res;
res = comparer.Compare (item4, t.item4);
if (res != 0) return res;
return comparer.Compare (item5, t.item5);
}
public override bool Equals (object obj)
{
return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
}
bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
{
var t = other as Tuple<T1, T2, T3, T4, T5>;
if (t == null)
return false;
return comparer.Equals (item1, t.item1) &&
comparer.Equals (item2, t.item2) &&
comparer.Equals (item3, t.item3) &&
comparer.Equals (item4, t.item4) &&
comparer.Equals (item5, t.item5);
}
public override int GetHashCode ()
{
return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
}
int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
{
int h = comparer.GetHashCode (item1);
h = (h << 5) - h + comparer.GetHashCode (item2);
h = (h << 5) - h + comparer.GetHashCode (item3);
h = (h << 5) - h + comparer.GetHashCode (item4);
h = (h << 5) - h + comparer.GetHashCode (item5);
return h;
}
public override string ToString ()
{
return String.Format ("({0}, {1}, {2}, {3}, {4})", item1, item2, item3, item4, item5);
}
}
[Serializable]
public class Tuple<T1, T2, T3, T4, T5, T6> : IStructuralEquatable, IStructuralComparable, IComparable
{
T1 item1;
T2 item2;
T3 item3;
T4 item4;
T5 item5;
T6 item6;
public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
{
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
this.item4 = item4;
this.item5 = item5;
this.item6 = item6;
}
public T1 Item1 {
get { return item1; }
}
public T2 Item2 {
get { return item2; }
}
public T3 Item3 {
get { return item3; }
}
public T4 Item4 {
get { return item4; }
}
public T5 Item5 {
get { return item5; }
}
public T6 Item6 {
get { return item6; }
}
int IComparable.CompareTo (object obj)
{
return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo (object other, IComparer comparer)
{
var t = other as Tuple<T1, T2, T3, T4, T5, T6>;
if (t == null) {
if (other == null) return 1;
throw new ArgumentException ("other");
}
int res = comparer.Compare (item1, t.item1);
if (res != 0) return res;
res = comparer.Compare (item2, t.item2);
if (res != 0) return res;
res = comparer.Compare (item3, t.item3);
if (res != 0) return res;
res = comparer.Compare (item4, t.item4);
if (res != 0) return res;
res = comparer.Compare (item5, t.item5);
if (res != 0) return res;
return comparer.Compare (item6, t.item6);
}
public override bool Equals (object obj)
{
return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
}
bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
{
var t = other as Tuple<T1, T2, T3, T4, T5, T6>;
if (t == null)
return false;
return comparer.Equals (item1, t.item1) &&
comparer.Equals (item2, t.item2) &&
comparer.Equals (item3, t.item3) &&
comparer.Equals (item4, t.item4) &&
comparer.Equals (item5, t.item5) &&
comparer.Equals (item6, t.item6);
}
public override int GetHashCode ()
{
return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
}
int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
{
int h = comparer.GetHashCode (item1);
h = (h << 5) - h + comparer.GetHashCode (item2);
h = (h << 5) - h + comparer.GetHashCode (item3);
h = (h << 5) - h + comparer.GetHashCode (item4);
h = (h << 5) - h + comparer.GetHashCode (item5);
h = (h << 5) - h + comparer.GetHashCode (item6);
return h;
}
public override string ToString ()
{
return String.Format ("({0}, {1}, {2}, {3}, {4}, {5})", item1, item2, item3, item4, item5, item6);
}
}
[Serializable]
public class Tuple<T1, T2, T3, T4, T5, T6, T7> : IStructuralEquatable, IStructuralComparable, IComparable
{
T1 item1;
T2 item2;
T3 item3;
T4 item4;
T5 item5;
T6 item6;
T7 item7;
public Tuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
{
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
this.item4 = item4;
this.item5 = item5;
this.item6 = item6;
this.item7 = item7;
}
public T1 Item1 {
get { return item1; }
}
public T2 Item2 {
get { return item2; }
}
public T3 Item3 {
get { return item3; }
}
public T4 Item4 {
get { return item4; }
}
public T5 Item5 {
get { return item5; }
}
public T6 Item6 {
get { return item6; }
}
public T7 Item7 {
get { return item7; }
}
int IComparable.CompareTo (object obj)
{
return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo (object other, IComparer comparer)
{
var t = other as Tuple<T1, T2, T3, T4, T5, T6, T7>;
if (t == null) {
if (other == null) return 1;
throw new ArgumentException ("other");
}
int res = comparer.Compare (item1, t.item1);
if (res != 0) return res;
res = comparer.Compare (item2, t.item2);
if (res != 0) return res;
res = comparer.Compare (item3, t.item3);
if (res != 0) return res;
res = comparer.Compare (item4, t.item4);
if (res != 0) return res;
res = comparer.Compare (item5, t.item5);
if (res != 0) return res;
res = comparer.Compare (item6, t.item6);
if (res != 0) return res;
return comparer.Compare (item7, t.item7);
}
public override bool Equals (object obj)
{
return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
}
bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
{
var t = other as Tuple<T1, T2, T3, T4, T5, T6, T7>;
if (t == null)
return false;
return comparer.Equals (item1, t.item1) &&
comparer.Equals (item2, t.item2) &&
comparer.Equals (item3, t.item3) &&
comparer.Equals (item4, t.item4) &&
comparer.Equals (item5, t.item5) &&
comparer.Equals (item6, t.item6) &&
comparer.Equals (item7, t.item7);
}
public override int GetHashCode ()
{
return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
}
int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
{
int h = comparer.GetHashCode (item1);
h = (h << 5) - h + comparer.GetHashCode (item2);
h = (h << 5) - h + comparer.GetHashCode (item3);
h = (h << 5) - h + comparer.GetHashCode (item4);
h = (h << 5) - h + comparer.GetHashCode (item5);
h = (h << 5) - h + comparer.GetHashCode (item6);
h = (h << 5) - h + comparer.GetHashCode (item7);
return h;
}
public override string ToString ()
{
return String.Format ("({0}, {1}, {2}, {3}, {4}, {5}, {6})", item1, item2, item3, item4, item5, item6, item7);
}
}
[Serializable]
public partial class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> : IStructuralEquatable, IStructuralComparable, IComparable
{
T1 item1;
T2 item2;
T3 item3;
T4 item4;
T5 item5;
T6 item6;
T7 item7;
TRest rest;
public T1 Item1 {
get { return item1; }
}
public T2 Item2 {
get { return item2; }
}
public T3 Item3 {
get { return item3; }
}
public T4 Item4 {
get { return item4; }
}
public T5 Item5 {
get { return item5; }
}
public T6 Item6 {
get { return item6; }
}
public T7 Item7 {
get { return item7; }
}
public TRest Rest {
get { return rest; }
}
int IComparable.CompareTo (object obj)
{
return ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);
}
int IStructuralComparable.CompareTo (object other, IComparer comparer)
{
var t = other as Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>;
if (t == null) {
if (other == null) return 1;
throw new ArgumentException ("other");
}
int res = comparer.Compare (item1, t.item1);
if (res != 0) return res;
res = comparer.Compare (item2, t.item2);
if (res != 0) return res;
res = comparer.Compare (item3, t.item3);
if (res != 0) return res;
res = comparer.Compare (item4, t.item4);
if (res != 0) return res;
res = comparer.Compare (item5, t.item5);
if (res != 0) return res;
res = comparer.Compare (item6, t.item6);
if (res != 0) return res;
res = comparer.Compare (item7, t.item7);
if (res != 0) return res;
return comparer.Compare (rest, t.rest);
}
public override bool Equals (object obj)
{
return ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);
}
bool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)
{
var t = other as Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>;
if (t == null)
return false;
return comparer.Equals (item1, t.item1) &&
comparer.Equals (item2, t.item2) &&
comparer.Equals (item3, t.item3) &&
comparer.Equals (item4, t.item4) &&
comparer.Equals (item5, t.item5) &&
comparer.Equals (item6, t.item6) &&
comparer.Equals (item7, t.item7) &&
comparer.Equals (rest, t.rest);
}
public override int GetHashCode ()
{
return ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);
}
int IStructuralEquatable.GetHashCode (IEqualityComparer comparer)
{
int h = comparer.GetHashCode (item1);
h = (h << 5) - h + comparer.GetHashCode (item2);
h = (h << 5) - h + comparer.GetHashCode (item3);
h = (h << 5) - h + comparer.GetHashCode (item4);
h = (h << 5) - h + comparer.GetHashCode (item5);
h = (h << 5) - h + comparer.GetHashCode (item6);
h = (h << 5) - h + comparer.GetHashCode (item7);
h = (h << 5) - h + comparer.GetHashCode (rest);
return h;
}
public override string ToString ()
{
return String.Format ("({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})", item1, item2, item3, item4, item5, item6, item7, rest);
}
}
}
#endif
#if FALSE
//
// generator script
//
using System;
using System.Text;
public class TupleGen
{
public static void Main () {
for (int arity = 1; arity < 9; ++arity) {
string type_name = GetTypeName (arity);
Console.WriteLine ("\t[Serializable]");
Console.Write ("\tpublic {0}class ", arity < 8 ? null : "partial ");
Console.Write (type_name);
Console.WriteLine (" : IStructuralEquatable, IStructuralComparable, IComparable");
Console.WriteLine ("\t{");
for (int i = 1; i <= arity; ++i)
Console.WriteLine ("\t\t{0} {1};", GetItemTypeName (i), GetItemName (i));
if (arity < 8) {
Console.WriteLine ();
Console.Write ("\t\tpublic Tuple (");
for (int i = 1; i <= arity; ++i) {
Console.Write ("{0} {1}", GetItemTypeName (i), GetItemName (i));
if (i < arity)
Console.Write (", ");
}
Console.WriteLine (")");
Console.WriteLine ("\t\t{");
for (int i = 1; i <= arity; ++i)
Console.WriteLine ("\t\t\t this.{0} = {0};", GetItemName (i));
Console.WriteLine ("\t\t}");
}
for (int i = 1; i <= arity; ++i) {
Console.WriteLine ();
Console.WriteLine ("\t\tpublic {0} {1} {{", GetItemTypeName (i),
System.Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase (GetItemName (i)));
Console.Write ("\t\t\tget { ");
Console.WriteLine ("return {0}; }}", GetItemName (i));
Console.WriteLine ("\t\t}");
}
Console.WriteLine ();
Console.WriteLine ("\t\tint IComparable.CompareTo (object obj)");
Console.WriteLine ("\t\t{");
Console.WriteLine ("\t\t\treturn ((IStructuralComparable) this).CompareTo (obj, Comparer<object>.Default);");
Console.WriteLine ("\t\t}");
Console.WriteLine ();
Console.WriteLine ("\t\tint IStructuralComparable.CompareTo (object other, IComparer comparer)");
Console.WriteLine ("\t\t{");
Console.WriteLine ("\t\t\tvar t = other as {0};", type_name);
Console.WriteLine ("\t\t\tif (t == null) {");
Console.WriteLine ("\t\t\t\tif (other == null) return 1;");
Console.WriteLine ("\t\t\t\tthrow new ArgumentException ("other");");
Console.WriteLine ("\t\t\t}");
Console.WriteLine ();
for (int i = 1; i < arity; ++i) {
Console.Write ("\t\t\t");
if (i == 1)
Console.Write ("int ");
Console.WriteLine ("res = comparer.Compare ({0}, t.{0});", GetItemName (i));
Console.WriteLine ("\t\t\tif (res != 0) return res;");
}
Console.WriteLine ("\t\t\treturn comparer.Compare ({0}, t.{0});", GetItemName (arity));
Console.WriteLine ("\t\t}");
Console.WriteLine ();
Console.WriteLine ("\t\tpublic override bool Equals (object obj)");
Console.WriteLine ("\t\t{");
Console.WriteLine ("\t\t\treturn ((IStructuralEquatable) this).Equals (obj, EqualityComparer<object>.Default);");
Console.WriteLine ("\t\t}");
Console.WriteLine ();
Console.WriteLine ("\t\tbool IStructuralEquatable.Equals (object other, IEqualityComparer comparer)");
Console.WriteLine ("\t\t{");
Console.WriteLine ("\t\t\tvar t = other as {0};", type_name);
Console.WriteLine ("\t\t\tif (t == null)");
Console.WriteLine ("\t\t\t\treturn false;");
Console.WriteLine ();
Console.Write ("\t\t\treturn");
for (int i = 1; i <= arity; ++i) {
if (i == 1)
Console.Write (" ");
else
Console.Write ("\t\t\t\t");
Console.Write ("comparer.Equals ({0}, t.{0})", GetItemName (i));
if (i != arity)
Console.WriteLine (" &&");
else
Console.WriteLine (";");
}
Console.WriteLine ("\t\t}");
Console.WriteLine ();
Console.WriteLine ("\t\tpublic override int GetHashCode ()");
Console.WriteLine ("\t\t{");
Console.WriteLine ("\t\t\treturn ((IStructuralEquatable) this).GetHashCode (EqualityComparer<object>.Default);");
Console.WriteLine ("\t\t}");
Console.WriteLine ();
Console.WriteLine ("\t\tint IStructuralEquatable.GetHashCode (IEqualityComparer comparer)");
Console.WriteLine ("\t\t{");
if (arity == 1) {
Console.WriteLine ("\t\t\treturn comparer.GetHashCode ({0});", GetItemName (arity));
} else {
Console.WriteLine ("\t\t\tint h = comparer.GetHashCode ({0});", GetItemName (1));
for (int i = 2; i <= arity; ++i)
Console.WriteLine ("\t\t\th = (h << 5) - h + comparer.GetHashCode ({0});", GetItemName (i));
Console.WriteLine ("\t\t\treturn h;");
}
Console.WriteLine ("\t\t}");
Console.WriteLine ();
Console.WriteLine ("\t\tpublic override string ToString ()");
Console.WriteLine ("\t\t{");
Console.Write ("\t\t\treturn String.Format (\"(");
for (int i = 1; i <= arity; ++i) {
Console.Write ("{" + (i - 1) + "}");
if (i < arity)
Console.Write (", ");
}
Console.Write (")\", ");
for (int i = 1; i <= arity; ++i) {
Console.Write (GetItemName (i));
if (i < arity)
Console.Write (", ");
}
Console.WriteLine (");");
Console.WriteLine ("\t\t}");
Console.WriteLine ("\t}\n");
}
}
static string GetTypeName (int arity)
{
StringBuilder sb = new StringBuilder ();
sb.Append ("Tuple<");
for (int i = 1; i <= arity; ++i) {
sb.Append (GetItemTypeName (i));
if (i < arity)
sb.Append (", ");
}
sb.Append (">");
return sb.ToString ();
}
static string GetItemName (int arity)
{
return arity < 8 ? "item" + arity.ToString () : "rest";
}
static string GetItemTypeName (int arity)
{
return arity < 8 ? "T" + arity.ToString () : "TRest";
}
}
#endif