コード例 #1
0
        public HbsRenderer(IServiceProvider services, IHandlebars handlebars, IOptions <HandlebarsViewEngineOptions> options, IExpressionCache expressionCache)
        {
            _options         = options.Value;
            _services        = services.CreateScope().ServiceProvider;
            _expressionCache = expressionCache;
            _handlebars      = handlebars;

            var helpersAction = _options.RegisterHelpers;

            if (helpersAction != null)
            {
                var helpers = new HelperList();
                helpersAction.Invoke(helpers);

                foreach (var helper in helpers)
                {
                    var h = _services.GetService(helper).Cast <HandlebarsBaseHelper>();
                    switch (h.HelperType)
                    {
                    case HelperType.HandlebarsHelper:
                        _handlebars.RegisterHelper(h.HelperName, h.HandlebarsHelper);
                        break;

                    case HelperType.HandlebarsBlockHelper:
                        _handlebars.RegisterHelper(h.HelperName, h.HandlebarsBlockHelper);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
        }
        public static IMvcBuilder AddHandlebarsViewEngine(this IMvcBuilder builder,
                                                          Action <HandlebarsViewEngineOptions> optionsAction = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.AddOptions()
            .AddTransient <IConfigureOptions <HandlebarsViewEngineOptions>, HandlebarsViewEngineOptionsSetup>();

            if (optionsAction != null)
            {
                builder.Services.Configure(optionsAction);
            }

            var options = builder.Services.BuildServiceProvider().GetService <IOptions <HandlebarsViewEngineOptions> >()
                          .Value;

            if (options.RegisterHelpers != null)
            {
                var helpers = new HelperList();
                options.RegisterHelpers.Invoke(helpers);
                var typeBaseHelper = typeof(HandlebarsBaseHelper);
                foreach (var helper in helpers)
                {
                    if (helper.IsSubclassOf(typeBaseHelper))
                    {
                        builder.Services.AddScoped(helper);
                    }
                    else
                    {
                        throw new TypeMismatchException(helper, typeBaseHelper);
                    }
                }
            }

            builder.Services
            .AddTransient <IConfigureOptions <MvcViewOptions>, HandlebarsMvcViewOptionsSetup>()
            .AddSingleton <IExpressionCache, ExpressionCache>()
            .AddSingleton <IHandlebars>(provider => Handlebars.Create(new HandlebarsConfiguration()
            {
                FileSystem = new DiskFileSystem()
            }))
            .AddSingleton <IHbsRenderer, HbsRenderer>()
            .AddSingleton <IHandlebarsViewEngine, HandlebarsViewEngine>();

            return(builder);
        }
コード例 #3
0
    public double[] MedianSlidingWindow(int[] nums, int k)
    {
        int n = nums.Length;

        double[]   ret = new double[n - k + 1];
        HelperList left = new HelperList(), right = new HelperList();

        for (int i = 0; i <= n; ++i)
        {
            if (i >= k)
            {
                double mid;
                if (left.Count == right.Count)
                {
                    mid = ((long)left.GetMax() + (long)right.GetMin()) / 2.0;
                }
                else
                {
                    mid = left.GetMax();
                }

                int remove = nums[i - k];
                if (mid >= (double)remove)
                {
                    left.Remove(remove);
                }
                else
                {
                    right.Remove(remove);
                }

                ret[i - k] = mid;
            }

            if (i < n)
            {
                int num = nums[i];
                if (left.Count > 0 && left.GetMax() >= num)
                {
                    left.Add(num);
                }
                else
                {
                    right.Add(num);
                }

                while (left.Count < right.Count)
                {
                    int move = right.GetMin();
                    right.Remove(move);
                    left.Add(move);
                }

                while (left.Count > right.Count + 1)
                {
                    int move = left.GetMax();
                    left.Remove(move);
                    right.Add(move);
                }
            }
        }
        return(ret);
    }