InputRange for data structure without order?

Ali Çehreli acehreli at yahoo.com
Tue Jun 3 20:51:05 UTC 2025


On 6/3/25 9:03 AM, Andy Valencia wrote:
 > I have a Set data structure which has no concept of order; its members
 > are stored and can be searched efficiently based on their hash.
 >
 > I have a situation where chain()'ing them together would be convenient,
 > but InputRange requires front() and popFront(). There really _isn't_ a
 > front

front() does not imply an order. It will work as long as you can provide 
the elements in a sequence. The built-in associative array feature is an 
example:

import std.stdio;
import std.range;

void main() {
     int[int] squares;
     foreach (i; 0 .. 10) {
         squares[i] = i * i;
     }

     writeln("keys  : ", squares.byKey);
     writeln("values: ", squares.byValue);
}

The elements are not in any particular order, yet associative arrays 
provide InputRanges (byKey and byValue return InputRange objects.):

keys  : [0, 6, 7, 2, 3, 1, 8, 5, 4, 9]
values: [0, 36, 49, 4, 9, 1, 64, 25, 16, 81]

Ali



More information about the Digitalmars-d-learn mailing list