Adding item to a rbtree with a custom binaryFun
Steven Schveighoffer
schveiguy at gmail.com
Tue Apr 14 01:38:02 UTC 2020
On 4/13/20 7:59 PM, Adnan wrote:
> I want to keep an ordered set of records and the standard provides me
> with RedBlackTree. The record is of type Tuple!(string, uint). Here's
> what it looks like:
>
> import std.json : parseJSON;
>
> uint[string] wordTable;
> import std.datetime.stopwatch : StopWatch, AutoStart;
>
> auto sw = StopWatch(AutoStart.yes);
> const auto j = parseJSON(get(link));
> const long downloadTime = sw.peek.total!"msecs";
>
> import std.typecons : Tuple, tuple;
> import std.container.rbtree : RedBlackTree;
> import std.functional : binaryFun;
>
// instead of this:
> RedBlackTree!(Tuple!(string, uint), binaryFun!("a[1] > b[1]"))
> records;
// do this: (note you don't need binaryFun)
auto records = new RedBlackTree!(Tuple!(String, uint), "a[1] >
b[1]");
>
> foreach (node; j["posts"].array()) {
> import std.stdio : writeln;
> import std.utf : decode;
>
> if ("com" in node) {
> import std.algorithm : splitter;
>
> foreach (word; getStr(node["com"].str()).splitter(' ')) {
> import std.string : strip;
>
> if (word.strip().length > 0)
> wordTable.require(word, 0)++;
>
> records ~= (tuple(word, wordTable[word])); // error
> }
> }
> }
>
> Now primarily I used `insert()` method to add a record to the records
> but it causes segfault in runtime.
Because RedBlackTree is a class, so you need to initialize it with a
call to new.
> So I decided to use ~= to see what's
> going on. Here's what the compiler says:
>
> Error: cannot append type Tuple!(string, uint) to type
> std.container.rbtree.RedBlackTree!(Tuple!(string, uint), binaryFun, false)
Because RedBlackTree doesn't overload appending.
-Steve
More information about the Digitalmars-d-learn
mailing list