refactor to use exception helper factories

This commit is contained in:
Vukašin Stepanović
2023-02-15 10:46:13 +00:00
parent 3e27279dc8
commit 722ce78fd0
132 changed files with 966 additions and 1132 deletions

View File

@@ -133,7 +133,7 @@ impl GenericGF {
*/
pub fn log(&self, a: i32) -> Result<i32, Exceptions> {
if a == 0 {
return Err(Exceptions::IllegalArgumentException(None));
return Err(Exceptions::illegalArgumentEmpty());
}
// let pos: usize = a.try_into().unwrap();
Ok(self.logTable[a as usize])
@@ -144,7 +144,7 @@ impl GenericGF {
*/
pub fn inverse(&self, a: i32) -> Result<i32, Exceptions> {
if a == 0 {
return Err(Exceptions::ArithmeticException(None));
return Err(Exceptions::arithmeticEmpty());
}
let log_t_loc: usize = a as usize;
let loc: usize = ((self.size as i32) - self.logTable[log_t_loc] - 1) as usize;

View File

@@ -49,9 +49,9 @@ impl GenericGFPoly {
*/
pub fn new(field: GenericGFRef, coefficients: &[i32]) -> Result<Self, Exceptions> {
if coefficients.is_empty() {
return Err(Exceptions::IllegalArgumentException(Some(String::from(
return Err(Exceptions::illegalArgument(String::from(
"coefficients cannot be empty",
))));
)));
}
Ok(Self {
field,
@@ -140,9 +140,9 @@ impl GenericGFPoly {
pub fn addOrSubtract(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
if self.field != other.field {
return Err(Exceptions::IllegalArgumentException(Some(
return Err(Exceptions::illegalArgument(
"GenericGFPolys do not have same GenericGF field".to_owned(),
)));
));
}
if self.isZero() {
return Ok(other.clone());
@@ -177,9 +177,9 @@ impl GenericGFPoly {
pub fn multiply(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
if self.field != other.field {
//if (!field.equals(other.field)) {
return Err(Exceptions::IllegalArgumentException(Some(
return Err(Exceptions::illegalArgument(
"GenericGFPolys do not have same GenericGF field".to_owned(),
)));
));
}
if self.isZero() || other.isZero() {
return Ok(self.getZero());
@@ -252,14 +252,12 @@ impl GenericGFPoly {
other: &GenericGFPoly,
) -> Result<(GenericGFPoly, GenericGFPoly), Exceptions> {
if self.field != other.field {
return Err(Exceptions::IllegalArgumentException(Some(
return Err(Exceptions::illegalArgument(
"GenericGFPolys do not have same GenericGF field".to_owned(),
)));
));
}
if other.isZero() {
return Err(Exceptions::IllegalArgumentException(Some(
"Divide by 0".to_owned(),
)));
return Err(Exceptions::illegalArgument("Divide by 0".to_owned()));
}
let mut quotient = self.getZero();
@@ -268,11 +266,7 @@ impl GenericGFPoly {
let denominator_leading_term = other.getCoefficient(other.getDegree());
let inverse_denominator_leading_term = match self.field.inverse(denominator_leading_term) {
Ok(val) => val,
Err(_issue) => {
return Err(Exceptions::IllegalArgumentException(Some(
"arithmetic issue".to_owned(),
)))
}
Err(_issue) => return Err(Exceptions::illegalArgument("arithmetic issue".to_owned())),
};
while remainder.getDegree() >= other.getDegree() && !remainder.isZero() {

View File

@@ -77,7 +77,7 @@ impl ReedSolomonDecoder {
return Ok(0);
}
let Ok(syndrome) = GenericGFPoly::new(self.field, &syndromeCoefficients) else {
return Err(Exceptions::ReedSolomonException(None));
return Err(Exceptions::reedSolomonEmpty());
};
let sigmaOmega = self.runEuclideanAlgorithm(
&GenericGF::buildMonomial(self.field, twoS as usize, 1),
@@ -92,15 +92,11 @@ impl ReedSolomonDecoder {
//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(Some(
"Bad error location".to_owned(),
)));
return Err(Exceptions::reedSolomon("Bad error location".to_owned()));
}
let position: isize = received.len() as isize - 1 - log_value as isize;
if position < 0 {
return Err(Exceptions::ReedSolomonException(Some(
"Bad error location".to_owned(),
)));
return Err(Exceptions::reedSolomon("Bad error location".to_owned()));
}
received[position as usize] =
GenericGF::addOrSubtract(received[position as usize], errorMagnitudes[i]);
@@ -138,9 +134,7 @@ 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(Some(
"r_{i-1} was zero".to_owned(),
)));
return Err(Exceptions::reedSolomon("r_{i-1} was zero".to_owned()));
}
r = rLastLast;
let mut q = r.getZero();
@@ -158,26 +152,20 @@ impl ReedSolomonDecoder {
t = (q.multiply(&tLast)?).addOrSubtract(&tLastLast)?;
if r.getDegree() >= rLast.getDegree() {
return Err(Exceptions::ReedSolomonException(Some(format!(
return Err(Exceptions::reedSolomon(format!(
"Division algorithm failed to reduce polynomial? r: {r}, rLast: {rLast}"
))));
)));
}
}
let sigmaTildeAtZero = t.getCoefficient(0);
if sigmaTildeAtZero == 0 {
return Err(Exceptions::ReedSolomonException(Some(
"sigmaTilde(0) was zero".to_owned(),
)));
return Err(Exceptions::reedSolomon("sigmaTilde(0) was zero".to_owned()));
}
let inverse = match self.field.inverse(sigmaTildeAtZero) {
Ok(res) => res,
Err(_err) => {
return Err(Exceptions::ReedSolomonException(Some(
"ArithmetricException".to_owned(),
)))
}
Err(_err) => return Err(Exceptions::reedSolomon("ArithmetricException".to_owned())),
};
let sigma = t.multiply_with_scalar(inverse);
let omega = r.multiply_with_scalar(inverse);
@@ -205,9 +193,9 @@ impl ReedSolomonDecoder {
}
}
if e != numErrors {
return Err(Exceptions::ReedSolomonException(Some(
return Err(Exceptions::reedSolomon(
"Error locator degree does not match number of roots".to_owned(),
)));
));
}
Ok(result)
}

View File

@@ -73,15 +73,15 @@ impl ReedSolomonEncoder {
pub fn encode(&mut self, to_encode: &mut Vec<i32>, ec_bytes: usize) -> Result<(), Exceptions> {
if ec_bytes == 0 {
return Err(Exceptions::IllegalArgumentException(Some(
return Err(Exceptions::illegalArgument(
"No error correction bytes".to_owned(),
)));
));
}
let data_bytes = to_encode.len() - ec_bytes;
if data_bytes == 0 {
return Err(Exceptions::IllegalArgumentException(Some(
return Err(Exceptions::illegalArgument(
"No data bytes provided".to_owned(),
)));
));
}
let fld = self.field;
let generator = self.buildGenerator(ec_bytes);
@@ -91,7 +91,7 @@ impl ReedSolomonEncoder {
let mut info = GenericGFPoly::new(fld, &info_coefficients)?;
info = info.multiply_by_monomial(ec_bytes, 1)?;
let remainder = &info
.divide(generator.ok_or(Exceptions::ReedSolomonException(None))?)?
.divide(generator.ok_or(Exceptions::reedSolomonEmpty())?)?
.1;
let coefficients = remainder.getCoefficients();
let num_zero_coefficients = ec_bytes - coefficients.len();