MCPcopy Create free account
hub / github.com/cosdata/cosdata / take_j_smallest

Function take_j_smallest

src/models/lru_cache.rs:366–390  ·  view source on GitHub ↗
(mut arr: Vec<(K, u32)>, j: usize)

Source from the content-addressed store, hash-verified

364}
365
366fn take_j_smallest<K>(mut arr: Vec<(K, u32)>, j: usize) -> Vec<(K, u32)> {
367 if j == 0 {
368 return Vec::new();
369 }
370 if j >= arr.len() {
371 return arr;
372 }
373
374 // Find the j-th smallest value
375 let (_, &mut (_, threshold), _) =
376 arr.select_nth_unstable_by_key(j - 1, |(_, counter)| *counter);
377
378 // Keep exactly j elements that are <= threshold
379 let mut remaining = j;
380 arr.retain(|&(_, val)| {
381 if remaining > 0 && val <= threshold {
382 remaining -= 1;
383 true // keep it
384 } else {
385 false // drop it
386 }
387 });
388
389 arr
390}
391
392pub struct Values<'a, K: 'a, V: 'a> {
393 iter: dashmap::iter::Iter<'a, K, (V, u32), std::hash::RandomState, DashMap<K, (V, u32)>>,

Callers 1

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected