Вы можете написать лямбду, которая удалит поддерево, используя передачу прав собственности.
mainTree = new Tree<string, Tree<int, string> >.full (mainTreeCompareDataFunction, free,
(data) => {
var tree = (Tree<string, Tree<int, string>>) data;
// Ownership is transfered to this local var which calls unref when it goes out of scope
var tree2 = (owned) tree;
});
Вот полный код:
using GLib;
using GLib.Random;
// Global variable
Tree<string, Tree<int, string> > mainTree;
public static int main (string[] args) {
// Initiate random seed
Random.set_seed ((uint32) get_monotonic_time());
// mainTree initialization
mainTree = new Tree<string, Tree<int, string> >.full (mainTreeCompareDataFunction, free,
(data) => {
var tree = (Tree<string, Tree<int, string>>) data;
// Ownership is transfered to this local var which calls unref when it goes out of scope
var tree2 = (owned) tree;
});
// Random sized for loop
for (int i = 0; i < int_range (1000, 10001); i++) {
// If a condition is met (i is even)
if (i % 2 == 0) {
// Create a Tree to nest onto mainTree
Tree<int, string> treeToNest = new Tree<int, string>.full (treeToNestCompareDataFunction, null, free);
// Insert random content into treeToNest
treeToNest.insert (int_range (0, 101), randomString ());
// Insert the tree onto mainTree
mainTree.insert (randomString (), treeToNest);
}
}
// Empty the tree
mainTree.@foreach ((mainTreeKey, mainTreeValue) => {
mainTree.remove (mainTreeKey);
return false;
});
return 0;
}
public string randomString () {
string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string stringToReturn = "";
// Create a random 8 character string
for (int i = 0; i < 8; i++) {
stringToReturn += charset[int_range (0, charset.length)].to_string ();
}
return stringToReturn;
}
public int treeToNestCompareDataFunction (int a, int b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
public int mainTreeCompareDataFunction (string a, string b) {
return strcmp (a, b);
}
Я бы не стал использовать класс GLib.Tree в Vala из-за сложности правильного управления памятью. Вам следует рассмотреть возможность использования Gee.TreeMap
.