Skip to content

Commit c1f3ca5

Browse files
committed
Sorting used item by stable id.
1 parent 64b88f0 commit c1f3ca5

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

crates/cairo-lang-semantic/src/items/imp.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
2323
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
2424
use cairo_lang_utils::{Intern, LookupIntern, define_short_id, extract_matches};
2525
use itertools::{Itertools, chain, izip};
26+
use salsa::InternKey;
2627
use smol_str::SmolStr;
2728
use syntax::attribute::structured::{Attribute, AttributeListStructurize};
2829
use syntax::node::ast::{self, GenericArg, ImplItem, MaybeImplBody, OptionReturnTypeClause};
@@ -1064,6 +1065,7 @@ pub fn impl_all_used_items(
10641065
all_used_items.extend(resolver_data.used_items.iter().cloned());
10651066
}
10661067
}
1068+
all_used_items.sort_by_cached_key(|k| k.stable_location(db).stable_ptr().as_intern_id());
10671069
Ok(all_used_items.into())
10681070
}
10691071

crates/cairo-lang-semantic/src/items/module.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use cairo_lang_syntax::node::ast;
1010
use cairo_lang_syntax::node::helpers::UsePathEx;
1111
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
1212
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
13+
use salsa::InternKey;
1314
use smol_str::SmolStr;
1415

1516
use super::feature_kind::FeatureKind;
@@ -173,6 +174,7 @@ pub fn module_all_used_items(
173174
}
174175
}
175176
}
177+
all_used_items.sort_by_cached_key(|k| k.stable_location(db).stable_ptr().as_intern_id());
176178
Ok(all_used_items.into())
177179
}
178180

crates/cairo-lang-semantic/src/items/trt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
1616
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
1717
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
1818
use cairo_lang_utils::{Intern, LookupIntern, define_short_id};
19+
use salsa::InternKey;
1920
use smol_str::SmolStr;
2021

2122
use super::TraitOrImplContext;
@@ -586,6 +587,7 @@ pub fn trait_all_used_items(
586587
all_used_items.extend(resolver_data.used_items.iter().cloned());
587588
}
588589
}
590+
all_used_items.sort_by_cached_key(|k| k.stable_location(db).stable_ptr().as_intern_id());
589591
Ok(all_used_items.into())
590592
}
591593

crates/cairo-lang-utils/src/ordered_hash_set.rs

+19
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ impl<Key: Hash + Eq, BH: BuildHasher> OrderedHashSet<Key, BH> {
115115
) -> indexmap::set::Union<'a, Key, BH> {
116116
self.0.union(&other.0)
117117
}
118+
119+
/// Sort the set’s values in place using the comparison function `cmp`.
120+
///
121+
/// Computes in **O(n log n)** time and **O(n)** space. The sort is stable.
122+
pub fn sort_by(&mut self, cmp: impl FnMut(&Key, &Key) -> std::cmp::Ordering) {
123+
self.0.sort_by(cmp);
124+
}
125+
126+
/// Sort the set’s values in place using a key extraction function.
127+
///
128+
/// During sorting, the function is called at most once per entry, by using temporary storage
129+
/// to remember the results of its evaluation. The order of calls to the function is
130+
/// unspecified and may change between versions of `indexmap` or the standard library.
131+
///
132+
/// Computes in **O(m n + n log n + c)** time () and **O(n)** space, where the function is
133+
/// **O(m)**, *n* is the length of the map, and *c* the capacity. The sort is stable.
134+
pub fn sort_by_cached_key<K: Ord>(&mut self, sort_key: impl FnMut(&Key) -> K) {
135+
self.0.sort_by_cached_key(sort_key);
136+
}
118137
}
119138

120139
impl<Key, BH> IntoIterator for OrderedHashSet<Key, BH> {

0 commit comments

Comments
 (0)