StackTree

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.

Constructors

this
this(RegionAllocator alloc)

Members

Functions

find
T find(U whatToFind)

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
void insert(T toInsert)

Insert an element.

opApply
int opApply(int delegate(ref T) dg)

Iterate over the elements of this tree in sorted order.

opIn_r
T* opIn_r(U whatToFind)

Find an element and return a pointer to it, or null if not present.

remove
void remove(U whatToRemove)

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.

Properties

length
size_t length [@property getter]

Number of elements in the tree.

Examples

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;

Meta