1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Microsoft Public License. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Microsoft Public License, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Microsoft Public License.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Contracts;
namespace Microsoft.Scripting.Utils {
/// <summary>
/// Allows wrapping of proxy types (like COM RCWs) to expose their IEnumerable functionality
/// which is supported after casting to IEnumerable, even though Reflection will not indicate
/// IEnumerable as a supported interface
/// </summary>
public class EnumerableWrapper : IEnumerable {
private IEnumerable _wrappedObject;
public EnumerableWrapper(IEnumerable o) {
_wrappedObject = o;
}
public IEnumerator GetEnumerator() {
return _wrappedObject.GetEnumerator();
}
}
public static class CollectionUtils {
public static void AddRange<T>(ICollection<T> collection, IEnumerable<T> items) {
ContractUtils.RequiresNotNull(collection, "collection");
ContractUtils.RequiresNotNull(items, "items");
List<T> list = collection as List<T>;
if (list != null) {
list.AddRange(items);
} else {
foreach (T item in items) {
collection.Add(item);
}
}
}
public static IEnumerable<T> ToEnumerable<T>(IEnumerable enumerable) {
foreach (T item in enumerable) {
yield return item;
}
}
public static IEnumerator<TSuper> ToCovariant<T, TSuper>(IEnumerator<T> enumerator)
where T : TSuper {
ContractUtils.RequiresNotNull(enumerator, "enumerator");
while (enumerator.MoveNext()) {
yield return enumerator.Current;
}
}
public static IEnumerable<TSuper> ToCovariant<T, TSuper>(IEnumerable<T> enumerable)
where T : TSuper {
return new CovariantConvertor<T, TSuper>(enumerable);
}
private class CovariantConvertor<T, TSuper> : IEnumerable<TSuper> where T : TSuper {
private IEnumerable<T> _enumerable;
public CovariantConvertor(IEnumerable<T> enumerable) {
ContractUtils.RequiresNotNull(enumerable, "enumerable");
_enumerable = enumerable;
}
[Pure]
public IEnumerator<TSuper> GetEnumerator() {
return CollectionUtils.ToCovariant<T, TSuper>(_enumerable.GetEnumerator());
}
[Pure]
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}
public static List<T> MakeList<T>(T item) {
List<T> result = new List<T>();
result.Add(item);
return result;
}
public static int CountOf<T>(IList<T> list, T item) where T : IEquatable<T> {
if (list == null) return 0;
int result = 0;
for (int i = 0; i < list.Count; i++) {
if (list[i].Equals(item)) {
result++;
}
}
return result;
}
public static int Max(this IEnumerable<int> values) {
ContractUtils.RequiresNotNull(values, "values");
int result = Int32.MinValue;
foreach (var value in values) {
if (value > result) {
result = value;
}
}
return result;
}
public static bool TrueForAll<T>(IEnumerable<T> collection, Predicate<T> predicate) {
ContractUtils.RequiresNotNull(collection, "collection");
ContractUtils.RequiresNotNull(predicate, "predicate");
foreach (T item in collection) {
if (!predicate(item)) return false;
}
return true;
}
public static List<T> GetRange<T>(IList<T> list, int index, int count) {
ContractUtils.RequiresNotNull(list, "list");
ContractUtils.RequiresArrayRange(list, index, count, "index", "count");
List<T> result = new List<T>(count);
int stop = index + count;
for (int i = index; i < stop; i++) {
result.Add(list[i]);
}
return result;
}
public static void InsertRange<T>(IList<T> collection, int index, IEnumerable<T> items) {
ContractUtils.RequiresNotNull(collection, "collection");
ContractUtils.RequiresNotNull(items, "items");
ContractUtils.RequiresArrayInsertIndex(collection, index, "index");
List<T> list = collection as List<T>;
if (list != null) {
list.InsertRange(index, items);
} else {
int i = index;
foreach (T obj in items) {
collection.Insert(i++, obj);
}
}
}
public static void RemoveRange<T>(IList<T> collection, int index, int count) {
ContractUtils.RequiresNotNull(collection, "collection");
ContractUtils.RequiresArrayRange(collection, index, count, "index", "count");
List<T> list = collection as List<T>;
if (list != null) {
list.RemoveRange(index, count);
} else {
for (int i = index + count - 1; i >= index; i--) {
collection.RemoveAt(i);
}
}
}
public static int FindIndex<T>(this IList<T> collection, Predicate<T> predicate) {
ContractUtils.RequiresNotNull(collection, "collection");
ContractUtils.RequiresNotNull(predicate, "predicate");
for (int i = 0; i < collection.Count; i++) {
if (predicate(collection[i])) {
return i;
}
}
return -1;
}
public static IList<T> ToSortedList<T>(this ICollection<T> collection, Comparison<T> comparison) {
ContractUtils.RequiresNotNull(collection, "collection");
ContractUtils.RequiresNotNull(comparison, "comparison");
var array = new T[collection.Count];
collection.CopyTo(array, 0);
Array.Sort(array, comparison);
return array;
}
}
}
|