1 /*******************************************************************************
2 
3     Mapping from access time to the index of an item in the cache items array.
4     Limits the number of available mappings to a fixed value and preallocates
5     all nodes in an array buffer.
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.cache.model.containers.TimeToIndex;
19 
20 
21 import ocean.util.container.ebtree.EBTree128;
22 import ocean.util.container.ebtree.nodepool.NodePool;
23 import ocean.util.container.cache.model.containers.ArrayPool;
24 
25 /******************************************************************************/
26 
27 class TimeToIndex: EBTree128!()
28 {
29     /***************************************************************************
30 
31         Node wrapper struct, the node pool element type which must have a size
32         that is an integer multiple of 16. This is because the libebtree
33         requires that the pointers to the nodes passed to it are integer
34         multiples of 16.
35 
36     ***************************************************************************/
37 
38     struct PaddedNode
39     {
40         /***********************************************************************
41 
42             Actual node.
43 
44         ***********************************************************************/
45 
46         Node node;
47 
48         /***********************************************************************
49 
50             Pad bytes.
51 
52         ***********************************************************************/
53 
54         private ubyte[Node.sizeof % 16] pad;
55 
56         /**********************************************************************/
57 
58         static assert(typeof(this).sizeof % 16 == 0,
59                       typeof(this).stringof ~ ".sizeof must be an integer "
60                     ~ "multiple of 16, not " ~ typeof(this).sizeof.stringof);
61     }
62 
63     /**************************************************************************/
64 
65     static class ArrayNodePool: NodePool!(Node)
66     {
67         /***********************************************************************
68 
69             Array of bucket elements.
70 
71         ***********************************************************************/
72 
73         private ArrayPool!(PaddedNode) elements;
74 
75         /***********************************************************************
76 
77             Constructor.
78 
79             Params:
80                 n = maximum number of elements in mapping
81 
82         ***********************************************************************/
83 
84         public this ( size_t n )
85         {
86             this.elements = new typeof(this.elements)(n);
87         }
88 
89         /***********************************************************************
90 
91             Obtains a new node from the array node pool.
92 
93             Returns:
94                 a new node.
95 
96             Out:
97                 The returned node pointer is an integer multiple of 16 as
98                 required by the libebtree (inherited postcondition).
99 
100         ***********************************************************************/
101 
102         protected override Node* newNode ( )
103         {
104             return &(this.elements.next.node);
105         }
106 
107         /***********************************************************************
108 
109             Marks all pool items as unused.
110 
111         ***********************************************************************/
112 
113         public void clear ()
114         {
115             this.resetFreeList();
116             this.elements.clear();
117         }
118     }
119 
120     /***************************************************************************
121 
122         Array pool of nodes.
123 
124     ***************************************************************************/
125 
126     private ArrayNodePool nodes;
127 
128     /***************************************************************************
129 
130         Constructor.
131 
132         Params:
133             n = maximum number of elements in mapping
134 
135     ***************************************************************************/
136 
137     public this ( size_t n )
138     {
139         super(this.nodes = new typeof(this.nodes)(n));
140     }
141 
142     /***************************************************************************
143 
144         Removes all values from the tree.
145 
146     ***************************************************************************/
147 
148     public override void clear ( )
149     {
150         super.clear();
151         this.nodes.clear();
152     }
153 }