rxing/pdf417/decoder/barcode_value.rs
1/*
2 * Copyright 2013 ZXing authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::collections::HashMap;
18
19/**
20 * @author Guenther Grau
21 */
22#[derive(Clone, Default)]
23pub struct BarcodeValue(HashMap<u32, u32>);
24
25impl BarcodeValue {
26 pub fn new() -> Self {
27 Self::default()
28 }
29
30 /**
31 * Add an occurrence of a value
32 */
33 pub fn setValue(&mut self, value: u32) {
34 self.0
35 .entry(value)
36 .and_modify(|confidence| *confidence += 1)
37 .or_insert(1);
38 }
39
40 /**
41 * Determines the maximum occurrence of a set value and returns all values which were set with this occurrence.
42 * @return an array of int, containing the values with the highest occurrence, or null, if no value was set
43 */
44 pub fn getValue(&self) -> Vec<u32> {
45 let mut maxConfidence = -1_i32;
46 let mut result = Vec::new();
47 for (key, value) in &self.0 {
48 match (*value as i32).cmp(&maxConfidence) {
49 std::cmp::Ordering::Greater => {
50 maxConfidence = *value as i32;
51 result.clear();
52 result.push(*key);
53 }
54 std::cmp::Ordering::Equal => result.push(*key),
55 std::cmp::Ordering::Less => {}
56 }
57 }
58
59 result
60 }
61
62 pub fn getConfidence(&self, value: u32) -> u32 {
63 *self.0.get(&value).unwrap_or(&0)
64 }
65}