Using sets in associative arrays

Sergey kornburn at yandex.ru
Tue Nov 12 07:47:58 UTC 2024


On Tuesday, 12 November 2024 at 00:42:41 UTC, Ralph wrote:
> Hi there,
>
> I know it is possible to declare an associative array which 
> takes arrays as values, i.e.
> **int[][int] arr;**
> A sample of this could be
> **arr[2] = [2, 3, 2, 4, 5];**
>
> I was wondering if there was a way to have this with sets 
> instead of arrays. In D, sets are equivalent to red-black 
> trees, afaik. Almost as expected, the declaration
> **redBlackTree[int] arr;**
> such that (according to the initial example) **arr[2]** would 
> be [2, 3, 4, 5]
> does not work.
>
> Would anybody know of a workaround?

D's std container situation is quite bad. redBlackTree is one 
workaround about sets for sure.
Another way to simulate it - is using another AA with bool/void 
parameters.
```d
bool[int][int] dict_of_sets;
dict_of_sets[1] = [1:true, 2:true, 3:true];
dict_of_sets[1][4] = true;
writeln(dict_of_sets); // [1:[4:true, 3:true, 2:true, 1:true]]
```


More information about the Digitalmars-d-learn mailing list