MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / resolve_position

Method resolve_position

atomic-core/src/crdt/apply/order.rs:374–424  ·  view source on GitHub ↗

Resolves the insertion position for a new element. Finds all existing elements that share the same "after" reference and determines where the new element fits based on ID ordering.

(&self, id: &Id, after: &Option<Id>)

Source from the content-addressed store, hash-verified

372 /// Finds all existing elements that share the same "after" reference
373 /// and determines where the new element fits based on ID ordering.
374 fn resolve_position(&self, id: &Id, after: &Option<Id>) -> InsertPosition {
375 // Find the starting point: either the referenced element or the start
376 let start_index = match after {
377 Some(ref after_id) => {
378 // Find the element we're inserting after
379 match self.elements.iter().position(|e| e == after_id) {
380 Some(idx) => idx + 1,
381 None => 0, // Reference not found, treat as start
382 }
383 }
384 None => 0,
385 };
386
387 // Find all concurrent insertions (elements with the same "after" reference)
388 let mut concurrent: Vec<&Id> = Vec::new();
389 for i in start_index..self.elements.len() {
390 if self.after_refs[i] == *after {
391 concurrent.push(&self.elements[i]);
392 } else {
393 // Once we see a different "after" reference, we've passed the concurrent group
394 break;
395 }
396 }
397
398 if concurrent.is_empty() {
399 if start_index == 0 {
400 InsertPosition::Start
401 } else {
402 InsertPosition::After(start_index - 1)
403 }
404 } else {
405 // Find where the new ID fits among concurrent insertions
406 match concurrent.binary_search(&id) {
407 Ok(_) => {
408 // Exact match (duplicate ID) - shouldn't happen
409 InsertPosition::After(start_index + concurrent.len() - 1)
410 }
411 Err(pos) => {
412 if pos == 0 {
413 if start_index == 0 {
414 InsertPosition::Start
415 } else {
416 InsertPosition::After(start_index - 1)
417 }
418 } else {
419 InsertPosition::After(start_index + pos - 1)
420 }
421 }
422 }
423 }
424 }
425
426 /// Checks if the sequence contains an element with the given ID.
427 #[inline]

Callers 1

insertMethod · 0.80

Calls 5

positionMethod · 0.45
iterMethod · 0.45
lenMethod · 0.45
pushMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected