Can you simplify nested Indexed types?
Steven Schveighoffer
schveiguy at gmail.com
Tue Dec 27 16:06:43 UTC 2022
On 12/27/22 10:31 AM, Sergei Nosov wrote:
> On Tuesday, 27 December 2022 at 15:20:24 UTC, Salih Dincer wrote:
>> On Tuesday, 27 December 2022 at 15:09:11 UTC, Sergei Nosov wrote:
>>> Consider, I have the following code:
>>>
>>> ```d
>>> auto a = [3, 6, 2, 1, 5, 4, 0];
>>>
>>> auto indicies = iota(3);
>>> auto ai = indexed(a, indicies);
>>> //ai = indexed(ai, iota(2));
>>>
>>> writeln(ai);
>>> ```
>>
>> I confuse about comment line that I mark...
>>
>> SDB at 79
>
> Not sure I'll be more helpful, but I'll try to add more details.
>
> I have an array and I use `indexed` on it. Conceptually, I now have a
> second array, but it doesn't exist in memory explicitly - only a
> function to map indicies from "second array" to "first array" is stored;
> all the values are stored once - in the "first array".
>
> Now, I want to have third array that will do the same trick with the
> second array. The problem is that the second array is not really an
> array (but, conceptually, it is an array with random access). If I
> create a new variable with `auto` as type - obviously, it works. But can
> I use the same variable I used to store the "second array"? (In the
> provided code that doesn't work because of the type mismatch).
So the short answer is no.
The long of it is that Indexed is using a source range to give it
indexes. In order to save it as the same type, you need to resolve the
source of the indexes, when indiexed with the new source range, into the
same type, which is not something the library can do. It would have to
be specialized to recognize it's using iota as the index range, and then
transform the iotas into one iota call. Which isn't impossible, but
would be something specialized to this problem.
What could be an answer is to have a function that takes 2 iotas, and
provides the values as if you were applying the indexes of one to the
other, something like:
```d
iota!T translate(T)(iota!T orig, iota!T mapping)
{
// you write this part
}
```
Then you can possibly define a function that can convert it properly.
-Steve
More information about the Digitalmars-d-learn
mailing list