malachite_base/sets/typst.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::slices::typst::fmt_typst_sequence;
10use crate::strings::typst::ToTypst;
11#[cfg(not(feature = "std"))]
12use alloc::collections::BTreeSet;
13use alloc::vec::Vec;
14use core::fmt::{Formatter, Result};
15use core::hash::Hash;
16#[cfg(not(feature = "std"))]
17use hashbrown::HashSet;
18#[cfg(feature = "std")]
19use std::collections::{BTreeSet, HashSet};
20
21impl<T: ToTypst> ToTypst for BTreeSet<T> {
22 /// Writes a [`BTreeSet`] as a LaTeX math-mode fragment.
23 ///
24 /// The elements' fragments are separated by commas and wrapped in braces, as a set is written
25 /// in mathematics. The elements come in the set's own order, which is ascending.
26 ///
27 /// Typst grows a matched pair of delimiters to fit what is between them, so the braces fit an
28 /// element that is taller than one line without being asked to. An empty set becomes `{}`
29 /// rather than nothing at all.
30 ///
31 /// # Worst-case complexity
32 /// $T(n) = O(n + \sum_{i=0}^{n-1}T^\prime(i))$
33 ///
34 /// $M(n) = O(\max_{i=0}^{n-1}M^\prime(i))$
35 ///
36 /// where $T$ is time, $M$ is additional memory, $n$ is `self.len()`, $i$ is an element's index,
37 /// and $T^\prime$ and $M^\prime$ are the time and memory functions of `fmt_typst` for `T`.
38 ///
39 /// # Examples
40 /// ```
41 /// use malachite_base::strings::typst::ToTypst;
42 /// use std::collections::BTreeSet;
43 ///
44 /// let empty = BTreeSet::<u8>::new();
45 /// assert_eq!(empty.to_typst_string(), "{}");
46 ///
47 /// let xs = BTreeSet::from([3u8, 1, 2]);
48 /// assert_eq!(xs.to_typst_string(), "{1, 2, 3}");
49 /// ```
50 ///
51 /// | value | fragment |
52 /// |-------------------------------|-------------|
53 /// | `BTreeSet::<u8>::new()` | `{}` |
54 /// | `BTreeSet::from([3u8, 1, 2])` | `{1, 2, 3}` |
55 #[inline]
56 fn fmt_typst(&self, f: &mut Formatter) -> Result {
57 fmt_typst_sequence(self.iter(), "{", "}", f)
58 }
59}
60
61impl<T: Eq + Hash + Ord + ToTypst> ToTypst for HashSet<T> {
62 /// Writes a [`HashSet`] as a LaTeX math-mode fragment.
63 ///
64 /// The elements' fragments are separated by commas and wrapped in braces, as a set is written
65 /// in mathematics.
66 ///
67 /// The elements are sorted first, which is why this asks for [`Ord`] where a [`HashSet`] does
68 /// not. A [`HashSet`] iterates in an order that depends on its hasher, so without sorting two
69 /// equal sets could have different fragments, and the same set could have a different fragment
70 /// in the next run. Sorting also makes a [`HashSet`]'s fragment agree with the [`BTreeSet`] of
71 /// the same elements.
72 ///
73 /// Typst grows a matched pair of delimiters to fit what is between them, so the braces fit an
74 /// element that is taller than one line without being asked to. An empty set becomes `{}`
75 /// rather than nothing at all.
76 ///
77 /// # Worst-case complexity
78 /// $T(n) = O(n \log n + \sum_{i=0}^{n-1}T^\prime(i))$
79 ///
80 /// $M(n) = O(n + \max_{i=0}^{n-1}M^\prime(i))$
81 ///
82 /// where $T$ is time, $M$ is additional memory, $n$ is `self.len()`, $i$ is an element's index,
83 /// and $T^\prime$ and $M^\prime$ are the time and memory functions of `fmt_typst` for `T`.
84 ///
85 /// # Examples
86 /// ```
87 /// use malachite_base::strings::typst::ToTypst;
88 /// use std::collections::HashSet;
89 ///
90 /// let empty = HashSet::<u8>::new();
91 /// assert_eq!(empty.to_typst_string(), "{}");
92 ///
93 /// // The elements are sorted, so the fragment does not depend on the hasher.
94 /// let xs = HashSet::from([3u8, 1, 2]);
95 /// assert_eq!(xs.to_typst_string(), "{1, 2, 3}");
96 /// ```
97 ///
98 /// | value | fragment |
99 /// |------------------------------|-------------|
100 /// | `HashSet::<u8>::new()` | `{}` |
101 /// | `HashSet::from([3u8, 1, 2])` | `{1, 2, 3}` |
102 fn fmt_typst(&self, f: &mut Formatter) -> Result {
103 let mut xs = self.iter().collect::<Vec<_>>();
104 xs.sort_unstable();
105 fmt_typst_sequence(xs.into_iter(), "{", "}", f)
106 }
107}