From 122881858837705261cd4dacca58b777afe6bcd7 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 10 Dec 2023 15:31:57 -0800 Subject: [PATCH] work --- rust/src/cooked.rs | 2 +- rust/src/dictionary.rs | 80 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/rust/src/cooked.rs b/rust/src/cooked.rs index be1b4c4b71..757e770520 100644 --- a/rust/src/cooked.rs +++ b/rust/src/cooked.rs @@ -834,7 +834,7 @@ impl WarnOnError for Result { #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Value { Number(Option>), - String(String), + String(Box), } impl Value { diff --git a/rust/src/dictionary.rs b/rust/src/dictionary.rs index fd408d7725..7e98575c5f 100644 --- a/rust/src/dictionary.rs +++ b/rust/src/dictionary.rs @@ -1,6 +1,7 @@ use std::{ collections::{HashMap, HashSet}, fmt::Debug, + ops::{Bound, RangeBounds}, }; use encoding_rs::Encoding; @@ -49,16 +50,77 @@ impl Dictionary { } } - pub fn delete_vars(&mut self, start: DictIndex, count: usize) { - self.update_dict_indexes(&|index| { - if index < start { - Some(index) - } else if index < start + count { - None - } else { - Some(index - count) + pub fn reorder_var(&mut self, from_index: DictIndex, to_index: DictIndex) { + if from_index != to_index { + self.variables.move_index(from_index, to_index); + self.update_dict_indexes(&|index| { + if index == from_index { + Some(to_index) + } else if from_index < to_index { + if index > from_index && index <= to_index { + Some(index - 1) + } else { + Some(index) + } + } else { + if index >= to_index && index < from_index { + Some(index + 1) + } else { + Some(index) + } + } + }) + } + } + + pub fn retain_vars(&mut self, keep: F) + where + F: Fn(&Variable) -> bool, + { + let mut deleted = Vec::new(); + let mut index = 0; + self.variables.retain(|var_by_id| { + let keep = keep(&var_by_id.0); + if !keep { + deleted.push(index); } - }) + index += 1; + keep + }); + if !deleted.is_empty() { + self.update_dict_indexes(&|index| match deleted.binary_search(&index) { + Ok(_) => None, + Err(position) => Some(position), + }) + } + } + + pub fn delete_vars(&mut self, range: R) + where + R: RangeBounds, + { + let start = match range.start_bound() { + Bound::Included(&start) => start, + Bound::Excluded(&start) => start + 1, + Bound::Unbounded => 0, + }; + let end = match range.end_bound() { + Bound::Included(&end) => end + 1, + Bound::Excluded(&end) => end, + Bound::Unbounded => self.variables.len(), + }; + if end > start { + self.variables.drain(start..end); + self.update_dict_indexes(&|index| { + if index < start { + Some(index) + } else if index < end { + None + } else { + Some(index - end - start) + } + }) + } } fn update_dict_indexes(&mut self, f: &F) -- 2.30.2