Adding item to a rbtree with a custom binaryFun

Adnan relay.public.adnan at outlook.com
Mon Apr 13 23:59:20 UTC 2020


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;

	RedBlackTree!(Tuple!(string, uint), binaryFun!("a[1] > b[1]")) 
records;

	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. 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)


So the gist of the question is, if I have an RedBlackTree with a 
custom binaryFun, how can I add tuples to it?


More information about the Digitalmars-d-learn mailing list