private ComponentIndex GetForTypes(List <Type> types)
        {
            // Assume the list is sorted at this stage.
            Debug.Assert(types.IsSorted(TypeComparer.Instance));

            ComponentIndex index;
            WeakReference  reference;

            if (!m_indexes.TryGetValue(types, out reference) || !reference.IsAlive)
            {
                if (reference == null)
                {
                    reference = new WeakReference(null);
                }

                index = new ComponentIndex(types);

                m_indexes[types] = reference;
            }
            else
            {
                index = (ComponentIndex)reference.Target;
            }

            return(index);
        }
        public ComponentIndex GetAfterRemove(ComponentIndex current, Type oldType, out int removalPoint)
        {
            List <Type> newList = current.Types.ToList();

            removalPoint = current.Index[oldType];

            newList.RemoveAt(removalPoint);

            return(GetForTypes(newList));
        }
        public ComponentIndex GetAfterInsert(ComponentIndex current, Type newType, out int insertionPoint)
        {
            List <Type> newList = current.Types.ToList();

            insertionPoint = ~newList.BinarySearch(newType, TypeComparer.Instance);

            newList.Insert(insertionPoint, newType);

            return(GetForTypes(newList));
        }
        public void Remove(Type slot)
        {
            if (!m_componentIndex.Index.ContainsKey(slot))
            {
                Debug.Fail("Component type not in container.");
                return;
            }

            int remove;

            m_componentIndex = Host.GetAfterRemove(m_componentIndex, slot, out remove);
            m_components.RemoveAt(remove);
        }
        /**
         * Create a component from a template.
         */
        public MyIndexedComponentContainer(MyComponentContainerTemplate <T> template)
        {
            m_components.Capacity = template.Components.Count;

            for (int i = 0; i < template.Components.Count; ++i)
            {
                var factory = template.Components[i];
                var type    = template.Components.m_componentIndex.Types[i];
                m_components.Add(factory.Invoke(type));
            }

            m_componentIndex = template.Components.m_componentIndex;
        }
        public void Add(Type slot, T component)
        {
            if (m_componentIndex.Index.ContainsKey(slot))
            {
                Debug.Fail("Component type already in container.");
                return;
            }

            int insert;

            m_componentIndex = Host.GetAfterInsert(m_componentIndex, slot, out insert);

            m_components.Insert(insert, component);
        }
 public void Clear()
 {
     m_components.Clear();
     m_componentIndex = Host.GetEmptyComponentIndex();
 }
 public MyIndexedComponentContainer()
 {
     m_componentIndex = Host.GetEmptyComponentIndex();
 }