') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Rollup merge of #147808 - hkBst:btree-3, r=joboet · rust-lang/rust@4ad5a39 · GitHub
Skip to content

Commit 4ad5a39

Browse files
authored
Rollup merge of #147808 - hkBst:btree-3, r=joboet
btree: cleanup difference, intersection, is_subset
2 parents 7af1ee3 + acd0294 commit 4ad5a39

2 files changed

Lines changed: 87 additions & 94 deletions

File tree

‎library/alloc/src/collections/btree/set.rs‎

Lines changed: 86 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -427,39 +427,35 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
427427
where
428428
T:Ord,
429429
{
430-
let(self_min, self_max) =
431-
iflet(Some(self_min),Some(self_max)) = (self.first(),self.last()){
432-
(self_min, self_max)
433-
}else{
434-
returnDifference{inner:DifferenceInner::Iterate(self.iter())};
435-
};
436-
let(other_min, other_max) =
437-
iflet(Some(other_min),Some(other_max)) = (other.first(), other.last()){
438-
(other_min, other_max)
439-
}else{
440-
returnDifference{inner:DifferenceInner::Iterate(self.iter())};
441-
};
442-
Difference{
443-
inner:match(self_min.cmp(other_max), self_max.cmp(other_min)){
444-
(Greater, _) | (_,Less) => DifferenceInner::Iterate(self.iter()),
445-
(Equal, _) => {
446-
letmut self_iter = self.iter();
447-
self_iter.next();
448-
DifferenceInner::Iterate(self_iter)
449-
}
450-
(_,Equal) => {
451-
letmut self_iter = self.iter();
452-
self_iter.next_back();
453-
DifferenceInner::Iterate(self_iter)
454-
}
455-
_ ifself.len() <= other.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF => {
456-
DifferenceInner::Search{self_iter:self.iter(),other_set: other }
457-
}
458-
_ => DifferenceInner::Stitch{
459-
self_iter:self.iter(),
460-
other_iter: other.iter().peekable(),
430+
ifletSome(self_min) = self.first()
431+
&& letSome(self_max) = self.last()
432+
&& letSome(other_min) = other.first()
433+
&& letSome(other_max) = other.last()
434+
{
435+
Difference{
436+
inner:match(self_min.cmp(other_max), self_max.cmp(other_min)){
437+
(Greater, _) | (_,Less) => DifferenceInner::Iterate(self.iter()),
438+
(Equal, _) => {
439+
letmut self_iter = self.iter();
440+
self_iter.next();
441+
DifferenceInner::Iterate(self_iter)
442+
}
443+
(_,Equal) => {
444+
letmut self_iter = self.iter();
445+
self_iter.next_back();
446+
DifferenceInner::Iterate(self_iter)
447+
}
448+
_ ifself.len() <= other.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF => {
449+
DifferenceInner::Search{self_iter:self.iter(),other_set: other }
450+
}
451+
_ => DifferenceInner::Stitch{
452+
self_iter:self.iter(),
453+
other_iter: other.iter().peekable(),
454+
},
461455
},
462-
},
456+
}
457+
}else{
458+
Difference{inner:DifferenceInner::Iterate(self.iter())}
463459
}
464460
}
465461

@@ -519,31 +515,27 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
519515
where
520516
T:Ord,
521517
{
522-
let(self_min, self_max) =
523-
iflet(Some(self_min),Some(self_max)) = (self.first(),self.last()){
524-
(self_min, self_max)
525-
}else{
526-
returnIntersection{inner:IntersectionInner::Answer(None)};
527-
};
528-
let(other_min, other_max) =
529-
iflet(Some(other_min),Some(other_max)) = (other.first(), other.last()){
530-
(other_min, other_max)
531-
}else{
532-
returnIntersection{inner:IntersectionInner::Answer(None)};
533-
};
534-
Intersection{
535-
inner:match(self_min.cmp(other_max), self_max.cmp(other_min)){
536-
(Greater, _) | (_,Less) => IntersectionInner::Answer(None),
537-
(Equal, _) => IntersectionInner::Answer(Some(self_min)),
538-
(_,Equal) => IntersectionInner::Answer(Some(self_max)),
539-
_ ifself.len() <= other.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF => {
540-
IntersectionInner::Search{small_iter:self.iter(),large_set: other }
541-
}
542-
_ if other.len() <= self.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF => {
543-
IntersectionInner::Search{small_iter: other.iter(),large_set:self}
544-
}
545-
_ => IntersectionInner::Stitch{a:self.iter(),b: other.iter()},
546-
},
518+
ifletSome(self_min) = self.first()
519+
&& letSome(self_max) = self.last()
520+
&& letSome(other_min) = other.first()
521+
&& letSome(other_max) = other.last()
522+
{
523+
Intersection{
524+
inner:match(self_min.cmp(other_max), self_max.cmp(other_min)){
525+
(Greater, _) | (_,Less) => IntersectionInner::Answer(None),
526+
(Equal, _) => IntersectionInner::Answer(Some(self_min)),
527+
(_,Equal) => IntersectionInner::Answer(Some(self_max)),
528+
_ ifself.len() <= other.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF => {
529+
IntersectionInner::Search{small_iter:self.iter(),large_set: other }
530+
}
531+
_ if other.len() <= self.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF => {
532+
IntersectionInner::Search{small_iter: other.iter(),large_set:self}
533+
}
534+
_ => IntersectionInner::Stitch{a:self.iter(),b: other.iter()},
535+
},
536+
}
537+
}else{
538+
Intersection{inner:IntersectionInner::Answer(None)}
547539
}
548540
}
549541

@@ -694,55 +686,56 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
694686
// Same result as self.difference(other).next().is_none()
695687
// but the code below is faster (hugely in some cases).
696688
ifself.len() > other.len(){
697-
returnfalse;
689+
returnfalse;// self has more elements than other
698690
}
699-
let(self_min, self_max) =
700-
iflet(Some(self_min),Some(self_max)) = (self.first(),self.last()){
701-
(self_min, self_max)
702-
}else{
703-
returntrue;// self is empty
704-
};
705-
let(other_min, other_max) =
706-
iflet(Some(other_min),Some(other_max)) = (other.first(), other.last()){
707-
(other_min, other_max)
708-
}else{
709-
returnfalse;// other is empty
710-
};
691+
let(Some(self_min),Some(self_max)) = (self.first(),self.last())else{
692+
returntrue;// self is empty
693+
};
694+
let(Some(other_min),Some(other_max)) = (other.first(), other.last())else{
695+
returnfalse;// other is empty
696+
};
711697
letmut self_iter = self.iter();
712698
match self_min.cmp(other_min){
713-
Less => returnfalse,
699+
Less => returnfalse,// other does not contain self_min
714700
Equal => {
715-
self_iter.next();
701+
self_iter.next();// self_min is contained in other, so remove it from consideration
702+
// other_min is now not in self_iter (used below)
716703
}
717-
Greater => (),
718-
}
704+
Greater => {}// other_min is not in self_iter (used below)
705+
};
706+
719707
match self_max.cmp(other_max){
720-
Greater => returnfalse,
708+
Greater => returnfalse,// other does not contain self_max
721709
Equal => {
722-
self_iter.next_back();
710+
self_iter.next_back();// self_max is contained in other, so remove it from consideration
711+
// other_max is now not in self_iter (used below)
723712
}
724-
Less => (),
725-
}
713+
Less => {}// other_max is not in self_iter (used below)
714+
};
726715
if self_iter.len() <= other.len() / ITER_PERFORMANCE_TIPPING_SIZE_DIFF{
727-
for next in self_iter {
728-
if !other.contains(next){
729-
returnfalse;
730-
}
731-
}
716+
self_iter.all(|e| other.contains(e))
732717
}else{
733718
letmut other_iter = other.iter();
734-
other_iter.next();
735-
other_iter.next_back();
736-
letmut self_next = self_iter.next();
737-
whileletSome(self1) = self_next {
738-
match other_iter.next().map_or(Less, |other1| self1.cmp(other1)){
739-
Less => returnfalse,
740-
Equal => self_next = self_iter.next(),
741-
Greater => (),
742-
}
719+
{
720+
// remove other_min and other_max as they are not in self_iter (see above)
721+
other_iter.next();
722+
other_iter.next_back();
743723
}
724+
// custom `self_iter.all(|e| other.contains(e))`
725+
self_iter.all(|self1| {
726+
whileletSome(other1) = other_iter.next(){
727+
match other1.cmp(self1){
728+
// happens up to `ITER_PERFORMANCE_TIPPING_SIZE_DIFF * self.len() - 1` times
729+
Less => continue,// skip over elements that are smaller
730+
// happens `self.len()` times
731+
Equal => returntrue,// self1 is in other
732+
// happens only once
733+
Greater => returnfalse,// self1 is not in other
734+
}
735+
}
736+
false
737+
})
744738
}
745-
true
746739
}
747740

748741
/// Returns `true` if the set is a superset of another,

‎library/alloctests/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repository = "https://github.com/rust-lang/rust.git"
66
description = "Tests for the Rust Allocation Library"
77
autotests = false
88
autobenches = false
9-
edition = "2021"
9+
edition = "2024"
1010

1111
[lib]
1212
path = "lib.rs"

0 commit comments

Comments
 (0)