using System; using System.Collections.Generic; using System.Collections.Concurrent; namespace Semmle.Util; public class MemoizedFunc where T1 : notnull { private readonly Func f; private readonly Dictionary cache = []; public MemoizedFunc(Func f) { this.f = f; } public T2 Invoke(T1 s) { if (!cache.TryGetValue(s, out var t)) { t = f(s); cache[s] = t; } return t; } } public class ConcurrentMemoizedFunc where T1 : notnull { private readonly Func f; private readonly ConcurrentDictionary cache = []; public ConcurrentMemoizedFunc(Func f) { this.f = f; } public T2 Invoke(T1 s) => cache.GetOrAdd(s, f); }