A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using System.Linq;
3
4namespace UnityEditor.ShaderGraph.Internal
5{
6 public static class KeywordDependentCollection
7 {
8 public enum KeywordPermutationInstanceType
9 {
10 Base,
11 Permutation,
12 }
13
14 public interface ISet<IInstance>
15 {
16 int instanceCount { get; }
17 IEnumerable<IInstance> instances { get; }
18 }
19
20 public interface IInstance
21 {
22 KeywordPermutationInstanceType type { get; }
23 int permutationIndex { get; }
24 }
25 }
26
27 public abstract class KeywordDependentCollection<TStorage, TAll, TAllPermutations, TForPermutation, TBase, TIInstance, TISet>
28 where TAll : TISet
29 where TAllPermutations : TISet
30 where TForPermutation : TISet, TIInstance
31 where TBase : TISet, TIInstance
32 where TISet : KeywordDependentCollection.ISet<TIInstance>
33 where TIInstance : KeywordDependentCollection.IInstance
34 where TStorage : new()
35 {
36 TStorage m_Base = new TStorage();
37 List<TStorage> m_PerPermutationIndex = new List<TStorage>();
38
39 public int permutationCount => m_PerPermutationIndex.Count;
40
41 public TForPermutation this[int index]
42 {
43 get
44 {
45 GetOrCreateForPermutationIndex(index);
46 return CreateForPermutationSmartPointer(index);
47 }
48 }
49
50 public TAll all => CreateAllSmartPointer();
51 public TAllPermutations allPermutations => CreateAllPermutationsSmartPointer();
52
53 /// <summary>
54 /// All permutation will inherit from base's active fields
55 /// </summary>
56 public TBase baseInstance => CreateBaseSmartPointer();
57
58 protected TStorage baseStorage
59 {
60 get => m_Base;
61 set => m_Base = value;
62 }
63
64 protected IEnumerable<TStorage> permutationStorages => m_PerPermutationIndex;
65
66 protected TStorage GetOrCreateForPermutationIndex(int index)
67 {
68 while (index >= m_PerPermutationIndex.Count)
69 m_PerPermutationIndex.Add(new TStorage());
70
71 return m_PerPermutationIndex[index];
72 }
73
74 protected void SetForPermutationIndex(int index, TStorage value)
75 {
76 while (index >= m_PerPermutationIndex.Count)
77 m_PerPermutationIndex.Add(new TStorage());
78
79 m_PerPermutationIndex[index] = value;
80 }
81
82 protected abstract TAll CreateAllSmartPointer();
83 protected abstract TAllPermutations CreateAllPermutationsSmartPointer();
84 protected abstract TForPermutation CreateForPermutationSmartPointer(int index);
85 protected abstract TBase CreateBaseSmartPointer();
86 }
87}