1use std::convert::TryFrom;
2use std::mem::swap;
3
4#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
6pub struct Vec1<T> {
7 head: T,
8 tail: Vec<T>,
9}
10
11impl<T> From<T> for Vec1<T> {
12 fn from(head: T) -> Self {
13 Self::new(head, Default::default())
14 }
15}
16
17impl<T> TryFrom<Vec<T>> for Vec1<T> {
18 type Error = ();
19
20 fn try_from(mut value: Vec<T>) -> Result<Self, Self::Error> {
21 if value.is_empty() {
22 Err(())
23 } else {
24 let head = value.remove(0);
25 Ok(Vec1::new(head, value))
26 }
27 }
28}
29
30impl<T> Into<Vec<T>> for Vec1<T> {
31 fn into(mut self) -> Vec<T> {
32 let mut v = Vec::with_capacity(self.len());
33 v.push(self.head);
34 v.append(&mut self.tail);
35 v
36 }
37}
38
39impl<T> Vec1<T> {
40 pub fn new(head: T, tail: Vec<T>) -> Self {
41 Self { head, tail }
42 }
43
44 pub fn len(&self) -> usize {
45 self.tail.len() + 1
46 }
47
48 pub fn is_empty(&self) -> bool {
49 false
50 }
51
52 pub fn is_single(&self) -> bool {
53 self.tail.is_empty()
54 }
55
56 pub fn into_vec(self) -> Vec<T> {
57 self.into()
58 }
59
60 pub fn push(&mut self, new: T) {
61 self.tail.push(new)
62 }
63
64 pub fn append_self_into(mut self, target: &mut Vec<T>) {
65 target.push(self.head);
66 target.append(&mut self.tail);
67 }
68
69 pub fn append(&mut self, mut new: Vec1<T>) {
70 self.push(new.head);
71 self.append_vec(&mut new.tail)
72 }
73
74 pub fn append_vec(&mut self, new: &mut Vec<T>) {
75 self.tail.append(new)
76 }
77
78 pub fn head(&self) -> &T {
79 &self.head
80 }
81
82 pub fn head_mut(&mut self) -> &mut T {
83 &mut self.head
84 }
85
86 pub fn tail(&self) -> &Vec<T> {
87 &self.tail
88 }
89
90 pub fn tail_mut(&mut self) -> &mut Vec<T> {
91 &mut self.tail
92 }
93
94 pub fn last(&self) -> &T {
95 self.tail.last().unwrap_or(&self.head)
96 }
97
98 pub fn last_mut(&mut self) -> &mut T {
99 self.tail.last_mut().unwrap_or(&mut self.head)
100 }
101
102 pub fn insert(&mut self, index: usize, mut new: T) {
103 if index == 0 {
104 swap(&mut new, &mut self.head);
105 self.tail.insert(0, new)
106 } else {
107 self.tail.insert(index - 1, new)
108 }
109 }
110
111 pub fn map<R>(self, mut f: impl FnMut(T) -> R) -> Vec1<R> {
112 Vec1::new(f(self.head), self.tail.into_iter().map(f).collect())
113 }
114
115 pub fn scan<St, B>(self, init: St, mut f: impl FnMut(St, T) -> (B, St)) -> (St, Vec1<B>) {
116 let (head, mut init) = f(init, self.head);
117 let mut ret = Vec::with_capacity(self.tail.len());
118 for e in self.tail {
119 let (e, new_init) = f(init, e);
120 init = new_init;
121 ret.push(e);
122 }
123 (init, Vec1::new(head, ret))
124 }
125
126 pub fn try_scan<St, B, E>(
127 self,
128 init: St,
129 mut f: impl FnMut(St, T) -> Result<(B, St), E>,
130 ) -> Result<(St, Vec1<B>), E> {
131 let (head, mut init) = f(init, self.head)?;
132 let mut ret = Vec::with_capacity(self.tail.len());
133 for e in self.tail {
134 let (e, new_init) = f(init, e)?;
135 init = new_init;
136 ret.push(e);
137 }
138 Ok((init, Vec1::new(head, ret)))
139 }
140
141 pub fn try_map<E, R>(self, mut f: impl FnMut(T) -> Result<R, E>) -> Result<Vec1<R>, E> {
142 Ok(Vec1::new(
143 f(self.head)?,
144 self.tail.into_iter().map(f).collect::<Result<_, _>>()?,
145 ))
146 }
147
148 pub fn try_fold1<E>(self, f: impl FnMut(T, T) -> Result<T, E>) -> Result<T, E> {
149 self.tail.into_iter().try_fold(self.head, f)
150 }
151
152 pub fn try_fold<R, E>(self, init: R, mut f: impl FnMut(R, T) -> Result<R, E>) -> Result<R, E> {
153 let init = f(init, self.head)?;
154 self.tail.into_iter().try_fold(init, f)
155 }
156
157 pub fn fold1(self, f: impl FnMut(T, T) -> T) -> T {
158 self.tail.into_iter().fold(self.head, f)
159 }
160
161 pub fn fold<R>(self, init: R, mut f: impl FnMut(R, T) -> R) -> R {
162 let init = f(init, self.head);
163 self.tail.into_iter().fold(init, f)
164 }
165
166 pub fn rev_fold1(self, f: impl FnMut(T, T) -> T) -> T {
167 self.tail.into_iter().rev().fold(self.head, f)
168 }
169
170 pub fn rev_fold<R>(self, init: R, mut f: impl FnMut(R, T) -> R) -> R {
171 let init = f(init, self.head);
172 self.tail.into_iter().rev().fold(init, f)
173 }
174}
175
176impl<T: Default> Default for Vec1<T> {
177 fn default() -> Self {
178 Self::from(T::default())
179 }
180}