Exemplo n.º 1
0
        public static IList <T> HRemoveAllNull <T>(this IList <T> values, bool bAssert = true)
            where T : class
        {
            //HDebug.Depreciated("use HRemoveAll(null), instead");
            HDebug.Break(bAssert && HRemoveAllNull_break);

            Predicate <T> match = delegate(T val)
            {
                return(val == null);
            };

            List <T> _values = new List <T>(values);

            _values.RemoveAll(match);
            return(_values);
        }
Exemplo n.º 2
0
        protected static void _ToString2(StringBuilder text, string format, object obj)
        {
            try
            {
                if (obj == null)
                {
                    return;
                }
                string name = obj.GetType().FullName;

                if (name == typeof(Vector).FullName)
                {
                    Vector data = (Vector)obj;
                    _ToString2(text, format, data._data);
                    return;
                }
                if (name == typeof(int).FullName)
                {
                    if (format == null)
                    {
                        text.Append(obj.ToString()); return;
                    }
                    else
                    {
                        text.Append(string.Format(format, (int)obj)); return;
                    }
                }
                if (name == typeof(double).FullName)
                {
                    string str;
                    if (format == null)
                    {
                        str = ((double)obj).ToString();
                    }
                    else
                    {
                        str = string.Format(format, (double)obj);
                    }

                    if (str.Contains("E") || str.Contains("e"))
                    {
                        str = str.Replace("e", "*10^");
                        str = str.Replace("E", "*10^");
                    }

                    text.Append(str);
                    return;
                }
                if (name == typeof(string).FullName)
                {
                    string str = obj.ToString();
                    if (str.StartsWith("(* ") && str.EndsWith(" *)"))
                    {
                        // add comment as it is
                        text.Append(str);
                    }
                    else
                    {
                        str = str.Replace("\\", "\\\\");
                        str = "\"" + str + "\"";

                        text.Append(str);
                    }
                    return;
                }
                if (name == typeof(char).FullName)
                {
                    string str = "\'" + (char)obj + "\'";

                    text.Append(str);
                    return;
                }

                if (obj is System.Runtime.CompilerServices.ITuple)
                {
                    _ToString2(text, format, obj as System.Runtime.CompilerServices.ITuple); return;
                }
                if (obj is System.Collections.IDictionary)
                {
                    _ToString2(text, format, obj as System.Collections.IDictionary); return;
                }
                if (obj is IMatrix <double> )
                {
                    _ToString2(text, format, obj as IMatrix <double>); return;
                }
                if (obj is System.Collections.IList)
                {
                    //HDebug.ToDo();
                    //var objs = obj as System.Collections.IList;
                    //objs.getleng
                }
                if (obj is System.Collections.IEnumerable)
                {
                    _ToString2(text, format, obj as System.Collections.IEnumerable); return;
                }

                //if(name==typeof(List<Tuple<double,int>>                              ).FullName) return _ToString2                           (format, (List<Tuple<double,int>>                              )obj);
                //if(name==typeof(List<Tuple<int,double>>                              ).FullName) return _ToString2                           (format, (List<Tuple<int,double>>                              )obj);
                //if(name==typeof(List<Tuple<double,double>>                           ).FullName) return _ToString2                           (format, (List<Tuple<double,double>>                           )obj);
                //if(name==typeof(List<double>                                         ).FullName) return _ToString2                           (format, (List<double>                                         )obj);
                //if(name==typeof(List<Vector>                                         ).FullName) return _ToString2                           (format, (List<Vector>                                         )obj);
                //if(name==typeof(List<DoubleVector3>                                  ).FullName) return _ToString2                           (format, (List<DoubleVector3>                                  )obj);
                //if(name==typeof(List<double[]>                                       ).FullName) return _ToString2                           (format, (List<double[]>                                       )obj);
                //if(name==typeof(List<double[,]>                                      ).FullName) return _ToString2                           (format, (List<double[,]>                                      )obj);
                //if(name==typeof(MatrixByArr[]                                        ).FullName) return _ToString2<MatrixByArr>              (format, (MatrixByArr[]                                        )obj);
                //if(name==typeof(Vector[]                                             ).FullName) return _ToString2<Vector>                   (format, (Vector[]                                             )obj);
                //if(name==typeof(string[]                                             ).FullName) return _ToString2<string>                   (format, (string[]                                             )obj);
                //if(name==typeof(string[,]                                            ).FullName) return _ToString2                           (format, (string[,]                                            )obj);
                //if(name==typeof(double[]                                             ).FullName) return _ToString2                           (format, (double[]                                             )obj);
                //if(name==typeof(int[]                                                ).FullName) return _ToString2                           (format, (int[]                                                )obj);
                //if(name==typeof(double[,]                                            ).FullName) return _ToString2                           (format, (double[,]                                            )obj);
                //if(name==typeof(double[,,]                                           ).FullName) return _ToString2                           (format, (double[,,]                                           )obj);
                //if(name==typeof(DoubleVector3[]                                      ).FullName) return _ToString2                           (format, (DoubleVector3[]                                      )obj);
                //if(name==typeof(double[][]                                           ).FullName) return _ToString2<double[]>                 (format, (double[][]                                           )obj);
                //if(name==typeof(double[][,]                                          ).FullName) return _ToString2                           (format, new List<double[,]>((double[][,]                      )obj));
                //if(name==typeof(double[,][]                                          ).FullName) return _ToString2                           (format, (double[,][]                                          )obj);
                //if(name==typeof(List<int>                                            ).FullName) return _ToString2<int>                      (format, (IEnumerable<int>                                     )obj);
                //if(name==typeof(List<string>                                         ).FullName) return _ToString2<string>                   (format, (IEnumerable<string>                                  )obj);
                //if(name==typeof(List<string[]>                                       ).FullName) return _ToString2<string[]>                 (format, (IEnumerable<string[]>                                )obj);
                //if(name==typeof(List<List<int>>                                      ).FullName) return _ToString2<List<int>>                (format, (IEnumerable<List<int>>                               )obj);
                //if(name==typeof(List<List<double>>                                   ).FullName) return _ToString2<List<double>>             (format, (IEnumerable<List<double>>                            )obj);
                //if(name==typeof(List<List<string>>                                   ).FullName) return _ToString2<List<string>>             (format, (IEnumerable<List<string>>                            )obj);
                //if(name==typeof(List<List<Vector>>                                   ).FullName) return _ToString2<List<Vector>>             (format, (IEnumerable<List<Vector>>                            )obj);
                //if(name==typeof(Dictionary<double,double>                            ).FullName) return _ToString2<double,double>            (format, (Dictionary<double,double>                            )obj);

                //if(name==typeof(KeyValuePair<double,Vector[]>                        ).FullName) return _ToString2<double,Vector[]>          (format, (KeyValuePair<double,Vector[]>                        )obj);
                //if(name==typeof(KeyValuePair<double,double[]>                        ).FullName) return _ToString2<double,double[]>          (format, (KeyValuePair<double,double[]>                        )obj);
                //if(name==typeof(KeyValuePair<double,string>                          ).FullName) return _ToString2<double,string>            (format, (KeyValuePair<double,string>                          )obj);
                //if(name==typeof(KeyValuePair<int, Tuple<Vector, Vector>>             ).FullName) return _ToString2<int, Tuple<Vector,Vector>>(format, (KeyValuePair<int, Tuple<Vector, Vector>>             )obj);
                //if(name==typeof(Dictionary<double,double[]>                          ).FullName) return _ToString2<double,double[]>          (format, (Dictionary<double,double[]>                          )obj);

                //if(name==typeof(Tuple<int   , int           >).FullName) { var val=(Tuple<int   , int           >)obj; return _ToString2(format, val.Item1, val.Item2); }
                //if(name==typeof(Tuple<int   , double        >).FullName) { var val=(Tuple<int   , double        >)obj; return _ToString2(format, val.Item1, val.Item2); }
                //if(name==typeof(Tuple<int   , double, double>).FullName) { var val=(Tuple<int   , double, double>)obj; return _ToString2(format, val.Item1, val.Item2, val.Item3); }
                //if(name==typeof(Tuple<double, double        >).FullName) { var val=(Tuple<double, double        >)obj; return _ToString2(format, val.Item1, val.Item2); }
                //if(name==typeof(Tuple<double, double, double>).FullName) { var val=(Tuple<double, double, double>)obj; return _ToString2(format, val.Item1, val.Item2, val.Item3); }
                //if(name==typeof(Tuple<double, int, int, int >).FullName) { var val=(Tuple<double, int, int, int >)obj; return _ToString2(format, val.Item1, val.Item2, val.Item3, val.Item4); }
                //if(name==typeof(Tuple<int   ,Vector         >).FullName) { var val=(Tuple<int   ,Vector         >)obj; return _ToString2(format, val.Item1, val.Item2); }
                //if(name==typeof(Tuple<string,string         >).FullName) { var val=(Tuple<string,string         >)obj; return _ToString2(format, val.Item1, val.Item2); }
                //if(name==typeof(Tuple<string,int            >).FullName) { var val=(Tuple<string,int            >)obj; return _ToString2(format, val.Item1, val.Item2); }

                //if(name==typeof(KeyValuePair<double, double>).FullName)
                //{
                //    KeyValuePair<double, double> kvp = (KeyValuePair<double, double>)obj;
                //    return _ToString2(format, kvp.Key, kvp.Value);
                //}
                //if(name == typeof(MatrixDouble).FullName)
                //{
                //    MatrixDouble data = (MatrixDouble)obj;
                //    return _ToString2(data.ToArray());
                //}
                //if(name == typeof(MatrixByArr).FullName)
                //{
                //    MatrixByArr data = (MatrixByArr)obj;
                //    return _ToString2(format, data.ToArray());
                //}
                //if(name == typeof(DoubleVector3).FullName)
                //{
                //    DoubleVector3 data = (DoubleVector3)obj;
                //    return _ToString2(data.v0, data.v1, data.v2);
                //}
                //if(name == typeof(DoubleMatrix3).FullName)
                //{
                //    DoubleMatrix3 data = (DoubleMatrix3)obj;
                //    return _ToString2(data.ToArray());
                //}
                if (name == typeof(object[]).FullName)
                {
                    _ToString2(text, format, (object[])obj);
                    return;
                }
                HDebug.Assert(false);
                HDebug.Break();
            }
            catch (Exception)
            {
                HDebug.Break();
            }
        }
Exemplo n.º 3
0
        protected static string _ToString(string format, object obj)
        {
            try{
                if (obj == null)
                {
                    return("");
                }
                string name = obj.GetType().FullName;

                if (obj is System.Runtime.CompilerServices.ITuple)
                {
                    return(_ToString(format, obj as System.Runtime.CompilerServices.ITuple));
                }
                if (obj is System.Collections.IDictionary)
                {
                    return(_ToString(format, obj as System.Collections.IDictionary));
                }

                //if(name==typeof(Tuple<DoubleVector3,Tuple<double,DoubleVector3>[]>     ).FullName) return _ToString                           (format, (Tuple<DoubleVector3,Tuple<double,DoubleVector3>[]>     )obj);
                //if(name==typeof(Tuple<double,DoubleVector3>[]                          ).FullName) return _ToString                           (format, (Tuple<double,DoubleVector3>[]                          )obj);
                //if(name==typeof(Tuple<double,DoubleVector3>                            ).FullName) return _ToString                           (format, (Tuple<double,DoubleVector3>                            )obj);
                //if(name==typeof(Tuple<DoubleVector3,Tuple<DoubleVector3,DoubleVector3>>).FullName) return _ToString                           (format, (Tuple<DoubleVector3,Tuple<DoubleVector3,DoubleVector3>>)obj);
                //if(name==typeof(Tuple<DoubleVector3,DoubleVector3>                     ).FullName) return _ToString                           (format, (Tuple<DoubleVector3,DoubleVector3>                     )obj);
                //if(name==typeof(Tuple<double[],double[]>                             ).FullName) return _ToString                           (format, (Tuple<double[],double[]>                             )obj);
                //if(name==typeof(Tuple<double,double>                                 ).FullName) return _ToString                           (format, (Tuple<double,double>                                 )obj);
                //if(name==typeof(Tuple<double,int>                                    ).FullName) return _ToString                           (format, (Tuple<double,int>                                    )obj);
                //if(name==typeof(Tuple<int,double>                                    ).FullName) return _ToString                           (format, (Tuple<int,double>                                    )obj);
                //if(name==typeof(Tuple<double,Vector>                                 ).FullName) return _ToString                           (format, (Tuple<double,Vector>                                 )obj);
                //if(name==typeof(Tuple<Vector,Vector>                                 ).FullName) return _ToString                           (format, (Tuple<Vector,Vector>                                 )obj);
                if (name == typeof(List <Tuple <double, int> >).FullName)
                {
                    return(_ToString(format, (List <Tuple <double, int> >)obj));
                }
                if (name == typeof(List <Tuple <int, double> >).FullName)
                {
                    return(_ToString(format, (List <Tuple <int, double> >)obj));
                }
                if (name == typeof(List <Tuple <double, double> >).FullName)
                {
                    return(_ToString(format, (List <Tuple <double, double> >)obj));
                }
                if (name == typeof(List <double>).FullName)
                {
                    return(_ToString(format, (List <double>)obj));
                }
                if (name == typeof(List <Vector>).FullName)
                {
                    return(_ToString(format, (List <Vector>)obj));
                }
                //if(name==typeof(List<DoubleVector3>                                  ).FullName) return _ToString                           (format, (List<DoubleVector3>                                  )obj);
                if (name == typeof(List <double[]>).FullName)
                {
                    return(_ToString(format, (List <double[]>)obj));
                }
                if (name == typeof(List <double[, ]>).FullName)
                {
                    return(_ToString(format, (List <double[, ]>)obj));
                }
                if (name == typeof(MatrixByArr[]).FullName)
                {
                    return(_ToString <MatrixByArr>              (format, (MatrixByArr[]                                        )obj));
                }
                if (name == typeof(Vector[]).FullName)
                {
                    return(_ToString <Vector>                   (format, (Vector[]                                             )obj));
                }
                if (name == typeof(string[]).FullName)
                {
                    return(_ToString <string>                   (format, (string[]                                             )obj));
                }
                if (name == typeof(string[, ]).FullName)
                {
                    return(_ToString(format, (string[, ])obj));
                }
                if (name == typeof(double[]).FullName)
                {
                    return(_ToString(format, (double[]                                             )obj));
                }
                if (name == typeof(int[]).FullName)
                {
                    return(_ToString(format, (int[]                                                )obj));
                }
                if (name == typeof(double[, ]).FullName)
                {
                    return(_ToString(format, (double[, ])obj));
                }
                if (name == typeof(double[, , ]).FullName)
                {
                    return(_ToString(format, (double[, , ])obj));
                }
                //if(name==typeof(DoubleVector3[]                                      ).FullName) return _ToString                           (format, (DoubleVector3[]                                      )obj);
                if (name == typeof(double[][]).FullName)
                {
                    return(_ToString <double[]>                 (format, (double[][]                                           )obj));
                }
                if (name == typeof(double[][, ]).FullName)
                {
                    return(_ToString(format, new List <double[, ]>((double[][, ])obj)));
                }
                if (name == typeof(double[, ][]).FullName)
                {
                    return(_ToString(format, (double[, ][])obj));
                }
                if (name == typeof(List <int>).FullName)
                {
                    return(_ToString <int>                      (format, (IEnumerable <int>)obj));
                }
                if (name == typeof(List <string>).FullName)
                {
                    return(_ToString <string>                   (format, (IEnumerable <string>)obj));
                }
                if (name == typeof(List <string[]>).FullName)
                {
                    return(_ToString <string[]>                 (format, (IEnumerable <string[]>)obj));
                }
                if (name == typeof(List <List <int> >).FullName)
                {
                    return(_ToString <List <int> >                (format, (IEnumerable <List <int> >)obj));
                }
                if (name == typeof(List <List <double> >).FullName)
                {
                    return(_ToString <List <double> >             (format, (IEnumerable <List <double> >)obj));
                }
                if (name == typeof(List <List <string> >).FullName)
                {
                    return(_ToString <List <string> >             (format, (IEnumerable <List <string> >)obj));
                }
                if (name == typeof(List <List <Vector> >).FullName)
                {
                    return(_ToString <List <Vector> >             (format, (IEnumerable <List <Vector> >)obj));
                }
                //if(name==typeof(Dictionary<double,double>                            ).FullName) return _ToString<double,double>            (format, (Dictionary<double,double>                            )obj);

                //if(name==typeof(KeyValuePair<double,Vector[]>                        ).FullName) return _ToString<double,Vector[]>          (format, (KeyValuePair<double,Vector[]>                        )obj);
                //if(name==typeof(KeyValuePair<double,double[]>                        ).FullName) return _ToString<double,double[]>          (format, (KeyValuePair<double,double[]>                        )obj);
                //if(name==typeof(KeyValuePair<double,string>                          ).FullName) return _ToString<double,string>            (format, (KeyValuePair<double,string>                          )obj);
                //if(name==typeof(KeyValuePair<int, Tuple<Vector, Vector>>             ).FullName) return _ToString<int, Tuple<Vector,Vector>>(format, (KeyValuePair<int, Tuple<Vector, Vector>>             )obj);
                //if(name==typeof(Dictionary<double,double[]>                          ).FullName) return _ToString<double,double[]>          (format, (Dictionary<double,double[]>                          )obj);

                //if(name==typeof(Tuple<int   , int           >).FullName) { var val=(Tuple<int   , int           >)obj; return _ToString(format, val.Item1, val.Item2); }
                //if(name==typeof(Tuple<int   , double        >).FullName) { var val=(Tuple<int   , double        >)obj; return _ToString(format, val.Item1, val.Item2); }
                //if(name==typeof(Tuple<int   , double, double>).FullName) { var val=(Tuple<int   , double, double>)obj; return _ToString(format, val.Item1, val.Item2, val.Item3); }
                //if(name==typeof(Tuple<double, double        >).FullName) { var val=(Tuple<double, double        >)obj; return _ToString(format, val.Item1, val.Item2); }
                //if(name==typeof(Tuple<double, double, double>).FullName) { var val=(Tuple<double, double, double>)obj; return _ToString(format, val.Item1, val.Item2, val.Item3); }
                //if(name==typeof(Tuple<double, int, int, int >).FullName) { var val=(Tuple<double, int, int, int >)obj; return _ToString(format, val.Item1, val.Item2, val.Item3, val.Item4); }
                //if(name==typeof(Tuple<int   ,Vector         >).FullName) { var val=(Tuple<int   ,Vector         >)obj; return _ToString(format, val.Item1, val.Item2); }
                //if(name==typeof(Tuple<string,string         >).FullName) { var val=(Tuple<string,string         >)obj; return _ToString(format, val.Item1, val.Item2); }
                //if(name==typeof(Tuple<string,int            >).FullName) { var val=(Tuple<string,int            >)obj; return _ToString(format, val.Item1, val.Item2); }

                if (name == typeof(KeyValuePair <double, double>).FullName)
                {
                    KeyValuePair <double, double> kvp = (KeyValuePair <double, double>)obj;
                    return(_ToString(format, kvp.Key, kvp.Value));
                }
                //if(name == typeof(MatrixDouble).FullName)
                //{
                //    MatrixDouble data = (MatrixDouble)obj;
                //    return _ToString(data.ToArray());
                //}
                if (name == typeof(MatrixByArr).FullName)
                {
                    MatrixByArr data = (MatrixByArr)obj;
                    return(_ToString(format, data.ToArray()));
                }
                if (name == typeof(Vector).FullName)
                {
                    Vector data = (Vector)obj;
                    return(_ToString(format, data._data));
                }
                //if(name == typeof(DoubleVector3).FullName)
                //{
                //    DoubleVector3 data = (DoubleVector3)obj;
                //    return _ToString(data.v0, data.v1, data.v2);
                //}
                //if(name == typeof(DoubleMatrix3).FullName)
                //{
                //    DoubleMatrix3 data = (DoubleMatrix3)obj;
                //    return _ToString(data.ToArray());
                //}
                if (name == typeof(int).FullName)
                {
                    if (format == null)
                    {
                        return(obj.ToString());
                    }
                    else
                    {
                        return(string.Format(format, (int)obj));
                    }
                }
                if (name == typeof(double).FullName)
                {
                    string text;
                    if (format == null)
                    {
                        text = ((double)obj).ToString();
                    }
                    else
                    {
                        text = string.Format(format, (double)obj);
                    }

                    if (text.Contains("E") || text.Contains("e"))
                    {
                        text = text.Replace("e", "*10^");
                        text = text.Replace("E", "*10^");
                    }
                    return(text);
                }
                if (name == typeof(string).FullName)
                {
                    string str = obj.ToString();
                    str = str.Replace("\\", "\\\\");
                    str = "\"" + str + "\"";
                    return(str);
                }
                if (name == typeof(object[]).FullName)
                {
                    return(_ToString(format, (object[])obj));
                }
                HDebug.Assert(false);
                HDebug.Break();
                return(null);
            }
            catch (Exception)
            {
                HDebug.Break();
                return(null);
            }
        }
Exemplo n.º 4
0
        protected static IEnumerable <string> _EnumString(string format, object obj)
        {
            if (obj == null)
            {
                yield break;
            }

            switch (obj)
            {
            case Vector data:
            {
                foreach (var str in _EnumString(format, data._data))
                {
                    yield return(str);
                }
            }
                yield break;

            case int data:
            {
                if (format == null)
                {
                    yield return(data.ToString());
                }
                else
                {
                    yield return(string.Format(format, data));
                }
            }
                yield break;

            case double data:
            {
                string str;
                if (format == null)
                {
                    str = data.ToString();
                }
                else
                {
                    str = string.Format(format, (double)obj);
                }

                if (str.Contains("E") || str.Contains("e"))
                {
                    str = str.Replace("e", "*10^");
                    str = str.Replace("E", "*10^");
                }

                yield return(str);
            }
                yield break;

            case string data:
            {
                string str = data;
                if (str.StartsWith("(* ") && str.EndsWith(" *)"))
                {
                    // add comment as it is
                    yield return(str);
                }
                else
                {
                    str = str.Replace("\\", "\\\\");
                    str = "\"" + str + "\"";

                    yield return(str);
                }
            }
                yield break;

            case char data:
            {
                string str = "\'" + data + "\'";
                yield return(str);
            }
                yield break;

            case ITuple data: foreach (var str in _EnumString(format, data))
                {
                    yield return(str);
                }
                yield break;

            case IDictionary data: foreach (var str in _EnumString(format, data))
                {
                    yield return(str);
                }
                yield break;

            case IEnumerable data: foreach (var str in _EnumString(format, data))
                {
                    yield return(str);
                }
                yield break;
            //case IList

            default:
            {
                if (obj is object[])
                {
                    foreach (var str in _EnumString(format, (object[])obj))
                    {
                        yield return(str);
                    }
                }
            }
                yield break;
            }


            HDebug.Assert(false);
            HDebug.Break();
        }