Creating a RedBlackTree
drug
drug2004 at bk.ru
Wed May 15 13:19:36 UTC 2019
On 15.05.2019 16:08, Bogdan wrote:
> I don't have any experience with using templates. Is it possible to
> create a RB tree containing structs, where the nodes are ordered by one
> struct member?
>
> ```
> import std.stdio;
> import std.container;
>
> enum KeyID: uint
> {
> KEY_A,
> KEY_S,
> KEY_D,
> KEY_W
> }
>
> struct KeyController
> {
> KeyID ID;
> bool isDown;
> }
>
>
> void main()
> {
> auto rbt = redBlackTree!KeyController;
> }
>
> ```
>
> When I run this I get a compile error:
> ```
> Error: template instance
> `std.container.rbtree.RedBlackTree!(KeyController)` does not match
> template declaration RedBlackTree(T, alias less = "a < b", bool
> allowDuplicates = false) if (is(typeof(binaryFun!less(T.init, T.init))))
> ```
>
You can use predicate for this purpose:
```
auto rbt = redBlackTree!((a, b) => a.ID < b.ID, KeyController);
```
https://run.dlang.io/is/CNRTQf
More information about the Digitalmars-d-learn
mailing list