Table and StringTable
Today I encountered the "age-old" problem of wanting to be able to use a StringBuilder
as a key in a lookup table keyed to strings. Working on the new Rogue Plasmacore, my message passing system transfers byte arrays containing various strings. Many of the strings are the same every frame, so I'd like to be able to assemble all the characters in a StringBuilder
and use it as a key without having to dynamically turn it into a new String
each time.
Rogue uses Table
as its core Table/HashTable/Map/Dictionary. More specifically, Table<<$KeyType,$ValueType>>
- so Table<<String,Int32>>
maps strings to integers.
One solution would have been to have both String
and StringBuilder
inherit from a common base class or aspect. But I came up with a simpler and cleaner solution.
I created a standard library class StringTable<<$ValueType>>
that extends Table<<String,$KeyType>>
and adds overloaded versions of all the key-based methods that accept a StringBuilder
. Using a simple find_key(StringBuilder)->String
method that does the nitty gritty comparison, all relevant methods transform the StringBuilder
into a String
and then forward the call. Pretty nifty!