洪城义警-正式版后台
zengh
2021-11-03 e7083d249f08f9c9980511ae9baa413b7b8e96d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.springblade.modules.words.internals;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
public class IntDictionary {
    private int[] _keys;
    private int[] _values;
    private int last;
 
    public IntDictionary() {
        last = -1;
    }
 
    public int[] getKeys() {
        return _keys;
    }
 
    public int[] getValues() {
        return _values;
    }
 
    public void SetDictionary(Map<Integer, Integer> dict) {
 
        List<Integer> keys = new ArrayList<Integer>();
        dict.forEach((k, v) -> {
            keys.add((int) k);
        });
 
        _keys = new int[dict.size()];
        _values = new int[dict.size()];
        for (int i = 0; i < keys.size(); i++) {
            _keys[i] = keys.get(i);
            _values[i] = dict.get(_keys[i]);
        }
        last = _keys.length - 1;
    }
 
    public void SetDictionary(int[] keys, int[] values) {
        _keys = keys;
        _values = values;
        last = _keys.length - 1;
    }
 
    public int IndexOf(int key) {
        if (last == -1) {
            return -1;
        }
        if (_keys[0] == key) {
            return 0;
        }
        if (_keys[last] == key) {
            return last;
        }
 
        int left = 0;
        int right = last;
        while (left + 1 < right) {
            int mid = (left + right) >> 1;
            int d = _keys[mid] - key;
 
            if (d == 0) {
                return mid;
            } else if (d > 0) {
                right = mid;
            } else {
                left = mid;
            }
        }
        return -1;
    }
 
    public int GetValue(int index){
        return _values[index];
    }
 
}