PreviousUpNext

15.4.1235  src/lib/std/src/vector-slice-of-chars.pkg

## vector-slice-of-chars.pkg
## Author: Matthias Blume (blume@tti-c.org)

# Compiled by:
#     src/lib/std/src/standard-core.sublib

package vector_slice_of_chars : Typelocked_Vector_Slice         # Typelocked_Vector_Slice       is from   src/lib/std/src/typelocked-vector-slice.api
                                 where  Element == Char
                                 where  Vector == vector_of_chars::Vector
                                 where  Slice == substring::Substring
= package {

                                                # inline_t              is from   src/lib/core/init/built-in.pkg
    # Fast add/subtract avoiding
    # the overflow test:
    #
    infix my --- +++;
    #
    fun x --- y = inline_t::tu::copyt_tagged_int (inline_t::tu::copyf_tagged_int x - inline_t::tu::copyf_tagged_int y);
    fun x +++ y = inline_t::tu::copyt_tagged_int (inline_t::tu::copyf_tagged_int x + inline_t::tu::copyf_tagged_int y);

    package ss = substring;                                     # substring     is from   src/lib/std/src/substring.pkg

    Element = Char;
    Vector  = vector_of_chars::Vector;
    Slice   = ss::Substring;

    unsafe_get = inline_t::vector_of_chars::get_byte_as_char;
    vlength    = inline_t::vector_of_chars::length;

    length          =  ss::size;
    get             =  ss::get;

    make_full_slice =  ss::from_string;
    make_slice      =  ss::extract;
    make_subslice   =  ss::make_slice;

    burst_slice   =  ss::burst_substring;
    to_vector       =  ss::to_string;

    is_empty        =  ss::is_empty;
    get_item        =  ss::getc;

    fun keyed_apply f vs
        =
        apply start
        where
            (ss::burst_substring  vs)
                ->
                (base, start, len);

            stop = start +++ len;

            fun apply i
                =
                if (i < stop)
                    #
                    f (i --- start,  unsafe_get (base, i));
                    apply (i +++ 1);
                fi;
        end;

    apply      = ss::apply;
    fold_forward  = ss::fold_forward;
    fold_backward = ss::fold_backward;
    cat       = ss::cat;
    compare_sequences    = ss::compare_sequences;

    fun keyed_fold_forward f init vs
    =
    fold (start, init)
    where
        my (base, start, len)
            =
            ss::burst_substring vs;

        stop = start +++ len;

        fun fold (i, a)
            =
            if (i >= stop)   a;
            else             fold (i +++ 1, f (i --- start, unsafe_get (base, i), a));
            fi;
    end;

    fun keyed_fold_backward f init vs
        =
        fold (stop --- 1, init)
        where
            my (base, start, len)
                =
                ss::burst_substring vs;

            stop = start +++ len;

            fun fold (i, a)
                =
                if (i < start)   a;
                else             fold (i --- 1, f (i --- start, unsafe_get (base, i), a));
                fi;
        end;

    fun keyed_map f sl
        =
        vector_of_chars::from_list (
            reverse (
                keyed_fold_forward
                    (\\ (i, x, a) =  f (i, x) ! a)
                    []
                    sl
            )
        );

    fun map f sl
        =
        vector_of_chars::from_list (
            reverse (
                fold_forward
                    (\\ (x, a) =  f x ! a)
                    []
                    sl
            )
        );

    fun keyed_find p vs
        =
        fnd start
        where
            my (base, start, len)
                =
                ss::burst_substring vs;

            stop =  start +++ len;

            fun fnd i
                =
                if (i >= stop)
                    #                    
                    NULL;
                else
                    x = unsafe_get (base, i);
                    #
                    if (p (i, x))   THE (i --- start, x);
                    else            fnd (i +++ 1);
                    fi;
                fi;
        end;

    fun find p vs
        =
        fnd start
        where
            my (base, start, len)
                =
                ss::burst_substring vs;

            stop =  start +++ len;

            fun fnd i
                =
                if (i >= stop)
                    #                    
                    NULL;
                else 
                    x = unsafe_get (base, i);
                    #
                    if (p x)   THE x;
                    else       fnd (i +++ 1);
                    fi;
                fi;

        end;

    fun exists p vs
        =
        ex start
        where
            my (base, start, len)
                =
                ss::burst_substring vs;

            stop =  start +++ len;

            fun ex i
                =
                i < stop
                and
                (   p (unsafe_get (base, i))
                    or
                    ex (i +++ 1)
                );
        end;

    fun all p vs
        =
        al start
        where
            my (base, start, len)
                =
                ss::burst_substring vs;

            stop = start +++ len;

            fun al i
                =
                i >= stop
                or
                (   p (unsafe_get (base, i))
                    and
                    al (i +++ 1)
                );
        end;
};


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext