rename helper methods

This commit is contained in:
Vukašin Stepanović
2023-02-15 12:52:59 +00:00
parent bfcdb397ad
commit 935519ced5
133 changed files with 858 additions and 1044 deletions

View File

@@ -133,7 +133,7 @@ impl GenericGF {
*/
pub fn log(&self, a: i32) -> Result<i32, Exceptions> {
if a == 0 {
return Err(Exceptions::illegalArgumentEmpty());
return Err(Exceptions::illegalArgument);
}
// 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::arithmeticEmpty());
return Err(Exceptions::arithmetic);
}
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,7 +49,9 @@ impl GenericGFPoly {
*/
pub fn new(field: GenericGFRef, coefficients: &[i32]) -> Result<Self, Exceptions> {
if coefficients.is_empty() {
return Err(Exceptions::illegalArgument("coefficients cannot be empty"));
return Err(Exceptions::illegalArgumentWith(
"coefficients cannot be empty",
));
}
Ok(Self {
field,
@@ -138,7 +140,7 @@ impl GenericGFPoly {
pub fn addOrSubtract(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
if self.field != other.field {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"GenericGFPolys do not have same GenericGF field",
));
}
@@ -175,7 +177,7 @@ impl GenericGFPoly {
pub fn multiply(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
if self.field != other.field {
//if (!field.equals(other.field)) {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"GenericGFPolys do not have same GenericGF field",
));
}
@@ -250,12 +252,12 @@ impl GenericGFPoly {
other: &GenericGFPoly,
) -> Result<(GenericGFPoly, GenericGFPoly), Exceptions> {
if self.field != other.field {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"GenericGFPolys do not have same GenericGF field",
));
}
if other.isZero() {
return Err(Exceptions::illegalArgument("Divide by 0"));
return Err(Exceptions::illegalArgumentWith("Divide by 0"));
}
let mut quotient = self.getZero();
@@ -264,7 +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::illegalArgument("arithmetic issue")),
Err(_issue) => return Err(Exceptions::illegalArgumentWith("arithmetic issue")),
};
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::reedSolomonEmpty());
return Err(Exceptions::reedSolomon);
};
let sigmaOmega = self.runEuclideanAlgorithm(
&GenericGF::buildMonomial(self.field, twoS as usize, 1),
@@ -92,11 +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::reedSolomon("Bad error location"));
return Err(Exceptions::reedSolomonWith("Bad error location"));
}
let position: isize = received.len() as isize - 1 - log_value as isize;
if position < 0 {
return Err(Exceptions::reedSolomon("Bad error location"));
return Err(Exceptions::reedSolomonWith("Bad error location"));
}
received[position as usize] =
GenericGF::addOrSubtract(received[position as usize], errorMagnitudes[i]);
@@ -134,7 +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::reedSolomon("r_{i-1} was zero"));
return Err(Exceptions::reedSolomonWith("r_{i-1} was zero"));
}
r = rLastLast;
let mut q = r.getZero();
@@ -152,7 +152,7 @@ impl ReedSolomonDecoder {
t = (q.multiply(&tLast)?).addOrSubtract(&tLastLast)?;
if r.getDegree() >= rLast.getDegree() {
return Err(Exceptions::reedSolomon(format!(
return Err(Exceptions::reedSolomonWith(format!(
"Division algorithm failed to reduce polynomial? r: {r}, rLast: {rLast}"
)));
}
@@ -160,12 +160,12 @@ impl ReedSolomonDecoder {
let sigmaTildeAtZero = t.getCoefficient(0);
if sigmaTildeAtZero == 0 {
return Err(Exceptions::reedSolomon("sigmaTilde(0) was zero"));
return Err(Exceptions::reedSolomonWith("sigmaTilde(0) was zero"));
}
let inverse = match self.field.inverse(sigmaTildeAtZero) {
Ok(res) => res,
Err(_err) => return Err(Exceptions::reedSolomon("ArithmetricException")),
Err(_err) => return Err(Exceptions::reedSolomonWith("ArithmetricException")),
};
let sigma = t.multiply_with_scalar(inverse);
let omega = r.multiply_with_scalar(inverse);
@@ -193,7 +193,7 @@ impl ReedSolomonDecoder {
}
}
if e != numErrors {
return Err(Exceptions::reedSolomon(
return Err(Exceptions::reedSolomonWith(
"Error locator degree does not match number of roots",
));
}

View File

@@ -73,11 +73,11 @@ impl ReedSolomonEncoder {
pub fn encode(&mut self, to_encode: &mut Vec<i32>, ec_bytes: usize) -> Result<(), Exceptions> {
if ec_bytes == 0 {
return Err(Exceptions::illegalArgument("No error correction bytes"));
return Err(Exceptions::illegalArgumentWith("No error correction bytes"));
}
let data_bytes = to_encode.len() - ec_bytes;
if data_bytes == 0 {
return Err(Exceptions::illegalArgument("No data bytes provided"));
return Err(Exceptions::illegalArgumentWith("No data bytes provided"));
}
let fld = self.field;
let generator = self.buildGenerator(ec_bytes);
@@ -86,9 +86,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.ok_or(Exceptions::reedSolomonEmpty())?)?
.1;
let remainder = &info.divide(generator.ok_or(Exceptions::reedSolomon)?)?.1;
let coefficients = remainder.getCoefficients();
let num_zero_coefficients = ec_bytes - coefficients.len();
for i in 0..num_zero_coefficients {