using System.Collections.Generic;
public override bool Equals(Object rhs) {
return id == ((Entity)rhs).id;
public override int GetHashCode() {
public static bool operator ==(Entity a, Entity b) {
public static bool operator !=(Entity a, Entity b) {
class Storage<T> : AStorage {
public Dictionary<Entity, T> m_comps = new Dictionary<Entity, T>();
Dictionary<int, AStorage> m_stores = new Dictionary<int, AStorage>();
public Entity spawn(string name) {
Storage<Info> store = store<Info>();
Entity entity = new Entity();
entity.id = ++m_nextEntityID;
Info info = attach<Info>(entity);
public T attach<T>(Entity entity) where T : new() {
Storage<T> store = store<T>();
if (!store.m_comps.ContainsKey(entity)) {
store.m_comps.Add(entity, t);
store.m_comps[entity] = t;
public T find<T>(Entity entity) {
Storage<T> store = store<T>();
if (store.m_comps.ContainsKey(entity)) {
return store.m_comps[entity];
int hash = typeof(T).GetHashCode();
if (!m_stores.ContainsKey(hash)) {
m_stores.Add(hash, new Storage<T>());
return m_stores[hash] as Storage<T>;
public static void Main()
Registry reg = new Registry();
Entity e1 = reg.spawn("E1");
Entity e2 = reg.spawn("E2");
Test test = reg.attach<Test>(e1);
test = reg.attach<Test>(e2);
Console.WriteLine("E1: " + reg.find<Test>(e1).value + "\nE2: " + reg.find<Test>(e2).value);