|
14 | 14 |
|
15 | 15 | use std::{
|
16 | 16 | collections::VecDeque,
|
| 17 | + iter::repeat_n, |
17 | 18 | ops::{ControlFlow, Not},
|
18 | 19 | sync::{Arc, RwLock},
|
19 | 20 | };
|
@@ -133,7 +134,8 @@ impl UpdateToVectorDiff {
|
133 | 134 | ///
|
134 | 135 | /// * [`Update::NewItemsChunk`] and [`Update::NewGapChunk`] are inserting a
|
135 | 136 | /// new pair with a chunk length of 0 at the appropriate index,
|
136 |
| - /// * [`Update::RemoveChunk`] is removing a pair, |
| 137 | + /// * [`Update::RemoveChunk`] is removing a pair, and is potentially |
| 138 | + /// emitting [`VectorDiff`], |
137 | 139 | /// * [`Update::PushItems`] is increasing the length of the appropriate pair
|
138 | 140 | /// by the number of new items, and is potentially emitting
|
139 | 141 | /// [`VectorDiff`],
|
@@ -318,22 +320,17 @@ impl UpdateToVectorDiff {
|
318 | 320 | }
|
319 | 321 | }
|
320 | 322 |
|
321 |
| - Update::RemoveChunk(expected_chunk_identifier) => { |
322 |
| - let chunk_index = self |
| 323 | + Update::RemoveChunk(chunk_identifier) => { |
| 324 | + let (offset, (chunk_index, _)) = |
| 325 | + self.map_to_offset(&Position(*chunk_identifier, 0)); |
| 326 | + |
| 327 | + let (_, number_of_items) = self |
323 | 328 | .chunks
|
324 |
| - .iter() |
325 |
| - .position(|(chunk_identifier, _)| { |
326 |
| - chunk_identifier == expected_chunk_identifier |
327 |
| - }) |
328 |
| - // SAFETY: Assuming `LinkedChunk` and `ObservableUpdates` are not buggy, and |
329 |
| - // assuming `Self::chunks` is correctly initialized, it is not possible to |
330 |
| - // remove a chunk that does not exist. If this predicate fails, it means |
331 |
| - // `LinkedChunk` or `ObservableUpdates` contain a bug. |
332 |
| - .expect("Removing a chunk: The chunk is not found"); |
| 329 | + .remove(chunk_index) |
| 330 | + .expect("Removing an index out of the bounds"); |
333 | 331 |
|
334 |
| - // It's OK to ignore the result. The `chunk_index` exists because it's been |
335 |
| - // found, and we don't care about its associated value. |
336 |
| - let _ = self.chunks.remove(chunk_index); |
| 332 | + // Removing at the same index because each `Remove` shifts items to the left. |
| 333 | + diffs.extend(repeat_n(VectorDiff::Remove { index: offset }, number_of_items)); |
337 | 334 | }
|
338 | 335 |
|
339 | 336 | Update::PushItems { at: position, items } => {
|
|
0 commit comments