/* Copyright (C) 2014-2019 de4dot@gmail.com This file is part of dnSpy dnSpy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. dnSpy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with dnSpy. If not, see . */ using System; using System.Collections.Generic; namespace dnSpy.Debugger.DotNet.Metadata.Impl.COMD { sealed class LazyList where T : class { readonly Dictionary dict; readonly Func readElementByRID; public T? this[uint index] { get { T? elem; if (dict.TryGetValue(index, out elem)) return elem; elem = readElementByRID(index + 1); if (elem is not null) dict[index] = elem; return elem; } } public T? TryGet(uint index) { dict.TryGetValue(index, out var elem); return elem; } public LazyList(Func readElementByRID) { this.readElementByRID = readElementByRID; dict = new Dictionary(); } } sealed class LazyList where TValue : class { readonly Dictionary dict; readonly Func readElementByRID; public TValue? this[uint index, TArg arg] { get { TValue? elem; if (dict.TryGetValue(index, out elem)) return elem; elem = readElementByRID(index + 1, arg); if (elem is not null) dict[index] = elem; return elem; } } public LazyList(Func readElementByRID) { this.readElementByRID = readElementByRID; dict = new Dictionary(); } } sealed class LazyList2 where TValue : class { readonly Dictionary dict; readonly Func readElementByRID; public TValue? this[uint index, TArg1 arg1, TArg2 arg2] { get { TValue? elem; if (dict.TryGetValue(index, out elem)) return elem; var info = readElementByRID(index + 1, arg1, arg2); if ((elem = info.elem) is not null && !info.containedGenericParams) dict[index] = elem; return elem; } } public LazyList2(Func readElementByRID) { this.readElementByRID = readElementByRID; dict = new Dictionary(); } } }