1 /*******************************************************************************
2 
3     Elastic binary tree node struct template
4 
5     Used as mixin in the EBTree classes.
6 
7     Copyright:
8         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
9         All rights reserved.
10 
11     License:
12         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
13         Alternatively, this file may be distributed under the terms of the Tango
14         3-Clause BSD License (see LICENSE_BSD.txt for details).
15 
16 *******************************************************************************/
17 
18 module ocean.util.container.ebtree.model.Node;
19 
20 /*******************************************************************************
21 
22     Node struct template
23 
24     Template parameters:
25         eb_node   = libebtree's internal node type
26         Key       = key type (signed/unsigned integer)
27         eb_getkey = Key ( eb_node* node ); returns the key of node.
28 
29         eb_next = eb_node* ( eb_node* n ); returns the next node for n or null
30                   if n is the last node in the tree.
31         eb_prev = eb_node* ( eb_node* n ); returns the previous node for n or
32                   null if n is the first node in the tree.
33 
34         eb_next_unique = same as eb_next but skips key duplicates.
35         eb_prev_unique = same as eb_prev but skips key duplicates.
36 
37         eb_delete = void ( eb_node* n ); removes n from the tree.
38 
39 *******************************************************************************/
40 
41 struct Node ( eb_node, Key, alias eb_getkey, alias eb_next, alias eb_prev,
42               alias eb_prev_unique, alias eb_next_unique, alias eb_delete )
43 {
44     /**************************************************************************
45 
46         Node data content.
47 
48      **************************************************************************/
49 
50     private eb_node node_;
51 
52     /**************************************************************************
53 
54         Obtains the key of this node.
55 
56         Returns:
57             key
58 
59      **************************************************************************/
60 
61     public Key key ( )
62     {
63         // TODO: Check if this works with signed keys.
64 
65 //        return this.node_.key_;
66         return eb_getkey(&this.node_);
67     }
68 
69     /**************************************************************************
70 
71         Obtains the next node in the tree to which this node is associated.
72 
73         Returns:
74             the next node or null if this is the last.
75 
76      **************************************************************************/
77 
78     public typeof(&this) next ( )
79     {
80         return this.ebCall!(eb_next);
81     }
82 
83     /**************************************************************************
84 
85         Obtains the previous node in the tree to which this node is associated.
86 
87         Returns:
88             the previous node or null if this is the first.
89 
90      **************************************************************************/
91 
92     public typeof(&this) prev ( )
93     {
94         return this.ebCall!(eb_prev);
95     }
96 
97     /**************************************************************************
98 
99         Obtains the next node in the tree to which this node is associated,
100         skipping key duplicates.
101 
102         Returns:
103             the next node with a unique key or null if this is the last.
104 
105      **************************************************************************/
106 
107     public typeof(&this) next_unique ( )
108     {
109         return this.ebCall!(eb_next_unique);
110     }
111 
112     /**************************************************************************
113 
114         Obtains the previous node in the tree to which this node is associated,
115         skipping key duplicates.
116 
117         Returns:
118             the previous node with a unique key or null if this is the
119             first.
120 
121      **************************************************************************/
122 
123     public typeof(&this) prev_unique ( )
124     {
125         return this.ebCall!(eb_prev_unique);
126     }
127 
128     /**************************************************************************
129 
130         Removes this node from the tree to which this it is associated.
131 
132         Returns:
133             this instance
134 
135      **************************************************************************/
136 
137     private typeof(&this) remove ( )
138     {
139         eb_delete(&this.node_);
140 
141         return &this;
142     }
143 
144     /**************************************************************************
145 
146         Library function call wrapper. Invokes eb_func with this instance as
147         first argument.
148 
149         Params:
150             eb_func = library function
151 
152         Returns:
153             passes through the return value of eb_func, which may be null.
154 
155      **************************************************************************/
156 
157     private typeof(&this) ebCall ( alias eb_func ) ( )
158     {
159         static assert (is(typeof(eb_func(&this.node_)) == eb_node*));
160 
161         return cast(typeof(&this)) eb_func(&this.node_);
162     }
163 }