@@ -89,19 +89,24 @@ final int tailoff(){
89
89
return cnt - tail .length ;
90
90
}
91
91
92
- public Object nth (int i ){
92
+ public Object [] nodeFor (int i ){
93
93
if (i >= 0 && i < cnt )
94
94
{
95
95
if (i >= tailoff ())
96
- return tail [ i & 0x01f ] ;
96
+ return tail ;
97
97
Object [] arr = root ;
98
98
for (int level = shift ; level > 0 ; level -= 5 )
99
99
arr = (Object []) arr [(i >>> level ) & 0x01f ];
100
- return arr [ i & 0x01f ] ;
100
+ return arr ;
101
101
}
102
102
throw new IndexOutOfBoundsException ();
103
103
}
104
104
105
+ public Object nth (int i ){
106
+ Object [] node = nodeFor (i );
107
+ return node [i & 0x01f ];
108
+ }
109
+
105
110
public PersistentVector assocN (int i , Object val ){
106
111
if (i >= 0 && i < cnt )
107
112
{
@@ -163,6 +168,75 @@ public PersistentVector cons(Object val){
163
168
return new PersistentVector (meta (), cnt + 1 , newshift , newroot , new Object []{val });
164
169
}
165
170
171
+ public IChunkedSeq chunkedSeq (){
172
+ if (count () == 0 )
173
+ return null ;
174
+ return new ChunkedSeq (this ,0 ,0 );
175
+ }
176
+
177
+ static public final class ChunkedSeq extends ASeq implements IChunkedSeq {
178
+
179
+ final PersistentVector vec ;
180
+ final Object [] node ;
181
+ final int i ;
182
+ final int offset ;
183
+
184
+ public ChunkedSeq (PersistentVector vec , int i , int offset ){
185
+ this .vec = vec ;
186
+ this .i = i ;
187
+ this .offset = offset ;
188
+ this .node = vec .nodeFor (i );
189
+ }
190
+
191
+ ChunkedSeq (IPersistentMap meta , PersistentVector vec , Object [] node , int i , int offset ){
192
+ super (meta );
193
+ this .vec = vec ;
194
+ this .node = node ;
195
+ this .i = i ;
196
+ this .offset = offset ;
197
+ }
198
+
199
+ ChunkedSeq (PersistentVector vec , Object [] node , int i , int offset ){
200
+ this .vec = vec ;
201
+ this .node = node ;
202
+ this .i = i ;
203
+ this .offset = offset ;
204
+ }
205
+
206
+ public Indexed chunkedFirst () throws Exception {
207
+ return new ArrayChunk (node , offset );
208
+ }
209
+
210
+ public ISeq chunkedNext (){
211
+ if (i + node .length < vec .cnt )
212
+ return new ChunkedSeq (vec ,i + node .length ,0 );
213
+ return null ;
214
+ }
215
+
216
+ public ISeq chunkedMore (){
217
+ ISeq s = chunkedNext ();
218
+ if (s == null )
219
+ return PersistentList .EMPTY ;
220
+ return s ;
221
+ }
222
+
223
+ public Obj withMeta (IPersistentMap meta ){
224
+ if (meta == this ._meta )
225
+ return this ;
226
+ return new ChunkedSeq (meta , vec , node , i , offset );
227
+ }
228
+
229
+ public Object first (){
230
+ return node [offset ];
231
+ }
232
+
233
+ public ISeq next (){
234
+ if (offset + 1 < node .length )
235
+ return new ChunkedSeq (vec , node , i , offset + 1 );
236
+ return chunkedNext ();
237
+ }
238
+ }
239
+
166
240
public IPersistentCollection empty (){
167
241
return EMPTY .withMeta (meta ());
168
242
}
0 commit comments