-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlatFrequencyTable.java
More file actions
113 lines (89 loc) · 3.51 KB
/
Copy pathFlatFrequencyTable.java
File metadata and controls
113 lines (89 loc) · 3.51 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package nayuki.arithcode;
/**
* An immutable frequency table where every symbol has the same frequency of 1. Useful as a fallback model when no statistics are available.
*/
public final class FlatFrequencyTable implements FrequencyTable {
// Total number of symbols, which is at least 1.
private final int numSymbols;
/**
* Creates a flat frequency table with the specified number of symbols.
* @param numSyms the number of symbols, which must be at least 1
* @throws IllegalArgumentException if {@code numSyms} ≤ 0
*/
public FlatFrequencyTable(int numSyms) {
if (numSyms <= 0)
throw new IllegalArgumentException("Number of symbols must be positive");
numSymbols = numSyms;
}
/**
* Returns the number of symbols in this table.
* @return the number of symbols in this table
*/
public int getSymbolLimit() {
return numSymbols;
}
/**
* Returns the frequency of the specified symbol, which is always 1. If the symbol index is out of bounds, then an exception is thrown instead.
* @param symbol the symbol to query
* @return the frequency of the symbol, which is 1
* @throws IllegalArgumentException if {@code symbol} < 0 or {@code symbol} ≥ {@code getSymbolLimit()}
*/
public int get(int symbol) {
if (symbol < 0 || symbol >= numSymbols)
throw new IllegalArgumentException("Symbol out of range");
return 1;
}
/**
* Returns the total of all symbol frequencies, which is always equal to the number of symbols in this table.
* @return the total of all symbol frequencies, which is {@code getSymbolLimit()}
*/
public int getTotal() {
return numSymbols;
}
/**
* Returns the total of the frequencies of all the symbols below the specified one, which is equal to {@code symbol}.
* @param symbol the symbol to query
* @return the total of the frequencies of all the symbols below the specified one, which is {@code symbol}
* @throws IllegalArgumentException if {@code symbol} < 0 or {@code symbol} ≥ {@code getSymbolLimit()}
*/
public int getLow(int symbol) {
if (symbol < 0 || symbol >= numSymbols)
throw new IllegalArgumentException("Symbol out of range");
return symbol;
}
/**
* Returns the total of the frequencies of the specified symbol and all the ones below, which is equal to {@code symbol} + 1.
* @param symbol the symbol to query
* @return the total of the frequencies of the specified symbol and all the ones below, which is {@code symbol} + 1
* @throws IllegalArgumentException if {@code symbol} < 0 or {@code symbol} ≥ {@code getSymbolLimit()}
*/
public int getHigh(int symbol) {
if (symbol < 0 || symbol >= numSymbols)
throw new IllegalArgumentException("Symbol out of range");
return symbol + 1;
}
/**
* Returns a string representation of this frequency table. The format is subject to change.
* @return a string representation of this frequency table
*/
public String toString() {
return "FlatFrequencyTable=" + numSymbols;
}
/**
* Unsupported operation, because this frequency table is immutable.
* @param symbol ignored
* @param freq ignored
* @throws UnsupportedOperationException because this frequency table is immutable
*/
public void set(int symbol, int freq) {
throw new UnsupportedOperationException();
}
/**
* Unsupported operation, because this frequency table is immutable.
* @param symbol ignored
* @throws UnsupportedOperationException because this frequency table is immutable
*/
public void increment(int symbol) {
throw new UnsupportedOperationException();
}
}