当前位置: 首页>>代码示例>>C#>>正文


C# SortedDictionary.AddSafe方法代码示例

本文整理汇总了C#中SortedDictionary.AddSafe方法的典型用法代码示例。如果您正苦于以下问题:C# SortedDictionary.AddSafe方法的具体用法?C# SortedDictionary.AddSafe怎么用?C# SortedDictionary.AddSafe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SortedDictionary的用法示例。


在下文中一共展示了SortedDictionary.AddSafe方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: toString

        private void toString(int? maxWidth, Action<string> outputString, Action<ConsoleColoredString> outputColoredString)
        {
            int rows = _cells.Count;
            if (rows == 0)
                return;
            int cols = _cells.Max(row => row.Count);

            // Create a lookup array which, for each column, and for each possible value of colspan, tells you which cells in that column have this colspan and end in this column
            var cellsByColspan = new SortedDictionary<int, List<int>>[cols];
            for (var col = 0; col < cols; col++)
            {
                var cellsInThisColumn = new SortedDictionary<int, List<int>>();
                for (int row = 0; row < rows; row++)
                {
                    if (col >= _cells[row].Count)
                        continue;
                    var cel = _cells[row][col];
                    if (cel == null)
                        continue;
                    if (cel is surrogateCell && ((surrogateCell) cel).RealRow != row)
                        continue;
                    int realCol = cel is surrogateCell ? ((surrogateCell) cel).RealCol : col;
                    var realCell = (trueCell) _cells[row][realCol];
                    if (realCol + realCell.ColSpan - 1 != col)
                        continue;
                    cellsInThisColumn.AddSafe(realCell.ColSpan, row);
                }
                cellsByColspan[col] = cellsInThisColumn;
            }

            // Find out the width that each column would have if the text wasn't wrapped.
            // If this fits into the total width, then we want each column to be at least this wide.
            var columnWidths = generateColumnWidths(cols, cellsByColspan, c => Math.Max(1, c.LongestParagraph()));
            var unwrapped = true;

            // If the table is now too wide, use the length of the longest word, or longest paragraph if nowrap
            if (maxWidth != null && columnWidths.Sum() > maxWidth - (cols - 1) * ColumnSpacing)
            {
                columnWidths = generateColumnWidths(cols, cellsByColspan, c => Math.Max(1, c.MinWidth()));
                unwrapped = false;
            }

            // If the table is still too wide, use the length of the longest paragraph if nowrap, otherwise 0
            if (maxWidth != null && columnWidths.Sum() > maxWidth - (cols - 1) * ColumnSpacing)
                columnWidths = generateColumnWidths(cols, cellsByColspan, c => c.NoWrap ? Math.Max(1, c.LongestParagraph()) : 1);

            // If the table is still too wide, we will have to wrap like crazy.
            if (maxWidth != null && columnWidths.Sum() > maxWidth - (cols - 1) * ColumnSpacing)
            {
                columnWidths = new int[cols];
                for (int i = 0; i < cols; i++) columnWidths[i] = 1;
            }

            // If the table is STILL too wide, all bets are off.
            if (maxWidth != null && columnWidths.Sum() > maxWidth - (cols - 1) * ColumnSpacing)
                throw new InvalidOperationException(@"The specified table width is too narrow. It is not possible to fit the {0} columns and the column spacing of {1} per column into a total width of {2} characters.".Fmt(cols, ColumnSpacing, maxWidth));

            // If we have any extra width to spare...
            var missingTotalWidth = maxWidth == null ? 0 : maxWidth - columnWidths.Sum() - (cols - 1) * ColumnSpacing;
            if (missingTotalWidth > 0 && (UseFullWidth || !unwrapped))
            {
                // Use the length of the longest paragraph in each column to calculate a proportion by which to enlarge each column
                var widthProportionByCol = new int[cols];
                for (var col = 0; col < cols; col++)
                    foreach (var kvp in cellsByColspan[col])
                        distributeEvenly(
                            widthProportionByCol,
                            col,
                            kvp.Key,
                            kvp.Value.Max(row => ((trueCell) _cells[row][col - kvp.Key + 1]).LongestParagraph()) - widthProportionByCol.Skip(col - kvp.Key + 1).Take(kvp.Key).Sum() - (unwrapped ? 0 : columnWidths.Skip(col - kvp.Key + 1).Take(kvp.Key).Sum())
                        );
                var widthProportionTotal = widthProportionByCol.Sum();

                // Adjust the width of the columns according to the calculated proportions so that they fill the missing width.
                // We do this in two steps. Step one: enlarge the column widths by the integer part of the calculated portion (round down).
                // After this the width remaining will be smaller than the number of columns, so each column is missing at most 1 character.
                var widthRemaining = missingTotalWidth;
                var fractionalParts = new double[cols];
                for (int col = 0; col < cols; col++)
                {
                    var widthToAdd = (double) (widthProportionByCol[col] * missingTotalWidth) / widthProportionTotal;
                    var integerPart = (int) widthToAdd;
                    columnWidths[col] += integerPart;
                    fractionalParts[col] = widthToAdd - integerPart;
                    widthRemaining -= integerPart;
                }

                // Step two: enlarge a few more columns by 1 character so that we reach the desired width.
                // The columns with the largest fractional parts here are the furthest from what we ideally want, so we favour those.
                foreach (var elem in fractionalParts.Select((frac, col) => new { Value = frac, Col = col }).OrderByDescending(e => e.Value))
                {
                    if (widthRemaining < 1) break;
                    columnWidths[elem.Col]++;
                    widthRemaining--;
                }
            }

            // Word-wrap all the contents of all the cells
            trueCell truCel;
            foreach (var row in _cells)
//.........这里部分代码省略.........
开发者ID:RT-Projects,项目名称:RT.Util,代码行数:101,代码来源:TextTable.cs


注:本文中的SortedDictionary.AddSafe方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。