open_hypergraphs/strict/
relation.rs1use crate::array::{Array, ArrayKind, NaturalArray, OrdArray};
3use crate::finite_function::FiniteFunction;
4use crate::indexed_coproduct::{HasLen, IndexedCoproduct};
5use num_traits::{One, Zero};
6
7pub fn converse<K: ArrayKind>(
24 r: &IndexedCoproduct<K, FiniteFunction<K>>,
25) -> IndexedCoproduct<K, FiniteFunction<K>>
26where
27 K::Type<K::I>: NaturalArray<K>,
28{
29 let values_table = {
30 let arange = K::Index::arange(&K::I::zero(), &r.sources.len());
31 let unsorted_values = r.sources.table.repeat(arange.get_range(..));
32 unsorted_values.sort_by(&r.values.table)
33 };
34
35 let sources_table =
36 (r.values.table.as_ref() as &K::Type<K::I>).bincount(r.values.target.clone());
37
38 let sources = FiniteFunction::new(sources_table, r.values.table.len() + K::I::one()).unwrap();
39 let values = FiniteFunction::new(values_table, r.len()).unwrap();
40
41 IndexedCoproduct::new(sources, values).unwrap()
42}