InputRange for data structure without order?

Andy Valencia dont at spam.me
Wed Jun 4 20:53:31 UTC 2025


On Tuesday, 3 June 2025 at 16:03:15 UTC, Andy Valencia wrote:
> I have a situation where chain()'ing them together would be 
> convenient, but InputRange requires front() and popFront().

As an exercise in chaining opApply based containers, I tried:

```d
// Chain across containers; their type is T and what they hold is 
type B
class Chain(T,B) {
     private T[] members;
     this(T...)(T args) {
         foreach(m; args) {
             this.members ~= *m;
         }
     }
     int
     opApply(int delegate(ref B) ops) const {
         int res = 0;
         foreach(m; this.members) {
             foreach(val; m) {
                 res = ops(val);
                 if (res) {
                     break;
                 }
             }
         }
         return res;
     }
}
```

I'm a little iffy on the walk of the args and the *m deref.  But 
it tests out OK in practice.  Using one looks like:

```d
     import tiny.set : Set;
     alias SetInt = Set!int;

     auto s1 = new SetInt();
     s1.add(1);
     s1.add(2);
     s1.add(3);
     auto s2 = new SetInt();
     s2.add(4);
     s2.add(5);
     s2.add(6);
     auto s3 = new SetInt();
     foreach(x; new Chain!(SetInt,int)(s1, s2)) {
         s3.add(x);
     }
```

Andy



More information about the Digitalmars-d-learn mailing list