1 |
2 |
alfik |
/*
|
2 |
|
|
* Copyright (c) 2014, Aleksander Osman
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
9 |
|
|
* list of conditions and the following disclaimer.
|
10 |
|
|
*
|
11 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
12 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
13 |
|
|
* and/or other materials provided with the distribution.
|
14 |
|
|
*
|
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18 |
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
21 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
22 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
23 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
package ao486.utils;
|
28 |
|
|
|
29 |
|
|
public class TreePseudoLRU {
|
30 |
|
|
|
31 |
|
|
static class Node {
|
32 |
|
|
Node(Node l, Node r, int index) {
|
33 |
|
|
this.l = l;
|
34 |
|
|
this.r = r;
|
35 |
|
|
this.index = index;
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
boolean left;
|
39 |
|
|
int index;
|
40 |
|
|
|
41 |
|
|
Node l, r;
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
static int find(Node n) {
|
45 |
|
|
if(n.index != -1) return n.index;
|
46 |
|
|
|
47 |
|
|
if(n.left) {
|
48 |
|
|
n.left = false;
|
49 |
|
|
return find(n.l);
|
50 |
|
|
}
|
51 |
|
|
else {
|
52 |
|
|
n.left = true;
|
53 |
|
|
return find(n.r);
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
public static void main(String args[]) throws Exception {
|
58 |
|
|
|
59 |
|
|
long vals[] = new long[32];
|
60 |
|
|
|
61 |
|
|
long idx = 0;
|
62 |
|
|
|
63 |
|
|
for(int i=0; i<vals.length; i++) vals[i] = idx++;
|
64 |
|
|
|
65 |
|
|
Node n1 = new Node(new Node(null,null, 0), new Node(null,null, 1), -1);
|
66 |
|
|
Node n2 = new Node(new Node(null,null, 2), new Node(null,null, 3), -1);
|
67 |
|
|
Node n3 = new Node(new Node(null,null, 4), new Node(null,null, 5), -1);
|
68 |
|
|
Node n4 = new Node(new Node(null,null, 6), new Node(null,null, 7), -1);
|
69 |
|
|
Node n5 = new Node(new Node(null,null, 8), new Node(null,null, 9), -1);
|
70 |
|
|
Node n6 = new Node(new Node(null,null, 10), new Node(null,null, 11), -1);
|
71 |
|
|
Node n7 = new Node(new Node(null,null, 12), new Node(null,null, 13), -1);
|
72 |
|
|
Node n8 = new Node(new Node(null,null, 14), new Node(null,null, 15), -1);
|
73 |
|
|
Node n9 = new Node(new Node(null,null, 16), new Node(null,null, 17), -1);
|
74 |
|
|
Node n10 = new Node(new Node(null,null, 18), new Node(null,null, 19), -1);
|
75 |
|
|
Node n11 = new Node(new Node(null,null, 20), new Node(null,null, 21), -1);
|
76 |
|
|
Node n12 = new Node(new Node(null,null, 22), new Node(null,null, 23), -1);
|
77 |
|
|
Node n13 = new Node(new Node(null,null, 24), new Node(null,null, 25), -1);
|
78 |
|
|
Node n14 = new Node(new Node(null,null, 26), new Node(null,null, 27), -1);
|
79 |
|
|
Node n15 = new Node(new Node(null,null, 28), new Node(null,null, 29), -1);
|
80 |
|
|
Node n16 = new Node(new Node(null,null, 30), new Node(null,null, 31), -1);
|
81 |
|
|
|
82 |
|
|
Node m1 = new Node(n1,n2, -1);
|
83 |
|
|
Node m2 = new Node(n3,n4, -1);
|
84 |
|
|
Node m3 = new Node(n5,n6, -1);
|
85 |
|
|
Node m4 = new Node(n7,n8, -1);
|
86 |
|
|
Node m5 = new Node(n9,n10, -1);
|
87 |
|
|
Node m6 = new Node(n11,n12, -1);
|
88 |
|
|
Node m7 = new Node(n13,n14, -1);
|
89 |
|
|
Node m8 = new Node(n15,n16, -1);
|
90 |
|
|
|
91 |
|
|
Node o1 = new Node(m1,m2, -1);
|
92 |
|
|
Node o2 = new Node(m3,m4, -1);
|
93 |
|
|
Node o3 = new Node(m5,m6, -1);
|
94 |
|
|
Node o4 = new Node(m7,m8, -1);
|
95 |
|
|
|
96 |
|
|
Node p1 = new Node(o1,o2, -1);
|
97 |
|
|
Node p2 = new Node(o3,o4, -1);
|
98 |
|
|
|
99 |
|
|
Node q1 = new Node(p1,p2, -1);
|
100 |
|
|
|
101 |
|
|
int found = 0;
|
102 |
|
|
int count = 10000;
|
103 |
|
|
for(int i=0; i<count; i++) {
|
104 |
|
|
// find minimal
|
105 |
|
|
long min = Long.MAX_VALUE;
|
106 |
|
|
for(int j=0; j<vals.length; j++) if(vals[j] < min) min = vals[j];
|
107 |
|
|
|
108 |
|
|
int index = find(q1);
|
109 |
|
|
if(vals[index] == min) found++;
|
110 |
|
|
|
111 |
|
|
vals[index] = idx++;
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
System.out.println((double)found / (double)count);
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
}
|