-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathFlatFrequencyTable.java
More file actions
133 lines (104 loc) · 3.72 KB
/
Copy pathFlatFrequencyTable.java
File metadata and controls
133 lines (104 loc) · 3.72 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Reference arithmetic coding
*
* Copyright (c) Project Nayuki
* MIT License. See readme file.
* https://www.nayuki.io/page/reference-arithmetic-coding
*/
/**
* 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 {
/*---- Fields ----*/
// Total number of symbols, which is at least 1.
private final int numSymbols;
/*---- Constructor ----*/
/**
* Constructs 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 the number of symbols is less than 1
*/
public FlatFrequencyTable(int numSyms) {
if (numSyms < 1)
throw new IllegalArgumentException("Number of symbols must be positive");
numSymbols = numSyms;
}
/*---- Methods ----*/
/**
* Returns the number of symbols in this table, which is at least 1.
* @return the number of symbols in this table
*/
public int getSymbolLimit() {
return numSymbols;
}
/**
* Returns the frequency of the specified symbol, which is always 1.
* @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) {
checkSymbol(symbol);
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 sum of the frequencies of all the symbols strictly below
* the specified symbol value. The returned value is equal to {@code symbol}.
* @param symbol the symbol to query
* @return the sum of the frequencies of all the symbols below {@code symbol}, which is {@code symbol}
* @throws IllegalArgumentException if {@code symbol} < 0 or {@code symbol} ≥ {@code getSymbolLimit()}
*/
public int getLow(int symbol) {
checkSymbol(symbol);
return symbol;
}
/**
* Returns the sum of the frequencies of the specified symbol and all
* the symbols below. The returned value is equal to {@code symbol + 1}.
* @param symbol the symbol to query
* @return the sum of the frequencies of {@code symbol} and all symbols below, which is {@code symbol + 1}
* @throws IllegalArgumentException if {@code symbol} < 0 or {@code symbol} ≥ {@code getSymbolLimit()}
*/
public int getHigh(int symbol) {
checkSymbol(symbol);
return symbol + 1;
}
// Returns silently if 0 <= symbol < numSymbols, otherwise throws an exception.
private void checkSymbol(int symbol) {
if (!(0 <= symbol && symbol < numSymbols))
throw new IllegalArgumentException("Symbol out of range");
}
/**
* 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();
}
}