C# 一个自己写的树结构代码(2)-Array,HashTable,List,String数据结构操作封装

来源:互联网 发布:网页美工培训学校 编辑:程序博客网 时间:2024/05/18 00:18

Array,HashTable,List,String数据结构操作封装。仅仅实现了功能,没有考虑性能。如果有更好的实现方式,希望能互相交流。


    public class ArrayUtils    {        static public T Get<T>(T[] arr, int n, T Default)        {            if (arr == null) return Default;            if (n < 0) return Default;            T value = Default;            if (arr.Count() > n)                try                {                    value = arr[n];                    if (value == null) value = Default;                }                catch                {                }            return value;        }        /// <summary>        /// 从数组中截取一部分成新的数组        /// </summary>        /// <param name="Source">原数组</param>        /// <param name="StartIndex">原数组的起始位置</param>        /// <param name="EndIndex">原数组的截止位置</param>        /// <returns></returns>        public static T[] SplitArray<T>(T[] Source, int StartIndex, int EndIndex)        {            if (Source.Length <= StartIndex)            {                return new T[0];            }            if (StartIndex < 0) StartIndex = 0;            if (EndIndex > Source.Length - 1) EndIndex = Source.Length - 1;            try            {                T[] result = new T[EndIndex - StartIndex + 1];                for (int i = 0; i <= EndIndex - StartIndex; i++) result[i] = Get<T>(Source, i + StartIndex, default(T));                return result;            }            catch (IndexOutOfRangeException ex)            {                Common.WriteException(ex);                return new T[0];            }            catch (Exception ex)            {                Common.WriteException(ex);                return new T[0];            }        }        /// <summary>        /// check string is null or empty.        /// </summary>        /// <param name="Arr"></param>        /// <returns></returns>        public static bool IsNullOrEmpty<T>(T[] Arr)        {            return Arr == null || Arr.Length == 0;        }        /// <summary>        /// check if arr index is in arr        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="Arr"></param>        /// <param name="n"></param>        /// <returns></returns>        public static bool IsValidIndex<T>(T[] Arr, int n)        {            if (IsNullOrEmpty(Arr)) return false;            return n >= 0 && n < Arr.Length - 1;        }        public static bool IsBetweenIndex<T>(T[] Arr, int n, int nStartIndex, int nEndIndex)        {            if ((IsValidIndex<T>(Arr, nStartIndex) && n < nStartIndex) ||                    (IsValidIndex<T>(Arr, nEndIndex) && n > nEndIndex))            {                return false;            }            return true;        }    }

    public class HashTableUtils    {        static public bool IsNullOrEmpty(Hashtable ht)        {            if (ht == null) return true;            if (ht.Count == 0) return true;            return false;        }        static public bool Equal(Hashtable ht1, Hashtable ht2)        {            if (ht1 == null || ht2 == null) return false;            if (ht1.Count != ht2.Count) return false;            foreach (DictionaryEntry item in ht1)            {                object obj = GetValue<object>(ht2, item.Key.ToString(), null);                if (!obj.Equals(item.Value))                    return false;            }            return true;        }        static public int Add(ref Hashtable ht, object key, object value)        {            if (ht == null) ht = new Hashtable();            if (key != null && value != null)            {                if (!ht.ContainsKey(key)) ht.Add(key, value);            }            return 0;        }        static public string GetString(Hashtable ht, string key, string Default = "")        {            object obj = GetValue(ht, key, Default);            return obj == null && Default == null ? null : obj.ToString();        }        static public T GetValue<T>(Hashtable ht, string key, T Default)        {            T value = Default;            if (ht == null || ht.Count == 0) return value;            try            {                value = (T)ht[key];                if (value == null) value = Default;            }            catch            {            }            return value;        }        static public int SetValue(ref Hashtable ht, object key, object value)        {            int nRet = 0;            if (ht == null) ht = new Hashtable();            if (key != null)            {                if (!ht.ContainsKey(key))                {                    ht.Add(key, value);                }                else                {                    ht[key] = value;                    nRet = -1;                }            }            return nRet;        }    }

    public class ListUtils    {        static public bool IsNullOrEmpty<T>(List<T> list)        {            if (list == null) return true;            if (list.Count == 0) return true;            return false;        }        static public T Get<T>(List<T> list, int n, T Default)        {            T value = Default;            if (IsNullOrEmpty<T>(list)) return value;            if (list.Count > n && n >= 0)                try                {                    value = list[n];                    if (value == null) value = Default;                }                catch                {                }            return value;        }        static public T Pop<T>(ref List<T> list, T Default = default(T))        {            T res = Get<T>(list, 0, Default);            Remove<T>(ref list, 0);            return res;        }        public static int Remove<T>(ref List<T> list, int n)        {            if (IsNullOrEmpty(list))                return -1;            if (n < 0 || n > list.Count - 1)                return -1;            list.RemoveAt(n);            return 0;        }    }

    public class StrUtils    {        public static bool Exist(string str, string tar, bool IgnoreCase = false)        {            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(tar)) return false;            if (IgnoreCase)            {                if (str.ToUpper().Length > 0 && tar.ToUpper().Length > 0)                {                    str = str.ToUpper();                    tar = tar.ToUpper();                }            }            int pos = str.IndexOf(tar, 0);            if (pos >= 0) return true;            return false;        }        static public string ConDirs(string root, string dir)        {            if (!IsExistVisibleChar(root))            {                // 说明:not need to connect. if no root                // 修改日期:2015-3-5 12:28:39                root = "";                return dir;            }            if (!IsExistVisibleChar(dir)) dir = "";            bool IsRootDir = IsDirString(root);            bool IsDirBgn = IsDirStringBgn(dir);            if (!IsRootDir && !IsDirBgn) return root.Trim() + "\\" + dir.Trim();            else if (IsRootDir && IsDirBgn) return Left(root, root.Length - 1).Trim() + "\\" + Mid(dir, 1, dir.Length - 1).Trim();            else return root + dir;        }        public static string Mid(string str, int nStartIndex, int nlength)        {            if (str == null || nlength < 0) return "";            nStartIndex = nStartIndex < 0 ? 0 : nStartIndex;            if (str.Length <= Math.Abs(nStartIndex + nlength))            {                nlength = str.Length - nStartIndex;            }            if (nlength <= 0) return "";            return Substring(str, nStartIndex, nlength);        }        static public bool IsDirStringBgn(string text)        {            if (text.Length < 1) return false;            string lst = Substring(text, 0, 1);            return lst == "\\" || lst == "/";        }        public static bool IsExistVisibleChar(string str)        {            if (string.IsNullOrEmpty(str)) return false;            Regex VisibleChar = new Regex(@"[^\r\n\t\s\v]+");            return VisibleChar.IsMatch(str);        }        static public string GetDirPart(string text)        {            if (!IsExistVisibleChar(text)) text = "";            text = GetSDirString(text);            int lstInt = text.LastIndexOf("\\");            int lstInt2 = text.LastIndexOf("/");            if (lstInt < 0 && lstInt2 < 0)            {                // 说明:error logic return text.now return empty string.                                // 修改日期:2015-9-1 21:56:05                //Logs.WriteError("path error: "+text);                return "";            }            lstInt = lstInt > lstInt2 ? lstInt : lstInt2;            return Substring(text, 0, lstInt + 1);        }        static public string GetFilePart(string text)        {            if (!IsExistVisibleChar(text)) text = "";            text = text.Trim();            int lstInt1 = text.LastIndexOf("\\");            int lstInt2 = text.LastIndexOf("/");            int lstInt = lstInt1 > lstInt2 ? lstInt1 : lstInt2;            if (lstInt < 0) return text;            return Substring(text, lstInt + 1, text.Length - lstInt - 1);        }        /// <summary>        /// string array trans to string split with flg.        /// 2016-02-18 13:24:47        /// add startindex and endindex.        /// </summary>        /// <param name="arr"></param>        /// <param name="split"></param>        /// <param name="surround1"></param>        /// <param name="surround2"></param>        /// <param name="nStartIndex"></param>        /// <param name="nEndIndex"></param>        /// <returns></returns>        public static string FromArr(string[] arr, string split = ";", string surround1 = "", string surround2 = "", int nStartIndex = -1, int nEndIndex = -1)        {            string str = "";            if (arr == null) return str;            int nIndex = -1;            foreach (string item in arr)            {                nIndex++;                if (!ArrayUtils.IsBetweenIndex<string>(arr, nIndex, nStartIndex, nEndIndex))                {                    continue;                }                if (str.Length > 0) str += split;                str += surround1 + item + surround2;            }            return str;        }        public static string[] Split(string str, string splitflg)        {            if (string.IsNullOrEmpty(str)) return new string[] { };            if (string.IsNullOrEmpty(splitflg))            {                return new string[] { str };            }            string[] strArr = null;            RegexOptions ro = RegexOptions.Multiline | RegexOptions.ExplicitCapture;            strArr = Regex.Split(str, splitflg, ro);            //char[] splitflgArr = splitflg.ToArray();            //strArr = str.Split(splitflgArr);            return strArr;        }        public static string FormatPath(string srcFilePath, bool IsWindows = true)        {            string splitflg1 = "/", splitflg2 = "\\";            if (string.IsNullOrEmpty(srcFilePath)) return "";            srcFilePath = srcFilePath.Trim();            if (!IsWindows)            {                splitflg1 = "\\";                splitflg2 = "/";            }            srcFilePath.Replace(splitflg1, splitflg2);            return srcFilePath;        }        static public string GetSDirString(string text)        {            if (text.Length < 1) return text;            if (IsDirString(text))                text = Left(text, text.Length - 1);            return text;        }        static public bool IsDirString(string text)        {            if (text.Length < 1) return false;            string lst = Substring(text, text.Length - 1, 1);            return lst == "\\" || lst == "/";        }        public static string Substring(string str, int nPos, int nlength = -1)        {            if (string.IsNullOrEmpty(str)) return "";            if (nlength == -1) nlength = str.Length - nPos;            char[] ch = new char[nlength];            for (int i = 0; i < nlength; i++)            {                ch[i] = str[nPos + i];            }            return new string(ch);        }        public static string Left(string str, int nlength)        {            if (str == null || nlength < 0) return "";            if (str.Length <= Math.Abs(nlength)) return str;            return Substring(str, 0, nlength);        }    }



0 0