/// <summary> /// Add two dicts /// </summary> /// <param name="dict">Other dict</param> /// <returns></returns> public Dict Add(Dict dict) { var result = new Dict(); foreach (var curr in Data) result.Set(curr.Key, curr.Value); foreach (var curr in dict.Data) result.Set(curr.Key, curr.Value); return result; }
/// <summary> /// Print a dict /// </summary> /// <param name="dict">Dict to print</param> private static void PrintDict(Dict dict) { var keys = dict.Keys(); var maxLength = keys.Max(str => str.Length); Console.WriteLine("{"); foreach(var curr in keys) Console.WriteLine(" {0,-" + maxLength.ToString() + "} => {1}", curr, dict.Get(curr)); Console.WriteLine("}"); }
/// <summary> /// Save the dict to file /// </summary> /// <param name="dict">Dict</param> public void SaveDict(Dict dict) { Create(); var keys = dict.Keys(); for(int idx = 0; idx < keys.Length; idx++) { var str = (idx == 0 ? "" : "\r\n") + keys[idx] + "\r\n" + dict.Get(keys[idx]); var buf = System.Text.Encoding.UTF8.GetBytes(str); FileStream.Write(buf, 0, buf.Length); } Close(); }
/// <summary> /// Load a dict from file /// </summary> /// <returns></returns> public Dict LoadDict() { if (FileStream == null) Open(); FileStream.Seek(0, SeekOrigin.Begin); var array = ReadAll().Split(new[] { "\r\n" }, StringSplitOptions.None); var dict = new Dict(); for (int idx = 0; idx < array.Length / 2; idx++) dict.Set(array[idx * 2], array[idx * 2 + 1]); return dict; }