fontoxide_misc/
classify_tools.rs1use std::collections::{BTreeMap, BTreeSet};
5
6#[derive(Debug, Clone)]
9pub struct Classifier<T> {
10 things: BTreeSet<T>,
12 sets: Vec<BTreeSet<T>>,
15 mapping: BTreeMap<T, usize>,
17 dirty: bool,
18 sort: bool,
19}
20
21impl<T: Ord + Clone> Default for Classifier<T> {
22 fn default() -> Self {
23 Self::new(true)
24 }
25}
26
27impl<T: Ord + Clone> Classifier<T> {
28 pub fn new(sort: bool) -> Self {
31 Classifier {
32 things: BTreeSet::new(),
33 sets: Vec::new(),
34 mapping: BTreeMap::new(),
35 dirty: false,
36 sort,
37 }
38 }
39
40 pub fn add(&mut self, set_of_things: impl IntoIterator<Item = T>) {
46 let s: BTreeSet<T> = set_of_things.into_iter().collect();
47 if s.is_empty() {
48 return;
49 }
50 self.dirty = true;
51 let (mut intersection, difference): (BTreeSet<T>, BTreeSet<T>) =
52 s.into_iter().partition(|t| self.things.contains(t));
53 if !difference.is_empty() {
55 let index = self.sets.len();
56 for thing in &difference {
57 self.things.insert(thing.clone());
58 self.mapping.insert(thing.clone(), index);
59 }
60 self.sets.push(difference);
61 }
62 while let Some(first) = intersection.first() {
63 let Some(&old) = self.mapping.get(first) else {
65 break;
66 };
67 let old_class_intersection: BTreeSet<T> = self.sets[old]
68 .intersection(&intersection)
69 .cloned()
70 .collect();
71 for thing in &old_class_intersection {
74 self.sets[old].remove(thing);
75 intersection.remove(thing);
76 }
77 let index = self.sets.len();
79 for thing in &old_class_intersection {
80 self.mapping.insert(thing.clone(), index);
81 }
82 self.sets.push(old_class_intersection);
83 }
84 }
85
86 pub fn update<I: IntoIterator<Item = T>>(&mut self, list_of_sets: impl IntoIterator<Item = I>) {
88 for s in list_of_sets {
89 self.add(s);
90 }
91 }
92
93 fn process(&mut self) {
95 if !self.dirty {
96 return;
97 }
98 let mut sets: Vec<BTreeSet<T>> = std::mem::take(&mut self.sets)
99 .into_iter()
100 .filter(|s| !s.is_empty())
101 .collect();
102 if self.sort {
103 sets.sort_by(|a, b| b.len().cmp(&a.len()).then_with(|| a.iter().cmp(b.iter())));
105 }
106 self.mapping.clear();
107 for (i, set) in sets.iter().enumerate() {
108 for thing in set {
109 self.mapping.insert(thing.clone(), i);
110 }
111 }
112 self.sets = sets;
113 self.dirty = false;
114 }
115
116 pub fn get_things(&mut self) -> &BTreeSet<T> {
118 self.process();
119 &self.things
120 }
121
122 pub fn get_classes(&mut self) -> &[BTreeSet<T>] {
124 self.process();
125 &self.sets
126 }
127
128 pub fn get_mapping(&mut self) -> &BTreeMap<T, usize> {
131 self.process();
132 &self.mapping
133 }
134}
135
136pub fn classify<T: Ord + Clone, I: IntoIterator<Item = T>>(
151 list_of_sets: impl IntoIterator<Item = I>,
152 sort: bool,
153) -> (Vec<BTreeSet<T>>, BTreeMap<T, BTreeSet<T>>) {
154 let mut classifier = Classifier::new(sort);
155 classifier.update(list_of_sets);
156 let classes = classifier.get_classes().to_vec();
157 let mapping = classifier
158 .get_mapping()
159 .iter()
160 .map(|(thing, &i)| (thing.clone(), classes[i].clone()))
161 .collect();
162 (classes, mapping)
163}