[Vala] Suggestion : Replace case/switch-based tests (for the type of a token) by Gee maps-based (or multimaps-based) tests in Vala scanners e.g. (valaparser.vala).



Suggestion : Replace case/switch-based tests (for the type of a token) by
Gee maps-based (or multimaps-based) tests in Vala scanners e.g.
(valaparser.vala).

Goal:

   - Render the lookup O(1) instead of O(N).
   - Make vala-based scanners, parsers, tokenizers faster.

Example:



    HashSet<char> stopCharSet =  new HashSet<char>();

    string stopChars = " \r\n\t.;,:?!+=|()*[]\\|/<>&^%$# `~\"'";

    public scanner (string fname) {
        f = FileStream.open(fname, "r");
        if (f==null) {
            Posix.stdout.printf("File missing !\n");
            exit(1);
        }
        for(var i= 0; i<stopChars.length; i++) {
            stopCharSet.add(stopChars[i]);
        }
    }
...


//usage:

            //if (isspace(c)) {
            if (c in stopCharSet) {

            /* The following test is O(1), not O(N), it does not slow down
linearly with respect to the size of "stopChars" */

                if (wordBuffer.len>0) {
                    //Posix.stdout.printf("word = %s\n", wordBuffer.str);
                    wordList.add(wordBuffer.str);
                    if (erased == false) {
                        wordBuffer.erase(0,-1);
                        wordBuffer_length = 0;
                        erased = true;
                    }
                }
            }
....

Serge.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]