Debian Codegraph @

lib/src/default_index/rev_walk_queue.rs

raw
1 // Copyright 2023 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::collections::BinaryHeap;
16 use std::mem;
17
18 #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
19 pub(super) struct RevWalkWorkItem<P, T> {
20 pub pos: P,
21 pub value: T,
22 }
23
24 #[derive(Clone)]
25 pub(super) struct RevWalkQueue<P, T> {
26 items: BinaryHeap<RevWalkWorkItem<P, T>>,
27 // Optionally keep the greatest item out of the heap, so pop() + push() of
28 // the greatest item won't have to rebalance the heap.
29 scratch_item: Option<RevWalkWorkItem<P, T>>,
30 min_pos: P,
31 }
32
33 impl<P: Ord, T: Ord> RevWalkQueue<P, T> {
34 pub fn with_min_pos(min_pos: P) -> Self {
35 Self {
36 items: BinaryHeap::new(),
37 scratch_item: None,
38 min_pos,
39 }
40 }
41
42 #[cfg_attr(not(test), expect(dead_code))]
43 pub fn len(&self) -> usize {
44 self.items.len() + usize::from(self.scratch_item.is_some())
45 }
46
47 pub fn push(&mut self, pos: P, value: T) {
48 if pos < self.min_pos {
49 return;
50 }
51 self.push_item(RevWalkWorkItem { pos, value });
52 }
53
54 fn push_item(&mut self, new: RevWalkWorkItem<P, T>) {
55 if let Some(next) = self.scratch_item.as_mut() {
56 if new < *next {
57 self.items.push(new);
58 } else {
59 let next = mem::replace(next, new);
60 self.items.push(next);
61 }
62 } else if let Some(next) = self.items.peek() {
63 if new < *next {
64 // items[0] could be moved to scratch_item, but simply leave
65 // scratch_item empty.
66 self.items.push(new);
67 } else {
68 self.scratch_item = Some(new);
69 }
70 } else {
71 self.scratch_item = Some(new);
72 }
73 }
74
75 pub fn extend(&mut self, positions: impl IntoIterator<Item = P>, value: T)
76 where
77 T: Clone,
78 {
79 for pos in positions {
80 self.push(pos, value.clone());
81 }
82 }
83
84 pub fn peek(&self) -> Option<&RevWalkWorkItem<P, T>> {
85 self.scratch_item.as_ref().or_else(|| self.items.peek())
86 }
87
88 pub fn pop(&mut self) -> Option<RevWalkWorkItem<P, T>> {
89 let next = self.scratch_item.take().or_else(|| self.items.pop())?;
90 Some(next)
91 }
92
93 pub fn pop_if(
94 &mut self,
95 predicate: impl FnOnce(&RevWalkWorkItem<P, T>) -> bool,
96 ) -> Option<RevWalkWorkItem<P, T>> {
97 let next = self.peek()?;
98 predicate(next).then(|| self.pop().unwrap())
99 }
100
101 pub fn pop_eq(&mut self, pos: &P) -> Option<RevWalkWorkItem<P, T>> {
102 self.pop_if(|next| next.pos == *pos)
103 }
104
105 pub fn skip_while_eq(&mut self, pos: &P) {
106 while self.pop_eq(pos).is_some() {
107 continue;
108 }
109 }
110 }
111
112 #[cfg(test)]
113 mod tests {
114 use assert_matches::assert_matches;
115
116 use super::*;
117
118 #[test]
119 fn test_push_pop_in_forward_order() {
120 let mut queue: RevWalkQueue<u32, ()> = RevWalkQueue::with_min_pos(0);
121
122 queue.push(0, ());
123 assert!(queue.scratch_item.is_some());
124 assert_eq!(queue.items.len(), 0);
125
126 queue.push(1, ());
127 assert!(queue.scratch_item.is_some());
128 assert_eq!(queue.items.len(), 1);
129
130 assert_matches!(queue.pop(), Some(RevWalkWorkItem { pos: 1, .. }));
131 assert!(queue.scratch_item.is_none());
132 assert_eq!(queue.items.len(), 1);
133
134 queue.push(2, ());
135 assert!(queue.scratch_item.is_some());
136 assert_eq!(queue.items.len(), 1);
137
138 assert_matches!(queue.pop(), Some(RevWalkWorkItem { pos: 2, .. }));
139 assert!(queue.scratch_item.is_none());
140 assert_eq!(queue.items.len(), 1);
141
142 assert_matches!(queue.pop(), Some(RevWalkWorkItem { pos: 0, .. }));
143 assert!(queue.scratch_item.is_none());
144 assert_eq!(queue.items.len(), 0);
145
146 assert_matches!(queue.pop(), None);
147 }
148
149 #[test]
150 fn test_push_pop_in_reverse_order() {
151 let mut queue: RevWalkQueue<u32, ()> = RevWalkQueue::with_min_pos(0);
152
153 queue.push(2, ());
154 assert!(queue.scratch_item.is_some());
155 assert_eq!(queue.items.len(), 0);
156
157 queue.push(1, ());
158 assert!(queue.scratch_item.is_some());
159 assert_eq!(queue.items.len(), 1);
160
161 assert_matches!(queue.pop(), Some(RevWalkWorkItem { pos: 2, .. }));
162 assert!(queue.scratch_item.is_none());
163 assert_eq!(queue.items.len(), 1);
164
165 queue.push(0, ());
166 assert!(queue.scratch_item.is_none());
167 assert_eq!(queue.items.len(), 2);
168
169 assert_matches!(queue.pop(), Some(RevWalkWorkItem { pos: 1, .. }));
170 assert!(queue.scratch_item.is_none());
171 assert_eq!(queue.items.len(), 1);
172
173 assert_matches!(queue.pop(), Some(RevWalkWorkItem { pos: 0, .. }));
174 assert!(queue.scratch_item.is_none());
175 assert_eq!(queue.items.len(), 0);
176
177 assert_matches!(queue.pop(), None);
178 }
179 }