#Update Vec<T> in place by comparing two Vec<U> (of possibly different lengths)
63 messages ยท Page 1 of 1 (latest)
And I assume you don't want to recreate a new T if you already have one?
data and old have the same lengths, right?
How I would do it:
- Consume
dataandoldto create aHashMap<U, T>. (I assume that there aren't duplicateUs.) - For each element in
new, check if it's in the hashmap. If it is, grab the corresponding value. Otherwise, compute the corresponding value.
Then create a HashMap<&U, T> instead, consuming only data, and borrowing old.
Oh, hmm...
You can use .drain() to consume the contents of the vec and leave behind an empty vec
Basically, what I'm proposing is that you remove all elements from data first, and then completely recompute it.
Well, you remove the elements from data, put it in a HashMap<U, T>. And then, before recomputing each element, check the hashmap to see if there's existing data already.
Do this, except instead of consuming data, you take ownership of the elements of data.
But can you check whether they're equal?
Compare two Us
Then use a HashMap<TypeId, T>. Would that work?
How do you know if they've swapped order in old vs new, then?
Or do you just assume that they're in the same order?
Suppose we have widget 1 and widget 2, and they have the same type.
In old, they're ordered as widget 1 then widget 2.
In new, they're ordered as widget 2 then widget 1.
How do you check whether this is the case?
Is this correct: The order of stuff in data initially match the order of stuff in old. When your processing is done, you have to change data so that the order of stuff in data matches the order of stuff in new
I'm confused ๐
Can there be two views of the same type in old? What about in new?
My question is: How do you tell apart a blue button from a red button?
What happens if:
(view) old: red button, blue button
(view) new: blue button but with a different radius, exact same red button
(widget) data: button widget 1, button widget 2
Do you mean they switch positions?
What happens if:
(view) old: red button, blue button
(view) new: red button but it got modified to be blue, blue button but it got modified to be red
(widget) data: button widget 1, button widget 2
Oh wait.
Ok, so lemme try to restate what I think is your problem statement.
Each view has a type. Multiple views can have the same type. Given a view, we can produce a widget with the same type.
You have a list of views (new). You want to compute a list of corresponding widgets. However, you want to reuse existing widgets as much as possible. You can only reuse a widget if it has the correct type.
Is the correct?
ok, close enough
So..... use a HashMap<TypeId, Vec<WidgetSomething>>
I thought you don't care about the order between widgets of the same type...
You iterate through new from left to right, populating data one by one
I think it's the simplest way.
Otherwise you'd have to do edit distance computation stuff, which is way more complicated.
If you don't want to use a "recompute the thing" approach, you need to deal with stuff like this https://www.geeksforgeeks.org/edit-distance-dp-5/
How do you currently do this? I don't understand what you mean by "a regular indexed for loop"?
I don't understand how it works...
Uhhh how does it work?
Could you describe in english, instead of code?
No. Which part is new?
You're zipping 3 things together and calling it a pair?
What does your code do if you have:
old: button, text
new: text, button
I assume it recreates both widgets?
Does it reuse the existing widgets?
Oh. My understanding of the problem statement was that you have to reuse the existing widgets in this case.
My "rebuild the thing" approach will reuse both widgets, swapping them.
I thought you wanted to reuse the second widget as the first widget, and reuse the first widget as the second widget.
That's why I went for a complicated method.
I don't understand the problem at all ๐
What are you trying to do? I'm confused.
Can't you just do:
data.iter_mut().zip(&old).zip_longest(&new) and then just mutate it right in the loop?
I'm so confused
Actually no, using indexes would be a lot easier.
Just loop with indexes I guess?
Why do you need to remove elements?
That's inefficient since removing and inserting will have to shift over all the remaining elements and then shift it back.
You should probably just assign (overwrite) the old value with the new value
I suggested the hashmap approach because I completely misunderstood what you're trying to do ๐
I think using indexes is probably the easiest method.
If you need to replace something in the middle of a Vec, I think you can just do my_vec[i] = thing, right? (Instead of removing then inserting.)
Wait what?
I thought you said that you can only reuse a widget if it's in the same position.
If you delete the first thing, then all the remaining things aren't in the same position any more.
I'm confused ๐ตโ๐ซ
Anyway, do you still have questions? ๐