Add helper methods to be able to get Pokemon by ID faster

This commit is contained in:
Deukhoofd 2022-01-14 16:33:53 +01:00
parent 382cbf2673
commit 07aac1b873
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
1 changed files with 19 additions and 1 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using PkmnLibSharp.Utilities; using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library namespace PkmnLibSharp.Library
@ -7,18 +8,25 @@ namespace PkmnLibSharp.Library
public class SpeciesLibrary : PointerWrapper public class SpeciesLibrary : PointerWrapper
{ {
private readonly Dictionary<string, Species> _cache = new Dictionary<string, Species>(StringComparer.InvariantCultureIgnoreCase); private readonly Dictionary<string, Species> _cache = new Dictionary<string, Species>(StringComparer.InvariantCultureIgnoreCase);
private readonly Dictionary<ushort, Species> _idCache = new Dictionary<ushort, Species>();
public ulong Count => Creaturelib.Generated.SpeciesLibrary.GetCount(Ptr); public ulong Count => Creaturelib.Generated.SpeciesLibrary.GetCount(Ptr);
public void Insert(string key, Species species) public void Insert(string key, Species species)
{ {
Creaturelib.Generated.SpeciesLibrary.Insert(Ptr, key.ToPtr(), species.Ptr).Assert(); Creaturelib.Generated.SpeciesLibrary.Insert(Ptr, key.ToPtr(), species.Ptr).Assert();
_cache.Add(key, species); _cache.Add(key, species);
_idCache.Add(species.Id, species);
} }
public void Delete(string key) public void Delete(string key)
{ {
Creaturelib.Generated.SpeciesLibrary.Delete(Ptr, key.ToPtr()).Assert(); Creaturelib.Generated.SpeciesLibrary.Delete(Ptr, key.ToPtr()).Assert();
_cache.Remove(key); if (_cache.TryGetValue(key, out var s))
{
_cache.Remove(key);
if (s != null)
_idCache.Remove(s.Id);
}
} }
public bool TryGet(string key, out Species? species) public bool TryGet(string key, out Species? species)
@ -47,6 +55,7 @@ namespace PkmnLibSharp.Library
if (TryResolvePointer(ptr, out Species? s)) if (TryResolvePointer(ptr, out Species? s))
{ {
_cache.Add(key, s!); _cache.Add(key, s!);
_idCache.Add(s!.Id, s);
return s!; return s!;
} }
species = Constructor.Active.ConstructSpecies(ptr)!; species = Constructor.Active.ConstructSpecies(ptr)!;
@ -56,12 +65,16 @@ namespace PkmnLibSharp.Library
public Species GetById(ushort id) public Species GetById(ushort id)
{ {
if (_idCache.TryGetValue(id, out var species))
return species;
var ptr = Creaturelib.Generated.SpeciesLibrary.GetById(Ptr, id); var ptr = Creaturelib.Generated.SpeciesLibrary.GetById(Ptr, id);
if (TryResolvePointer(ptr, out Species? s)) if (TryResolvePointer(ptr, out Species? s))
{ {
_idCache.Add(id, s!);
return s!; return s!;
} }
s = Constructor.Active.ConstructSpecies(ptr)!; s = Constructor.Active.ConstructSpecies(ptr)!;
_idCache.Add(id, s!);
return s; return s;
} }
@ -89,6 +102,11 @@ namespace PkmnLibSharp.Library
} }
} }
public IEnumerable<ushort> GetCachedIdEnumerator()
{
return _idCache.Keys;
}
internal SpeciesLibrary(IntPtr ptr) : base(ptr) internal SpeciesLibrary(IntPtr ptr) : base(ptr)
{ {
} }