Skip to content

Commit

Permalink
refactor: remove Arc from many areas
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimke committed Jul 31, 2024
1 parent 628b1ec commit e807342
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 117 deletions.
8 changes: 4 additions & 4 deletions src/datamatrix/encoder/encoder_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::{SymbolInfo, SymbolInfoLookup, SymbolShapeHint};
const ISO_8859_1_ENCODER: CharacterSet = CharacterSet::ISO8859_1;

pub struct EncoderContext<'a> {
symbol_lookup: Arc<SymbolInfoLookup<'a>>,
symbol_lookup: SymbolInfoLookup<'a>,
msg: String,
shape: SymbolShapeHint,
minSize: Option<Dimension>,
Expand All @@ -39,10 +39,10 @@ pub struct EncoderContext<'a> {
impl<'a> EncoderContext<'_> {
pub fn with_symbol_info_lookup(
msg: &str,
symbol_lookup: Arc<SymbolInfoLookup<'a>>,
symbol_lookup: SymbolInfoLookup<'a>,
) -> Result<EncoderContext<'a>> {
let mut new_self = EncoderContext::new(msg)?;
new_self.symbol_lookup = symbol_lookup.clone();
new_self.symbol_lookup = symbol_lookup;
Ok(new_self)
}

Expand All @@ -67,7 +67,7 @@ impl<'a> EncoderContext<'_> {
));
};
Ok(Self {
symbol_lookup: Arc::new(SymbolInfoLookup::new()),
symbol_lookup: SymbolInfoLookup::new(),
msg: sb,
shape: SymbolShapeHint::FORCE_NONE,
codewords: String::with_capacity(msg.chars().count()),
Expand Down
16 changes: 8 additions & 8 deletions src/datamatrix/encoder/high_level_encode_test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ fn testC40EncodationSpecialCases1() {
let substitute_symbols = SymbolInfoLookup::new();
let substitute_symbols = useTestSymbols(substitute_symbols);

let sil = Arc::new(substitute_symbols);
let sil = substitute_symbols;

let visualized = encodeHighLevelCompareSIL("AIMAIMAIMAIMAIMAIM", false, Some(sil.clone()));
let visualized = encodeHighLevelCompareSIL("AIMAIMAIMAIMAIMAIM", false, Some(sil));
assert_eq!("230 91 11 91 11 91 11 91 11 91 11 91 11", visualized);
//case "a": Unlatch is not required

let visualized = encodeHighLevelCompareSIL("AIMAIMAIMAIMAIMAI", false, Some(sil.clone()));
let visualized = encodeHighLevelCompareSIL("AIMAIMAIMAIMAIMAI", false, Some(sil));
assert_eq!("230 91 11 91 11 91 11 91 11 91 11 90 241", visualized);
//case "b": Add trailing shift 0 and Unlatch is not required

let visualized = encodeHighLevelSIL("AIMAIMAIMAIMAIMA", sil.clone());
let visualized = encodeHighLevelSIL("AIMAIMAIMAIMAIMA", sil);
assert_eq!("230 91 11 91 11 91 11 91 11 91 11 254 66", visualized);
//case "c": Unlatch and write last character in ASCII

let substitute_symbols = resetSymbols(substitute_symbols);
let sil = Arc::new(substitute_symbols);
let sil = substitute_symbols;

let visualized = encodeHighLevelSIL("AIMAIMAIMAIMAIMAI", sil.clone());
let visualized = encodeHighLevelSIL("AIMAIMAIMAIMAIMAI", sil);
assert_eq!(
"230 91 11 91 11 91 11 91 11 91 11 254 66 74 129 237",
visualized
Expand Down Expand Up @@ -565,14 +565,14 @@ fn encodeHighLevel(msg: &str) -> String {
encodeHighLevelCompare(msg, true)
}

fn encodeHighLevelSIL(msg: &str, sil: Arc<SymbolInfoLookup>) -> String {
fn encodeHighLevelSIL(msg: &str, sil: SymbolInfoLookup) -> String {
encodeHighLevelCompareSIL(msg, true, Some(sil))
}

fn encodeHighLevelCompareSIL(
msg: &str,
compareSizeToMinimalEncoder: bool,
sil: Option<Arc<SymbolInfoLookup>>,
sil: Option<SymbolInfoLookup>,
) -> String {
let encoded = high_level_encoder::encodeHighLevelSIL(msg, sil).expect("encodes");
let encoded2 = minimal_encoder::encodeHighLevel(msg).expect("encodes");
Expand Down
7 changes: 2 additions & 5 deletions src/datamatrix/encoder/high_level_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ pub fn encodeHighLevel(msg: &str) -> Result<String> {
* @param msg the message
* @return the encoded message (the char values range from 0 to 255)
*/
pub fn encodeHighLevelSIL(
msg: &str,
symbol_lookup: Option<Arc<SymbolInfoLookup>>,
) -> Result<String> {
pub fn encodeHighLevelSIL(msg: &str, symbol_lookup: Option<SymbolInfoLookup>) -> Result<String> {
encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
msg,
SymbolShapeHint::FORCE_NONE,
Expand Down Expand Up @@ -184,7 +181,7 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
minSize: Option<Dimension>,
maxSize: Option<Dimension>,
forceC40: bool,
symbol_lookup: Option<Arc<SymbolInfoLookup>>,
symbol_lookup: Option<SymbolInfoLookup>,
) -> Result<String> {
//the codewords 0..255 are encoded as Unicode characters
let c40Encoder = Arc::new(C40Encoder::new());
Expand Down
2 changes: 1 addition & 1 deletion src/pdf417/decoder/bounding_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl BoundingBox {
})
}

pub fn from_other(boundingBox: Arc<BoundingBox>) -> BoundingBox {
pub fn from_other(boundingBox: &BoundingBox) -> BoundingBox {
BoundingBox {
image: boundingBox.image.clone(),
topLeft: boundingBox.topLeft,
Expand Down
13 changes: 5 additions & 8 deletions src/pdf417/decoder/detection_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ const ADJUST_ROW_NUMBER_SKIP: u32 = 2;
pub struct DetectionRXingResult {
barcodeMetadata: BarcodeMetadata,
detectionRXingResultColumns: Vec<Option<Box<dyn DetectionRXingResultColumnTrait>>>,
boundingBox: Arc<BoundingBox>,
boundingBox: BoundingBox,
barcodeColumnCount: usize,
}

impl DetectionRXingResult {
pub fn new(
barcodeMetadata: BarcodeMetadata,
boundingBox: Arc<BoundingBox>,
) -> DetectionRXingResult {
pub fn new(barcodeMetadata: BarcodeMetadata, boundingBox: BoundingBox) -> DetectionRXingResult {
let mut columns = Vec::with_capacity(barcodeMetadata.getColumnCount() as usize + 2);
for _i in 0..(barcodeMetadata.getColumnCount() as usize + 2) {
columns.push(None);
Expand Down Expand Up @@ -601,12 +598,12 @@ impl DetectionRXingResult {
self.barcodeMetadata.getErrorCorrectionLevel()
}

pub fn setBoundingBox(&mut self, boundingBox: Arc<BoundingBox>) {
pub fn setBoundingBox(&mut self, boundingBox: BoundingBox) {
self.boundingBox = boundingBox;
}

pub fn getBoundingBox(&self) -> Arc<BoundingBox> {
self.boundingBox.clone()
pub fn getBoundingBox(&self) -> &BoundingBox {
&self.boundingBox
}

pub fn setDetectionRXingResultColumn(
Expand Down
12 changes: 6 additions & 6 deletions src/pdf417/decoder/detection_result_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use super::{BoundingBox, Codeword, DetectionRXingResultRowIndicatorColumn};
const MAX_NEARBY_DISTANCE: u32 = 5;

pub trait DetectionRXingResultColumnTrait {
fn new_column(boundingBox: Arc<BoundingBox>) -> DetectionRXingResultColumn
fn new_column(boundingBox: &BoundingBox) -> DetectionRXingResultColumn
where
Self: Sized;
fn new_with_is_left(boundingBox: Arc<BoundingBox>, isLeft: bool) -> DetectionRXingResultColumn
fn new_with_is_left(boundingBox: &BoundingBox, isLeft: bool) -> DetectionRXingResultColumn
where
Self: Sized;
fn getCodewordNearby(&self, imageRow: u32) -> &Option<Codeword>;
Expand All @@ -48,17 +48,17 @@ pub struct DetectionRXingResultColumn {
}

impl DetectionRXingResultColumnTrait for DetectionRXingResultColumn {
fn new_column(boundingBox: Arc<BoundingBox>) -> DetectionRXingResultColumn {
fn new_column(boundingBox: &BoundingBox) -> DetectionRXingResultColumn {
DetectionRXingResultColumn {
boundingBox: BoundingBox::from_other(boundingBox.clone()),
boundingBox: BoundingBox::from_other(boundingBox),
codewords: vec![None; (boundingBox.getMaxY() - boundingBox.getMinY() + 1) as usize],
isLeft: None,
}
}

fn new_with_is_left(boundingBox: Arc<BoundingBox>, isLeft: bool) -> DetectionRXingResultColumn {
fn new_with_is_left(boundingBox: &BoundingBox, isLeft: bool) -> DetectionRXingResultColumn {
DetectionRXingResultColumn {
boundingBox: BoundingBox::from_other(boundingBox.clone()),
boundingBox: BoundingBox::from_other(boundingBox),
codewords: vec![None; (boundingBox.getMaxY() - boundingBox.getMinY() + 1) as usize],
isLeft: Some(isLeft),
}
Expand Down
22 changes: 10 additions & 12 deletions src/pdf417/decoder/ec/error_correction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

use std::sync::Arc;

use crate::{
common::Result,
pdf417::{decoder::ec::ModulusGF, pdf_417_common::NUMBER_OF_CODEWORDS},
Expand Down Expand Up @@ -64,22 +62,22 @@ pub fn decode(received: &mut [u32], numECCodewords: u32, erasures: &mut [u32]) -
return Ok(0);
}

let mut knownErrors: Arc<ModulusPoly> = ModulusPoly::getOne(field);
let mut knownErrors: ModulusPoly = ModulusPoly::getOne(field);
let mut b;
let mut term;
let mut kE: Arc<ModulusPoly>;
let mut kE: ModulusPoly;
if !erasures.is_empty() {
for erasure in erasures {
// for (int erasure : erasures) {
b = field.exp(received.len() as u32 - 1 - *erasure);
// Add (1 - bx) term:
term = ModulusPoly::new(field, vec![field.subtract(0, b), 1])?;
kE = knownErrors.clone();
knownErrors = kE.multiply(Arc::new(term))?;
knownErrors = kE.multiply(term)?;
}
}

let syndrome = Arc::new(ModulusPoly::new(field, S)?);
let syndrome = ModulusPoly::new(field, S)?;
//syndrome = syndrome.multiply(knownErrors);

let sigmaOmega = runEuclideanAlgorithm(
Expand Down Expand Up @@ -110,11 +108,11 @@ pub fn decode(received: &mut [u32], numECCodewords: u32, erasures: &mut [u32]) -
}

fn runEuclideanAlgorithm(
a: Arc<ModulusPoly>,
b: Arc<ModulusPoly>,
a: ModulusPoly,
b: ModulusPoly,
R: u32,
field: &'static ModulusGF,
) -> Result<[Arc<ModulusPoly>; 2]> {
) -> Result<[ModulusPoly; 2]> {
// Assume a's degree is >= b's
let mut a = a;
let mut b = b;
Expand Down Expand Up @@ -169,7 +167,7 @@ fn runEuclideanAlgorithm(
Ok([sigma, omega])
}

fn findErrorLocations(errorLocator: Arc<ModulusPoly>, field: &ModulusGF) -> Result<Vec<u32>> {
fn findErrorLocations(errorLocator: ModulusPoly, field: &ModulusGF) -> Result<Vec<u32>> {
// This is a direct application of Chien's search
let numErrors = errorLocator.getDegree();
let mut result = vec![0u32; numErrors as usize];
Expand All @@ -190,8 +188,8 @@ fn findErrorLocations(errorLocator: Arc<ModulusPoly>, field: &ModulusGF) -> Resu
}

fn findErrorMagnitudes(
errorEvaluator: Arc<ModulusPoly>,
errorLocator: Arc<ModulusPoly>,
errorEvaluator: ModulusPoly,
errorLocator: ModulusPoly,
errorLocations: &mut [u32],
field: &'static ModulusGF,
) -> Vec<u32> {
Expand Down
Loading

0 comments on commit e807342

Please sign in to comment.