1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using System.Collections.Generic;
10 
11 namespace Antmicro.Renode.UnitTests.Utilities
12 {
13     public sealed class LazyPool<T>
14     {
LazyPool(Func<T> factory)15         public LazyPool(Func<T> factory)
16         {
17             this.factory = factory;
18             list = new List<T>();
19         }
20 
21         public T this[int index]
22         {
23             get
24             {
25                 CheckIndex(index);
26                 return list[index];
27             }
28             set
29             {
30                 CheckIndex(index);
31                 list[index] = value;
32             }
33         }
34 
CheckIndex(int index)35         private void CheckIndex(int index)
36         {
37             while(index >= list.Count)
38             {
39                 list.Add(factory());
40             }
41         }
42 
43         private readonly List<T> list;
44         private readonly Func<T> factory;
45     }
46 }
47