You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.5 KiB

  1. #nullable enable
  2. // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  5. // software and associated documentation files (the "Software"), to deal in the Software
  6. // without restriction, including without limitation the rights to use, copy, modify, merge,
  7. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  8. // to whom the Software is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in all copies or
  11. // substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  14. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  16. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. // DEALINGS IN THE SOFTWARE.
  19. using System;
  20. using System.Collections.Generic;
  21. namespace ICSharpCode.Decompiler.Util
  22. {
  23. public sealed class ProjectedList<TInput, TOutput> : IReadOnlyList<TOutput> where TOutput : class
  24. {
  25. readonly IList<TInput> input;
  26. readonly Func<TInput, TOutput> projection;
  27. readonly TOutput?[] items;
  28. public ProjectedList(IList<TInput> input, Func<TInput, TOutput> projection)
  29. {
  30. if (input == null)
  31. throw new ArgumentNullException(nameof(input));
  32. if (projection == null)
  33. throw new ArgumentNullException(nameof(projection));
  34. this.input = input;
  35. this.projection = projection;
  36. this.items = new TOutput?[input.Count];
  37. }
  38. public TOutput this[int index] {
  39. get {
  40. TOutput? output = LazyInit.VolatileRead(ref items[index]);
  41. if (output != null)
  42. {
  43. return output;
  44. }
  45. return LazyInit.GetOrSet(ref items[index], projection(input[index]));
  46. }
  47. }
  48. public int Count {
  49. get { return items.Length; }
  50. }
  51. public IEnumerator<TOutput> GetEnumerator()
  52. {
  53. for (int i = 0; i < this.Count; i++)
  54. {
  55. yield return this[i];
  56. }
  57. }
  58. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  59. {
  60. return GetEnumerator();
  61. }
  62. }
  63. public sealed class ProjectedList<TContext, TInput, TOutput> : IReadOnlyList<TOutput> where TOutput : class
  64. {
  65. readonly IList<TInput> input;
  66. readonly TContext context;
  67. readonly Func<TContext, TInput, TOutput> projection;
  68. readonly TOutput?[] items;
  69. public ProjectedList(TContext context, IList<TInput> input, Func<TContext, TInput, TOutput> projection)
  70. {
  71. if (input == null)
  72. throw new ArgumentNullException(nameof(input));
  73. if (projection == null)
  74. throw new ArgumentNullException(nameof(projection));
  75. this.input = input;
  76. this.context = context;
  77. this.projection = projection;
  78. this.items = new TOutput?[input.Count];
  79. }
  80. public TOutput this[int index] {
  81. get {
  82. TOutput? output = LazyInit.VolatileRead(ref items[index]);
  83. if (output != null)
  84. {
  85. return output;
  86. }
  87. return LazyInit.GetOrSet(ref items[index], projection(context, input[index]));
  88. }
  89. }
  90. public int Count {
  91. get { return items.Length; }
  92. }
  93. public IEnumerator<TOutput> GetEnumerator()
  94. {
  95. for (int i = 0; i < this.Count; i++)
  96. {
  97. yield return this[i];
  98. }
  99. }
  100. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  101. {
  102. return GetEnumerator();
  103. }
  104. }
  105. }