build: add shims for nightly feature map_first_last

This commit is contained in:
Waleed Khan 2022-02-20 21:14:48 -08:00
parent 325eb4f4ca
commit 261cd1a1c4
5 changed files with 75 additions and 10 deletions

View file

@ -19,6 +19,8 @@ use std::ops::Range;
use itertools::Itertools;
use crate::nightly_shims::BTreeMapExt;
pub fn find_line_ranges(text: &[u8]) -> Vec<Range<usize>> {
let mut ranges = vec![];
let mut start = 0;
@ -175,7 +177,7 @@ pub(crate) fn unchanged_ranges(
let max_occurrences = 100;
let mut left_histogram = Histogram::calculate(left, left_ranges, max_occurrences);
if *left_histogram.count_to_words.first_entry().unwrap().key() > max_occurrences {
if *left_histogram.count_to_words.ext_first_key().unwrap() > max_occurrences {
// If there are very many occurrences of all words, then we just give up.
return vec![];
}
@ -185,7 +187,7 @@ pub(crate) fn unchanged_ranges(
// the LCS.
let mut uncommon_shared_words = vec![];
while !left_histogram.count_to_words.is_empty() && uncommon_shared_words.is_empty() {
let left_words = left_histogram.count_to_words.pop_first().unwrap().1;
let left_words = left_histogram.count_to_words.ext_pop_first_value().unwrap();
for left_word in left_words {
if right_histogram.word_to_positions.contains_key(left_word) {
uncommon_shared_words.push(left_word);

View file

@ -34,6 +34,7 @@ use thiserror::Error;
use crate::backend::{ChangeId, CommitId};
use crate::commit::Commit;
use crate::file_util::persist_content_addressed_temp_file;
use crate::nightly_shims::BTreeSetExt;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub struct IndexPosition(u32);
@ -812,25 +813,25 @@ impl<'a> CompositeIndex<'a> {
let mut result = BTreeSet::new();
while !(items1.is_empty() || items2.is_empty()) {
let entry1 = items1.last().unwrap();
let entry2 = items2.last().unwrap();
let entry1 = items1.ext_last().unwrap();
let entry2 = items2.ext_last().unwrap();
match entry1.cmp(entry2) {
Ordering::Greater => {
let entry1 = items1.pop_last().unwrap();
let entry1 = items1.ext_pop_last().unwrap();
for parent_entry in entry1.0.parents() {
items1.insert(IndexEntryByGeneration(parent_entry));
}
}
Ordering::Less => {
let entry2 = items2.pop_last().unwrap();
let entry2 = items2.ext_pop_last().unwrap();
for parent_entry in entry2.0.parents() {
items2.insert(IndexEntryByGeneration(parent_entry));
}
}
Ordering::Equal => {
result.insert(entry1.0.pos);
items1.pop_last();
items2.pop_last();
items1.ext_pop_last();
items2.ext_pop_last();
}
}
}

View file

@ -13,7 +13,6 @@
// limitations under the License.
#![feature(assert_matches)]
#![feature(map_first_last)]
#![deny(unused_must_use)]
#[macro_use]
@ -39,6 +38,7 @@ pub mod index_store;
pub mod local_backend;
pub mod lock;
pub mod matchers;
pub mod nightly_shims;
pub mod op_heads_store;
pub mod op_store;
pub mod operation;

61
lib/src/nightly_shims.rs Normal file
View file

@ -0,0 +1,61 @@
use std::collections::{BTreeMap, BTreeSet};
pub trait BTreeMapExt<K, V> {
fn ext_first_key(&self) -> Option<&K>;
fn ext_last_key(&self) -> Option<&K>;
fn ext_pop_first_key(&mut self) -> Option<K>;
fn ext_pop_last_key(&mut self) -> Option<K>;
fn ext_pop_first_value(&mut self) -> Option<V>;
fn ext_pop_last_value(&mut self) -> Option<V>;
}
impl<K: Ord + Clone, V> BTreeMapExt<K, V> for BTreeMap<K, V> {
fn ext_first_key(&self) -> Option<&K> {
self.keys().next()
}
fn ext_last_key(&self) -> Option<&K> {
self.keys().next_back()
}
fn ext_pop_first_key(&mut self) -> Option<K> {
let key = self.ext_first_key()?;
let key = key.clone(); // ownership hack
Some(self.remove_entry(&key).unwrap().0)
}
fn ext_pop_last_key(&mut self) -> Option<K> {
let key = self.ext_last_key()?;
let key = key.clone(); // ownership hack
Some(self.remove_entry(&key).unwrap().0)
}
fn ext_pop_first_value(&mut self) -> Option<V> {
let key = self.ext_first_key()?;
let key = key.clone(); // ownership hack
Some(self.remove_entry(&key).unwrap().1)
}
fn ext_pop_last_value(&mut self) -> Option<V> {
let key = self.ext_last_key()?;
let key = key.clone(); // ownership hack
Some(self.remove_entry(&key).unwrap().1)
}
}
pub trait BTreeSetExt<K> {
fn ext_last(&self) -> Option<&K>;
fn ext_pop_last(&mut self) -> Option<K>;
}
impl<K: Ord + Clone> BTreeSetExt<K> for BTreeSet<K> {
fn ext_last(&self) -> Option<&K> {
self.iter().next_back()
}
fn ext_pop_last(&mut self) -> Option<K> {
let key = self.ext_last()?;
let key = key.clone(); // ownership hack
self.take(&key)
}
}

View file

@ -16,6 +16,7 @@ use std::cmp::min;
use std::collections::{BTreeMap, HashSet};
use crate::index::{IndexEntry, IndexPosition};
use crate::nightly_shims::BTreeMapExt;
use crate::revset::RevsetIterator;
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
@ -149,7 +150,7 @@ impl<'revset, 'repo> RevsetGraphIterator<'revset, 'repo> {
}
fn next_index_entry(&mut self) -> Option<IndexEntry<'repo>> {
if let Some((_, index_entry)) = self.look_ahead.pop_last() {
if let Some(index_entry) = self.look_ahead.ext_pop_last_value() {
return Some(index_entry);
}
self.input_set_iter.next()