Skip to content

Commit

Permalink
Merge pull request #152 from dlang-community/tree-tweaks
Browse files Browse the repository at this point in the history
Make some enhancements to the treemap structure, and to the T-Tree do…
merged-on-behalf-of: Richard Andrew Cattermole <alphaglosined@gmail.com>
  • Loading branch information
dlang-bot authored Sep 26, 2019
2 parents f0f6681 + d44cb6a commit 3f1b64e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/containers/treemap.d
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,26 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
return tree[].map!(n => KeyValue(n.key, cast(CETV) n.value));
}

/**
* Returns: The value associated with the first key in the map.
*/
auto front(this This)() inout pure @trusted
{
alias CETV = ContainerElementType!(This, V);

return cast(CETV) tree.front.value;
}

/**
* Returns: The value associated with the last key in the map.
*/
auto back(this This)() inout pure @trusted
{
alias CETV = ContainerElementType!(This, V);

return cast(CETV) tree.back.value;
}

private:

import containers.ttree : TTree;
Expand Down
14 changes: 10 additions & 4 deletions src/containers/ttree.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ private import stdx.allocator.mallocator : Mallocator;
*
* T-tree Nodes are (by default) sized to fit within a 64-byte
* cache line. The number of items stored per node can be read from the
* `nodeCapacity` field. Each node has 0, 1, or 2 children. Each node has between
* `nodeCapacity` enum. Each node has 0, 1, or 2 children. Each node has between
* 1 and `nodeCapacity` items, or it has `nodeCapacity` items and 0 or
* more children.
*
* Inserting or removing items while iterating a range returned from `opSlice`,
* `upperBound`, `equalRange`, or other similar functions will result in
* unpredicable and likely invalid iteration orders.
*
* Params:
* T = the element type
* Allocator = the allocator to use. Defaults to `Mallocator`.
Expand Down Expand Up @@ -254,7 +258,7 @@ struct TTree(T, Allocator = Mallocator, bool allowDuplicates = false,
/**
* Returns: the first element in the tree.
*/
inout(T) front(this This)() inout pure @trusted @property
auto front(this This)() inout pure @trusted @property
{
import std.exception : enforce;

Expand All @@ -269,7 +273,7 @@ struct TTree(T, Allocator = Mallocator, bool allowDuplicates = false,
/**
* Returns: the last element in the tree.
*/
inout(T) back(this This)() inout pure @trusted @property
auto back(this This)() inout pure @trusted @property
{
import std.exception : enforce;

Expand Down Expand Up @@ -465,6 +469,9 @@ struct TTree(T, Allocator = Mallocator, bool allowDuplicates = false,

mixin AllocatorState!Allocator;

/// The number of values that can be stored in a single T-Tree node.
enum size_t nodeCapacity = N[0];

private:

import containers.internal.element_type : ContainerElementType;
Expand All @@ -478,7 +485,6 @@ private:

alias N = FatNodeInfo!(T.sizeof, 3, cacheLineSize, ulong.sizeof);
alias Value = ContainerStorageType!T;
enum size_t nodeCapacity = N[0];
alias BookkeepingType = N[1];
enum HEIGHT_BIT_OFFSET = 48UL;
enum fullBitPattern = fullBits!(ulong, nodeCapacity);
Expand Down

0 comments on commit 3f1b64e

Please # to comment.