use std::boxed::Box;
use std::default::Default;
use std::sync::{Once,ONCE_INIT};
use fnv::FnvHashMap;
use m4ri_rust::friendly::BinMatrix;
use m4ri_rust::friendly::BinVector;
use crate::codes::BinaryCode;
/// ``[{{n}}, {{k}}]`` {{ name }} code
///
/// Decodes using Syndrome decoding
#[derive(Clone, Serialize)]
pub struct {{ name }}Code{{n}}_{{k}};
static INIT: Once = Once::new();
static mut GENERATOR_MATRIX: *const BinMatrix = 0 as *const BinMatrix;
static mut PARITY_MATRIX: *const BinMatrix = 0 as *const BinMatrix;
static mut SYNDROME_MAP: *const FnvHashMap<usize, [bool; {{ n }}]> = 0 as *const FnvHashMap<usize, [bool; {{ n }}]>;
fn init() {
INIT.call_once(|| {
unsafe {
let matrix = Box::new(BinMatrix::new(vec![
{% for row in generator %}BinVector::from_bools(&[{{ row|boollist }}]),
{% endfor %}
]));
GENERATOR_MATRIX = Box::into_raw(matrix);
let matrix = Box::new(BinMatrix::new(vec![
{% for row in parity_matrix %}BinVector::from_bools(&[{{ row|boollist }}]),
{% endfor %}
]));
PARITY_MATRIX = Box::into_raw(matrix);
let mut map = Box::new(FnvHashMap::with_capacity_and_hasher({{ syndrome_map|length }}, Default::default()));
{% for (he, e) in syndrome_map.items() %}map.insert({{ he }}, [{{ e|boollist }}]); // {{ he }} => {{ e }}
{% endfor %}
SYNDROME_MAP = Box::into_raw(map);
}
});
}
impl BinaryCode for {{ name }}Code{{n}}_{{k}} {
fn name(&self) -> String {
"[{{ n }}, {{ k }}] {{ name }} code".to_owned()
}
fn length(&self) -> usize {
{{ n }}
}
fn dimension(&self) -> usize {
{{ k }}
}
fn generator_matrix(&self) -> &BinMatrix {
init();
unsafe {
GENERATOR_MATRIX.as_ref().unwrap()
}
}
fn parity_check_matrix(&self) -> &BinMatrix {
init();
unsafe {
PARITY_MATRIX.as_ref().unwrap()
}
}
fn decode_to_code(&self, c: &BinVector) -> Result<BinVector, &str> {
init();
let map = unsafe {
SYNDROME_MAP.as_ref().unwrap()
};
debug_assert_eq!(c.len(), self.length(), "the length doesn't match the expected length (length of the code)");
let he = self.parity_check_matrix() * c;
let error = BinVector::from_bools(&map[&(he.as_u64() as usize)]);
debug_assert_eq!(error.len(), self.length(), "internal: the error vector is of the wrong length");
let result = c + &error;
debug_assert_eq!(result.len(), self.length(), "internal: the result vector is of the wrong length");
debug_assert_eq!((self.parity_check_matrix() * &result).count_ones(), 0);
Ok(result)
}
fn decode_to_message(&self, c: &BinVector) -> Result<BinVector, &str> {
{% if info_set|max == k - 1 %}
let mut codeword = self.decode_to_code(c)?;
codeword.truncate({{ k }});
Ok(codeword)
{% else %}
let codeword = self.decode_to_code(c)?;
let mut new_codeword = BinVector::with_capacity({{k}});
{% for idx in info_set %}new_codeword.push(codeword[{{idx}}]);
{% endfor %}
Ok(new_codeword)
{% endif %}
}
{% if name == "Hamming" or (name == "Golay" and n == 23) %}
/// We know how to give the bias directly for this code
fn bias(&self, delta: f64) -> f64 {
{% if name == "Hamming" %}
(1f64 + f64::from({{ n }}) * delta) / f64::from({{ n }} + 1)
{% elif name == "Golay" and n == 23%}
(1.0 + 23.0 * delta + 253.0 * delta.powi(2) + 1771.0 * delta.powi(3))/2f64.powi(11)
{% endif %}
}
{% endif %}
}
#[cfg(test)]
mod tests {
use super::*;
use m4ri_rust::friendly::BinVector;
#[test]
fn size() {
let code = {{ name }}Code{{n}}_{{k}}.generator_matrix();
assert_eq!(code.ncols(), {{n}});
assert_eq!(code.nrows(), {{k}});
}
#[test]
fn random_decode_tests() {
{% for testcase in testcases %}
{
let code = {{ name}}Code{{n}}_{{ k }};
let randvec = BinVector::from_bools(&[{{ testcase.randvec|boollist }}]);
let codeword = BinVector::from_bools(&[{{ testcase.codeword|boollist }}]);
assert_eq!(code.decode_to_code(&randvec), Ok(codeword));
}
{% endfor %}
}
}