mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-27 21:02:35 +00:00
Implement clippy suggestions
This commit is contained in:
@@ -38,9 +38,9 @@ const DECODER_TEST_ITERATIONS: i32 = 10;
|
||||
fn test_data_matrix() {
|
||||
let dm256 = super::get_predefined_genericgf(super::PredefinedGenericGF::DataMatrixField256);
|
||||
// real life test cases
|
||||
test_encode_decode(&dm256, &vec![142, 164, 186], &vec![114, 25, 5, 88, 102]);
|
||||
test_encode_decode(dm256, &vec![142, 164, 186], &vec![114, 25, 5, 88, 102]);
|
||||
test_encode_decode(
|
||||
&dm256,
|
||||
dm256,
|
||||
&vec![
|
||||
0x69, 0x75, 0x75, 0x71, 0x3B, 0x30, 0x30, 0x64, 0x70, 0x65, 0x66, 0x2F, 0x68, 0x70,
|
||||
0x70, 0x68, 0x6D, 0x66, 0x2F, 0x64, 0x70, 0x6E, 0x30, 0x71, 0x30, 0x7B, 0x79, 0x6A,
|
||||
@@ -62,7 +62,7 @@ fn test_qr_code() {
|
||||
let qrcf256 = super::get_predefined_genericgf(super::PredefinedGenericGF::QrCodeField256);
|
||||
// Test case from example given in ISO 18004, Annex I
|
||||
test_encode_decode(
|
||||
&qrcf256,
|
||||
qrcf256,
|
||||
&vec![
|
||||
0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11,
|
||||
0xEC, 0x11,
|
||||
@@ -70,7 +70,7 @@ fn test_qr_code() {
|
||||
&vec![0xA5, 0x24, 0xD4, 0xC1, 0xED, 0x36, 0xC7, 0x87, 0x2C, 0x55],
|
||||
);
|
||||
test_encode_decode(
|
||||
&qrcf256,
|
||||
qrcf256,
|
||||
&vec![
|
||||
0x72, 0x67, 0x2F, 0x77, 0x69, 0x6B, 0x69, 0x2F, 0x4D, 0x61, 0x69, 0x6E, 0x5F, 0x50,
|
||||
0x61, 0x67, 0x65, 0x3B, 0x3B, 0x00, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11,
|
||||
@@ -92,28 +92,28 @@ fn test_qr_code() {
|
||||
fn test_aztec() {
|
||||
// real life test cases
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
&vec![0x5, 0x6],
|
||||
&vec![0x3, 0x2, 0xB, 0xB, 0x7],
|
||||
);
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
&vec![0x0, 0x0, 0x0, 0x9],
|
||||
&vec![0xA, 0xD, 0x8, 0x6, 0x5, 0x6],
|
||||
);
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
&vec![0x2, 0x8, 0x8, 0x7],
|
||||
&vec![0xE, 0xC, 0xA, 0x9, 0x6, 0x8],
|
||||
);
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData6),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData6),
|
||||
&vec![0x9, 0x32, 0x1, 0x29, 0x2F, 0x2, 0x27, 0x25, 0x1, 0x1B],
|
||||
&vec![0x2C, 0x2, 0xD, 0xD, 0xA, 0x16, 0x28, 0x9, 0x22, 0xA, 0x14],
|
||||
);
|
||||
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData8),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData8),
|
||||
&vec![
|
||||
0xE0, 0x86, 0x42, 0x98, 0xE8, 0x4A, 0x96, 0xC6, 0xB9, 0xF0, 0x8C, 0xA7, 0x4A, 0xDA,
|
||||
0xF8, 0xCE, 0xB7, 0xDE, 0x88, 0x64, 0x29, 0x8E, 0x84, 0xA9, 0x6C, 0x6B, 0x9F, 0x08,
|
||||
@@ -129,7 +129,7 @@ fn test_aztec() {
|
||||
],
|
||||
);
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData10),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData10),
|
||||
&vec![
|
||||
0x15C, 0x1E1, 0x2D5, 0x02E, 0x048, 0x1E2, 0x037, 0x0CD, 0x02E, 0x056, 0x26A, 0x281,
|
||||
0x1C2, 0x1A6, 0x296, 0x045, 0x041, 0x0AA, 0x095, 0x2CE, 0x003, 0x38F, 0x2CD, 0x1A2,
|
||||
@@ -176,7 +176,7 @@ fn test_aztec() {
|
||||
],
|
||||
);
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData12),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData12),
|
||||
&vec![
|
||||
0x571, 0xE1B, 0x542, 0xE12, 0x1E2, 0x0DC, 0xCD0, 0xB85, 0x69A, 0xA81, 0x709, 0xA6A,
|
||||
0x584, 0x510, 0x4AA, 0x256, 0xCE0, 0x0F8, 0xFB3, 0x5A2, 0x0D9, 0xAD1, 0x389, 0x09C,
|
||||
@@ -432,7 +432,7 @@ fn test_encode_decode_random(field: GenericGFRef, dataSize: usize, ecSize: usize
|
||||
ecWords[0..ecSize].clone_from_slice(&message[dataSize..dataSize + ecSize]);
|
||||
//System.arraycopy(message, dataSize, ecWords, 0, ecSize);
|
||||
// check to see if Decoder can fix up to ecWords/2 random errors
|
||||
test_decoder(&field, &dataWords, &ecWords);
|
||||
test_decoder(field, &dataWords, &ecWords);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -536,7 +536,7 @@ fn test_decoder(field: GenericGFRef, dataWords: &Vec<i32>, ecWords: &Vec<i32>) {
|
||||
ecWords.len(),
|
||||
i
|
||||
),
|
||||
&dataWords,
|
||||
dataWords,
|
||||
&message,
|
||||
);
|
||||
}
|
||||
@@ -545,7 +545,7 @@ fn test_decoder(field: GenericGFRef, dataWords: &Vec<i32>, ecWords: &Vec<i32>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_data_equals(message: String, expected: &Vec<i32>, received: &Vec<i32>) {
|
||||
fn assert_data_equals(message: String, expected: &Vec<i32>, received: &[i32]) {
|
||||
for i in 0..expected.len() {
|
||||
//for (int i = 0; i < expected.length; i++) {
|
||||
if expected[i] != received[i] {
|
||||
@@ -553,7 +553,7 @@ fn assert_data_equals(message: String, expected: &Vec<i32>, received: &Vec<i32>)
|
||||
"{}. Mismatch at {}. Expected {}, got {}",
|
||||
message,
|
||||
i,
|
||||
array_to_string(&expected),
|
||||
array_to_string(expected),
|
||||
array_to_string(&received[0..expected.len()])
|
||||
);
|
||||
//fail();
|
||||
@@ -563,12 +563,12 @@ fn assert_data_equals(message: String, expected: &Vec<i32>, received: &Vec<i32>)
|
||||
|
||||
fn array_to_string(data: &[i32]) -> String {
|
||||
let mut sb = String::from("{");
|
||||
for i in 0..data.len() {
|
||||
for dtm in data.iter() {
|
||||
//for (int i = 0; i < data.length; i++) {
|
||||
//sb.append(String.format(i > 0 ? ",%X" : "%X", data[i]));
|
||||
sb.push_str(&format!("{}", data[i]));
|
||||
sb.push_str(&format!("{}", *dtm));
|
||||
}
|
||||
sb.push_str("}");
|
||||
sb.push('}');
|
||||
|
||||
sb
|
||||
}
|
||||
|
||||
@@ -42,10 +42,11 @@ impl GenericGF {
|
||||
let mut expTable = vec![0; size];
|
||||
let mut logTable = vec![0; size];
|
||||
let mut x = 1;
|
||||
for i in 0..size {
|
||||
for expTableEntry in expTable.iter_mut().take(size) {
|
||||
// for i in 0..size {
|
||||
//for (int i = 0; i < size; i++) {
|
||||
//expTable.push(x);
|
||||
expTable[i] = x;
|
||||
*expTableEntry = x;
|
||||
x *= 2; // we're assuming the generator alpha is 2
|
||||
if x >= size as i32 {
|
||||
x ^= primitive;
|
||||
@@ -53,10 +54,11 @@ impl GenericGF {
|
||||
x &= sz_m_1;
|
||||
}
|
||||
}
|
||||
for i in 0..size - 1 {
|
||||
for (i, loc) in expTable.iter().enumerate().take(size - 1) {
|
||||
// for i in 0..size - 1 {
|
||||
//for (int i = 0; i < size - 1; i++) {
|
||||
let loc: usize = expTable[i] as usize;
|
||||
logTable[loc] = i as i32;
|
||||
// let loc: usize = expTable[i] as usize;
|
||||
logTable[*loc as usize] = i as i32;
|
||||
}
|
||||
logTable[0] = 0;
|
||||
|
||||
@@ -106,7 +108,7 @@ impl GenericGF {
|
||||
}
|
||||
let mut coefficients = vec![0; degree + 1];
|
||||
coefficients[0] = coefficient;
|
||||
return GenericGFPoly::new(source, &coefficients).unwrap();
|
||||
GenericGFPoly::new(source, &coefficients).unwrap()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,7 +117,7 @@ impl GenericGF {
|
||||
* @return sum/difference of a and b
|
||||
*/
|
||||
pub fn addOrSubtract(a: i32, b: i32) -> i32 {
|
||||
return a ^ b;
|
||||
a ^ b
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,7 +125,7 @@ impl GenericGF {
|
||||
*/
|
||||
pub fn exp(&self, a: i32) -> i32 {
|
||||
// let pos: usize = a.try_into().unwrap();
|
||||
return self.expTable[a as usize];
|
||||
self.expTable[a as usize]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,10 +133,10 @@ impl GenericGF {
|
||||
*/
|
||||
pub fn log(&self, a: i32) -> Result<i32, Exceptions> {
|
||||
if a == 0 {
|
||||
return Err(Exceptions::IllegalArgumentException("".to_owned()));
|
||||
return Err(Exceptions::IllegalArgumentException(None));
|
||||
}
|
||||
// let pos: usize = a.try_into().unwrap();
|
||||
return Ok(self.logTable[a as usize]);
|
||||
Ok(self.logTable[a as usize])
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,11 +144,11 @@ impl GenericGF {
|
||||
*/
|
||||
pub fn inverse(&self, a: i32) -> Result<i32, Exceptions> {
|
||||
if a == 0 {
|
||||
return Err(Exceptions::ArithmeticException("".to_owned()));
|
||||
return Err(Exceptions::ArithmeticException(None));
|
||||
}
|
||||
let log_t_loc: usize = a as usize;
|
||||
let loc: usize = ((self.size as i32) - self.logTable[log_t_loc] - 1) as usize;
|
||||
return Ok(self.expTable[loc]);
|
||||
Ok(self.expTable[loc])
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,15 +161,15 @@ impl GenericGF {
|
||||
let a_loc: usize = a as usize; //.try_into().unwrap();
|
||||
let b_loc: usize = b as usize; //.try_into().unwrap();
|
||||
let comb_loc: usize = (self.logTable[a_loc] + self.logTable[b_loc]) as usize;
|
||||
return self.expTable[comb_loc % (self.size - 1)];
|
||||
self.expTable[comb_loc % (self.size - 1)]
|
||||
}
|
||||
|
||||
pub fn getSize(&self) -> usize {
|
||||
return self.size;
|
||||
self.size
|
||||
}
|
||||
|
||||
pub fn getGeneratorBase(&self) -> i32 {
|
||||
return self.generatorBase;
|
||||
self.generatorBase
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,13 +48,13 @@ impl GenericGFPoly {
|
||||
* constant polynomial (that is, it is not the monomial "0")
|
||||
*/
|
||||
pub fn new(field: GenericGFRef, coefficients: &Vec<i32>) -> Result<Self, Exceptions> {
|
||||
if coefficients.len() == 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
if coefficients.is_empty() {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"coefficients.len()".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
Ok(Self {
|
||||
field: field,
|
||||
field,
|
||||
coefficients: {
|
||||
let coefficients_length = coefficients.len();
|
||||
if coefficients_length > 1 && coefficients[0] == 0 {
|
||||
@@ -85,28 +85,28 @@ impl GenericGFPoly {
|
||||
}
|
||||
|
||||
pub fn getCoefficients(&self) -> &Vec<i32> {
|
||||
return &self.coefficients;
|
||||
&self.coefficients
|
||||
}
|
||||
|
||||
/**
|
||||
* @return degree of this polynomial
|
||||
*/
|
||||
pub fn getDegree(&self) -> usize {
|
||||
return self.coefficients.len() - 1;
|
||||
self.coefficients.len() - 1
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true iff this polynomial is the monomial "0"
|
||||
*/
|
||||
pub fn isZero(&self) -> bool {
|
||||
return self.coefficients[0] == 0;
|
||||
self.coefficients[0] == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* @return coefficient of x^degree term in this polynomial
|
||||
*/
|
||||
pub fn getCoefficient(&self, degree: usize) -> i32 {
|
||||
return self.coefficients[self.coefficients.len() - 1 - degree];
|
||||
self.coefficients[self.coefficients.len() - 1 - degree]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,18 +131,18 @@ impl GenericGFPoly {
|
||||
for i in 1..size {
|
||||
//for (int i = 1; i < size; i++) {
|
||||
result = GenericGF::addOrSubtract(
|
||||
self.field.multiply(a as i32, result as i32),
|
||||
self.field.multiply(a as i32, result),
|
||||
self.coefficients[i],
|
||||
);
|
||||
}
|
||||
return result;
|
||||
result
|
||||
}
|
||||
|
||||
pub fn addOrSubtract(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
|
||||
if self.field != other.field {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
if self.isZero() {
|
||||
return Ok(other.clone());
|
||||
@@ -171,15 +171,15 @@ impl GenericGFPoly {
|
||||
);
|
||||
}
|
||||
|
||||
return Ok(GenericGFPoly::new(self.field, &sumDiff)?);
|
||||
GenericGFPoly::new(self.field, &sumDiff)
|
||||
}
|
||||
|
||||
pub fn multiply(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
|
||||
if self.field != other.field {
|
||||
//if (!field.equals(other.field)) {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
if self.isZero() || other.isZero() {
|
||||
return Ok(self.getZero());
|
||||
@@ -200,7 +200,7 @@ impl GenericGFPoly {
|
||||
);
|
||||
}
|
||||
}
|
||||
return Ok(GenericGFPoly::new(self.field, &product)?);
|
||||
GenericGFPoly::new(self.field, &product)
|
||||
}
|
||||
|
||||
pub fn multiply_with_scalar(&self, scalar: i32) -> GenericGFPoly {
|
||||
@@ -213,11 +213,12 @@ impl GenericGFPoly {
|
||||
let size = self.coefficients.len();
|
||||
|
||||
let mut product = vec![0; size];
|
||||
for i in 0..size {
|
||||
for (i, prod) in product.iter_mut().enumerate().take(size) {
|
||||
// for i in 0..size {
|
||||
//for (int i = 0; i < size; i++) {
|
||||
product[i] = self.field.multiply(self.coefficients[i], scalar);
|
||||
*prod = self.field.multiply(self.coefficients[i], scalar);
|
||||
}
|
||||
return GenericGFPoly::new(self.field, &product).unwrap();
|
||||
GenericGFPoly::new(self.field, &product).unwrap()
|
||||
}
|
||||
|
||||
pub fn getZero(&self) -> Self {
|
||||
@@ -238,11 +239,12 @@ impl GenericGFPoly {
|
||||
}
|
||||
let size = self.coefficients.len();
|
||||
let mut product = vec![0; size + degree];
|
||||
for i in 0..size {
|
||||
for (i, prod) in product.iter_mut().enumerate().take(size) {
|
||||
// for i in 0..size {
|
||||
//for (int i = 0; i < size; i++) {
|
||||
product[i] = self.field.multiply(self.coefficients[i], coefficient);
|
||||
*prod = self.field.multiply(self.coefficients[i], coefficient);
|
||||
}
|
||||
return Ok(GenericGFPoly::new(self.field, &product)?);
|
||||
GenericGFPoly::new(self.field, &product)
|
||||
}
|
||||
|
||||
pub fn divide(
|
||||
@@ -250,14 +252,14 @@ impl GenericGFPoly {
|
||||
other: &GenericGFPoly,
|
||||
) -> Result<(GenericGFPoly, GenericGFPoly), Exceptions> {
|
||||
if self.field != other.field {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
if other.isZero() {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Divide by 0".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
let mut quotient = self.getZero();
|
||||
@@ -267,9 +269,9 @@ impl GenericGFPoly {
|
||||
let inverse_denominator_leading_term = match self.field.inverse(denominator_leading_term) {
|
||||
Ok(val) => val,
|
||||
Err(_issue) => {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"arithmetic issue".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
@@ -285,7 +287,7 @@ impl GenericGFPoly {
|
||||
remainder = remainder.addOrSubtract(&term)?;
|
||||
}
|
||||
|
||||
return Ok((quotient, remainder));
|
||||
Ok((quotient, remainder))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,22 +303,20 @@ impl fmt::Display for GenericGFPoly {
|
||||
if coefficient != 0 {
|
||||
if coefficient < 0 {
|
||||
if degree == self.getDegree() {
|
||||
result.push_str("-");
|
||||
result.push('-');
|
||||
} else {
|
||||
result.push_str(" - ");
|
||||
}
|
||||
coefficient = -coefficient;
|
||||
} else {
|
||||
if result.len() > 0 {
|
||||
result.push_str(" + ");
|
||||
}
|
||||
} else if !result.is_empty() {
|
||||
result.push_str(" + ");
|
||||
}
|
||||
if degree == 0 || coefficient != 1 {
|
||||
if let Ok(alpha_power) = self.field.log(coefficient) {
|
||||
if alpha_power == 0 {
|
||||
result.push_str("1");
|
||||
result.push('1');
|
||||
} else if alpha_power == 1 {
|
||||
result.push_str("a");
|
||||
result.push('a');
|
||||
} else {
|
||||
result.push_str("a^");
|
||||
result.push_str(&format!("{}", alpha_power));
|
||||
@@ -325,7 +325,7 @@ impl fmt::Display for GenericGFPoly {
|
||||
}
|
||||
if degree != 0 {
|
||||
if degree == 1 {
|
||||
result.push_str("x");
|
||||
result.push('x');
|
||||
} else {
|
||||
result.push_str("x^");
|
||||
result.push_str(&format!("{}", degree));
|
||||
|
||||
@@ -48,7 +48,7 @@ pub struct ReedSolomonDecoder {
|
||||
|
||||
impl ReedSolomonDecoder {
|
||||
pub fn new(field: GenericGFRef) -> Self {
|
||||
Self { field: field }
|
||||
Self { field }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,11 +78,7 @@ impl ReedSolomonDecoder {
|
||||
}
|
||||
let syndrome = match GenericGFPoly::new(self.field, &syndromeCoefficients) {
|
||||
Ok(res) => res,
|
||||
Err(_fail) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
}
|
||||
Err(_fail) => return Err(Exceptions::ReedSolomonException(None)),
|
||||
};
|
||||
let sigmaOmega = self.runEuclideanAlgorithm(
|
||||
&GenericGF::buildMonomial(self.field, twoS as usize, 1),
|
||||
@@ -91,21 +87,21 @@ impl ReedSolomonDecoder {
|
||||
)?;
|
||||
let sigma = &sigmaOmega[0];
|
||||
let omega = &sigmaOmega[1];
|
||||
let errorLocations = self.findErrorLocations(&sigma)?;
|
||||
let errorMagnitudes = self.findErrorMagnitudes(&omega, &errorLocations);
|
||||
let errorLocations = self.findErrorLocations(sigma)?;
|
||||
let errorMagnitudes = self.findErrorMagnitudes(omega, &errorLocations);
|
||||
for i in 0..errorLocations.len() {
|
||||
//for (int i = 0; i < errorLocations.length; i++) {
|
||||
let log_value = self.field.log(errorLocations[i] as i32)?;
|
||||
if log_value > received.len() as i32 - 1 {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"Bad error location".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
let position: isize = received.len() as isize - 1 - log_value as isize;
|
||||
if position < 0 {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"Bad error location".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
received[position as usize] =
|
||||
GenericGF::addOrSubtract(received[position as usize], errorMagnitudes[i]);
|
||||
@@ -143,9 +139,9 @@ impl ReedSolomonDecoder {
|
||||
// Divide rLastLast by rLast, with quotient in q and remainder in r
|
||||
if rLast.isZero() {
|
||||
// Oops, Euclidean algorithm already terminated?
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"r_{i-1} was zero".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
r = rLastLast;
|
||||
let mut q = r.getZero();
|
||||
@@ -153,9 +149,9 @@ impl ReedSolomonDecoder {
|
||||
let dltInverse = match self.field.inverse(denominatorLeadingTerm) {
|
||||
Ok(inv) => inv,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"ArithmetricException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
while r.getDegree() >= rLast.getDegree() && !r.isZero() {
|
||||
@@ -167,24 +163,24 @@ impl ReedSolomonDecoder {
|
||||
{
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
r = match r.addOrSubtract(&match rLast.multiply_by_monomial(degreeDiff, scale) {
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
}) {
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -192,47 +188,47 @@ impl ReedSolomonDecoder {
|
||||
t = match (match q.multiply(&tLast) {
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
})
|
||||
.addOrSubtract(&tLastLast)
|
||||
{
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
if r.getDegree() >= rLast.getDegree() {
|
||||
return Err(Exceptions::ReedSolomonException(format!(
|
||||
return Err(Exceptions::ReedSolomonException(Some(format!(
|
||||
"Division algorithm failed to reduce polynomial? r: {}, rLast: {}",
|
||||
r, rLast
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
|
||||
let sigmaTildeAtZero = t.getCoefficient(0);
|
||||
if sigmaTildeAtZero == 0 {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"sigmaTilde(0) was zero".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
let inverse = match self.field.inverse(sigmaTildeAtZero) {
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"ArithmetricException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
let sigma = t.multiply_with_scalar(inverse);
|
||||
let omega = r.multiply_with_scalar(inverse);
|
||||
return Ok(vec![sigma, omega]);
|
||||
Ok(vec![sigma, omega])
|
||||
}
|
||||
|
||||
fn findErrorLocations(&self, errorLocator: &GenericGFPoly) -> Result<Vec<usize>, Exceptions> {
|
||||
@@ -254,20 +250,20 @@ impl ReedSolomonDecoder {
|
||||
result[e] = match self.field.inverse(i as i32) {
|
||||
Ok(res) => res as usize,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"ArithmetricException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
e += 1;
|
||||
}
|
||||
}
|
||||
if e != numErrors {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"Error locator degree does not match number of roots".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
return Ok(result);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn findErrorMagnitudes(
|
||||
@@ -282,14 +278,15 @@ impl ReedSolomonDecoder {
|
||||
//for (int i = 0; i < s; i++) {
|
||||
let xiInverse = self.field.inverse(errorLocations[i] as i32).unwrap();
|
||||
let mut denominator = 1;
|
||||
for j in 0..s {
|
||||
for (j, loc) in errorLocations.iter().enumerate().take(s) {
|
||||
// for j in 0..s {
|
||||
//for (int j = 0; j < s; j++) {
|
||||
if i != j {
|
||||
//denominator = field.multiply(denominator,
|
||||
// GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse)));
|
||||
// Above should work but fails on some Apple and Linux JDKs due to a Hotspot bug.
|
||||
// Below is a funny-looking workaround from Steven Parkes
|
||||
let term = self.field.multiply(errorLocations[j] as i32, xiInverse);
|
||||
let term = self.field.multiply(*loc as i32, xiInverse);
|
||||
let termPlus1 = if (term & 0x1) == 0 {
|
||||
term | 1
|
||||
} else {
|
||||
@@ -306,6 +303,6 @@ impl ReedSolomonDecoder {
|
||||
result[i] = self.field.multiply(result[i], xiInverse);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,10 +45,7 @@ impl ReedSolomonEncoder {
|
||||
|
||||
fn buildGenerator(&mut self, degree: usize) -> &GenericGFPoly {
|
||||
if degree >= self.cachedGenerators.len() {
|
||||
let mut lastGenerator = self
|
||||
.cachedGenerators
|
||||
.get(self.cachedGenerators.len() - 1)
|
||||
.unwrap();
|
||||
let mut lastGenerator = self.cachedGenerators.last().unwrap();
|
||||
let cg_len = self.cachedGenerators.len();
|
||||
let mut nextGenerator;
|
||||
for d in cg_len..=degree {
|
||||
@@ -71,20 +68,20 @@ impl ReedSolomonEncoder {
|
||||
}
|
||||
}
|
||||
let rv = self.cachedGenerators.get(degree).unwrap();
|
||||
return rv;
|
||||
rv
|
||||
}
|
||||
|
||||
pub fn encode(&mut self, to_encode: &mut Vec<i32>, ec_bytes: usize) -> Result<(), Exceptions> {
|
||||
if ec_bytes == 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"No error correction bytes".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
let data_bytes = to_encode.len() - ec_bytes;
|
||||
if data_bytes == 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"No data bytes provided".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
let fld = self.field;
|
||||
let generator = self.buildGenerator(ec_bytes);
|
||||
@@ -93,7 +90,7 @@ impl ReedSolomonEncoder {
|
||||
//System.arraycopy(toEncode, 0, infoCoefficients, 0, dataBytes);
|
||||
let mut info = GenericGFPoly::new(fld, &info_coefficients)?;
|
||||
info = info.multiply_by_monomial(ec_bytes, 1)?;
|
||||
let remainder = &info.divide(&generator)?.1;
|
||||
let remainder = &info.divide(generator)?.1;
|
||||
let coefficients = remainder.getCoefficients();
|
||||
let num_zero_coefficients = ec_bytes - coefficients.len();
|
||||
for i in 0..num_zero_coefficients {
|
||||
|
||||
Reference in New Issue
Block a user