SparseSet.java 1.99 KB
Newer Older
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
package emu.grasscutter.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public final class SparseSet {
    /*
     * A convenience class for constructing integer sets out of large ranges
     * Designed to be fed literal strings from this project only -
     * can and will throw exceptions to tell you to fix your code if you feed it garbage. :)
     */
    private static class Range {
        private final int min, max;

        public Range(int min, int max) {
            if (min > max) {
                throw new IllegalArgumentException("Range passed minimum higher than maximum - " + Integer.toString(min) + " > " + Integer.toString(max));
            }
            this.min = min;
            this.max = max;
        }

        public boolean check(int value) {
            return value >= this.min && value <= this.max;
        }
    }

    private final List<Range> rangeEntries;
    private final Set<Integer> denseEntries;

    public SparseSet(String csv) {
        this.rangeEntries = new ArrayList<>();
        this.denseEntries = new TreeSet<>();

        for (String token : csv.replace("\n", "").replace(" ", "").split(",")) {
            String[] tokens = token.split("-");
            switch (tokens.length) {
                case 1:
                    this.denseEntries.add(Integer.parseInt(tokens[0]));
                    break;
                case 2:
                    this.rangeEntries.add(new Range(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1])));
                    break;
                default:
                    throw new IllegalArgumentException("Invalid token passed to SparseSet initializer - " + token + " (split length " + Integer.toString(tokens.length) + ")");
            }
        }
    }

    public boolean contains(int i) {
        for (Range range : this.rangeEntries) {
            if (range.check(i)) {
                return true;
            }
        }
        return this.denseEntries.contains(i);
    }
}