1/*
2 * Permission is hereby granted, free of charge, to any person obtaining a copy of
3 * this software and associated documentation files (the "Software"), to deal in
4 * the Software without restriction, including without limitation the rights to
5 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
6 * of the Software, and to permit persons to whom the Software is furnished to do
7 * so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all
10 * copies or substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 * SOFTWARE.
19 */
20package jdk.nashorn.internal.runtime.regexp.joni;
21
22final class OptMapInfo {
23
24    final MinMaxLen mmd = new MinMaxLen();          /* info position */
25    final OptAnchorInfo anchor = new OptAnchorInfo();
26
27    int value;                                      /* weighted value */
28    final byte map[] = new byte[Config.CHAR_TABLE_SIZE];
29
30    void clear() {
31        mmd.clear();
32        anchor.clear();
33        value = 0;
34        for (int i=0; i<map.length; i++) {
35            map[i] = 0;
36        }
37    }
38
39    void copy(final OptMapInfo other) {
40        mmd.copy(other.mmd);
41        anchor.copy(other.anchor);
42        value = other.value;
43        //for(int i=0; i<map.length; i++) map[i] = other.map[i];
44        System.arraycopy(other.map, 0, map, 0, other.map.length);
45    }
46
47    void addChar(final int c) {
48        final int c_ = c & 0xff;
49        if (map[c_] == 0) {
50            map[c_] = 1;
51            value += positionValue(c_);
52        }
53    }
54
55    void addCharAmb(final char[] chars, final int p, final int end, final int caseFoldFlag) {
56        addChar(chars[p]);
57
58        final char[]items = EncodingHelper.caseFoldCodesByString(caseFoldFlag & ~Config.INTERNAL_ENC_CASE_FOLD_MULTI_CHAR, chars[p]);
59
60        for (int i=0; i<items.length; i++) {
61            addChar(items[i]);
62        }
63    }
64
65    // select_opt_map_info
66    private static final int z = 1<<15; /* 32768: something big value */
67    void select(final OptMapInfo alt) {
68        if (alt.value == 0) {
69            return;
70        }
71        if (value == 0) {
72            copy(alt);
73            return;
74        }
75
76        final int v1 = z / value;
77        final int v2 = z /alt.value;
78
79        if (mmd.compareDistanceValue(alt.mmd, v1, v2) > 0) {
80            copy(alt);
81        }
82    }
83
84    // alt_merge_opt_map_info
85    void altMerge(final OptMapInfo other) {
86        /* if (! is_equal_mml(&to->mmd, &add->mmd)) return ; */
87        if (value == 0) {
88            return;
89        }
90        if (other.value == 0 || mmd.max < other.mmd.max) {
91            clear();
92            return;
93        }
94
95        mmd.altMerge(other.mmd);
96
97        int val = 0;
98        for (int i=0; i<Config.CHAR_TABLE_SIZE; i++) {
99            if (other.map[i] != 0) {
100                map[i] = 1;
101            }
102            if (map[i] != 0) {
103                val += positionValue(i);
104            }
105        }
106
107        value = val;
108        anchor.altMerge(other.anchor);
109    }
110
111    static final short ByteValTable[] = {
112        5,  1,  1,  1,  1,  1,  1,  1,  1, 10, 10,  1,  1, 10,  1,  1,
113        1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
114       12,  4,  7,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,
115        6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,  5,  5,  5,  5,
116        5,  6,  6,  6,  6,  7,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
117        6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  6,  5,  5,  5,
118        5,  6,  6,  6,  6,  7,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
119        6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,  5,  5,  1
120     };
121
122    // map_position_value
123    static int positionValue(final int i) {
124        if (i < ByteValTable.length) {
125            return ByteValTable[i];
126        }
127        return 4; /* Take it easy. */
128    }
129
130}
131