jean_core/sequence/
seq.rs1use std::fmt::Display;
2
3use crate::prelude::Complement;
4use num_traits::Float;
5
6#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
8pub struct Seq<T>(pub Vec<T>);
9
10impl<T> Seq<T> {
11 pub fn new() -> Self {
12 Self(Vec::new())
13 }
14
15 pub fn len(&self) -> usize {
16 self.0.len()
17 }
18
19 pub fn iter(&self) -> std::slice::Iter<T> {
20 self.0.iter()
21 }
22
23 pub fn iter_mut(&mut self) -> std::slice::IterMut<T> {
24 self.0.iter_mut()
25 }
26
27 pub fn push(&mut self, t: T) {
28 self.0.push(t)
29 }
30
31 pub fn insert(&mut self, index: usize, t: T) {
32 self.0.insert(index, t)
33 }
34
35 pub fn frequencies<F>(&self) -> std::collections::BTreeMap<&T, F>
36 where
37 F: Float,
38 T: Ord,
39 {
40 let len = F::from(self.len()).unwrap();
41 let mut frequencies = std::collections::BTreeMap::new();
42
43 self.iter().for_each(|t| {
44 let e = frequencies.entry(t).or_insert(F::zero());
45 *e = *e + F::one();
46 });
47
48 frequencies.iter_mut().for_each(|(_, e)| *e = *e / len);
49
50 frequencies
51 }
52}
53
54impl<T> Complement for Seq<T>
55where
56 T: Complement,
57{
58 fn complement(&self) -> Self {
59 Self(self.0.iter().map(|t| t.complement()).collect())
60 }
61}
62
63impl<T> IntoIterator for Seq<T> {
64 type Item = T;
65 type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
66
67 fn into_iter(self) -> Self::IntoIter {
68 self.0.into_iter()
69 }
70}
71
72impl<'a, T> IntoIterator for &'a Seq<T> {
73 type Item = &'a T;
74 type IntoIter = std::slice::Iter<'a, T>;
75
76 fn into_iter(self) -> Self::IntoIter {
77 self.iter()
78 }
79}
80
81impl<'a, T> IntoIterator for &'a mut Seq<T> {
82 type Item = &'a mut T;
83 type IntoIter = std::slice::IterMut<'a, T>;
84
85 fn into_iter(self) -> Self::IntoIter {
86 self.iter_mut()
87 }
88}
89
90impl<T, I: std::slice::SliceIndex<[T]>> std::ops::Index<I> for Seq<T> {
91 type Output = I::Output;
92
93 fn index(&self, index: I) -> &Self::Output {
94 &self.0[index]
95 }
96}
97
98impl<T, I: std::slice::SliceIndex<[T]>> std::ops::IndexMut<I> for Seq<T> {
99 fn index_mut(&mut self, index: I) -> &mut Self::Output {
100 &mut self.0[index]
101 }
102}
103
104impl<T> From<Vec<T>> for Seq<T> {
105 fn from(value: Vec<T>) -> Self {
106 Self(value)
107 }
108}
109
110impl<'a, T> From<&'a Vec<T>> for Seq<T>
111where
112 T: Clone,
113{
114 fn from(value: &'a Vec<T>) -> Self {
115 Self(value.to_vec())
116 }
117}
118
119impl<'a, T> From<&'a [T]> for Seq<T>
120where
121 T: Clone,
122{
123 fn from(value: &'a [T]) -> Self {
124 Self(value.to_vec())
125 }
126}
127
128impl<'a, T> TryFrom<&'a str> for Seq<T>
129where
130 T: TryFrom<char> + Clone,
131{
132 type Error = &'static str;
133
134 fn try_from(value: &'a str) -> Result<Self, Self::Error> {
135 let internal: Result<Vec<T>, _> = value.chars().map(|c| T::try_from(c)).collect();
136
137 match internal {
138 Ok(internal) => Ok(Self::from(&internal[..])),
139 Err(_) => Err("failed"),
140 }
141 }
142}
143
144impl<T> Display for Seq<T>
145where
146 T: Copy,
147 char: From<T>,
148{
149 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
150 for t in &self.0 {
151 write!(f, "{}", char::from(*t))?;
152 }
153
154 Ok(())
155 }
156}