Skip to content

Commit 3f1b64e

Browse files
authored
Merge pull request #152 from dlang-community/tree-tweaks
Make some enhancements to the treemap structure, and to the T-Tree do… merged-on-behalf-of: Richard Andrew Cattermole <alphaglosined@gmail.com>
2 parents f0f6681 + d44cb6a commit 3f1b64e

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/containers/treemap.d

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,26 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
218218
return tree[].map!(n => KeyValue(n.key, cast(CETV) n.value));
219219
}
220220

221+
/**
222+
* Returns: The value associated with the first key in the map.
223+
*/
224+
auto front(this This)() inout pure @trusted
225+
{
226+
alias CETV = ContainerElementType!(This, V);
227+
228+
return cast(CETV) tree.front.value;
229+
}
230+
231+
/**
232+
* Returns: The value associated with the last key in the map.
233+
*/
234+
auto back(this This)() inout pure @trusted
235+
{
236+
alias CETV = ContainerElementType!(This, V);
237+
238+
return cast(CETV) tree.back.value;
239+
}
240+
221241
private:
222242

223243
import containers.ttree : TTree;

src/containers/ttree.d

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ private import stdx.allocator.mallocator : Mallocator;
1616
*
1717
* T-tree Nodes are (by default) sized to fit within a 64-byte
1818
* cache line. The number of items stored per node can be read from the
19-
* `nodeCapacity` field. Each node has 0, 1, or 2 children. Each node has between
19+
* `nodeCapacity` enum. Each node has 0, 1, or 2 children. Each node has between
2020
* 1 and `nodeCapacity` items, or it has `nodeCapacity` items and 0 or
2121
* more children.
2222
*
23+
* Inserting or removing items while iterating a range returned from `opSlice`,
24+
* `upperBound`, `equalRange`, or other similar functions will result in
25+
* unpredicable and likely invalid iteration orders.
26+
*
2327
* Params:
2428
* T = the element type
2529
* Allocator = the allocator to use. Defaults to `Mallocator`.
@@ -254,7 +258,7 @@ struct TTree(T, Allocator = Mallocator, bool allowDuplicates = false,
254258
/**
255259
* Returns: the first element in the tree.
256260
*/
257-
inout(T) front(this This)() inout pure @trusted @property
261+
auto front(this This)() inout pure @trusted @property
258262
{
259263
import std.exception : enforce;
260264

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

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

466470
mixin AllocatorState!Allocator;
467471

472+
/// The number of values that can be stored in a single T-Tree node.
473+
enum size_t nodeCapacity = N[0];
474+
468475
private:
469476

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

479486
alias N = FatNodeInfo!(T.sizeof, 3, cacheLineSize, ulong.sizeof);
480487
alias Value = ContainerStorageType!T;
481-
enum size_t nodeCapacity = N[0];
482488
alias BookkeepingType = N[1];
483489
enum HEIGHT_BIT_OFFSET = 48UL;
484490
enum fullBitPattern = fullBits!(ulong, nodeCapacity);

0 commit comments

Comments
 (0)