-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from chickensoft-games/feat/entity-table
feat: entity table
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace Chickensoft.Collections.Tests; | ||
|
||
using Shouldly; | ||
using Xunit; | ||
|
||
public class EntityTableTest { | ||
[Fact] | ||
public void Initializes() { | ||
new EntityTable<int>().ShouldBeAssignableTo<EntityTable<int>>(); | ||
new EntityTable().ShouldBeAssignableTo<EntityTable<string>>(); | ||
} | ||
|
||
[Fact] | ||
public void StoresValues() { | ||
var table = new EntityTable(); | ||
|
||
table.Set("a", "one"); | ||
table.Set("b", new object()); | ||
|
||
table.Get<string>("a").ShouldBe("one"); | ||
table.Get<object>("b").ShouldNotBeNull(); | ||
|
||
table.Remove("a"); | ||
table.Remove(null); | ||
|
||
table.Get<string>("a").ShouldBeNull(); | ||
table.Get<object>("b").ShouldNotBeNull(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
Chickensoft.Collections/src/collections/entity_table/EntityTable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Chickensoft.Collections; | ||
|
||
/// <summary> | ||
/// Very simple wrapper over a concurrent dictionary that allows entities to | ||
/// be associated to a string id and retrieved by that id as a specific type, if | ||
/// the entity is of that type. | ||
/// </summary> | ||
public sealed class EntityTable : EntityTable<string> { } |
50 changes: 50 additions & 0 deletions
50
Chickensoft.Collections/src/collections/entity_table/EntityTableOfTId.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
namespace Chickensoft.Collections; | ||
|
||
using System.Collections.Concurrent; | ||
|
||
/// <summary> | ||
/// Very simple wrapper over a concurrent dictionary that allows entities to | ||
/// be associated to an id and retrieved by that id as a specific type, if | ||
/// the entity is of that type. | ||
/// </summary> | ||
/// <typeparam name="TId">Key type.</typeparam> | ||
public class EntityTable<TId> where TId : notnull { | ||
private readonly ConcurrentDictionary<TId, object> _entities = new(); | ||
|
||
/// <summary> | ||
/// Set an entity in the table. | ||
/// </summary> | ||
/// <param name="id">Entity id.</param> | ||
/// <param name="entity">Entity object.</param> | ||
public void Set(TId id, object entity) => _entities.TryAdd(id, entity); | ||
|
||
/// <summary> | ||
/// Remove an entity from the table. | ||
/// </summary> | ||
/// <param name="id">Entity id.</param> | ||
public void Remove(TId? id) { | ||
if (id is null) { return; } | ||
|
||
_entities.TryRemove(id, out _); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieve an entity from the table. | ||
/// </summary> | ||
/// <typeparam name="TUsage">Type to use the entity as — entity must be | ||
/// assignable to this type.</typeparam> | ||
/// <param name="id"></param> | ||
/// <returns>Entity with the associated id as the given type, if the entity | ||
/// exists and is of that type.</returns> | ||
public TUsage? Get<TUsage>(TId? id) where TUsage : class { | ||
if ( | ||
id is not null && | ||
_entities.TryGetValue(id, out var entity) && | ||
entity is TUsage expected | ||
) { | ||
return expected; | ||
} | ||
|
||
return default; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters