Improve time complexity of symbol lookup

This commit is contained in:
Adrian Conlon
2024-06-05 15:35:57 +01:00
parent 2123d2195a
commit 0e9967bfa2
2 changed files with 47 additions and 18 deletions

View File

@@ -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);

View File

@@ -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
{