1 |
| package org.drools.util; |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| import java.io.Serializable; |
43 |
| |
44 |
| public class PrimitiveLongStack |
45 |
| implements |
46 |
| Serializable |
47 |
| { |
48 |
| private final int tableSize; |
49 |
| private int currentPageId; |
50 |
| private Page currentPage; |
51 |
| |
52 |
71
| public PrimitiveLongStack()
|
53 |
| { |
54 |
71
| this( 256 );
|
55 |
| } |
56 |
| |
57 |
71
| public PrimitiveLongStack(int tableSize)
|
58 |
| { |
59 |
71
| this.tableSize = tableSize;
|
60 |
71
| this.currentPageId = 0;
|
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
71
| this.currentPage = new Page( null,
|
66 |
| this.currentPageId, |
67 |
| this.tableSize ); |
68 |
| } |
69 |
| |
70 |
54
| public void push(long value)
|
71 |
| { |
72 |
54
| if ( this.currentPage.getPosition( ) == this.tableSize - 1 )
|
73 |
| { |
74 |
| |
75 |
0
| Page node = new Page( this.currentPage,
|
76 |
| ++this.currentPageId, |
77 |
| this.tableSize ); |
78 |
0
| this.currentPage = node;
|
79 |
| } |
80 |
| |
81 |
54
| this.currentPage.push( value );
|
82 |
| } |
83 |
| |
84 |
0
| public long pop()
|
85 |
| { |
86 |
0
| if ( this.currentPage.getPosition( ) == -1 )
|
87 |
| { |
88 |
0
| if ( this.currentPageId == 0 )
|
89 |
| { |
90 |
0
| throw new RuntimeException( "Unable to pop" );
|
91 |
| } |
92 |
| |
93 |
0
| Page node = this.currentPage;
|
94 |
0
| this.currentPage = node.getPreviousSibling( );
|
95 |
0
| this.currentPageId--;
|
96 |
0
| node.remove( );
|
97 |
| |
98 |
| } |
99 |
| |
100 |
0
| return this.currentPage.pop( );
|
101 |
| } |
102 |
| |
103 |
872
| public boolean isEmpty()
|
104 |
| { |
105 |
872
| return this.currentPageId == 0 && this.currentPage.getPosition( ) == -1;
|
106 |
| } |
107 |
| |
108 |
| private static final class Page |
109 |
| implements |
110 |
| Serializable |
111 |
| { |
112 |
| private final int pageId; |
113 |
| private Page nextSibling; |
114 |
| private Page previousSibling; |
115 |
| private long[] table; |
116 |
| private int lastKey; |
117 |
| |
118 |
71
| Page(Page previousSibling,
|
119 |
| int nodeId, |
120 |
| int tableSize) |
121 |
| { |
122 |
| |
123 |
71
| this.previousSibling = previousSibling;
|
124 |
71
| if ( this.previousSibling != null )
|
125 |
| { |
126 |
0
| this.previousSibling.setNextSibling( this );
|
127 |
| } |
128 |
71
| this.pageId = nodeId;
|
129 |
71
| lastKey = -1;
|
130 |
| |
131 |
| |
132 |
71
| this.table = new long[tableSize];
|
133 |
| } |
134 |
| |
135 |
0
| public int getNodeId()
|
136 |
| { |
137 |
0
| return this.pageId;
|
138 |
| } |
139 |
| |
140 |
0
| void setNextSibling(Page nextSibling)
|
141 |
| { |
142 |
0
| this.nextSibling = nextSibling;
|
143 |
| } |
144 |
| |
145 |
0
| public Page getNextSibling()
|
146 |
| { |
147 |
0
| return this.nextSibling;
|
148 |
| } |
149 |
| |
150 |
0
| public Page getPreviousSibling()
|
151 |
| { |
152 |
0
| return this.previousSibling;
|
153 |
| } |
154 |
| |
155 |
0
| public long pop()
|
156 |
| { |
157 |
0
| return this.table[this.lastKey--];
|
158 |
| } |
159 |
| |
160 |
54
| public void push(long value)
|
161 |
| { |
162 |
54
| this.table[++this.lastKey] = value;
|
163 |
| } |
164 |
| |
165 |
926
| public int getPosition()
|
166 |
| { |
167 |
926
| return this.lastKey;
|
168 |
| } |
169 |
| |
170 |
0
| void remove()
|
171 |
| { |
172 |
0
| previousSibling.setNextSibling( null );
|
173 |
0
| this.previousSibling = null;
|
174 |
0
| this.table = null;
|
175 |
| } |
176 |
| } |
177 |
| } |