public void GetAndContainsKey() { var i = 0; var mtc = new KeyValueCache <int, string>((key) => { i++; return(key.ToString()); }); Assert.IsFalse(mtc.ContainsKey(1)); Assert.AreEqual(0, i); Assert.IsTrue(mtc.TryGetByKey(1, out string val)); Assert.AreEqual(1, i); Assert.AreEqual("1", val); Assert.IsTrue(mtc.ContainsKey(1)); }
private string CalcAtomStr(string Function, List <string> Prev, List <string> Curr, List <string> Next) { string ResultStr = Function; if (RegPattern.IsMatch(Function)) { MatchCollection mcs = RegPattern.Matches(Function); for (int i = 0; i < mcs.Count; i++) { Match m = mcs[i]; string key = m.Groups[1].Value.ToLower(); string val = m.Groups[2].Value.Trim(); List <string> CurMap = new List <string>(); string KeyIn = Curr[0] + "[]"; switch (key) { case "currentnote": CurMap = Curr; KeyIn = CurMap.Count == 0 ? "" : Function.ToLower().Replace("currentnote", CurMap[0]); break; case "prevnote": CurMap = Prev; KeyIn = CurMap.Count == 0 ? "" : Function.ToLower().Replace("prevnote", CurMap[0]); break; case "nextnote": CurMap = Next; KeyIn = CurMap.Count == 0 ? "" : Function.ToLower().Replace("nextnote", CurMap[0]); break; default: continue; } string KeyAtom = ""; // if (KeyIn == "") return ""; if (KeyIn != "") { if (KeyValueCache.ContainsKey(KeyIn)) { KeyAtom = KeyValueCache[KeyIn]; } else { int idx = 0; if (val != "") { val = val.Replace("n", (CurMap.Count - 1).ToString()); try { idx = (int)Calcer.Compute(val, ""); } catch {; } } if (idx < CurMap.Count) { KeyAtom = CurMap[idx]; try { KeyValueCache.Add(KeyIn, KeyAtom); } catch {; } } } } ResultStr = ResultStr.Replace(m.Groups[0].Value, KeyAtom); } return(ResultStr); } else { return(""); } }