feat: allow making a weighted container ordered #320
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The primary purpose is to allow a
foreach (item in container)
type iteration andfor
loop iteration with indices instead of the currentforeach (item, weight in container)
. This helps the container to be used interchangeably with arrays without having to do atypeof
check and duplicateforeach
loops separately for arrays and weighted containers.I ran into this issue while coding the Dynamic Spawns Framework whereby the same member variable in a class could be an array or a weighted container. In many functions I want to iterate over that variable in
foreach
loops and sometimes infor
loops with usingi
as index while being agnostic to its type. Having the possibility of an ordered weighted container allows me to set it as ordered during creation, and then the class can do its thing while not worrying about it.While, in theory, we could implement a child class of WeightedContainer that is ordered (I did that in Dynamic Spawns and called it WeightedArray) - but that violates the Liskov Substitution Principle, which is why I'd like to have a basic implementation of this in the base class.
Alternatively, we implement a base abstract class and then inherit WeightedContainer from it and WeightedArray from it separately. Because ideally I'd like the WeightedArray to support all kinds of array functions e.g.
pop()
,push()
etc.