提交 58b38b9a 编写于 作者: T TomasMatousek

ImmutableArrayExtensions should be internal - we shouldn't be exposing public...

ImmutableArrayExtensions should be internal - we shouldn't be exposing public extension methods on collection types. (changeset 1392421)
上级 83e43491
......@@ -113,6 +113,7 @@
<Compile Include="Collections\ImmutableArrayExtensions.cs" />
<Compile Include="Collections\ImmutableMemoryStream.cs" />
<Compile Include="Collections\OrderedSet.cs" />
<Compile Include="Collections\StaticCast.cs" />
<Compile Include="Compilation\SymbolFilter.cs" />
<Compile Include="DiagnosticAnalyzer\CompilerDiagnosticAnalyzer.CompilationAnalyzer.cs" />
<Compile Include="DiagnosticAnalyzer\CompilerDiagnosticAnalyzer.cs" />
......@@ -265,7 +266,6 @@
<Compile Include="InternalUtilities\ISetExtensions.cs" />
<Compile Include="InternalUtilities\KeyValuePair.cs" />
<Compile Include="InternalUtilities\MultiDictionary.cs" />
<Compile Include="InternalUtilities\ObjectPool`1.cs" />
<Compile Include="InternalUtilities\OneOrMany.cs" />
<Compile Include="InternalUtilities\ReaderWriterLockSlimExtensions.cs" />
<Compile Include="InternalUtilities\ReadOnlyUnmanagedMemoryStream.cs" />
......
......@@ -6,16 +6,14 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// The collection of extension methods for the <see cref="ImmutableArray{T}"/> type
/// </summary>
public static partial class ImmutableArrayExtensions
internal static class ImmutableArrayExtensions
{
/// <summary>
/// Converts a sequence to an immutable array.
......@@ -213,11 +211,13 @@ public static ImmutableArray<T> WhereAsArray<T>(this ImmutableArray<T> array, Fu
{
continue;
}
Debug.Assert(i > 0);
if (builder == null)
{
builder = ArrayBuilder<T>.GetInstance();
}
builder.Add(a);
}
else
......@@ -227,6 +227,7 @@ public static ImmutableArray<T> WhereAsArray<T>(this ImmutableArray<T> array, Fu
all = false;
continue;
}
Debug.Assert(i > 0);
if (all)
{
......@@ -258,30 +259,9 @@ public static ImmutableArray<T> WhereAsArray<T>(this ImmutableArray<T> array, Fu
}
}
/// <summary>
/// Adds all elements of the immutable array into the list; The list must not be null.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
/// <param name="list"></param>
/// <param name="items"></param>
public static void AddRange<T, U>(this List<T> list, ImmutableArray<U> items) where U : T
{
Debug.Assert(list != null);
foreach (var u in items)
{
list.Add(u);
}
}
/// <summary>
/// Casts the immutable array of a Type to an immutable array of its base type.
/// </summary>
/// <typeparam name="TDerived"></typeparam>
/// <typeparam name="TBase"></typeparam>
/// <param name="items"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ImmutableArray<TBase> Cast<TDerived, TBase>(this ImmutableArray<TDerived> items)
where TDerived : class, TBase
......@@ -311,7 +291,7 @@ public static bool SetEquals<T>(this ImmutableArray<T> array1, ImmutableArray<T>
var count1 = array1.Length;
var count2 = array2.Length;
//avoid constructing HashSets in these common cases
// avoid constructing HashSets in these common cases
if (count1 == 0)
{
return count2 == 0;
......@@ -338,9 +318,6 @@ public static bool SetEquals<T>(this ImmutableArray<T> array1, ImmutableArray<T>
/// <summary>
/// Returns an empty array if the input array is null (defaut)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static ImmutableArray<T> NullToEmpty<T>(this ImmutableArray<T> array)
{
return array.IsDefault ? ImmutableArray<T>.Empty : array;
......@@ -350,10 +327,6 @@ public static ImmutableArray<T> NullToEmpty<T>(this ImmutableArray<T> array)
/// Returns an array of distinct elements, preserving the order in the original array.
/// If the array has no duplicates, the original array is returned. The original array must not be null.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="comparer"></param>
/// <returns></returns>
public static ImmutableArray<T> Distinct<T>(this ImmutableArray<T> array, IEqualityComparer<T> comparer = null)
{
Debug.Assert(!array.IsDefault);
......@@ -447,13 +420,18 @@ internal static bool HasDuplicates<T>(this ImmutableArray<T> array, IEqualityCom
case 0:
case 1:
return false;
case 2:
return comparer.Equals(array[0], array[1]);
default:
var set = new HashSet<T>(comparer);
foreach (var i in array)
{
if (!set.Add(i)) return true;
if (!set.Add(i))
{
return true;
}
}
return false;
......@@ -475,15 +453,8 @@ public static int Count<T>(this ImmutableArray<T> items, Func<T, bool> predicate
++count;
}
}
return count;
}
}
internal static class StaticCast<T>
{
internal static ImmutableArray<T> From<TDerived>(ImmutableArray<TDerived> from) where TDerived : class, T
{
return ImmutableArray.Create<T, TDerived>(from);
return count;
}
}
}
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
namespace Microsoft.CodeAnalysis
{
internal static class StaticCast<T>
{
internal static ImmutableArray<T> From<TDerived>(ImmutableArray<TDerived> from) where TDerived : class, T
{
return ImmutableArray.Create<T, TDerived>(from);
}
}
}
......@@ -15,6 +15,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ArrayBuilder.Enumerator.cs">
<ExcludeFromStyleCop>true</ExcludeFromStyleCop>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)ObjectPool`1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PooledDictionary.cs">
<ExcludeFromStyleCop>true</ExcludeFromStyleCop>
</Compile>
......
......@@ -26,9 +26,6 @@
<Compile Include="..\..\Compilers\Core\Portable\InternalUtilities\ComStreamWrapper.cs">
<Link>InternalUtilities\ComStreamWrapper.cs</Link>
</Compile>
<Compile Include="..\..\Compilers\Core\Portable\InternalUtilities\ObjectPool`1.cs">
<Link>InternalUtilities\ObjectPool`1.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Label="File References">
<Reference Include="..\..\..\packages\MDbg.0.1.0\lib\net40\CorApi.dll" />
......
......@@ -47,7 +47,7 @@ private static ImmutableArray<Assembly> CreateDefaultAssemblies()
"Microsoft.CodeAnalysis.VisualBasic.Workspaces.Desktop",
};
return MefHostServices.DefaultAssemblies.Concat(MefHostServices.LoadNearbyAssemblies(assemblyNames)).ToImmutableArray();
return MefHostServices.DefaultAssemblies.Concat(MefHostServices.LoadNearbyAssemblies(assemblyNames));
}
}
}
\ No newline at end of file
......@@ -55,7 +55,7 @@ public override ImmutableArray<ITypeSymbol> TypeArguments
{
get
{
return this.typeArguments.AsImmutable();
return ImmutableArray.CreateRange(this.typeArguments);
}
}
......
......@@ -30,7 +30,7 @@ public override ImmutableArray<ITypeSymbol> TypeArguments
{
get
{
return this.typeArguments.AsImmutable();
return ImmutableArray.CreateRange(this.typeArguments);
}
}
......@@ -143,7 +143,7 @@ public override ImmutableArray<IMethodSymbol> Constructors
public override ImmutableArray<INamedTypeSymbol> GetTypeMembers()
{
// TODO(cyrusn): construct these.
return this.constructedFrom.TypeMembers.Cast<INamedTypeSymbol>().AsImmutable();
return ImmutableArray.CreateRange(this.constructedFrom.TypeMembers.Cast<INamedTypeSymbol>());
}
public override TypeKind TypeKind
......
......@@ -168,7 +168,7 @@ public override ImmutableArray<ITypeParameterSymbol> TypeParameters
{
get
{
return this.typeParameters.AsImmutable();
return ImmutableArray.CreateRange(this.typeParameters);
}
}
......@@ -184,18 +184,18 @@ public override ImmutableArray<INamedTypeSymbol> Interfaces
{
get
{
return this.interfaces.AsImmutable();
return ImmutableArray.CreateRange(this.interfaces);
}
}
public override ImmutableArray<ISymbol> GetMembers()
{
return this.members.Concat(this.TypeMembers).AsImmutable();
return ImmutableArray.CreateRange(this.members.Concat(this.TypeMembers));
}
public override ImmutableArray<INamedTypeSymbol> GetTypeMembers()
{
return this.TypeMembers.Cast<INamedTypeSymbol>().AsImmutable();
return ImmutableArray.CreateRange(this.TypeMembers.Cast<INamedTypeSymbol>());
}
public override ImmutableArray<IMethodSymbol> InstanceConstructors
......@@ -203,9 +203,8 @@ public override ImmutableArray<IMethodSymbol> InstanceConstructors
get
{
// NOTE(cyrusn): remember to Construct the result if we implement this.
return this.GetMembers().OfType<IMethodSymbol>()
.Where(m => m.MethodKind == MethodKind.Constructor && !m.IsStatic)
.AsImmutable();
return ImmutableArray.CreateRange(
this.GetMembers().OfType<IMethodSymbol>().Where(m => m.MethodKind == MethodKind.Constructor && !m.IsStatic));
}
}
......@@ -214,9 +213,8 @@ public override ImmutableArray<IMethodSymbol> StaticConstructors
get
{
// NOTE(cyrusn): remember to Construct the result if we implement this.
return this.GetMembers().OfType<IMethodSymbol>()
.Where(m => m.MethodKind == MethodKind.StaticConstructor && m.IsStatic)
.AsImmutable();
return ImmutableArray.CreateRange(
this.GetMembers().OfType<IMethodSymbol>().Where(m => m.MethodKind == MethodKind.StaticConstructor && m.IsStatic));
}
}
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
......@@ -136,8 +137,7 @@ public static IMethodSymbol RenameParameters(this IMethodSymbol method, IList<st
// Now we update the constraints.
foreach (var newTypeParameter in newTypeParameters)
{
newTypeParameter.ConstraintTypes = newTypeParameter.ConstraintTypes.Select(
t => t.SubstituteTypes(mapping, typeGenerator)).AsImmutable();
newTypeParameter.ConstraintTypes = ImmutableArray.CreateRange(newTypeParameter.ConstraintTypes, t => t.SubstituteTypes(mapping, typeGenerator));
}
return newTypeParameters.Cast<ITypeParameterSymbol>().ToList();
......
......@@ -427,7 +427,7 @@ public static ImmutableArray<ITypeSymbol> GetAllTypeArguments(this ISymbol symbo
containingType = containingType.ContainingType;
}
return results.AsImmutable();
return ImmutableArray.CreateRange(results);
}
public static bool IsAttribute(this ISymbol symbol)
......
......@@ -49,6 +49,9 @@
</ProjectReference>
</ItemGroup>
<ItemGroup Label="Linked Files">
<Compile Include="..\..\..\Compilers\Core\Portable\Collections\ImmutableArrayExtensions.cs">
<Link>InternalUtilities\ImmutableArrayExtensions.cs</Link>
</Compile>
<Compile Include="..\..\..\Compilers\Core\Portable\MetadataReference\MetadataFileReferenceProvider.cs">
<Link>MetadataReference\MetadataFileReferenceProvider.cs</Link>
</Compile>
......@@ -188,9 +191,6 @@
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\WeakReferenceExtensions.cs">
<Link>InternalUtilities\WeakReferenceExtensions.cs</Link>
</Compile>
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\ObjectPool`1.cs">
<Link>InternalUtilities\ObjectPool`1.cs</Link>
</Compile>
<Compile Include="..\..\..\Compilers\Core\Portable\Serialization\FixedObjectBinder.cs">
<Link>Serialization\FixedObjectBinder.cs</Link>
</Compile>
......@@ -953,6 +953,7 @@
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="..\..\..\Compilers\Core\SharedCollections\SharedCollections.projitems" Label="Shared" />
<ImportGroup Label="Targets">
<Import Project="..\..\..\Tools\Microsoft.CodeAnalysis.Toolset.Open\Targets\VSL.Imports.targets" />
<Import Project="..\..\..\..\packages\StyleCop.MSBuild.4.7.48.2\build\StyleCop.MSBuild.Targets" Condition="Exists('..\..\..\..\packages\StyleCop.MSBuild.4.7.48.2\build\StyleCop.MSBuild.Targets')" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册