Context — what you are building
Implement a symbol table that maps identifiers to unique canonical strings for a small DSL or config parser. The table should intern only from a bounded vocabulary, avoid interning arbitrary unbounded inputs, and expose fast reference comparisons for tokens.
Public API
public sealed class SymbolTable
{
public SymbolTable(IEnumerable<string> allowed); // seed with a bounded set
public string Canonicalize(string text); // returns canonical interned instance if allowed; else returns original
public bool AreSameSymbol(string a, string b); // fast ref compare when both canonicalized
}
Hints - Mental model (who does what)
-
Maintain a HashSet of allowed tokens to bound the vocabulary.
-
For allowed tokens, return
string.Intern(text). -
For everything else, return the original text untouched to avoid growing the pool.
Hints - Steps with checkpoints
Step A: Construct with a normalized HashSet (e.g., StringComparer.Ordinal).
Step B: In Canonicalize, check membership; call string.Intern only if present.
Step C: AreSameSymbol first canonicalizes both inputs, then uses ReferenceEquals for O(1) equality.Checkpoint: Allowed tokens collapse to one reference; disallowed tokens don’t bloat the pool.
Hints - Validation & errors
-
Guard
allowedagainstnull. -
Choose
StringComparer.Ordinalfor stable, culture-agnostic symbol identity.
Hints - Pitfalls
-
Interning everything (memory leak risk).
-
Using culture-sensitive comparisons for symbols.
-
Assuming
ReferenceEqualsimplies equal content without prior canonicalization.
Hints - TODO Skeleton
using System;
using System.Collections.Generic;
public sealed class SymbolTable
{
private readonly HashSet<string> _allowed;
public SymbolTable(IEnumerable<string> allowed)
{
// HINT: validate; create HashSet with StringComparer.Ordinal; for each token -> string.Intern and add
throw new NotImplementedException(); // TODO
}
public string Canonicalize(string text)
{
// HINT: if _allowed.Contains(text) -> return string.Intern(text); else return text
throw new NotImplementedException(); // TODO
}
public bool AreSameSymbol(string a, string b)
{
// HINT: return ReferenceEquals(Canonicalize(a), Canonicalize(b));
throw new NotImplementedException(); // TODO
}
}