コード例 #1
0
ファイル: StackArray.cs プロジェクト: vanloc0301/mychongchong
 public void Pop(object targetForm, StackMode stackMode)
 {
     if (stackMode != StackMode.None)
     {
         if (stackMode == StackMode.FirstAvailable)
         {
             for (int i = 0; i < this.m_syncList.Count; i++)
             {
                 if (this.m_syncList[i] == targetForm)
                 {
                     this.m_syncList[i] = null;
                     this.m_iCount--;
                     break;
                 }
             }
         }
         else if ((stackMode == StackMode.Top) && this.m_syncList.Contains(targetForm))
         {
             this.m_syncList.Remove(targetForm);
             this.m_iCount--;
         }
         if (this.m_iCount == 0)
         {
             this.m_syncList.Clear();
         }
     }
 }
コード例 #2
0
 /**
  * Creates an inventory.
  *
  * @param capacity The capacity.
  * @param mode The {@link StackMode}.
  * @throws IllegalArgumentException If the capacity is negative.
  * @throws NullPointerException If the mode is {@code null}.
  */
 public Inventory(int capacity, StackMode mode)
 {
     Guard.Argument(capacity, nameof(capacity)).GreaterThan(0);
     this._capacity = capacity;
     this._mode     = mode;
     _items         = new Item[capacity];
 }
コード例 #3
0
ファイル: StackArray.cs プロジェクト: vanloc0301/mychongchong
 public object Peek(StackMode stackMode)
 {
     if (this.m_syncList.Count > 0)
     {
         return this.m_syncList[this.m_syncList.Count - 1];
     }
     return null;
 }
コード例 #4
0
ファイル: BoxLayout.cs プロジェクト: iomeone/Core2D
        public static void Stack(IEnumerable <IBaseShape> shapes, StackMode mode, IHistory history)
        {
            var boxes = new List <ShapeBox>();

            foreach (var shape in shapes)
            {
                boxes.Add(new ShapeBox(shape));
            }

            if (boxes.Count < 2)
            {
                return;
            }

            var bounds = new GroupBox(boxes);

            switch (mode)
            {
            case StackMode.Horizontal:
            {
                boxes.Sort(ShapeBox.CompareLeft);
                double offset = boxes[0].Left + boxes[0].Width;
                for (int i = 1; i <= boxes.Count - 1; i++)
                {
                    var    box = boxes[i];
                    double dx  = offset - box.Left;
                    double dy  = 0.0;
                    MoveShapeByWithHistory(box.Shape, dx, dy, history);
                    offset += box.Width;
                }
            }
            break;

            case StackMode.Vertical:
            {
                boxes.Sort(ShapeBox.CompareTop);
                double offset = boxes[0].Top + boxes[0].Height;
                for (int i = 1; i <= boxes.Count - 1; i++)
                {
                    var    box = boxes[i];
                    double dx  = 0.0;
                    double dy  = offset - box.Top;
                    MoveShapeByWithHistory(box.Shape, dx, dy, history);
                    offset += box.Height;
                }
            }
            break;
            }
        }
コード例 #5
0
ファイル: BoxLayout.cs プロジェクト: dandanyouxiang/Core2D
        public static void Stack(IEnumerable <IBaseShape> shapes, StackMode mode, IHistory history)
        {
            var groupBox = new GroupBox(shapes.ToList());

            if (groupBox.Boxes.Length < 2)
            {
                return;
            }

            var boxes = groupBox.Boxes.ToList();

            switch (mode)
            {
            case StackMode.Horizontal:
            {
                boxes.Sort(ShapeBox.CompareLeft);
                double offset = boxes[0].Bounds.Left + boxes[0].Bounds.Width;
                for (int i = 1; i <= boxes.Count - 1; i++)
                {
                    var    box = boxes[i];
                    double dx  = offset - box.Bounds.Left;
                    double dy  = 0.0;
                    box.MoveByWithHistory(dx, dy, history);
                    offset += box.Bounds.Width;
                }
            }
            break;

            case StackMode.Vertical:
            {
                boxes.Sort(ShapeBox.CompareTop);
                double offset = boxes[0].Bounds.Top + boxes[0].Bounds.Height;
                for (int i = 1; i <= boxes.Count - 1; i++)
                {
                    var    box = boxes[i];
                    double dx  = 0.0;
                    double dy  = offset - box.Bounds.Top;
                    box.MoveByWithHistory(dx, dy, history);
                    offset += box.Bounds.Height;
                }
            }
            break;
            }
        }
コード例 #6
0
ファイル: StackArray.cs プロジェクト: vanloc0301/mychongchong
 public object Push(object newObject, StackMode stackMode)
 {
     bool flag = false;
     object obj2 = null;
     if (stackMode == StackMode.None)
     {
         return null;
     }
     if (stackMode != StackMode.FirstAvailable)
     {
         if (stackMode == StackMode.Top)
         {
             if (this.m_syncList.Count > 0)
             {
                 obj2 = this.m_syncList[this.m_syncList.Count - 1];
             }
             this.m_syncList.Add(newObject);
         }
     }
     else
     {
         for (int i = 0; i < this.m_syncList.Count; i++)
         {
             if (this.m_syncList[i] == null)
             {
                 this.m_syncList[i] = newObject;
                 flag = true;
                 break;
             }
             obj2 = this.m_syncList[i];
         }
         if (!flag)
         {
             this.m_syncList.Add(newObject);
         }
     }
     this.m_iCount++;
     return obj2;
 }
コード例 #7
0
        /// <summary>
        /// Stacks the points.
        /// </summary>
        /// <param name="stackables">The stackables.</param>
        /// <param name="stackAt">The stack at.</param>
        /// <param name="stackIndex">Index of the stack.</param>
        /// <param name="mode">The mode.</param>
        protected void StackPoints(IEnumerable <ISeriesView> stackables, AxisOrientation stackAt, int stackIndex,
                                   StackMode mode = StackMode.Values)
        {
            var groupedStackables = stackables.GroupBy(s => s is IGroupedStackedSeriesView ? (s as IGroupedStackedSeriesView).Grouping : 0).ToList();

            foreach (var groupedStack in groupedStackables)
            {
                var stackedColumns = groupedStack.SelectMany(x => x.ActualValues.GetPoints(x))
                                     .GroupBy(x => stackAt == AxisOrientation.X ? x.Y : x.X);

                double mostLeft = 0, mostRight = 0;

                foreach (var column in stackedColumns)
                {
                    double sumLeft = 0, sumRight = 0;

                    foreach (var item in column)
                    {
                        var s = stackAt == AxisOrientation.X ? item.X : item.Y;
                        if (s < 0)
                        {
                            sumLeft += s;
                        }
                        else
                        {
                            sumRight += s;
                        }
                    }

                    var lastLeft  = 0d;
                    var lastRight = 0d;
                    var leftPart  = 0d;
                    var rightPart = 0d;

                    foreach (var point in column)
                    {
                        var pulled = stackAt == AxisOrientation.X ? point.X : point.Y;

                        //notice using (pulled < 0) or (pulled <= 0) could cause an issue similar to
                        //https://github.com/beto-rodriguez/Live-Charts/issues/231
                        //from that issue I changed <= to <
                        //only because it is more common to use positive values than negative
                        //you could face a similar issue if you are stacking only negative values
                        //a work around is forcing (pulled < 0) to be true,
                        //instead of using zero values, use -0.000000001/

                        if (pulled < 0)
                        {
                            point.From          = lastLeft;
                            point.To            = lastLeft + pulled;
                            point.Sum           = sumLeft;
                            point.Participation = (point.To - point.From) / point.Sum;
                            point.Participation = double.IsNaN(point.Participation)
                                ? 0
                                : point.Participation;
                            leftPart += point.Participation;
                            point.StackedParticipation = leftPart;

                            lastLeft = point.To;
                        }
                        else
                        {
                            point.From          = lastRight;
                            point.To            = lastRight + pulled;
                            point.Sum           = sumRight;
                            point.Participation = (point.To - point.From) / point.Sum;
                            point.Participation = double.IsNaN(point.Participation)
                                ? 0
                                : point.Participation;
                            rightPart += point.Participation;
                            point.StackedParticipation = rightPart;

                            lastRight = point.To;
                        }
                    }

                    if (sumLeft < mostLeft)
                    {
                        mostLeft = sumLeft;
                    }
                    if (sumRight > mostRight)
                    {
                        mostRight = sumRight;
                    }
                }

                if (stackAt == AxisOrientation.X)
                {
                    var ax = AxisX[stackIndex];

                    if (mode == StackMode.Percentage)
                    {
                        if (double.IsNaN(ax.MinValue))
                        {
                            ax.BotLimit = 0;
                        }
                        if (double.IsNaN(ax.MaxValue))
                        {
                            ax.TopLimit = 1;
                        }
                    }
                    else
                    {
                        if (mostLeft < ax.BotLimit)
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            if (double.IsNaN(ax.MinValue))
                            {
                                ax.BotLimit = mostLeft == 0.0
                                    ? 0.0
                                    : Math.Floor(mostLeft / ax.S) * ax.S;
                            }
                        }
                        if (mostRight > ax.TopLimit)
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            if (double.IsNaN(ax.MaxValue))
                            {
                                ax.TopLimit = mostRight == 0.0
                                    ? 0.0
                                    : (Math.Floor(mostRight / ax.S) + 1.0) * ax.S;
                            }
                        }
                    }
                }

                if (stackAt == AxisOrientation.Y)
                {
                    var ay = AxisY[stackIndex];

                    if (mode == StackMode.Percentage)
                    {
                        if (double.IsNaN(ay.MinValue))
                        {
                            ay.BotLimit = 0;
                        }
                        if (double.IsNaN(ay.MaxValue))
                        {
                            ay.TopLimit = 1;
                        }
                    }
                    else
                    {
                        if (mostLeft < ay.BotLimit)
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            if (double.IsNaN(ay.MinValue))
                            {
                                ay.BotLimit = mostLeft == 0.0
                                    ? 0.0
                                    : Math.Floor(mostLeft / ay.S) * ay.S;
                            }
                        }
                        if (mostRight > ay.TopLimit)
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            if (double.IsNaN(ay.MaxValue))
                            {
                                ay.TopLimit = mostRight == 0.0
                                    ? 0.0
                                    : (Math.Floor(mostRight / ay.S) + 1.0) * ay.S;
                            }
                        }
                    }
                }
            }
        }
コード例 #8
0
ファイル: WidgetGroup.cs プロジェクト: HippoAR/DemoPowerUI
        /// <summary>Opens a widget.</summary>
        public Widget open(string typeName, string url, WidgetDelegate preload, Dictionary <string, object> globals)
        {
            if (Manager.widgetTypes == null)
            {
                // Load the widgets now!
                Modular.AssemblyScanner.FindAllSubTypesNow(typeof(Widgets.Widget),
                                                           delegate(Type t){
                    // Add it as an available widget:
                    Manager.Add(t);
                }
                                                           );
            }

            Type type;

            if (!Manager.widgetTypes.TryGetValue(typeName, out type))
            {
                UnityEngine.Debug.Log("Warning: Requested to open a widget called '" + typeName + "' but it doesn't exist.");

                if (preload != null)
                {
                    // Invoke the load method:
                    preload(null);
                }

                return(null);
            }

            // Get existing:
            Widget same = get(typeName, null);

            // Get stacking behaviour:
            StackMode stacking = StackMode.Close;

            if (same != null)
            {
                stacking = same.StackMode;
            }

            object stackModeObj;

            if (globals != null && globals.TryGetValue("-spark-stack-mode", out stackModeObj))
            {
                string stackMode = stackModeObj.ToString().Trim().ToLower();

                if (stackMode == "hide")
                {
                    stacking = StackMode.Hide;
                }
                else if (stackMode == "close")
                {
                    stacking = StackMode.Close;
                }
                else if (stackMode == "hijack")
                {
                    stacking = StackMode.Hijack;
                }
                else
                {
                    stacking = StackMode.Over;
                }
            }

            if (stacking == StackMode.Hijack)
            {
                // Hijack an existing widget! Just load straight into it but clear its event handlers:
                same.RunLoad = true;
                same.ClearEvents();

                if (preload != null)
                {
                    preload(same);
                }

                same.Load(url, globals);
                same.TryLoadEvent(globals);

                return(same);
            }

            // instance it now:
            Widget w = Activator.CreateInstance(type) as Widget;

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

            if (stacking == StackMode.Hide)
            {
                // Hides any widget of the same type.

                if (same != null)
                {
                    // Make sure it's actually the visible one:
                    same = same.GetVisibleWidget();

                    same.Visibility(false, w);
                    w.HidWidget = same;
                }
            }
            else if (stacking == StackMode.Close && same != null)
            {
                // Close all widgets of the same type.

                // For each one..
                for (int i = Widgets.Count - 1; i >= 0; i--)
                {
                    same = Widgets[i];

                    // Match?
                    if (same.Type == typeName)
                    {
                        same.close();
                    }
                }
            }

            // Apply type:
            w.Type = typeName;

            if (preload != null)
            {
                preload(w);
            }

            // Add now:
            SetupWidget(w, url, globals);

            return(w);
        }
コード例 #9
0
        public static void Stack(IToolContext context, StackMode mode)
        {
            if (context.DocumentContainer?.ContainerView?.SelectionState != null)
            {
                var shapes = new List <IBaseShape>(context.DocumentContainer.ContainerView.SelectionState?.Shapes);
                var boxes  = new List <Box>();

                foreach (var shape in shapes)
                {
                    if (!(shape is IPointShape))
                    {
                        boxes.Add(new Box(shape));
                    }
                }

                if (boxes.Count >= 2)
                {
                    context.DocumentContainer?.ContainerView?.SelectionState?.Dehover();
                    context.DocumentContainer?.ContainerView?.SelectionState?.Clear();

                    var bounds = new Bounds(boxes);

                    switch (mode)
                    {
                    case StackMode.Horizontal:
                    {
                        boxes.Sort(Box.CompareHorizontalLeft);
                        boxes[0].shape.Select(context.DocumentContainer.ContainerView.SelectionState);
                        double offset = boxes[0].ax + boxes[0].w;
                        for (int i = 1; i <= boxes.Count - 1; i++)
                        {
                            var    box = boxes[i];
                            double dx  = offset - box.ax;
                            box.shape.Move(context.DocumentContainer.ContainerView.SelectionState, dx, 0.0);
                            box.shape.Select(context.DocumentContainer.ContainerView.SelectionState);
                            offset += box.w;
                        }
                    }
                    break;

                    case StackMode.Vertical:
                    {
                        boxes.Sort(Box.CompareVerticalTop);
                        boxes[0].shape.Select(context.DocumentContainer.ContainerView.SelectionState);
                        double offset = boxes[0].ay + boxes[0].h;
                        for (int i = 1; i <= boxes.Count - 1; i++)
                        {
                            var    box = boxes[i];
                            double dy  = offset - box.ay;
                            box.shape.Move(context.DocumentContainer.ContainerView.SelectionState, 0.0, dy);
                            box.shape.Select(context.DocumentContainer.ContainerView.SelectionState);
                            offset += box.h;
                        }
                    }
                    break;
                    }

                    context.DocumentContainer?.ContainerView?.InputService?.Redraw?.Invoke();
                }
            }
        }
コード例 #10
0
ファイル: StackContent.cs プロジェクト: ChelseaLing/UForms
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="mode">Stacking mode.</param>
 /// <param name="overflow">Overflow handling mode.</param>
 public StackContent( StackMode mode, OverflowMode overflow )
     : base()
 {
     Mode = mode;
     Overflow = overflow;
 }
コード例 #11
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="mode">Stacking mode.</param>
 /// <param name="overflow">Overflow handling mode.</param>
 public StackContent(StackMode mode, OverflowMode overflow)
     : base()
 {
     Mode     = mode;
     Overflow = overflow;
 }
コード例 #12
0
        protected void StackPoints(IEnumerable <ISeriesView> stackables, AxisOrientation stackAt, int stackIndex,
                                   StackMode mode = StackMode.Values)
        {
            var stackedColumns = stackables.Select(x => x.ActualValues.GetPoints(x).ToArray()).ToArray();

            var maxI = stackedColumns.Select(x => x.Length).DefaultIfEmpty(0).Max();

            for (var i = 0; i < maxI; i++)
            {
                var cols = stackedColumns
                           .Select(x => x.Length > i
                        ? new StackedSum(Pull(x[i], stackAt))
                        : new StackedSum()).ToArray();

                var sum = new StackedSum
                {
                    Left  = cols.Select(x => x.Left).DefaultIfEmpty(0).Sum(),
                    Right = cols.Select(x => x.Right).DefaultIfEmpty(0).Sum()
                };

                if (stackAt == AxisOrientation.X)
                {
                    if (mode == StackMode.Percentage)
                    {
                        AxisX[stackIndex].BotLimit = 0;
                        AxisX[stackIndex].TopLimit = 1;
                    }
                    else
                    {
                        if (sum.Left < AxisX[stackIndex].BotLimit)
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            AxisX[stackIndex].BotLimit = sum.Left == 0
                                ? 0
                                : ((int)(sum.Left / AxisX[stackIndex].S) - 1) * AxisX[stackIndex].S;
                        }
                        if (sum.Right > AxisX[stackIndex].TopLimit)
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            AxisX[stackIndex].TopLimit = sum.Right == 0
                                ? 0
                                : ((int)(sum.Right / AxisX[stackIndex].S) + 1) * AxisX[stackIndex].S;
                        }
                    }
                }
                if (stackAt == AxisOrientation.Y)
                {
                    if (mode == StackMode.Percentage)
                    {
                        AxisY[stackIndex].BotLimit = 0;
                        AxisY[stackIndex].TopLimit = 1;
                    }
                    else
                    {
                        if (sum.Left < AxisY[stackIndex].BotLimit)
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            AxisY[stackIndex].BotLimit = sum.Left == 0
                                ? 0
                                : ((int)(sum.Left / AxisY[stackIndex].S) - 1) * AxisY[stackIndex].S;
                        }
                        if (sum.Right > AxisY[stackIndex].TopLimit)
                        {
                            // ReSharper disable once CompareOfFloatsByEqualityOperator
                            AxisY[stackIndex].TopLimit = sum.Right == 0
                                ? 0
                                : ((int)(sum.Right / AxisY[stackIndex].S) + 1) * AxisY[stackIndex].S;
                        }
                    }
                }

                var lastLeft  = 0d;
                var lastRight = 0d;
                var leftPart  = 0d;
                var rightPart = 0d;

                foreach (var col in stackedColumns)
                {
                    if (i >= col.Length)
                    {
                        continue;
                    }
                    var point  = col[i];
                    var pulled = Pull(point, stackAt);

                    //notice using (pulled < 0) or (pulled <= 0) could cause an issue similar to
                    //https://github.com/beto-rodriguez/Live-Charts/issues/231
                    //from that issue I changed <= to <
                    //only because it is more common to use positive values than negative
                    //you could face a similar issue if you are stacking only negative values
                    //a work around is forcing (pulled < 0) to be true,
                    //instead of using zero values, use -0.000000001/
                    if (pulled < 0)
                    {
                        point.From          = lastLeft;
                        point.To            = lastLeft + pulled;
                        point.Sum           = sum.Left;
                        point.Participation = (point.To - point.From) / point.Sum;
                        point.Participation = double.IsNaN(point.Participation)
                            ? 0
                            : point.Participation;
                        leftPart += point.Participation;
                        point.StackedParticipation = leftPart;

                        lastLeft = point.To;
                    }
                    else
                    {
                        point.From          = lastRight;
                        point.To            = lastRight + pulled;
                        point.Sum           = sum.Right;
                        point.Participation = (point.To - point.From) / point.Sum;
                        point.Participation = double.IsNaN(point.Participation)
                            ? 0
                            : point.Participation;
                        rightPart += point.Participation;
                        point.StackedParticipation = rightPart;

                        lastRight = point.To;
                    }
                }
            }
        }
コード例 #13
0
ファイル: ChartCore.cs プロジェクト: sandboxorg/Live-Charts
        protected void StackPoints(IEnumerable <ISeriesView> stackables, AxisTags stackAt, int stackIndex,
                                   StackMode mode = StackMode.Values)
        {
            var stackedColumns = stackables.Select(x => x.Values.Points.ToArray()).ToArray();

            var maxI = stackedColumns.Select(x => x.Length).DefaultIfEmpty(0).Max();

            for (var i = 0; i < maxI; i++)
            {
                var cols = stackedColumns
                           .Select(x => x.Length > i
                        ? new StackedSum(Pull(x[i], stackAt))
                        : new StackedSum()).ToArray();

                var sum = new StackedSum
                {
                    Left  = cols.Select(x => x.Left).DefaultIfEmpty(0).Sum(),
                    Right = cols.Select(x => x.Right).DefaultIfEmpty(0).Sum()
                };

                if (stackAt == AxisTags.X)
                {
                    if (mode == StackMode.Percentage)
                    {
                        AxisX[stackIndex].MinLimit = 0;
                        AxisX[stackIndex].MaxLimit = 1;
                    }
                    else
                    {
                        if (sum.Left < AxisX[stackIndex].MinLimit)
                        {
                            AxisX[stackIndex].MinLimit = sum.Left == 0
                                ? 0
                                : ((int)(sum.Left / AxisX[stackIndex].S) - 1) * AxisX[stackIndex].S;
                        }
                        if (sum.Right > AxisX[stackIndex].MaxLimit)
                        {
                            AxisX[stackIndex].MaxLimit = sum.Right == 0
                                ? 0
                                : ((int)(sum.Right / AxisX[stackIndex].S) + 1) * AxisX[stackIndex].S;
                        }
                    }
                }
                if (stackAt == AxisTags.Y)
                {
                    if (mode == StackMode.Percentage)
                    {
                        AxisY[stackIndex].MinLimit = 0;
                        AxisY[stackIndex].MaxLimit = 1;
                    }
                    else
                    {
                        if (sum.Left < AxisY[stackIndex].MinLimit)
                        {
                            AxisY[stackIndex].MinLimit = sum.Left == 0
                                ? 0
                                : ((int)(sum.Left / AxisY[stackIndex].S) - 1) * AxisY[stackIndex].S;
                        }
                        if (sum.Right > AxisY[stackIndex].MaxLimit)
                        {
                            AxisY[stackIndex].MaxLimit = sum.Right == 0
                                ? 0
                                : ((int)(sum.Right / AxisY[stackIndex].S) + 1) * AxisY[stackIndex].S;
                        }
                    }
                }

                var lastLeft  = 0d;
                var lastRight = 0d;
                var leftPart  = 0d;
                var rightPart = 0d;

                foreach (var col in stackedColumns)
                {
                    if (i >= col.Length)
                    {
                        continue;
                    }
                    var point  = col[i];
                    var pulled = Pull(point, stackAt);

                    if (pulled <= 0)
                    {
                        point.From          = lastLeft;
                        point.To            = lastLeft + pulled;
                        point.Sum           = sum.Left;
                        point.Participation = (point.To - point.From) / point.Sum;
                        point.Participation = double.IsNaN(point.Participation)
                            ? 0
                            : point.Participation;
                        leftPart += point.Participation;
                        point.StackedParticipation = leftPart;

                        lastLeft = point.To;
                    }
                    else
                    {
                        point.From          = lastRight;
                        point.To            = lastRight + pulled;
                        point.Sum           = sum.Right;
                        point.Participation = (point.To - point.From) / point.Sum;
                        point.Participation = double.IsNaN(point.Participation)
                            ? 0
                            : point.Participation;
                        rightPart += point.Participation;
                        point.StackedParticipation = rightPart;

                        lastRight = point.To;
                    }
                }
            }
        }
コード例 #14
0
        public static int[][] Arrangements(int n, int k)
        {
            if (n < 1 || k > n)
            {
                return(new int[0][]);
            }

            List <int[]> results = new List <int[]>();
            Stack <int>  stack   = new Stack <int>();

            stack.Push(0);
            StackMode stackMode = StackMode.GoUp;

            while (stack.Count > 0)
            {
                switch (stackMode)
                {
                case StackMode.GoUp:
                    bool canGoUp = stack.Count < k;
                    if (canGoUp)
                    {
                        // incercam sa urcam
                        for (int i = 0; i < n; i++)
                        {
                            bool isValidValue = !stack.Contains(i);
                            if (isValidValue)
                            {
                                // pot urca
                                stack.Push(i);

                                bool isSolution = stack.Count == k;
                                if (isSolution)
                                {
                                    int[] solutionArray = stack.ToArray();
                                    Array.Reverse(solutionArray);

                                    results.Add(solutionArray);
                                    // Console.WriteLine(string.Join(", ", stack));
                                }

                                // am reusit sa pun o valoare valida pe nivelul curent
                                // si continui sa incerc sa urc in stiva
                                break;
                            }
                        }
                    }
                    else
                    {
                        stackMode = StackMode.IncrementCurrent;
                    }
                    break;

                case StackMode.IncrementCurrent:
                    int nextValue = stack.Peek() + 1;
                    while (nextValue < n)
                    {
                        bool isValidValue = !stack.Contains(nextValue);
                        if (isValidValue)
                        {
                            stack.Pop();
                            stack.Push(nextValue);

                            bool isSolution = stack.Count == k;
                            if (isSolution)
                            {
                                int[] solutionArray = stack.ToArray();
                                Array.Reverse(solutionArray);

                                results.Add(solutionArray);
                                // Console.WriteLine(string.Join(", ", stack));
                            }
                            else
                            {
                                stackMode = StackMode.GoUp;
                            }

                            break;
                        }

                        nextValue++;
                    }

                    if (nextValue >= n)
                    {
                        stackMode = StackMode.GoDown;
                    }
                    break;

                case StackMode.GoDown:
                    bool canGoDown = stack.Count > 0;
                    if (canGoDown)
                    {
                        stack.Pop();
                        stackMode = StackMode.IncrementCurrent;
                    }
                    break;
                }
            }

            return(results.ToArray());
        }
コード例 #15
0
        /// <summary>
        /// Stacks the points.
        /// </summary>
        /// <param name="stackables">The stackables.</param>
        /// <param name="stackAt">The stack at.</param>
        /// <param name="stackIndex">Index of the stack.</param>
        /// <param name="mode">The mode.</param>
        protected void StackPoints(IEnumerable<ISeriesView> stackables, AxisOrientation stackAt, int stackIndex,
            StackMode mode = StackMode.Values)
        {
            var stackedColumns = stackables.SelectMany(x => x.ActualValues.GetPoints(x))
                .GroupBy(x => stackAt == AxisOrientation.X ? x.Y : x.X);

            double mostLeft = 0, mostRight = 0;

            foreach (var column in stackedColumns)
            {
                double sumLeft = 0, sumRight = 0;

                foreach (var item in column)
                {
                    var s = stackAt == AxisOrientation.X ? item.X : item.Y;
                    if (s < 0)
                        sumLeft += s;
                    else
                        sumRight += s;
                }

                var lastLeft = 0d;
                var lastRight = 0d;
                var leftPart = 0d;
                var rightPart = 0d;

                foreach (var point in column)
                {
                    var pulled = stackAt == AxisOrientation.X ? point.X : point.Y;

                    //notice using (pulled < 0) or (pulled <= 0) could cause an issue similar to
                    //https://github.com/beto-rodriguez/Live-Charts/issues/231
                    //from that issue I changed <= to <
                    //only because it is more common to use positive values than negative
                    //you could face a similar issue if you are stacking only negative values
                    //a work around is forcing (pulled < 0) to be true,
                    //instead of using zero values, use -0.000000001/

                    if (pulled < 0)
                    {
                        point.From = lastLeft;
                        point.To = lastLeft + pulled;
                        point.Sum = sumLeft;
                        point.Participation = (point.To - point.From) / point.Sum;
                        point.Participation = double.IsNaN(point.Participation)
                            ? 0
                            : point.Participation;
                        leftPart += point.Participation;
                        point.StackedParticipation = leftPart;

                        lastLeft = point.To;
                    }
                    else
                    {
                        point.From = lastRight;
                        point.To = lastRight + pulled;
                        point.Sum = sumRight;
                        point.Participation = (point.To - point.From) / point.Sum;
                        point.Participation = double.IsNaN(point.Participation)
                            ? 0
                            : point.Participation;
                        rightPart += point.Participation;
                        point.StackedParticipation = rightPart;

                        lastRight = point.To;
                    }
                }

                if (sumLeft < mostLeft) mostLeft = sumLeft;
                if (sumRight > mostRight) mostRight = sumRight;
            }

            if (stackAt == AxisOrientation.X)
            {
                if (mode == StackMode.Percentage)
                {
                    AxisX[stackIndex].BotLimit = 0;
                    AxisX[stackIndex].TopLimit = 1;
                }
                else
                {
                    if (mostLeft < AxisX[stackIndex].BotLimit)
                        // ReSharper disable once CompareOfFloatsByEqualityOperator
                        AxisX[stackIndex].BotLimit = mostLeft == 0
                            ? 0
                            : ((int) (mostLeft/AxisX[stackIndex].S) - 1)*AxisX[stackIndex].S;
                    if (mostRight > AxisX[stackIndex].TopLimit)
                        // ReSharper disable once CompareOfFloatsByEqualityOperator
                        AxisX[stackIndex].TopLimit = mostRight == 0
                            ? 0
                            : ((int) (mostRight/AxisX[stackIndex].S) + 1)*AxisX[stackIndex].S;
                }
            }

            if (stackAt == AxisOrientation.Y)
            {
                if (mode == StackMode.Percentage)
                {
                    AxisY[stackIndex].BotLimit = 0;
                    AxisY[stackIndex].TopLimit = 1;
                }
                else
                {
                    if (mostLeft < AxisY[stackIndex].BotLimit)
                        // ReSharper disable once CompareOfFloatsByEqualityOperator
                        AxisY[stackIndex].BotLimit = mostLeft == 0
                            ? 0
                            : ((int) (mostLeft/AxisY[stackIndex].S) - 1)*AxisY[stackIndex].S;
                    if (mostRight > AxisY[stackIndex].TopLimit)
                        // ReSharper disable once CompareOfFloatsByEqualityOperator
                        AxisY[stackIndex].TopLimit = mostRight == 0
                            ? 0
                            : ((int) (mostRight/AxisY[stackIndex].S) + 1)*AxisY[stackIndex].S;
                }
            }
        }