Skip to content

Conversation

@Anda275
Copy link

@Anda275 Anda275 commented Jan 2, 2024

This approach uses a HashMap to create an inverse mapping between the index of an element and the corresponding row in a list. When you want to set a selected element by index, you can directly use the reverse map to find the corresponding row without traversing the entire list.

///
/// ```ignore
/// table.set_on_sort(|siv: &mut Cursive, column: BasicColumn, order: Ordering| {
/// ```rust

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why these are modified?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that one of the tests ignored from the beginning was solved and I modified it to solve this one as well

Comment on lines +909 to +914
let reverse_mapping: HashMap<usize, usize> = self.rows_to_items.iter().enumerate().map(|(row, &item)| (item, row)).collect();

// Use the reverse mapping to find the row corresponding to the item_index
if let Some(&row) = reverse_mapping.get(&item_index) {
self.focus = row;
self.scroll_core.scroll_to_y(row);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless you always keep the hasmap up to date when adding and deleting items, this is slower than the original code. It does one O(n) traversal to build the hashmap, and for each item an O(log n) to insert into the hasmap. It does another O(log n) to retrieve the item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants