⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 switchgenerator.java

📁 javascript语言的解释器源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        int max_column_index = find_max_different_column(begin, end, L);        int max_column = columns[max_column_index];        int count = count_different_chars(begin, end, max_column);        columns[max_column_index] = columns[L - 1];        if (inside_if) { P.nl(); P.indent(indent_level); }        else { P.p(' '); }        boolean use_if;        if (count <= use_if_threshold) {            use_if = true;            c_was_defined = true;            P.p(v_c); P.p("="); P.p(v_s);            P.p(".charAt("); P.p(max_column); P.p(");");        }        else {            use_if = false;            if (!label_was_defined) {                label_was_defined = true;                P.p(v_label); P.p(": ");            }            P.p("switch ("); P.p(v_s);            P.p(".charAt("); P.p(max_column); P.p(")) {");        }        int same_char_begin = begin;        int cur_ch = pairs[begin].id.charAt(max_column), ch = 0;        for (int i = begin;;) {            ++i;            if (i == end || (ch = pairs[i].id.charAt(max_column)) != cur_ch) {                int next_indent;                if (use_if) {                    P.nl(); P.indent(indent_level);                    if (same_char_begin != begin) { P.p("else "); }                    P.p("if ("); P.p(v_c); P.p("==");                    P.qchar(cur_ch); P.p(") {");                    next_indent = indent_level + 1;                }                else {                    P.nl(); P.indent(indent_level);                    P.p("case "); P.qchar(cur_ch); P.p(":");                    next_indent = indent_level + 1;                }                boolean after_unreachable = generate_letter_switch_r                    (same_char_begin, i, L - 1,                     next_indent, label_was_defined, use_if);                if (use_if) {                    P.p("}");                }                else {                    if (!after_unreachable) {                        P.p("break "); P.p(v_label); P.p(";");                    }                }                if (i == end) { break; }                same_char_begin = i;                cur_ch = ch;            }        }        if (use_if) {            P.nl();            if (inside_if) { P.indent(indent_level - 1); }            else { P.indent(indent_level); }        }        else {            P.nl(); P.indent(indent_level); P.p("}");            if (inside_if) { P.nl(); P.indent(indent_level - 1);}            else { P.p(' '); }        }        columns[max_column_index] = max_column;        return next_is_unreachable;    }    private int count_different_lengths(int begin, int end) {        int lengths_count = 0;        int cur_l = -1;        for (; begin != end; ++begin) {            int l = pairs[begin].idLength;            if (cur_l != l) {                ++lengths_count; cur_l = l;            }        }        return lengths_count;    }    private int find_max_different_column(int begin, int end, int L) {        int max_count = 0;        int max_index = 0;        for (int i = 0; i != L; ++i) {            int column = columns[i];            sort_pairs(begin, end, column);            int count = count_different_chars(begin, end, column);            if (count == end - begin) { return i; }            if (max_count < count) {                max_count = count;                max_index = i;            }        }        if (max_index != L - 1) {            sort_pairs(begin, end, columns[max_index]);        }        return max_index;    }    private int count_different_chars(int begin, int end, int column) {        int chars_count = 0;        int cur_ch = -1;        for (; begin != end; ++begin) {            int ch = pairs[begin].id.charAt(column);            if (ch != cur_ch) {                ++chars_count; cur_ch = ch;            }        }        return chars_count;    }    private void check_all_is_different(int begin, int end) {        if (begin != end) {            IdValuePair prev = pairs[begin];            while (++begin != end) {                IdValuePair current = pairs[begin];                if (prev.id.equals(current.id)) {                    throw on_same_pair_fail(prev, current);                }                prev = current;            }        }    }    private EvaluatorException on_same_pair_fail(IdValuePair a, IdValuePair b) {        StringBuffer sb = new StringBuffer();        int line1 = a.getLineNumber(), line2 = b.getLineNumber();        if (line2 > line1) { int tmp = line1; line1 = line2; line2 = tmp; }        String error_text = ToolErrorReporter.getMessage(            "msg.idswitch.same_string", a.id, new Integer(line2));        return R.runtimeError(error_text, source_file, line1, null, 0);    }    private void sort_pairs(int begin, int end, int comparator) {        heap4Sort(pairs, begin, end - begin, comparator);    }    private static boolean bigger        (IdValuePair a, IdValuePair b, int comparator)    {        if (comparator < 0) {        // For length selection switch it is enough to compare just length,        // but to detect same strings full comparison is essential            //return a.idLength > b.idLength;            int diff = a.idLength - b.idLength;            if (diff != 0) { return diff > 0; }            return a.id.compareTo(b.id) > 0;        }        else {            return a.id.charAt(comparator) > b.id.charAt(comparator);        }    }    private static void heap4Sort        (IdValuePair[] array, int offset, int size, int comparator)    {        if (size <= 1) { return; }        makeHeap4(array, offset, size, comparator);        while (size > 1) {            --size;            IdValuePair v1 = array[offset + size];            IdValuePair v2 = array[offset + 0];            array[offset + size] = v2;            array[offset + 0] = v1;            heapify4(array, offset, size, 0, comparator);        }    }    private static void makeHeap4        (IdValuePair[] array, int offset, int size, int comparator)    {        for (int i = ((size + 2) >> 2); i != 0;) {            --i;            heapify4(array, offset, size, i, comparator);        }    }    private static void heapify4        (IdValuePair[] array, int offset, int size, int i, int comparator)    {        int new_i1, new_i2, new_i3;        IdValuePair i_val = array[offset + i];        for (;;) {            int base = (i << 2);            new_i1 = base | 1;            new_i2 = base | 2;            new_i3 = base | 3;            int new_i4 = base + 4;            if (new_i4 >= size) { break; }            IdValuePair val1 = array[offset + new_i1];            IdValuePair val2 = array[offset + new_i2];            IdValuePair val3 = array[offset + new_i3];            IdValuePair val4 = array[offset + new_i4];            if (bigger(val2, val1, comparator)) {                val1 = val2; new_i1 = new_i2;            }            if (bigger(val4, val3, comparator)) {                val3 = val4; new_i3 = new_i4;            }            if (bigger(val3, val1, comparator)) {                val1 = val3; new_i1 = new_i3;            }            if (bigger(i_val, val1, comparator)) { return; }            array[offset + i] = val1;            array[offset + new_i1] = i_val;            i = new_i1;        }        if (new_i1 < size) {            IdValuePair val1 = array[offset + new_i1];            if (new_i2 != size) {                IdValuePair val2 = array[offset + new_i2];                if (bigger(val2, val1, comparator)) {                    val1 = val2; new_i1 = new_i2;                }                if (new_i3 != size) {                    IdValuePair val3 = array[offset + new_i3];                    if (bigger(val3, val1, comparator)) {                        val1 = val3; new_i1 = new_i3;                    }                }            }            if (bigger(val1, i_val, comparator)) {                array[offset + i] = val1;                array[offset + new_i1] = i_val;            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -