1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
//! ref: composer/src/Composer/DependencyResolver/RuleSetIterator.php
use crate::dependency_resolver::rule::Rule;
use indexmap::IndexMap;
/// Implements PHP \Iterator over a grouped rule set.
#[derive(Debug)]
pub struct RuleSetIterator {
pub(crate) rules: IndexMap<i64, Vec<Rule>>,
pub(crate) types: Vec<i64>,
pub(crate) current_offset: i64,
pub(crate) current_type: i64,
pub(crate) current_type_offset: i64,
}
impl RuleSetIterator {
pub fn new(rules: IndexMap<i64, Vec<Rule>>) -> Self {
let mut types: Vec<i64> = rules.keys().copied().collect();
types.sort();
let mut iter = Self {
rules,
types,
current_offset: 0,
current_type: -1,
current_type_offset: 0,
};
iter.rewind();
iter
}
pub fn current(&self) -> &Rule {
&self.rules[&self.current_type][self.current_offset as usize]
}
pub fn key(&self) -> i64 {
self.current_type
}
pub fn next(&mut self) {
self.current_offset += 1;
if !self.rules.contains_key(&self.current_type) {
return;
}
if self.current_offset >= self.rules[&self.current_type].len() as i64 {
self.current_offset = 0;
loop {
self.current_type_offset += 1;
if self.types.get(self.current_type_offset as usize).is_none() {
self.current_type = -1;
break;
}
self.current_type = self.types[self.current_type_offset as usize];
if self.rules[&self.current_type].len() != 0 {
break;
}
}
}
}
pub fn rewind(&mut self) {
self.current_offset = 0;
self.current_type_offset = -1;
self.current_type = -1;
loop {
self.current_type_offset += 1;
if self.types.get(self.current_type_offset as usize).is_none() {
self.current_type = -1;
break;
}
self.current_type = self.types[self.current_type_offset as usize];
if self.rules[&self.current_type].len() != 0 {
break;
}
}
}
pub fn valid(&self) -> bool {
if let Some(rules) = self.rules.get(&self.current_type) {
rules.get(self.current_offset as usize).is_some()
} else {
false
}
}
}
|