1use std::{fmt, str::FromStr};
4
5pub static HE: &[char] = &['何', '劾', '合', '呵', '和', '喝', '嗬', '壑', '曷', '核', '河', '涸', '盍', '盒', '禾', '翮', '翯', '荷', '菏', '蚵', '褐', '詥', '诃', '贺', '赫', '郃', '阂', '阖', '颌', '饸', '鹖', '鹤'];
9
10pub static YI: &[char] = &['一', '义', '乙', '亦', '亿', '以', '仪', '伊', '佚', '佾', '依', '倚', '刈', '劓', '医', '呓', '咦', '咿', '噫', '圯', '壹', '夷', '奕', '姨', '宜', '屹', '峄', '已', '异', '弈', '弋', '彝', '役', '忆', '怡', '怿', '悒', '意', '懿', '抑', '挹', '揖', '旖', '易', '椅', '欹', '殪', '毅', '沂', '溢', '漪', '熠', '猗', '疑', '疫', '痍', '癔', '益', '眙', '睪', '矣', '祎', '移', '绎', '缢', '羿', '翊', '翌', '翳', '翼', '耴', '肄', '胰', '臆', '舣', '艺', '苡', '薏', '蚁', '蛜', '蛡', '蜴', '衣', '裔', '议', '译', '诒', '诣', '谊', '豷', '贻', '轶', '迤', '迻', '逸', '遗', '邑', '钇', '铱', '镒', '镱', '颐', '饴', '驿', '鮨', '黟'];
12
13pub static WEI: &[char] = &['为', '伟', '伪', '位', '偎', '卫', '危', '味', '唯', '喂', '围', '圩', '委', '威', '娓', '尉', '尾', '嵬', '巍', '帏', '帷', '微', '惟', '慰', '未', '桅', '涠', '渭', '炜', '煨', '猥', '猬', '玮', '畏', '痿', '硙', '磈', '纬', '维', '胃', '艉', '苇', '萎', '葳', '蔚', '薇', '诿', '谓', '违', '逶', '闱', '隈', '霨', '韦', '韪', '魏', '鲔'];
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub struct Hyw {
19 he: usize,
21 yi: usize,
23 wei: usize,
25}
26
27impl Hyw {
28 pub const fn new() -> Self {
30 Self { he: 0, yi: 0, wei: 0 }
31 }
32
33 pub const fn from_index(index: usize) -> Option<Self> {
35 let he_len = HE.len();
36 let yi_len = YI.len();
37 let wei_len = WEI.len();
38 let total_len = he_len * yi_len * wei_len;
39
40 if index >= total_len {
41 return None;
42 }
43
44 let he = index / (yi_len * wei_len);
45 let yi = (index / wei_len) % yi_len;
46 let wei = index % wei_len;
47
48 Some(Self { he, yi, wei })
49 }
50
51 pub const fn to_index(&self) -> usize {
53 let yi_len = YI.len();
54 let wei_len = WEI.len();
55 self.he * yi_len * wei_len + self.yi * wei_len + self.wei
56 }
57
58 pub const fn next(&self) -> Option<Self> {
60 let mut he = self.he;
61 let mut yi = self.yi;
62 let mut wei = self.wei + 1;
63
64 if wei >= WEI.len() {
65 wei = 0;
66 yi += 1;
67 if yi >= YI.len() {
68 yi = 0;
69 he += 1;
70 if he >= HE.len() {
71 return None;
72 }
73 }
74 }
75
76 Some(Self { he, yi, wei })
77 }
78
79 pub const fn previous(&self) -> Option<Self> {
81 let mut he = self.he;
82 let mut yi = self.yi;
83 let mut wei = self.wei;
84
85 if wei == 0 {
86 wei = WEI.len() - 1;
87 if yi == 0 {
88 yi = YI.len() - 1;
89 if he == 0 {
90 return None;
91 } else {
92 he -= 1;
93 }
94 } else {
95 yi -= 1;
96 }
97 } else {
98 wei -= 1;
99 }
100
101 Some(Self { he, yi, wei })
102 }
103
104 pub const fn iter_from(self) -> HywIterator {
106 HywIterator { current: Some(self) }
107 }
108
109 pub const fn all() -> HywIterator {
111 HywIterator { current: Some(Hyw::new()) }
112 }
113
114 pub const fn set_he(&mut self, he: usize) -> bool {
116 if he < HE.len() {
117 self.he = he;
118 true
119 } else {
120 false
121 }
122 }
123
124 pub const fn set_yi(&mut self, yi: usize) -> bool {
126 if yi < YI.len() {
127 self.yi = yi;
128 true
129 } else {
130 false
131 }
132 }
133
134 pub const fn set_wei(&mut self, wei: usize) -> bool {
136 if wei < WEI.len() {
137 self.wei = wei;
138 true
139 } else {
140 false
141 }
142 }
143}
144
145impl fmt::Display for Hyw {
146 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147 write!(f, "{}{}{}", HE[self.he], YI[self.yi], WEI[self.wei])
148 }
149}
150
151impl Default for Hyw {
152 fn default() -> Self {
153 Self::new()
154 }
155}
156
157impl FromStr for Hyw {
158 type Err = ();
159
160 fn from_str(s: &str) -> Result<Self, Self::Err> {
161 let mut chars = s.chars();
162 let he_char = chars.next().ok_or(())?;
163 let yi_char = chars.next().ok_or(())?;
164 let wei_char = chars.next().ok_or(())?;
165
166 let he_index = HE.iter().position(|&c| c == he_char).ok_or(())?;
167 let yi_index = YI.iter().position(|&c| c == yi_char).ok_or(())?;
168 let wei_index = WEI.iter().position(|&c| c == wei_char).ok_or(())?;
169
170 Ok(Hyw {
171 he: he_index,
172 yi: yi_index,
173 wei: wei_index,
174 })
175 }
176}
177
178#[derive(Debug, Clone, Copy, PartialEq, Eq)]
180pub struct HywIterator {
181 current: Option<Hyw>,
182}
183
184impl Iterator for HywIterator {
185 type Item = Hyw;
186
187 fn next(&mut self) -> Option<Self::Item> {
188 let current = self.current?;
189 self.current = current.next();
190 Some(current)
191 }
192
193 fn size_hint(&self) -> (usize, Option<usize>) {
194 let len = self.len();
195 (len, Some(len))
196 }
197}
198
199impl ExactSizeIterator for HywIterator {
200 fn len(&self) -> usize {
201 match self.current {
202 Some(hyw) => HE.len() * YI.len() * WEI.len() - hyw.to_index(),
203 None => 0,
204 }
205 }
206}
207
208#[cfg(test)]
209mod tests {
210 use super::*;
211
212 #[test]
213 fn test_hyw_new() {
214 let hyw = Hyw::new();
215 assert_eq!(hyw.to_index(), 0);
216 assert_eq!(hyw.to_string(), format!("{}{}{}", HE[0], YI[0], WEI[0]));
217 }
218
219 #[test]
220 fn test_hyw_from_index() {
221 let hyw = Hyw::from_index(0).unwrap();
222 assert_eq!(hyw.to_string(), format!("{}{}{}", HE[0], YI[0], WEI[0]));
223
224 let hyw = Hyw::from_index(1).unwrap();
225 assert_eq!(hyw.to_string(), format!("{}{}{}", HE[0], YI[0], WEI[1]));
226
227 let total = HE.len() * YI.len() * WEI.len();
229 assert!(Hyw::from_index(total - 1).is_some());
230 assert!(Hyw::from_index(total).is_none());
231 }
232
233 #[test]
234 fn test_hyw_to_index_roundtrip() {
235 for i in [0, 1, 100, 1000, 10000] {
236 if let Some(hyw) = Hyw::from_index(i) {
237 assert_eq!(hyw.to_index(), i);
238 }
239 }
240 }
241
242 #[test]
243 fn test_hyw_next() {
244 let mut hyw = Hyw::new();
245 let next = hyw.next().unwrap();
246 assert_eq!(next.to_index(), 1);
247
248 let last_wei = WEI.len() - 1;
250 hyw.set_wei(last_wei);
251 let next = hyw.next().unwrap();
252 assert_eq!(next.to_index(), hyw.to_index() + 1);
253
254 let total = HE.len() * YI.len() * WEI.len();
256 let last = Hyw::from_index(total - 1).unwrap();
257 assert!(last.next().is_none());
258 }
259
260 #[test]
261 fn test_hyw_previous() {
262 let hyw = Hyw::from_index(100).unwrap();
263 let prev = hyw.previous().unwrap();
264 assert_eq!(prev.to_index(), 99);
265
266 let first = Hyw::new();
268 assert!(first.previous().is_none());
269
270 let mut hyw = Hyw::new();
272 hyw.set_yi(1);
273 let prev = hyw.previous().unwrap();
274 assert_eq!(prev.to_index(), hyw.to_index() - 1);
275 }
276
277 #[test]
278 fn test_hyw_from_str() {
279 let hyw_str = format!("{}{}{}", HE[2], YI[3], WEI[4]);
280 let hyw = Hyw::from_str(&hyw_str).unwrap();
281 assert_eq!(hyw.he, 2);
282 assert_eq!(hyw.yi, 3);
283 assert_eq!(hyw.wei, 4);
284
285 assert!(Hyw::from_str("abc").is_err());
287 assert!(Hyw::from_str("不存在的字符").is_err());
288 }
289
290 #[test]
291 fn test_iterator_basic() {
292 let mut iter = Hyw::all();
293 let first = iter.next().unwrap();
294 assert_eq!(first, Hyw::new());
295
296 let second = iter.next().unwrap();
297 assert_eq!(second, Hyw::new().next().unwrap());
298 }
299
300 #[test]
301 fn test_iterator_count() {
302 let total = HE.len() * YI.len() * WEI.len();
303 let count = Hyw::all().count();
304 assert_eq!(count, total);
305 }
306
307 #[test]
308 fn test_iterator_take() {
309 let first_10: Vec<_> = Hyw::all().take(10).collect();
310 assert_eq!(first_10.len(), 10);
311 assert_eq!(first_10[0], Hyw::new());
312 assert_eq!(first_10[9], Hyw::from_index(9).unwrap());
313 }
314
315 #[test]
316 fn test_iterator_skip() {
317 let start_from_100 = Hyw::all().skip(100).next().unwrap();
318 assert_eq!(start_from_100, Hyw::from_index(100).unwrap());
319 }
320
321 #[test]
322 fn test_iterator_iter_from() {
323 let start = Hyw::from_index(50).unwrap();
324 let mut iter = start.iter_from();
325 assert_eq!(iter.next().unwrap(), start);
326 assert_eq!(iter.next().unwrap(), start.next().unwrap());
327 }
328
329 #[test]
330 fn test_iterator_size_hint() {
331 let total = HE.len() * YI.len() * WEI.len();
332 let iter = Hyw::all();
333 assert_eq!(iter.size_hint(), (total, Some(total)));
334
335 let iter = Hyw::all().skip(100);
336 let (lower, upper) = iter.size_hint();
337 assert_eq!(lower, total - 100);
338 assert_eq!(upper, Some(total - 100));
339 }
340
341 #[test]
342 fn test_iterator_len() {
343 let total = HE.len() * YI.len() * WEI.len();
344 let iter = Hyw::all();
345 assert_eq!(iter.len(), total);
346
347 let iter = Hyw::from_index(100).unwrap().iter_from();
348 assert_eq!(iter.len(), total - 100);
349
350 let last = Hyw::from_index(total - 1).unwrap();
352 let mut iter = last.iter_from();
353 iter.next(); assert_eq!(iter.len(), 0);
355 }
356
357 #[test]
358 fn test_iterator_exhausted() {
359 let last = Hyw::from_index(HE.len() * YI.len() * WEI.len() - 1).unwrap();
360 let mut iter = last.iter_from();
361 assert_eq!(iter.next(), Some(last));
362 assert_eq!(iter.next(), None);
363 assert_eq!(iter.next(), None); }
365
366 #[test]
367 fn test_set_methods() {
368 let mut hyw = Hyw::new();
369
370 assert!(hyw.set_he(10));
371 assert!(hyw.set_yi(20));
372 assert!(hyw.set_wei(30));
373
374 assert_eq!(hyw.to_string(), format!("{}{}{}", HE[10], YI[20], WEI[30]));
375
376 assert!(!hyw.set_he(HE.len()));
378 assert!(!hyw.set_yi(YI.len()));
379 assert!(!hyw.set_wei(WEI.len()));
380 }
381
382 #[test]
383 fn test_display_format() {
384 let hyw = Hyw::new();
385 let display = format!("{}", hyw);
386 let expected = format!("{}{}{}", HE[0], YI[0], WEI[0]);
387 assert_eq!(display, expected);
388 }
389}