## char-map.pkg
#
# Fast, read-only, maps from characters to values.
# Compiled by:
#
src/lib/std/standard.lib### "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, . . .
### The natural numbers, which are the primary subject of this book,
### do not end with the digits with which we represent them.
### They continue indefinitely -- as the three dots indicate -- to infinity.
### And they are all interesting: for if there were any uninteresting numbers,
### there would of necessity be a smallest uninteresting number and it,
### for that reason alone, would be very interesting."
###
### -- Constance Reid
### From Zero to Infinity
stipulate
package chr = char; # char is from
src/lib/std/char.pkg package rov = vector; # vector is from
src/lib/std/src/vector.pkg package rwv = rw_vector; # rw_vector is from
src/lib/std/src/rw-vector.pkgherein
package char_map
: Char_Map # Char_Map is from
src/lib/src/char-map.api {
# A finite map from characters to X
#
Char_Map(X) = rov::Vector(X);
# Make a character map which maps the bound characters to their
# values and maps everything else to the default value:
#
fun make_char_map { default, namings }
=
{ val_map = rov::from_list (default ! (map #2 namings));
# This rw_vector maps characters to indices in the valMap
# NOTE: once we have Wright's value restriction, this can use the rw_vector
# to directly represent the char_map. XXX SUCKO FIXME
#
arr = rwv::make_rw_vector (chr::max_ord, 0);
fun do_naming ([], _) => ();
#
do_naming ((s, _) ! r, idx)
=>
{ fun do_char [] => ();
do_char (c ! r) => { rwv::set (arr, chr::to_int c, idx); do_char r; };
end;
do_char (explode s);
do_naming (r, idx+1);
};
end;
do_naming (namings, 1);
rov::from_fn (
chr::max_ord,
\\ i = rov::get (val_map, rwv::get (arr, i))
);
};
# Map the given character ordinal
#
fun map_char charmap i
=
rov::get (charmap, chr::to_int i);
# (map_string_char c (s, i)) is equivalent to
# (map_char c (string::get_byte_as_char (s, i)))
#
fun map_string_char charmap (s, i)
=
rov::get (charmap, string::get_byte (s, i));
}; # package char_map
end;
## AUTHOR: John Reppy
## AT&T Bell Laboratories
## Murray Hill, NJ 07974
## jhr@research.att.com
## COPYRIGHT (c) 1994 by AT&T Bell Laboratories. See SMLNJ-COPYRIGHT file for details.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.