Can std.variant be used with std.container.rbtree?

Vijay Nayar madric at gmail.com
Sat Apr 2 10:30:30 UTC 2022


On Saturday, 2 April 2022 at 10:03:19 UTC, JG wrote:
> You need an order on the elements in a red black tree. Am I 
> correct in thinking you want a container of the form given a 
> key (a string) recover some data (of different types). If so 
> make the elements you store in the red black tree tuples (k,d) 
> where k is a string and d is a variant. Define the order 
> (k1,d1)<(k2,d2) if k1<k2. Make sure to allo duplicates. The 
> when you search for a key you get the pair and take the second 
> part. I hope this makes sense.

This is correct, although it seems that Variant is not suitable 
for this purpose. The data structure roughly looks like this:

- FieldName (string) => Index
- Index has PrimaryKey (Variant) and a Value (also Variant)
- Index Entries are a tuple of the form (PrimaryKey, Value)

Each index is roughly used by passing in a value, and it spits 
out a range of PrimaryKeys that are equal, lessThan, or 
greaterThan that value depending on what's needed.

The special Entry data type (rather than just the raw value) was 
added because I still need the IDs when I look up values, and I 
think using the Entry is every so slightly more efficient than 
maintaining a separate data structure keeping a set of Ids per 
Value.

Variant itself works well for comparison, because it simply 
passes down the compare operation to the underlying type, but 
it's major flaw seems to be the lack of support for noThrow, 
@safe, etc.

On Saturday, 2 April 2022 at 10:04:49 UTC, vit wrote:
> Try use ```std.sumtype```.

Very good suggestion, I haven't tried this before. I'll do some 
digging to see if I can make it work. Because it requires one to 
list the data types in advance, it can work for primary keys, 
however, it may not work in my particular use case because the 
values too that I'm organizing are also Variants. I hope it works.



More information about the Digitalmars-d-learn mailing list