= stringinttab: string table lookup module include "strinttab.m"; StringInt: import StringIntTab; strinttab := load StringIntTab StringIntTab->PATH; StringInt: adt{ key: string; val: int; }; lookup: fn(tab: array of StringInt, key: string) : (int, int); revlookup: fn(tab: array of StringInt, val: int) : string;Stringinttab provides functions that perform forward andreverse searches of an ordered table of key-value pairs.A table should be constructed as an array of StringIntentries, ordered by increasing key value. Keys ordering isdetermined by comparison of the Unicode value of their char‐acters.Lookup performs a binary search of tab for the StringIntentry whose key field has a value equal to key and returns atuple: an int whose value is non-zero if a match has beenfound and zero otherwise, and an int whose value is equal tothe val field of the matching StringInt entry.Revlookup searches tab for the first StringInt entry whoseval field has a value equal to val and returns the corre‐sponding key string.== examplesT := load StringIntTab StringIntTab->PATH;fmtstringtab := array[] of { Strinttab->StringInt ("html", FHtml), ("latex", FLatex), ("latexbook", FLatexBook), ("latexpart", FLatexPart), ("latexproc", FLatexProc), ("latexslides", FLatexSlides),};(fnd, fmt) := T->lookup(fmtstringtab, "latexbook");fmtstring := T->revlookup(fmtstringtab, FLatex);== source/appl/lib/strinttab.b== see ALSOhash(2)== bugsRevlookup performs a sequential search of the table.
= stringinttab: string table lookup module
include "strinttab.m";
StringInt: import StringIntTab;
strinttab := load StringIntTab StringIntTab->PATH;
StringInt: adt{
key: string;
val: int;
};
lookup: fn(tab: array of StringInt, key: string) : (int, int);
revlookup: fn(tab: array of StringInt, val: int) : string;
Stringinttab provides functions that perform forward and
reverse searches of an ordered table of key-value pairs.
A table should be constructed as an array of StringInt
entries, ordered by increasing key value. Keys ordering is
determined by comparison of the Unicode value of their char‐
acters.
Lookup performs a binary search of tab for the StringInt
entry whose key field has a value equal to key and returns a
tuple: an int whose value is non-zero if a match has been
found and zero otherwise, and an int whose value is equal to
the val field of the matching StringInt entry.
Revlookup searches tab for the first StringInt entry whose
val field has a value equal to val and returns the corre‐
sponding key string.
== examples
T := load StringIntTab StringIntTab->PATH;
fmtstringtab := array[] of { Strinttab->StringInt
("html", FHtml),
("latex", FLatex),
("latexbook", FLatexBook),
("latexpart", FLatexPart),
("latexproc", FLatexProc),
("latexslides", FLatexSlides),
};
(fnd, fmt) := T->lookup(fmtstringtab, "latexbook");
fmtstring := T->revlookup(fmtstringtab, FLatex);
== source
/appl/lib/strinttab.b
== see ALSO
hash(2)
== bugs
Revlookup performs a sequential search of the table.