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

Function find_insert_position

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

Finds the insertion position for a new element among concurrent insertions. Given a new element ID and a list of existing elements that were all inserted after the same reference point, determines where the new element should be placed based on ID ordering. # Arguments `new_id` - The ID of the element being inserted `concurrent` - Existing elements that share the same insertion point # Returns

(new_id: &T, concurrent: &[T])

Source from the content-addressed store, hash-verified

157/// assert_eq!(pos, InsertPosition::After(0));
158/// ```
159pub fn find_insert_position<T: Ord>(new_id: &T, concurrent: &[T]) -> InsertPosition {
160 if concurrent.is_empty() {
161 return InsertPosition::Start;
162 }
163
164 // Find where the new ID fits in the sorted order
165 match concurrent.binary_search(new_id) {
166 // Exact match (shouldn't happen with unique IDs)
167 Ok(idx) => InsertPosition::After(idx),
168 // Not found, idx is where it would be inserted
169 Err(idx) => {
170 if idx == 0 {
171 InsertPosition::Start
172 } else if idx >= concurrent.len() {
173 InsertPosition::End
174 } else {
175 InsertPosition::After(idx - 1)
176 }
177 }
178 }
179}
180
181/// Finds the insertion position for a branch among existing branches.
182///

Callers 2

Calls 2

is_emptyMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected