Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / Microsoft / Scripting / Utils / CollectionExtensions.cs / 1305376 / CollectionExtensions.cs
/* ****************************************************************************
*
* 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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
#if !MICROSOFT_SCRIPTING_CORE
using System.Diagnostics.Contracts;
#endif
using System.Runtime.CompilerServices;
namespace System.Dynamic.Utils {
internal static class CollectionExtensions {
///
/// Wraps the provided enumerable into a ReadOnlyCollection{T}
///
/// Copies all of the data into a new array, so the data can't be
/// changed after creation. The exception is if the enumerable is
/// already a ReadOnlyCollection{T}, in which case we just return it.
///
#if !MICROSOFT_SCRIPTING_CORE
[Pure]
#endif
internal static ReadOnlyCollection ToReadOnly(this IEnumerable enumerable) {
if (enumerable == null) {
return EmptyReadOnlyCollection.Instance;
}
var troc = enumerable as TrueReadOnlyCollection;
if (troc != null) {
return troc;
}
var builder = enumerable as ReadOnlyCollectionBuilder;
if (builder != null) {
return builder.ToReadOnlyCollection();
}
var collection = enumerable as ICollection;
if (collection != null) {
int count = collection.Count;
if (count == 0) {
return EmptyReadOnlyCollection.Instance;
}
T[] clone = new T[count];
collection.CopyTo(clone, 0);
return new TrueReadOnlyCollection(clone);
}
// ToArray trims the excess space and speeds up access
return new TrueReadOnlyCollection(new List(enumerable).ToArray());
}
// We could probably improve the hashing here
internal static int ListHashCode(this IEnumerable list) {
var cmp = EqualityComparer.Default;
int h = 6551;
foreach (T t in list) {
h ^= (h << 5) ^ cmp.GetHashCode(t);
}
return h;
}
#if !MICROSOFT_SCRIPTING_CORE
[Pure]
#endif
internal static bool ListEquals(this ICollection first, ICollection second) {
if (first.Count != second.Count) {
return false;
}
var cmp = EqualityComparer.Default;
var f = first.GetEnumerator();
var s = second.GetEnumerator();
while (f.MoveNext()) {
s.MoveNext();
if (!cmp.Equals(f.Current, s.Current)) {
return false;
}
}
return true;
}
internal static IEnumerable Select(this IEnumerable enumerable, Func select) {
foreach (T t in enumerable) {
yield return select(t);
}
}
// Name needs to be different so it doesn't conflict with Enumerable.Select
internal static U[] Map(this ICollection collection, Func select) {
int count = collection.Count;
U[] result = new U[count];
count = 0;
foreach (T t in collection) {
result[count++] = select(t);
}
return result;
}
internal static IEnumerable Where(this IEnumerable enumerable, Func where) {
foreach (T t in enumerable) {
if (where(t)) {
yield return t;
}
}
}
internal static bool Any(this IEnumerable source, Func predicate) {
foreach (T element in source) {
if (predicate(element)) {
return true;
}
}
return false;
}
internal static bool All(this IEnumerable source, Func predicate) {
foreach (T element in source) {
if (!predicate(element)) {
return false;
}
}
return true;
}
internal static T[] RemoveFirst(this T[] array) {
T[] result = new T[array.Length - 1];
Array.Copy(array, 1, result, 0, result.Length);
return result;
}
internal static T[] RemoveLast(this T[] array) {
T[] result = new T[array.Length - 1];
Array.Copy(array, 0, result, 0, result.Length);
return result;
}
internal static T[] AddFirst(this IList list, T item) {
T[] res = new T[list.Count + 1];
res[0] = item;
list.CopyTo(res, 1);
return res;
}
internal static T[] AddLast(this IList list, T item) {
T[] res = new T[list.Count + 1];
list.CopyTo(res, 0);
res[list.Count] = item;
return res;
}
internal static T First(this IEnumerable source) {
var list = source as IList;
if (list != null) {
return list[0];
}
using (var e = source.GetEnumerator()) {
if (e.MoveNext()) return e.Current;
}
throw new InvalidOperationException();
}
internal static T Last(this IList list) {
return list[list.Count - 1];
}
internal static T[] Copy(this T[] array) {
T[] copy = new T[array.Length];
Array.Copy(array, copy, array.Length);
return copy;
}
}
internal static class EmptyReadOnlyCollection {
internal static ReadOnlyCollection Instance = new TrueReadOnlyCollection(new T[0]);
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
/* ****************************************************************************
*
* 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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
#if !MICROSOFT_SCRIPTING_CORE
using System.Diagnostics.Contracts;
#endif
using System.Runtime.CompilerServices;
namespace System.Dynamic.Utils {
internal static class CollectionExtensions {
///
/// Wraps the provided enumerable into a ReadOnlyCollection{T}
///
/// Copies all of the data into a new array, so the data can't be
/// changed after creation. The exception is if the enumerable is
/// already a ReadOnlyCollection{T}, in which case we just return it.
///
#if !MICROSOFT_SCRIPTING_CORE
[Pure]
#endif
internal static ReadOnlyCollection ToReadOnly(this IEnumerable enumerable) {
if (enumerable == null) {
return EmptyReadOnlyCollection.Instance;
}
var troc = enumerable as TrueReadOnlyCollection;
if (troc != null) {
return troc;
}
var builder = enumerable as ReadOnlyCollectionBuilder;
if (builder != null) {
return builder.ToReadOnlyCollection();
}
var collection = enumerable as ICollection;
if (collection != null) {
int count = collection.Count;
if (count == 0) {
return EmptyReadOnlyCollection.Instance;
}
T[] clone = new T[count];
collection.CopyTo(clone, 0);
return new TrueReadOnlyCollection(clone);
}
// ToArray trims the excess space and speeds up access
return new TrueReadOnlyCollection(new List(enumerable).ToArray());
}
// We could probably improve the hashing here
internal static int ListHashCode(this IEnumerable list) {
var cmp = EqualityComparer.Default;
int h = 6551;
foreach (T t in list) {
h ^= (h << 5) ^ cmp.GetHashCode(t);
}
return h;
}
#if !MICROSOFT_SCRIPTING_CORE
[Pure]
#endif
internal static bool ListEquals(this ICollection first, ICollection second) {
if (first.Count != second.Count) {
return false;
}
var cmp = EqualityComparer.Default;
var f = first.GetEnumerator();
var s = second.GetEnumerator();
while (f.MoveNext()) {
s.MoveNext();
if (!cmp.Equals(f.Current, s.Current)) {
return false;
}
}
return true;
}
internal static IEnumerable Select(this IEnumerable enumerable, Func select) {
foreach (T t in enumerable) {
yield return select(t);
}
}
// Name needs to be different so it doesn't conflict with Enumerable.Select
internal static U[] Map(this ICollection collection, Func select) {
int count = collection.Count;
U[] result = new U[count];
count = 0;
foreach (T t in collection) {
result[count++] = select(t);
}
return result;
}
internal static IEnumerable Where(this IEnumerable enumerable, Func where) {
foreach (T t in enumerable) {
if (where(t)) {
yield return t;
}
}
}
internal static bool Any(this IEnumerable source, Func predicate) {
foreach (T element in source) {
if (predicate(element)) {
return true;
}
}
return false;
}
internal static bool All(this IEnumerable source, Func predicate) {
foreach (T element in source) {
if (!predicate(element)) {
return false;
}
}
return true;
}
internal static T[] RemoveFirst(this T[] array) {
T[] result = new T[array.Length - 1];
Array.Copy(array, 1, result, 0, result.Length);
return result;
}
internal static T[] RemoveLast(this T[] array) {
T[] result = new T[array.Length - 1];
Array.Copy(array, 0, result, 0, result.Length);
return result;
}
internal static T[] AddFirst(this IList list, T item) {
T[] res = new T[list.Count + 1];
res[0] = item;
list.CopyTo(res, 1);
return res;
}
internal static T[] AddLast(this IList list, T item) {
T[] res = new T[list.Count + 1];
list.CopyTo(res, 0);
res[list.Count] = item;
return res;
}
internal static T First(this IEnumerable source) {
var list = source as IList;
if (list != null) {
return list[0];
}
using (var e = source.GetEnumerator()) {
if (e.MoveNext()) return e.Current;
}
throw new InvalidOperationException();
}
internal static T Last(this IList list) {
return list[list.Count - 1];
}
internal static T[] Copy(this T[] array) {
T[] copy = new T[array.Length];
Array.Copy(array, copy, array.Length);
return copy;
}
}
internal static class EmptyReadOnlyCollection {
internal static ReadOnlyCollection Instance = new TrueReadOnlyCollection(new T[0]);
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SafeEventLogWriteHandle.cs
- ITreeGenerator.cs
- DataGridDefaultColumnWidthTypeConverter.cs
- X509Certificate.cs
- DataSourceCache.cs
- PolyLineSegmentFigureLogic.cs
- cache.cs
- GenericTransactionFlowAttribute.cs
- PerformanceCounterLib.cs
- OpacityConverter.cs
- ApplicationDirectory.cs
- GlobalizationAssembly.cs
- DataGridViewMethods.cs
- DateTimeConverter2.cs
- DataGridViewRowPrePaintEventArgs.cs
- ComPlusContractBehavior.cs
- AspNetHostingPermission.cs
- NameTable.cs
- XPathNodeList.cs
- PartialTrustHelpers.cs
- XsdCachingReader.cs
- IUnknownConstantAttribute.cs
- HtmlHistory.cs
- HighContrastHelper.cs
- MetadataHelper.cs
- XmlAutoDetectWriter.cs
- Speller.cs
- ActivityCodeGenerator.cs
- ControllableStoryboardAction.cs
- CompressionTransform.cs
- QueryPageSettingsEventArgs.cs
- HttpProfileBase.cs
- MissingSatelliteAssemblyException.cs
- LambdaExpression.cs
- ProcessHostConfigUtils.cs
- DetailsViewRow.cs
- GridViewPageEventArgs.cs
- Crypto.cs
- ScrollContentPresenter.cs
- ArrayWithOffset.cs
- CodeIdentifier.cs
- ReachPageContentCollectionSerializerAsync.cs
- StaticExtension.cs
- PersonalizableTypeEntry.cs
- TransportContext.cs
- Point3DAnimation.cs
- FilteredDataSetHelper.cs
- ReadOnlyHierarchicalDataSourceView.cs
- WindowsGraphics.cs
- WCFServiceClientProxyGenerator.cs
- ping.cs
- StateMachineSubscriptionManager.cs
- DataContractSerializer.cs
- AnimatedTypeHelpers.cs
- PropertyItemInternal.cs
- SubclassTypeValidator.cs
- OleDbParameter.cs
- Quack.cs
- ImageListStreamer.cs
- TraceData.cs
- DataGridViewComboBoxCell.cs
- RowsCopiedEventArgs.cs
- HitTestParameters.cs
- MemberNameValidator.cs
- LocatorManager.cs
- ConfigurationPermission.cs
- ConnectionProviderAttribute.cs
- Command.cs
- TiffBitmapEncoder.cs
- WindowsListViewSubItem.cs
- PrincipalPermissionMode.cs
- AnimationStorage.cs
- WindowsIdentity.cs
- TagMapInfo.cs
- EnterpriseServicesHelper.cs
- GlyphManager.cs
- DataGridPageChangedEventArgs.cs
- SynchronizationFilter.cs
- PrintPreviewControl.cs
- DocComment.cs
- ClientBuildManagerCallback.cs
- VerticalAlignConverter.cs
- AudioException.cs
- TextBoxDesigner.cs
- TextEncodedRawTextWriter.cs
- Geometry3D.cs
- followingquery.cs
- ThreadSafeMessageFilterTable.cs
- EntityKeyElement.cs
- PropertyGeneratedEventArgs.cs
- NamespaceDisplayAutomationPeer.cs
- ControlDesigner.cs
- CqlLexerHelpers.cs
- InfoCardRSAOAEPKeyExchangeFormatter.cs
- MonitoringDescriptionAttribute.cs
- MSAAWinEventWrap.cs
- MimeWriter.cs
- DataDocumentXPathNavigator.cs
- SQLBinaryStorage.cs
- OleDbRowUpdatedEvent.cs