-
Notifications
You must be signed in to change notification settings - Fork 351
-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why IndexedSet not update the index of items? #240
Comments
Hi Urahara, Can you paste the stack trace, and maybe a reproduction of the problem? I never rule out the possibility of bugs, but IndexedSet updates the index in the code you've linked. |
@mahmoud Of course! Here we go: from boltons.setutils import IndexedSet
original_list = []
for i in range(0, 8):
original_list.append(i)
indexed_list = IndexedSet()
for i in original_list:
key = "key_{}".format(i)
indexed_list.add(key)
for i in original_list:
key = "key_{}".format(i)
index = indexed_list.index(key)
indexed_list.pop(index) Exception:
For what i see this kind of error begin ocurrs after I alter def pop(self, index=None):
"pop(index) -> remove the item at a given index (-1 by default)"
item_index_map = self.item_index_map
len_self = len(item_index_map)
if index is None or index == -1 or index == len_self - 1:
ret = self.item_list.pop()
del item_index_map[ret]
else:
real_index = self._get_real_index(index)
ret = self.item_list[real_index]
self.item_list.pop(real_index)
del item_index_map[ret]
for idx, item in enumerate(self.item_list): # recalculating index
item_index_map[item] = idx
#self._add_dead(real_index)
self._cull()
return ret And the error go away and code works like expected, but i need to know the reason of the existence of |
Ah, I had understood your original report to be about The short answer is that for performance reasons, There may be an issue in keeping these in sync with the "list" portion of the API ( If you'd like to provide a PR which maintains the amortized behavior, I'd definitely consider and appreciate it. :) |
At first i was using @mahmoud Do you have any suggestion how can i implement this without compromise the perfomance? |
I went ahead and did it, by adding an internal method As far as performance, the amortized culling mechanisms throughout the rest of the API maintain the basic time complexity, so I didn't get too fancy. If you run into performance troubles for the core APIs, we can perhaps optimize further. Thanks again for the excellent report! |
@mahmoud Hi! Thanks, i upgraded my module and i find another bug, now i get negative indexes and exceptions too: from boltons.setutils import IndexedSet
original_list = []
for i in range(0, 10):
original_list.append(i)
indexed_list = IndexedSet()
for i in original_list:
key = "key_{}".format(i)
indexed_list.add(key)
for i in original_list:
if i > 2:
key = "key_{}".format(i)
index = indexed_list.index(key)
print(index)
indexed_list.pop(index) Result:
This fix is only when i pop items in order? |
Oh my goodness, thanks for circling back and rechecking! I added more tests (including that one), and caught/fixed the bug. If you want to test on master, it's there now. |
I get the same error. :( I get the file from Also: i found another user case: from setutils import IndexedSet
original_list = []
for i in range(32):
original_list.append(i)
indexed_list = IndexedSet()
for i in original_list:
key = "key_{}".format(i)
indexed_list.add(key)
for i in original_list:
if i % 3:
key = "key_{}".format(i)
index = indexed_list.index(key)
print(index)
indexed_list.pop(index) Thanks for quickly feedback! |
Ah, yes, I saw the problem with this one pretty quick. Test added and new fix pushed. Keep those tests coming, I say! :) |
Now i think is 100%, thanks again man! Great work! :) |
Wouldn't be 100% without your diligence, huge thanks! I'll get this released ASAP. |
What is the motivation of not updating the current index of items when they are removed? It's this a bug? Because i starting using IndexedSet to pop items from a list that i have and i start get index of range errors.
boltons/boltons/setutils.py
Lines 201 to 209 in bf8a659
The text was updated successfully, but these errors were encountered: