using System.Collections.Generic; using System.Linq; namespace Data.Util { public static class KeywordDependentCollection { public enum KeywordPermutationInstanceType { Base, Permutation, } public interface ISet { int instanceCount { get; } IEnumerable instances { get; } } public interface IInstance { KeywordPermutationInstanceType type { get; } int permutationIndex { get; } } } public abstract class KeywordDependentCollection where TAll: TISet where TAllPermutations: TISet where TForPermutation: TISet, TIInstance where TBase: TISet, TIInstance where TISet: KeywordDependentCollection.ISet where TIInstance: KeywordDependentCollection.IInstance where TStorage: new() { TStorage m_Base = new TStorage(); List m_PerPermutationIndex = new List(); public int permutationCount => m_PerPermutationIndex.Count; public TForPermutation this[int index] { get { GetOrCreateForPermutationIndex(index); return CreateForPermutationSmartPointer(index); } } public TAll all => CreateAllSmartPointer(); public TAllPermutations allPermutations => CreateAllPermutationsSmartPointer(); /// /// All permutation will inherit from base's active fields /// public TBase baseInstance => CreateBaseSmartPointer(); protected TStorage baseStorage { get => m_Base; set => m_Base = value; } protected IEnumerable permutationStorages => m_PerPermutationIndex; protected TStorage GetOrCreateForPermutationIndex(int index) { while(index >= m_PerPermutationIndex.Count) m_PerPermutationIndex.Add(new TStorage()); return m_PerPermutationIndex[index]; } protected void SetForPermutationIndex(int index, TStorage value) { while(index >= m_PerPermutationIndex.Count) m_PerPermutationIndex.Add(new TStorage()); m_PerPermutationIndex[index] = value; } protected abstract TAll CreateAllSmartPointer(); protected abstract TAllPermutations CreateAllPermutationsSmartPointer(); protected abstract TForPermutation CreateForPermutationSmartPointer(int index); protected abstract TBase CreateBaseSmartPointer(); } }