summaryrefslogtreecommitdiff
path: root/assignments/assignment2/Bounded Retransmission Protocol Tester/Source/brpcompare/HashAlphabet.java
blob: 46742d2c01d1345ec770bd74b6794b507ee52c66 (plain) (blame)
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
package brpcompare;

import net.automatalib.words.Alphabet;
import net.automatalib.words.abstractimpl.AbstractAlphabet;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by joshua on 14/07/2016.
 * I noticed that getSymbolIndex was called a lot. So I optimized for that, with a look up structure.
 * Too bad it wasn't in LearnLib already.
 */
public class HashAlphabet<I> extends AbstractAlphabet<I> {
    private final ArrayList<I> symbols;
    private final HashMap<I, Integer> indices;

    public HashAlphabet(Alphabet<I> in) {
        symbols = new ArrayList<>(in);
        indices = new HashMap<>(in.size());
        for (int i = 0; i < symbols.size(); i++) {
            indices.put(symbols.get(i), i);
        }
    }

    @Override
    public I getSymbol(int index) {
        return symbols.get(index);
    }

    @Override
    public int getSymbolIndex(I symbol) {
        return indices.get(symbol);
    }

    @Override
    public int size() {
        return symbols.size();
    }

    /* (non-Javadoc)
     * @see net.automatalib.words.abstractimpl.AbstractAlphabet#writeToArray(int, java.lang.Object[], int, int)
     */
    @Override
    public void writeToArray(int offset, Object[] array, int tgtOfs, int num) {
        System.arraycopy(symbols, offset, array, tgtOfs, num);
    }

    @Override
    public boolean containsSymbol(I symbol) {
        return indices.containsKey(symbol);
    }

}