PreviousUpNext

15.4.859  src/lib/src/char-map.pkg

## char-map.pkg
## AUTHOR:   John Reppy
##          AT&T Bell Laboratories
##          Murray Hill, NJ 07974
##          jhr@research.att.com

# Compiled by:
#     src/lib/std/standard.lib

# Fast, read-only, maps from characters to values.


###          "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



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) = vector::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 =  vector::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 BUGGO FIXME
            #
            arr = rw_vector::make_rw_vector (char::max_ord, 0);

            fun do_naming ([], _) => ();

                do_naming ((s, _) ! r, idx)
                    =>
                    {   fun do_char []      =>   ();
                            do_char (c ! r) =>   { rw_vector::set (arr, char::to_int c, idx);   do_char r; };
                        end;

                       do_char (explode s);
                       do_naming (r, idx+1);
                    };
            end;
          
            do_naming (namings, 1);

            vector::tabulate (
              char::max_ord,
              fn i =  vector::get (val_map, rw_vector::get (arr, i))
            );
        };


    # Map the given character ordinal 
    #
    fun map_char charmap i
        =
        vector::get (charmap, char::to_int i);


    # (map_string_char c (s, i)) is equivalent to
    # (map_char c (string::get (s, i))) 
    #
    fun map_string_char charmap (s, i)
        =
        vector::get (charmap, char::to_int (string::get (s, i)));


};      # package char_map 



## COPYRIGHT (c) 1994 by AT&T Bell Laboratories.  See COPYRIGHT file for details.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2011,
## released under Gnu Public Licence version 3.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext