ActiveFields.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Data.Util
  4. {
  5. public interface IActiveFields: KeywordDependentCollection.IInstance, KeywordDependentCollection.ISet<IActiveFields>
  6. {
  7. IEnumerable<string> fields { get; }
  8. bool Add(string field);
  9. bool Contains(string field);
  10. }
  11. public interface IActiveFieldsSet: KeywordDependentCollection.ISet<IActiveFields>
  12. {
  13. void AddAll(string field);
  14. }
  15. public sealed class ActiveFields: KeywordDependentCollection<
  16. HashSet<string>,
  17. ActiveFields.All,
  18. ActiveFields.AllPermutations,
  19. ActiveFields.ForPermutationIndex,
  20. ActiveFields.Base,
  21. IActiveFields,
  22. IActiveFieldsSet
  23. >
  24. {
  25. public struct ForPermutationIndex: IActiveFields, IActiveFieldsSet
  26. {
  27. private ActiveFields m_Source;
  28. private int m_PermutationIndex;
  29. public KeywordDependentCollection.KeywordPermutationInstanceType type => KeywordDependentCollection.KeywordPermutationInstanceType.Permutation;
  30. public IEnumerable<IActiveFields> instances => Enumerable.Repeat<IActiveFields>(this, 1);
  31. public IEnumerable<string> fields =>
  32. m_Source.baseStorage.Union(m_Source.GetOrCreateForPermutationIndex(m_PermutationIndex));
  33. public int instanceCount => 1;
  34. public int permutationIndex => m_PermutationIndex;
  35. internal ForPermutationIndex(ActiveFields source, int index)
  36. {
  37. m_Source = source;
  38. m_PermutationIndex = index;
  39. }
  40. public bool Add(string field)
  41. => m_Source.GetOrCreateForPermutationIndex(m_PermutationIndex).Add(field);
  42. public bool Contains(string field) =>
  43. m_Source.baseStorage.Contains(field)
  44. || m_Source.GetOrCreateForPermutationIndex(m_PermutationIndex).Contains(field);
  45. public void AddAll(string field) => Add(field);
  46. }
  47. public struct Base : IActiveFields, IActiveFieldsSet
  48. {
  49. private ActiveFields m_Source;
  50. public IEnumerable<string> fields => m_Source.baseStorage;
  51. public int instanceCount => 1;
  52. public int permutationIndex => -1;
  53. public KeywordDependentCollection.KeywordPermutationInstanceType type => KeywordDependentCollection.KeywordPermutationInstanceType.Base;
  54. public IEnumerable<IActiveFields> instances => Enumerable.Repeat<IActiveFields>(this, 1);
  55. internal Base(ActiveFields source)
  56. {
  57. m_Source = source;
  58. }
  59. public bool Add(string field) => m_Source.baseStorage.Add(field);
  60. public bool Contains(string field) => m_Source.baseStorage.Contains(field);
  61. public void AddAll(string field) => Add(field);
  62. }
  63. public struct All : IActiveFieldsSet
  64. {
  65. private ActiveFields m_Source;
  66. public int instanceCount => m_Source.permutationCount + 1;
  67. internal All(ActiveFields source)
  68. {
  69. m_Source = source;
  70. }
  71. public void AddAll(string field)
  72. {
  73. m_Source.baseInstance.Add(field);
  74. for (var i = 0; i < m_Source.permutationCount; ++i)
  75. m_Source.GetOrCreateForPermutationIndex(i).Add(field);
  76. }
  77. public IEnumerable<IActiveFields> instances
  78. {
  79. get
  80. {
  81. var self = this;
  82. return m_Source.permutationStorages
  83. .Select((v, i) => new ForPermutationIndex(self.m_Source, i) as IActiveFields)
  84. .Union(Enumerable.Repeat((IActiveFields)m_Source.baseInstance, 1));
  85. }
  86. }
  87. }
  88. public struct AllPermutations : IActiveFieldsSet
  89. {
  90. private ActiveFields m_Source;
  91. public int instanceCount => m_Source.permutationCount;
  92. internal AllPermutations(ActiveFields source)
  93. {
  94. m_Source = source;
  95. }
  96. public void AddAll(string field)
  97. {
  98. for (var i = 0; i < m_Source.permutationCount; ++i)
  99. m_Source.GetOrCreateForPermutationIndex(i).Add(field);
  100. }
  101. public IEnumerable<IActiveFields> instances
  102. {
  103. get
  104. {
  105. var self = this;
  106. return m_Source.permutationStorages
  107. .Select((v, i) => new ForPermutationIndex(self.m_Source, i) as IActiveFields);
  108. }
  109. }
  110. }
  111. public struct NoPermutation : IActiveFields, IActiveFieldsSet
  112. {
  113. private ActiveFields m_Source;
  114. public IEnumerable<string> fields => m_Source.baseStorage;
  115. public int instanceCount => 1;
  116. public int permutationIndex => -1;
  117. public KeywordDependentCollection.KeywordPermutationInstanceType type => KeywordDependentCollection.KeywordPermutationInstanceType.Base;
  118. internal NoPermutation(ActiveFields source)
  119. {
  120. m_Source = source;
  121. }
  122. public bool Add(string field) => m_Source.baseInstance.Add(field);
  123. public bool Contains(string field) => m_Source.baseStorage.Contains(field);
  124. public void AddAll(string field) => Add(field);
  125. public IEnumerable<IActiveFields> instances => Enumerable.Repeat<IActiveFields>(this, 1);
  126. }
  127. protected override All CreateAllSmartPointer() => new All(this);
  128. protected override AllPermutations CreateAllPermutationsSmartPointer() => new AllPermutations(this);
  129. protected override ForPermutationIndex CreateForPermutationSmartPointer(int index) => new ForPermutationIndex(this, index);
  130. protected override Base CreateBaseSmartPointer() => new Base(this);
  131. }
  132. }