-
Notifications
You must be signed in to change notification settings - Fork 7
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
Append Sequence of Renumbered Items #566
Comments
So right now |
Correct. Should it? Most likely yes, because that is consistent with the current |
I'm thinking about this and trying to think of the most efficient algorithm to find an open slot. Poor implementation feels like it be easily O(N^2). I thinking:
This seems like it would be efficient. |
That would work. My first thought was just to add onto the end rather than finding the next available slot. need = set(range(min_num, max_num + step)) # entire range must be available
nums = set(self.numbers)
if need & nums:
oldmax = max(self.numbers)
return range(oldmax, oldmax + max_num - min_num + step, step)
else:
return range(min_num, max_num+step, step) |
Just adding it all onto the end would effectively disregard the user's intent. Also what you proposed would allow something: used_nums = {1,3,5,7}
need = {2,4,6,8} While valid I don't think the user would like that much, probably. |
I agree that adding it onto the end if a match is not found right away would displease users. |
Oh ok. I see why you didn't do |
Agreed. It's very efficient if you try once and then give up. Your solution is much better for always finding the next available slot. |
Is your feature request related to a problem? Please describe.
The method
NumberedDataObjectCollection.append_renumber(step=...)
:Feature request: implement a related method to add a sequence of items with the same starting number and step. Use the next available complete sequence. We could call it something like
NumberedObjectCollection.extend_renumber()
.Describe the solution you'd like
Example: Assume we are building an input deck. We have cells 1, 2, 3, and 100. The next number is 4.
Because there is not room for the 100 new cells on the interval [4, 100), the
Cells
collection adds them at [101, 200].When
step > 1
, use the next uninterrupted range. For instance, if the user saidcells.extend_renumber(new_cells, step=5)
, the sequence should not start at 4, even though it would safely skip over 100. Instead, the sequence should start at either 101 or 105. (Which?)Describe alternatives you've considered
Right now, the user can just iterate over
append_renumber()
. That will work, but doing anextend_renumber()
will be more efficient and will allow for a collection of related objects to be added in a sequence.The user can also figure out the next sequence of available numbers manually and then just call
extend()
.Additional context
This will be useful for practical problems where a range of numbers is available for an addition to a model. If there are enough available numbers in the range, the numbered objects will be added there; but it is more important to keep them together than to put only some in the range.
The text was updated successfully, but these errors were encountered: