namespace Arch.Relationships;
using global::System.Collections.Generic;
using global::System.Runtime.CompilerServices;
using Arch.Core;
///
/// The interface
/// is an interface that provides all methods required to act as a relationship.
///
internal interface IRelationship
{
///
/// The amount of relationships currently in the buffer.
///
int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
}
///
/// Removes the buffer as a component from the given world and entity.
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Destroy(World world, Entity source);
///
/// Removes the relationship targeting from this buffer.
///
/// The in the relationship to remove.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Remove(Entity target);
}
///
/// A buffer storing relationships of and .
///
/// The type of the second relationship element.
public class Relationship : IRelationship
{
///
/// Its relations.
///
internal readonly SortedList _elements;
///
/// Initializes a new instance of an .
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Relationship()
{
_elements = [];
}
///
/// Initializes a new instance of an .
/// Mostly for binary serialization.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Relationship(SortedList elements)
{
_elements = elements;
}
///
int IRelationship.Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _elements.Count;
}
///
internal int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ((IRelationship)this).Count;
}
///
/// Adds a relationship to this buffer.
///
/// The instance of the relationship.
/// The target of the relationship.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Add(in T relationship, Entity target) => _elements.Add(target, relationship);
///
/// Sets the stored for the given .
///
/// The .
/// The data to store.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set(Entity entity, T data = default!) => _elements[entity] = data;
///
/// Determines whether the given contains the passed or not.
///
/// The .
/// True or false.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(Entity entity) => _elements.ContainsKey(entity);
///
/// Returns the stored for the given .
///
/// The .
/// The stored .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Get(Entity entity) => _elements[entity];
///
/// Returns the stored for the given .
///
/// The .
/// The stored .
/// The stored .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetValue(Entity entity, out T value) => _elements.TryGetValue(entity, out value!);
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IRelationship.Remove(Entity target) => _elements.Remove(target);
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Remove(Entity target) => ((IRelationship)this).Remove(target);
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IRelationship.Destroy(World world, Entity source) => world.Remove>(source);
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Destroy(World world, Entity source) => ((IRelationship)this).Destroy(world, source);
///
/// Creates a new .
///
/// The new .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public SortedListEnumerator GetEnumerator() => new(_elements);
};