use crate::strings::latex::ToLatex;
#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use core::fmt::{Formatter, Result};
use core::hash::Hash;
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::collections::{BTreeMap, HashMap};
fn fmt_latex_entries<'a, K: ToLatex + 'a, V: ToLatex + 'a>(
entries: impl Iterator<Item = (&'a K, &'a V)>,
f: &mut Formatter,
) -> Result {
f.write_str("\\left\\{")?;
for (i, (k, v)) in entries.enumerate() {
if i != 0 {
f.write_str(", ")?;
}
k.fmt_latex(f)?;
f.write_str(" \\mapsto ")?;
v.fmt_latex(f)?;
}
f.write_str("\\right\\}")
}
impl<K: ToLatex, V: ToLatex> ToLatex for BTreeMap<K, V> {
#[cfg_attr(dylint_lib = "malachite_lints", expect(long_lines))]
#[inline]
fn fmt_latex(&self, f: &mut Formatter) -> Result {
fmt_latex_entries(self.iter(), f)
}
}
impl<K: Eq + Hash + Ord + ToLatex, V: ToLatex> ToLatex for HashMap<K, V> {
#[cfg_attr(dylint_lib = "malachite_lints", expect(long_lines))]
fn fmt_latex(&self, f: &mut Formatter) -> Result {
let mut entries = self.iter().collect::<Vec<_>>();
entries.sort_unstable_by_key(|&(k, _)| k);
fmt_latex_entries(entries.into_iter(), f)
}
}