Skip to content
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

feat: allow making a weighted container ordered #320

Draft
wants to merge 2 commits into
base: development
Choose a base branch
from

Conversation

LordMidas
Copy link
Member

@LordMidas LordMidas commented Oct 7, 2023

The primary purpose is to allow a foreach (item in container) type iteration and for loop iteration with indices instead of the current foreach (item, weight in container). This helps the container to be used interchangeably with arrays without having to do a typeof check and duplicate foreach 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 in for loops with using i 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.

The primary purpose is to allow a `foreach (item in container)` type iteration instead of the standard `foreach (item, weight in container)`. This helps the container to be used interchangeably with arrays without having to do a typeof check and duplicate foreach loops separately for arrays and weighted containers.
@LordMidas LordMidas added this to the 1.3.0 milestone Oct 7, 2023
@LordMidas LordMidas requested review from TaroEld and Enduriel October 7, 2023 01:54
@Enduriel
Copy link
Member

We discussed and this is cool but as you said it should be a sibling of WeightedContainer and seems like it requires still quite a bit of work

@Enduriel Enduriel modified the milestones: 1.3.0, 1.4.0 Oct 19, 2023
@Suor
Copy link
Contributor

Suor commented Sep 2, 2024

If you don't really care about order but just want to iterate over index, item instead of index, weight then you may just add an iterator method to it:

class WeightedContainer {
    ...    
    function items() {
        foreach (k, _ in this.Table) yield k;
    }
}

// Use
local items = type cont == "array" ? cont : cont.items();
foreach (item in items) {
    // ... do stuff ...
}

Unfortunately typeof/items cannot be removed completely because WeightedContainer already has a way to iterate. Can pass cont.items() to functions waiting for arrays in many cases though.

@Suor
Copy link
Contributor

Suor commented Sep 2, 2024

Another approach is to provide external to WeightedContainer function, which will know what to do:

// Util.iter() supposed to know what "items" mean for different types of containers,
// i.e. typeof and iterator will be hidden there.
foreach (item in Util.iter(cont)) {
    // ... do stuff ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants