Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="K"></typeparam>
        /// <typeparam name="V"></typeparam>
        ///
        /// <param name="dictionary"></param>
        /// <param name="stringbuilder"></param>
        /// <param name="formatProvider"></param>
        /// <param name="rest"></param>
        /// <returns></returns>
        public static bool ShowDictionary <K, V>(IDictionary <K, V> dictionary, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
        {
            bool sorted = dictionary is ISortedDictionary <K, V>;

            stringbuilder.Append(sorted ? "[ " : "{ ");
            rest -= 4;                             // Account for "( " and " )"
            bool first    = true;
            bool complete = true;

            foreach (KeyValuePair <K, V> p in dictionary)
            {
                complete = false;
                if (rest <= 0)
                {
                    break;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    stringbuilder.Append(", ");
                    rest -= 2;
                }
                complete = Showing.Show(p, stringbuilder, ref rest, formatProvider);
            }
            if (!complete)
            {
                stringbuilder.Append("...");
                rest -= 3;
            }
            stringbuilder.Append(sorted ? " ]" : " }");
            return(complete);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stringbuilder"></param>
        /// <param name="rest"></param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
        {
            bool incomplete = true;

            stringbuilder.Append("(");
            rest -= 2;
            try
            {
                if (incomplete = !Showing.Show(X1, stringbuilder, ref rest, formatProvider))
                {
                    return(false);
                }
                stringbuilder.Append(", ");
                rest -= 2;
                if (incomplete = !Showing.Show(X2, stringbuilder, ref rest, formatProvider))
                {
                    return(false);
                }
            }
            finally
            {
                if (incomplete)
                {
                    stringbuilder.Append("...");
                    rest -= 3;
                }
                stringbuilder.Append(")");
            }
            return(true);
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="stringbuilder"></param>
        /// <param name="rest"></param>
        /// <param name="formatProvider"></param>
        /// <returns>True if collection was shown completely</returns>
        public static bool ShowCollectionValue <T>(ICollectionValue <T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
        {
            string startdelim = "{ ", enddelim = " }";
            bool   showIndexes        = false;
            bool   showMultiplicities = false;
            //TODO: do not test here at run time, but select code at compile time
            //      perhaps by delivering the print type to this metod
            IList <T>       list;
            ICollection <T> coll = items as ICollection <T>;

            if ((list = items as IList <T>) != null)
            {
                startdelim = "[ ";
                enddelim   = " ]";
                //TODO: should have been (items as IIndexed<T>).IndexingSpeed
                showIndexes = list.IndexingSpeed == Speed.Constant;
            }
            else if (coll != null)
            {
                if (coll.AllowsDuplicates)
                {
                    startdelim = "{{ ";
                    enddelim   = " }}";
                    if (coll.DuplicatesByCounting)
                    {
                        showMultiplicities = true;
                    }
                }
            }

            stringbuilder.Append(startdelim);
            rest -= 2 * startdelim.Length;
            bool first    = true;
            bool complete = true;
            int  index    = 0;

            if (showMultiplicities)
            {
                foreach (KeyValuePair <T, int> p in coll.ItemMultiplicities())
                {
                    complete = false;
                    if (rest <= 0)
                    {
                        break;
                    }
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        stringbuilder.Append(", ");
                        rest -= 2;
                    }
                    if (complete = Showing.Show(p.Key, stringbuilder, ref rest, formatProvider))
                    {
                        string multiplicityString = string.Format("(*{0})", p.Value);
                        stringbuilder.Append(multiplicityString);
                        rest -= multiplicityString.Length;
                    }
                }
            }
            else
            {
                foreach (T x in items)
                {
                    complete = false;
                    if (rest <= 0)
                    {
                        break;
                    }
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        stringbuilder.Append(", ");
                        rest -= 2;
                    }
                    if (showIndexes)
                    {
                        string indexString = string.Format("{0}:", index++);
                        stringbuilder.Append(indexString);
                        rest -= indexString.Length;
                    }
                    complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);
                }
            }
            if (!complete)
            {
                stringbuilder.Append("...");
                rest -= 3;
            }
            stringbuilder.Append(enddelim);
            return(complete);
        }