Names: a new feature's and input's number found without searching the design per number (#6377)
A new feature is named after the first free number of its stem (Cylinder1, Cylinder2 …), and every number tried searched all features' names: the thousandth cylinder of a design tried a thousand names against a thousand features. A new input's dN name likewise wrote out every d1, d2 … it tried. Both now gather the numbers taken after their stem once (params::numbers_after, which counts only names written as the number is) and take the first number not among them. The names are the same. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3122,14 +3122,11 @@ impl Document {
|
||||
}
|
||||
|
||||
fn unique_name(&self, base: &str) -> String {
|
||||
let mut n = 1;
|
||||
loop {
|
||||
let name = format!("{base}{n}");
|
||||
if !self.features.iter().any(|f| f.name == name) {
|
||||
return name;
|
||||
}
|
||||
n += 1;
|
||||
}
|
||||
// The numbers taken after `base`, gathered once: the thousandth cylinder of a design
|
||||
// tried Cylinder1, Cylinder2 … each against every feature's name.
|
||||
let taken = crate::params::numbers_after(base, self.features.iter().map(|f| f.name.as_str()));
|
||||
let n = (1..).find(|n| !taken.contains(n)).unwrap_or(1);
|
||||
format!("{base}{n}")
|
||||
}
|
||||
|
||||
/// The deepest timeline folder spanning the insertion slot between `marker - 1` and
|
||||
|
||||
+16
-10
@@ -136,16 +136,12 @@ impl Document {
|
||||
pub fn next_d_name(&self) -> String {
|
||||
// Every taken name at once: asking `name_taken` for d1, d2, … searched the whole
|
||||
// design once per number, quadratic in the number of named inputs.
|
||||
let taken: std::collections::HashSet<&str> =
|
||||
self.params.iter().map(|p| p.name.as_str()).chain(self.features.iter().flat_map(|f| f.param_names.iter().map(String::as_str))).collect();
|
||||
let mut n = 1;
|
||||
loop {
|
||||
let c = format!("d{n}");
|
||||
if !taken.contains(c.as_str()) {
|
||||
return c;
|
||||
}
|
||||
n += 1;
|
||||
}
|
||||
// Their numbers, so trying d1, d2 … writes no names: a design with thousands of named
|
||||
// inputs wrote that many for every new one.
|
||||
let names = self.params.iter().map(|p| p.name.as_str()).chain(self.features.iter().flat_map(|f| f.param_names.iter().map(String::as_str)));
|
||||
let taken = numbers_after("d", names);
|
||||
let n = (1..).find(|n| !taken.contains(n)).unwrap_or(1);
|
||||
format!("d{n}")
|
||||
}
|
||||
|
||||
/// Give a feature's inputs parameter names (keeping existing names in place).
|
||||
@@ -1016,3 +1012,13 @@ pub fn cycles(list: &[(String, String, Kind)]) -> BTreeSet<String> {
|
||||
}
|
||||
bad
|
||||
}
|
||||
|
||||
/// The numbers `n` for which `{base}{n}` is among `names` (written as `n` is: no sign or
|
||||
/// leading zeros).
|
||||
pub(crate) fn numbers_after<'a>(base: &str, names: impl Iterator<Item = &'a str>) -> std::collections::HashSet<u64> {
|
||||
names
|
||||
.filter_map(|name| name.strip_prefix(base))
|
||||
.filter(|digits| !digits.is_empty() && !digits.starts_with('0') && digits.bytes().all(|b| b.is_ascii_digit()))
|
||||
.filter_map(|digits| digits.parse().ok())
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -469,3 +469,26 @@ fn self_referencing_construction_planes_are_errors() {
|
||||
m.evaluate(&doc);
|
||||
assert!(m.results.iter().any(|r| r.error.is_some()), "a cyclic perpendicular plane must report an error: {:?}", m.results);
|
||||
}
|
||||
|
||||
/// New names take the first free number after their stem, whatever else is named alike: a gap
|
||||
/// is filled, and `Sketch07`, `Sketch+2` or `SketchLid` take no number (they are not `Sketch7`).
|
||||
#[test]
|
||||
fn new_names_take_the_first_free_number() {
|
||||
let taken = crate::params::numbers_after("Box", ["Box1", "Box3", "Box07", "Box+2", "BoxLid", "Box", "Cylinder2"].into_iter());
|
||||
assert_eq!(taken, std::collections::HashSet::from([1, 3]));
|
||||
let mut doc = Document::new("names");
|
||||
let sketch = |doc: &mut Document| {
|
||||
doc.add_feature(FeatureKind::Sketch { plane: PlaneRef::Origin { name: "XY".into() }, sketch: Sketch::new() }, None).unwrap()
|
||||
};
|
||||
let name = |doc: &Document, id: u64| doc.feature(id).map(|f| f.name.clone()).unwrap_or_default();
|
||||
let ids: Vec<u64> = (0..3).map(|_| sketch(&mut doc)).collect();
|
||||
let names: Vec<String> = ids.iter().map(|id| name(&doc, *id)).collect();
|
||||
assert!(names.iter().all(|n| n.starts_with("Sketch")) && names[0] != names[1] && names[1] != names[2], "{names:?}");
|
||||
for (id, other) in [(ids[0], "SketchLid"), (ids[1], "Sketch07")] {
|
||||
if let Some(f) = doc.feature_mut(id) {
|
||||
f.name = other.into();
|
||||
}
|
||||
}
|
||||
let next = sketch(&mut doc);
|
||||
assert_eq!(name(&doc, next), names[0], "the first freed number is taken again");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user