1 /*******************************************************************************
2
3 Mapping from key to the time-to-index mapping of an item in the cache.
4 Limits the number of available mappings to a fixed value and preallocates
5 all bucket elements 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.KeyToNode;
19
20
21 import ocean.util.container.map.HashMap;
22 import ocean.util.container.map.model.BucketElementFreeList;
23 import ocean.util.container.cache.model.containers.ArrayPool;
24 import ocean.util.container.cache.model.containers.TimeToIndex;
25
26 /******************************************************************************/
27
28 class KeyToNode: HashMap!(TimeToIndex.Node*)
29 {
30 static class ArrayAllocatedFreeBucketElements: BucketElementFreeList!(Bucket.Element)
31 {
32 /***********************************************************************
33
34 Preallocated pool of bucket elements.
35
36 ***********************************************************************/
37
38 private GenericArrayPool pool;
39
40 /***********************************************************************
41
42 Constructor.
43
44 Params:
45 n = number of elements in the pool
46
47 ***********************************************************************/
48
49 private this ( size_t n )
50 {
51 this.pool = new GenericArrayPool(n, Bucket.Element.sizeof);
52 }
53
54 /***********************************************************************
55
56 Obtains a new element from the pool.
57
58 Returns:
59 A new pool element.
60
61 ***********************************************************************/
62
63 protected override Bucket.Element* newElement ( )
64 {
65 return cast(Bucket.Element*)this.pool.next;
66 }
67 }
68
69 /***************************************************************************
70
71 Bucket elements allocator.
72 The elements are allocated only during container construction,
73 and never freed.
74
75 ***************************************************************************/
76
77 private ArrayAllocatedFreeBucketElements allocator;
78
79 /***********************************************************************
80
81 Constructor.
82
83 Params:
84 n = maximum number of elements in mapping
85
86 ***********************************************************************/
87
88 public this ( size_t n )
89 {
90 this.allocator = new ArrayAllocatedFreeBucketElements(n);
91 super(this.allocator, n);
92 }
93 }
94
95 unittest
96 {
97 KeyToNode map = new KeyToNode(5);
98 (*map.put(1)) = new TimeToIndex.Node();
99 (*map.put(2)) = new TimeToIndex.Node();
100 (*map.put(3)) = new TimeToIndex.Node();
101 (*map.put(4)) = new TimeToIndex.Node();
102 (*map.put(5)) = new TimeToIndex.Node();
103 map.clear();
104 map.clear();
105 map.clear();
106 }
107
108 unittest
109 {
110 KeyToNode map = new KeyToNode(100000);
111 map.clear();
112 map.put(9207674216414740734);
113 map.put(8595442437537477107);
114 map.clear();
115 map.put(8595442437537477107);
116 map.clear();
117 map.put(9207674216414740734);
118 map.put(8595442437537477106);
119 map.put(8595442437537477108);
120 map.put(8595442437537477110);
121 map.put(8595442437537477112);
122 map.clear();
123 }