Exemplo n.º 1
0
        // don't use OrderBy for performance reason.
        TElement[] FastSort(IEnumerable <TElement> datasource, Func <TElement, TKey> indexSelector, IComparer <TKey> comparer)
        {
            var collection = datasource as ICollection <TElement>;

            if (collection != null)
            {
                var array      = new TElement[collection.Count];
                var sortSource = new TKey[collection.Count];
                var i          = 0;
                foreach (var item in collection)
                {
                    array[i]      = item;
                    sortSource[i] = indexSelector(item);
                    i++;
                }
                Array.Sort(sortSource, array, 0, collection.Count, comparer);
                return(array);
            }
            else
            {
                var array      = new ExpandableArray <TElement>();
                var sortSource = new ExpandableArray <TKey>();
                foreach (var item in datasource)
                {
                    array.Add(item);
                    sortSource.Add(indexSelector(item));
                }

                Array.Sort(sortSource.items, array.items, 0, array.count, comparer);

                Array.Resize(ref array.items, array.count);
                return(array.items);
            }
        }
        public static IEnumerable <ServiceRegistration> FilterExclusiveOrDefault(this IEnumerable <ServiceRegistration> registrations,
                                                                                 TypeInformation typeInformation,
                                                                                 ResolutionContext resolutionContext,
                                                                                 IRegistrationSelectionRule[] registrationSelectionRules)
        {
            var common   = new ExpandableArray <ServiceRegistration>();
            var priority = new ExpandableArray <ServiceRegistration>();

            foreach (var serviceRegistration in registrations)
            {
                if (!registrationSelectionRules.IsSelectionPassed(typeInformation, serviceRegistration, resolutionContext, out var weight))
                {
                    continue;
                }

                if (weight > 0)
                {
                    priority.Add(serviceRegistration);
                }
                else
                {
                    common.Add(serviceRegistration);
                }
            }

            if (common.Length == 0 && priority.Length == 0)
            {
                return(null);
            }

            return(priority.Length > 0
                    ? priority
                    : common);
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public void Validate()
        {
            this.ThrowIfDisposed();

            var exceptions = new ExpandableArray <Exception>();

            foreach (var serviceRegistration in this.ContainerContext.RegistrationRepository
                     .GetRegistrationMappings()
                     .Where(reg => !reg.Key.IsOpenGenericType()))
            {
                try
                {
                    this.resolutionStrategy.BuildExpressionForRegistration(serviceRegistration.Value,
                                                                           new ResolutionContext(this.ContainerContext.RootScope.GetActiveScopeNames(),
                                                                                                 this.ContainerContext, false, false, true),
                                                                           new TypeInformation(serviceRegistration.Key, serviceRegistration.Value.RegistrationContext.Name));
                }
                catch (Exception ex)
                {
                    exceptions.Add(ex);
                }
            }

            if (exceptions.Length > 0)
            {
                throw new AggregateException("Container validation failed. See the inner exceptions for details.", exceptions);
            }
        }
Exemplo n.º 4
0
        public Expression ConstructBuildUpExpression(
            ResolutionContext resolutionContext,
            Expression instance,
            Type serviceType)
        {
            var registrationContext = RegistrationContext.Empty;
            var typeInfo            = instance.Type.GetTypeInfo();

            var methods = typeInfo.GetUsableMethods();
            var members = typeInfo.GetUsableMembers(registrationContext,
                                                    resolutionContext.CurrentContainerContext.ContainerConfiguration);

            if (members.Length == 0 && methods.Length == 0)
            {
                return(instance);
            }

            var variable = instance.Type.AsVariable();
            var assign   = variable.AssignTo(instance);

            var lines = new ExpandableArray <Expression> {
                assign
            };

            lines.AddRange(this.GetMemberExpressions(members,
                                                     registrationContext, resolutionContext, variable));

            lines.AddRange(this.CreateMethodExpressions(methods,
                                                        registrationContext, resolutionContext, instance));

            lines.Add(variable.Type != serviceType ? variable.ConvertTo(serviceType) : variable);

            return(lines.AsBlock(variable));
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        public void Validate()
        {
            var exceptions = new ExpandableArray <Exception>();

            try
            {
                this.RootContainer.Validate();
            }
            catch (AggregateException ex)
            {
                exceptions.Add(new AggregateException($"Root container validation failed. See the inner exceptions for details.", ex.InnerExceptions));
            }

            foreach (var tenant in this.tenantRepository.Walk())
            {
                try
                {
                    tenant.Value.Validate();
                }
                catch (AggregateException ex)
                {
                    exceptions.Add(new AggregateException($"Tenant validation failed for '{tenant.Key}'. See the inner exceptions for details.", ex.InnerExceptions));
                }
            }

            if (exceptions.Length > 0)
            {
                throw new AggregateException("Tenant distributor validation failed. See the inner exceptions for details.", exceptions);
            }
        }
Exemplo n.º 6
0
 public TreeAnalyzer()
 {
     this.CapturedParameters = new ExpandableArray <Expression>();
     this.DefinedVariables   = new ExpandableArray <Expression>();
     this.NestedLambdas      = new ExpandableArray <LambdaExpression, NestedLambda>();
     this.Constants          = new ExpandableArray <object>();
 }
Exemplo n.º 7
0
        public unsafe void Size_Correctly_Set_After_Expand()
        {
            ExpandableArray <TestStruct> sut = stackalloc TestStruct[1];

            sut.Expand(stackalloc TestStruct[1]);

            Assert.AreEqual(2, sut.Length);
        }
Exemplo n.º 8
0
        public BufferedArray(CountedEnumerable <byte> source, int initialBufferSize)
        {
            Contracts.Requires.That(source.Values != null);
            Contracts.Requires.That(initialBufferSize >= 0);

            this.source      = source.Values.GetEnumerator();
            this.sourceCount = source.Count;
            this.buffer      = new ExpandableArray <byte>(initialBufferSize);
        }
Exemplo n.º 9
0
        public CompilerContext CreateNew(ExpandableArray <Expression> definedVariables, bool isNestedLambda, bool hasCapturedArgument)
        {
            var clone = (CompilerContext)this.MemberwiseClone();

            clone.DefinedVariables             = definedVariables;
            clone.IsNestedLambda               = isNestedLambda;
            clone.HasCapturedVariablesArgument = hasCapturedArgument;
            return(clone);
        }
Exemplo n.º 10
0
 internal RegistrationContext()
 {
     this.AttributeConditions    = new ExpandableArray <Type>();
     this.TargetTypeConditions   = new ExpandableArray <Type>();
     this.ResolutionConditions   = new ExpandableArray <Func <TypeInformation, bool> >();
     this.AdditionalServiceTypes = new ExpandableArray <Type>();
     this.InjectionParameters    = new ExpandableArray <KeyValuePair <string, object> >();
     this.DependencyBindings     = new Dictionary <object, object>();
 }
Exemplo n.º 11
0
        public unsafe void Item_Access_Out_Of_Bound()
        {
            Assert.Throws <IndexOutOfRangeException>(() =>
            {
                ExpandableArray <TestStruct> sut = stackalloc TestStruct[1];

                var expected = sut[1];
            });
        }
Exemplo n.º 12
0
        public MeshDataTransfer16(MeshDataTransferOptions options = null)
        {
            Contracts.Requires.That(options?.IsValid16Bit() ?? true);

            options = MeshDataTransferOptions.New16Bit();

            this.indices     = new ExpandableArray <ushort>(options.InitialIndices);
            this.vertices    = new ExpandableArray <TVertex>(options.InitialVertices);
            this.MaxVertices = options.MaxVertices;
        }
Exemplo n.º 13
0
        public BufferedByteStream(Stream stream, int initialBufferSize)
        {
            Contracts.Requires.That(stream != null);
            Contracts.Requires.That(stream.CanRead);
            Contracts.Requires.That(initialBufferSize >= 0);

            this.stream  = stream;
            this.buffer  = new ExpandableArray <byte>(initialBufferSize);
            this.HasNext = true;
        }
        public ByteStreamSerializer(Stream stream, ISerializerDeserializer <T> serializer, int initialBufferSize)
        {
            Contracts.Requires.That(stream != null);
            Contracts.Requires.That(stream.CanWrite);
            Contracts.Requires.That(serializer != null);
            Contracts.Requires.That(initialBufferSize >= 0);

            this.stream     = stream;
            this.serializer = serializer;
            this.byteBuffer = new ExpandableArray <byte>(initialBufferSize);
        }
Exemplo n.º 15
0
        public Expression ConstructExpression(
            ServiceRegistration serviceRegistration,
            ResolutionContext resolutionContext)
        {
            var constructors = serviceRegistration.ImplementationTypeInfo.GetUsableConstructors();
            var methods      = serviceRegistration.ImplementationTypeInfo.GetUsableMethods();
            var members      = serviceRegistration.ImplementationTypeInfo.GetUsableMembers(serviceRegistration.RegistrationContext,
                                                                                           resolutionContext.CurrentContainerContext.ContainerConfiguration);

            var initExpression = this.CreateInitExpression(
                serviceRegistration.ImplementationType,
                serviceRegistration.RegistrationContext,
                resolutionContext,
                constructors);

            if (initExpression == null)
            {
                return(null);
            }

            if (members.Length > 0)
            {
                initExpression = initExpression.InitMembers(this.GetMemberBindings(members,
                                                                                   serviceRegistration.RegistrationContext, resolutionContext));
            }

            if (methods.Length == 0 && serviceRegistration.RegistrationContext.Initializer == null)
            {
                return(initExpression);
            }

            var variable = initExpression.Type.AsVariable();
            var assign   = variable.AssignTo(initExpression);

            var lines = new ExpandableArray <Expression> {
                assign
            };

            lines.AddRange(this.CreateMethodExpressions(methods,
                                                        serviceRegistration.RegistrationContext, resolutionContext, variable));

            if (serviceRegistration.RegistrationContext.Initializer != null)
            {
                lines.Add(serviceRegistration.RegistrationContext.Initializer.AsConstant()
                          .CallMethod(serviceRegistration.RegistrationContext.Initializer.GetType().GetSingleMethod("Invoke"),
                                      variable, resolutionContext.CurrentScopeParameter));
            }

            lines.Add(variable);

            return(lines.AsBlock(variable));
        }
Exemplo n.º 16
0
 public CompilerContext(Closure target,
                        ExpandableArray <object> constants,
                        ExpandableArray <Expression> definedVariables,
                        ExpandableArray <Expression> capturedArguments,
                        ExpandableArray <LambdaExpression, NestedLambda> nestedLambdas)
 {
     this.Target                       = target;
     this.Constants                    = constants;
     this.DefinedVariables             = definedVariables;
     this.CapturedArguments            = capturedArguments;
     this.NestedLambdas                = nestedLambdas;
     this.HasClosure                   = target != null;
     this.HasCapturedVariablesArgument = capturedArguments.Length > 0;
 }
Exemplo n.º 17
0
        public Expression ConstructBuildUpExpression(
            ServiceRegistration serviceRegistration,
            ResolutionContext resolutionContext,
            Expression instance,
            Type serviceType)
        {
            if (instance.Type != serviceRegistration.ImplementationType)
            {
                instance = instance.ConvertTo(serviceRegistration.ImplementationType);
            }

            var methods = serviceRegistration.ImplementationTypeInfo.GetUsableMethods();
            var members = serviceRegistration.ImplementationTypeInfo.GetUsableMembers(serviceRegistration.RegistrationContext,
                                                                                      resolutionContext.CurrentContainerContext.ContainerConfiguration);

            if (members.Length == 0 && methods.Length == 0 &&
                serviceRegistration.RegistrationContext.Initializer == null)
            {
                return(instance);
            }

            var variable = instance.Type.AsVariable();
            var assign   = variable.AssignTo(instance);

            var lines = new ExpandableArray <Expression> {
                assign
            };

            lines.AddRange(this.GetMemberExpressions(members,
                                                     serviceRegistration.RegistrationContext, resolutionContext, variable));

            lines.AddRange(this.CreateMethodExpressions(methods,
                                                        serviceRegistration.RegistrationContext, resolutionContext, variable));

            if (serviceRegistration.RegistrationContext.Initializer != null)
            {
                lines.Add(serviceRegistration.RegistrationContext.Initializer.AsConstant()
                          .CallMethod(serviceRegistration.RegistrationContext.Initializer.GetType().GetSingleMethod("Invoke"),
                                      variable, resolutionContext.CurrentScopeParameter));
            }

            lines.Add(variable.Type != serviceType ? variable.ConvertTo(serviceType) : variable);

            return(lines.AsBlock(variable));
        }
        public void ExpandableArraySmokeTest()
        {
            var array = new ExpandableArray <int> {
                1, 5, 10, 11
            };

            Assert.Equal(4, array.Count);
            Assert.Equal(new[] { 1, 5, 10, 11 }, array);

            array[3] += 11;

            Assert.Equal(22, array.Buffer[3]);

            array.Add(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 });

            Assert.Equal(12, array.Count);
            Assert.Equal(new[] { 1, 5, 10, 22, 2, 3, 5, 7, 11, 13, 17, 19 }, array);
        }
Exemplo n.º 19
0
        public Expression ConstructExpression(
            ResolutionContext resolutionContext,
            Type serviceType)
        {
            var registrationContext = RegistrationContext.Empty;
            var typeInfo            = serviceType.GetTypeInfo();
            var methods             = typeInfo.GetUsableMethods();
            var members             = typeInfo.GetUsableMembers(registrationContext,
                                                                resolutionContext.CurrentContainerContext.ContainerConfiguration);

            var initExpression = (Expression)this.SelectConstructor(
                serviceType,
                registrationContext,
                resolutionContext,
                typeInfo.DeclaredConstructors,
                out var parameters).MakeNew(parameters);

            if (members.Length > 0)
            {
                initExpression = initExpression.InitMembers(this.GetMemberBindings(members,
                                                                                   registrationContext, resolutionContext));
            }

            if (methods.Length == 0)
            {
                return(initExpression);
            }

            var variable = initExpression.Type.AsVariable();
            var assign   = variable.AssignTo(initExpression);

            var lines = new ExpandableArray <Expression> {
                assign
            };

            lines.AddRange(this.CreateMethodExpressions(methods, registrationContext, resolutionContext, variable));
            lines.Add(variable);

            return(lines.AsBlock(variable));
        }
Exemplo n.º 20
0
        public void Test()
        {
            const int blockCount = 10;
            const int elemPerItem = 5;
            var itemsPerBlock = ExpandableArray<int>.ComputeItemsPerBlock(sizeof(int));

            var x = new ExpandableArray<int>(elemPerItem, sizeof(int));
            Assert.AreEqual(itemsPerBlock, x.ElementsPerBlock / elemPerItem);
            Assert.AreEqual(0, x.GetLocalIndex(0));
            Assert.AreEqual(1, x.GetLocalIndex(1));
            Assert.AreEqual(x.ElementsPerBlock-1, x.GetLocalIndex(x.ElementsPerBlock-1));
            Assert.AreEqual(0, x.GetLocalIndex(x.ElementsPerBlock));
            Assert.AreEqual(0, x.GetLocalIndex(itemsPerBlock*elemPerItem));
            Assert.AreEqual(0, x.GetLocalIndex(itemsPerBlock*blockCount*elemPerItem));
            Assert.AreEqual(2, x.GetLocalIndex(itemsPerBlock*blockCount*elemPerItem + 2));
            Assert.AreEqual(x.ElementsPerBlock-1, x.GetLocalIndex(itemsPerBlock*blockCount*elemPerItem + x.ElementsPerBlock-1));

            x.EnsureCapacity(itemsPerBlock*blockCount*elemPerItem + 1);
            var realBlockCount = x.EnumerateBlocks().Count();

            Assert.IsTrue(realBlockCount == 1 + blockCount + x.BlockCountIncrement);
        }
Exemplo n.º 21
0
        public void Test()
        {
            const int blockCount    = 10;
            const int elemPerItem   = 5;
            var       itemsPerBlock = ExpandableArray <int> .ComputeItemsPerBlock(sizeof(int));

            var x = new ExpandableArray <int>(elemPerItem, sizeof(int));

            Assert.AreEqual(itemsPerBlock, x.ElementsPerBlock / elemPerItem);
            Assert.AreEqual(0, x.GetLocalIndex(0));
            Assert.AreEqual(1, x.GetLocalIndex(1));
            Assert.AreEqual(x.ElementsPerBlock - 1, x.GetLocalIndex(x.ElementsPerBlock - 1));
            Assert.AreEqual(0, x.GetLocalIndex(x.ElementsPerBlock));
            Assert.AreEqual(0, x.GetLocalIndex(itemsPerBlock * elemPerItem));
            Assert.AreEqual(0, x.GetLocalIndex(itemsPerBlock * blockCount * elemPerItem));
            Assert.AreEqual(2, x.GetLocalIndex(itemsPerBlock * blockCount * elemPerItem + 2));
            Assert.AreEqual(x.ElementsPerBlock - 1, x.GetLocalIndex(itemsPerBlock * blockCount * elemPerItem + x.ElementsPerBlock - 1));

            x.EnsureCapacity(itemsPerBlock * blockCount * elemPerItem + 1);
            var realBlockCount = x.EnumerateBlocks().Count();

            Assert.IsTrue(realBlockCount == 1 + blockCount + x.BlockCountIncrement);
        }
Exemplo n.º 22
0
        public unsafe void Size_Correctly_Set()
        {
            ExpandableArray <TestStruct> sut = stackalloc TestStruct[1];

            Assert.AreEqual(1, sut.Length);
        }
Exemplo n.º 23
0
 public NestedLambda(ExpandableArray <Expression> parameterExpressions, bool usesCapturedArgument)
 {
     this.ParameterExpressions = parameterExpressions;
     this.UsesCapturedArgument = usesCapturedArgument;
 }
Exemplo n.º 24
0
        public unsafe void Item_Correctly_Set()
        {
            ExpandableArray <TestStruct> sut = stackalloc TestStruct[1];

            ref var expected = ref sut[0];
        private static void ReadBitVectorFromStore(BinaryReader reader, ExpandableArray<int> bitVectorData, int count)
        {
            var readSoFar = 0;
            bitVectorData.EnsureCapacity(count);
            const int elementsPerItem = 32;
            while (count > 0)
            {
                var block = bitVectorData.GetBlock(readSoFar);

                for (var i = 0; count > 0 && i < block.Length; i++, count -= elementsPerItem)
                {
                    block[i] = reader.ReadInt32();
                }

                readSoFar += bitVectorData.ElementsPerBlock;
            }
        }
Exemplo n.º 26
0
 internal static BlockExpression AsBlock(this ExpandableArray <Expression> expressions, params ParameterExpression[] variables) =>
 Expression.Block(variables, expressions);
Exemplo n.º 27
0
 internal static BlockExpression AsBlock(this ExpandableArray <Expression> expressions, IEnumerable <ParameterExpression> variables) =>
 Expression.Block(variables, expressions);