mirror of
https://github.com/MoleskiCoder/EightBitNet.git
synced 2026-03-11 05:41:49 +00:00
Improve time complexity of symbol lookup
This commit is contained in:
@@ -32,25 +32,41 @@
|
||||
public List<Symbol> Labels { get; } = [];
|
||||
public List<Symbol> Equates { get; } = [];
|
||||
|
||||
// Value lookup structures
|
||||
public Dictionary<int, List<Symbol>> Addresses { get; } = [];
|
||||
public Dictionary<int, List<Symbol>> Constants { get; } = [];
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lookups
|
||||
|
||||
#region Label lookup
|
||||
|
||||
public List<Symbol> LookupLabels(int address)
|
||||
public void AddLabel(Symbol symbol)
|
||||
{
|
||||
var returned = new List<Symbol>();
|
||||
foreach (var label in this.Labels)
|
||||
if (symbol.Type != "lab")
|
||||
{
|
||||
if (label.Value == address)
|
||||
{
|
||||
returned.Add(label);
|
||||
}
|
||||
throw new ArgumentOutOfRangeException(nameof(symbol), "Not a label");
|
||||
}
|
||||
return returned;
|
||||
this.Labels.Add(symbol);
|
||||
this.AddAddress(symbol);
|
||||
}
|
||||
|
||||
private void AddAddress(Symbol symbol)
|
||||
{
|
||||
var value = symbol.Value;
|
||||
if (this.Addresses.TryGetValue(value, out var symbols))
|
||||
{
|
||||
symbols.Add(symbol);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Addresses.Add(value, [symbol]);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Symbol> LookupLabels(int address) => this.Addresses.TryGetValue(address, out var symbols) ? symbols : [];
|
||||
|
||||
public Symbol? LookupLabel(int address)
|
||||
{
|
||||
var labels = this.LookupLabels(address);
|
||||
@@ -80,19 +96,31 @@
|
||||
|
||||
#region Constant lookup
|
||||
|
||||
public List<Symbol> LookupEquates(int constant)
|
||||
public void AddEquate(Symbol symbol)
|
||||
{
|
||||
var returned = new List<Symbol>();
|
||||
foreach (var equate in this.Equates)
|
||||
if (symbol.Type != "equ")
|
||||
{
|
||||
if (equate.Value == constant)
|
||||
{
|
||||
returned.Add(equate);
|
||||
}
|
||||
throw new ArgumentOutOfRangeException(nameof(symbol), "Not an equate");
|
||||
}
|
||||
return returned;
|
||||
this.Equates.Add(symbol);
|
||||
this.AddConstant(symbol);
|
||||
}
|
||||
|
||||
private void AddConstant(Symbol symbol)
|
||||
{
|
||||
var value = symbol.Value;
|
||||
if (this.Constants.TryGetValue(value, out var symbols))
|
||||
{
|
||||
symbols.Add(symbol);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Constants.Add(value, [symbol]);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Symbol> LookupEquates(int constant) => this.Constants.TryGetValue(constant, out var symbols) ? symbols : [];
|
||||
|
||||
public Symbol? LookupEquate(int constant)
|
||||
{
|
||||
var equates = this.LookupEquates(constant);
|
||||
|
||||
@@ -42,11 +42,12 @@
|
||||
base.Parse(parent, entries);
|
||||
if (this.Type is "lab")
|
||||
{
|
||||
this._parent.Labels.Add(this);
|
||||
this._parent?.AddLabel(this);
|
||||
|
||||
}
|
||||
else if (this.Type is "equ")
|
||||
{
|
||||
this._parent.Equates.Add(this);
|
||||
this._parent?.AddEquate(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user