I am writing a method that does some manipulation on a List. During each iteration, the algorithm will either delete, add or move an existing item (so the list is changing, one item at a time). Move operation will be performed using an extension method that is implemented with the following logic:
T item = base[oldIndex];
base.RemoveItem(oldIndex);
base.InsertItem(newIndex, item);
Also, during each iteration, one item in the list will be queried for its current index. List.IndexOf(item) is an expensive operation (O(n)), and I wonder if there is a way to improve the performance of this operation. I thought about keeping a dictionary of the indices, but then on every insertion and removal of an item I will need to update the indices of O(n) items the come after it, so that does not help.
Note: I am ware that List.InsertItem and List.RemoveItem take O(n) anyway, but I am specifically asking about how to improve the performance of the IndexOf operation in such a scenario.
Thanks