Find an element and return it. Throw an exception if it is not * present. U is expected to be the type of the key that this tree is * sorted on.
Insert an element.
Iterate over the elements of this tree in sorted order.
Find an element and return a pointer to it, or null if not present.
Remove an element from this tree. The type of U is expected to be the type of the key that this tree is sorted on.
Number of elements in the tree.
1 struct StringNum { 2 string someString; 3 uint num; 4 } 5 6 // Create a StackTree of StringNums, sorted in descending order, using 7 // someString for comparison. 8 auto alloc = newRegionAllocator(); 9 auto myTree = StackTree!(StringNum, "a.someString", "a > b")(alloc); 10 11 // Add some elements. 12 myTree.insert( StringNum("foo", 1)); 13 myTree.insert( StringNum("bar", 2)); 14 myTree.insert( StringNum("foo", 3)); 15 16 assert(myTree.find("foo") == StringNum("foo", 3)); 17 assert(myTree.find("bar") == StringNum("bar", 2));
Note: This tree supports a compile-time interface similar to StackSet and can be used as a finite set implementation.
Warning: This implementation places removed nodes on an internal free list and recycles them, since there is no way to delete RegionAllocator-allocated data in a non-LIFO order. Therefore, you may not retain the address of a variable stored in a StackTree after deleting it from the StackTree. For example, DO NOT do this:
SomeType* myPtr = "foo" in myTree; myTree.remove("foo"); *myPtr = someValue;
An AVL tree implementation on top of RegionAllocator. If elements are removed, they are stored on an internal free list and recycled when new elements are added to the tree.
Template paramters:
T = The type to be stored in the tree.
key = Function to access the key that what you're storing is to be compared on.
compFun = The function for comparing keys.