@@ -178,6 +178,14 @@ public Iterator iterator(){
178
178
return new SeqIterator (seq ());
179
179
}
180
180
181
+ public Object kvreduce (IFn f , Object init ){
182
+ init = hasNull ?f .invoke (init ,null ,nullValue ):init ;
183
+ if (root != null ){
184
+ return root .kvreduce (f ,init );
185
+ }
186
+ return init ;
187
+ }
188
+
181
189
public int count (){
182
190
return count ;
183
191
}
@@ -311,6 +319,9 @@ static interface INode extends Serializable {
311
319
INode assoc (AtomicReference <Thread > edit , int shift , int hash , Object key , Object val , Box addedLeaf );
312
320
313
321
INode without (AtomicReference <Thread > edit , int shift , int hash , Object key , Box removedLeaf );
322
+
323
+ public Object kvreduce (IFn f , Object init );
324
+
314
325
}
315
326
316
327
final static class ArrayNode implements INode {
@@ -371,6 +382,16 @@ public ISeq nodeSeq(){
371
382
return Seq .create (array );
372
383
}
373
384
385
+ public Object kvreduce (IFn f , Object init ){
386
+ for (INode node : array )
387
+ {
388
+ if (node != null )
389
+ init = node .kvreduce (f ,init );
390
+ }
391
+ return init ;
392
+ }
393
+
394
+
374
395
private ArrayNode ensureEditable (AtomicReference <Thread > edit ){
375
396
if (this .edit == edit )
376
397
return this ;
@@ -600,6 +621,11 @@ public ISeq nodeSeq(){
600
621
return NodeSeq .create (array );
601
622
}
602
623
624
+ public Object kvreduce (IFn f , Object init ){
625
+ return NodeSeq .kvreduce (array ,f ,init );
626
+ }
627
+
628
+
603
629
private BitmapIndexedNode ensureEditable (AtomicReference <Thread > edit ){
604
630
if (this .edit == edit )
605
631
return this ;
@@ -784,6 +810,10 @@ public ISeq nodeSeq(){
784
810
return NodeSeq .create (array );
785
811
}
786
812
813
+ public Object kvreduce (IFn f , Object init ){
814
+ return NodeSeq .kvreduce (array ,f ,init );
815
+ }
816
+
787
817
public int findIndex (Object key ){
788
818
for (int i = 0 ; i < 2 *count ; i +=2 )
789
819
{
@@ -1017,6 +1047,21 @@ static ISeq create(Object[] array) {
1017
1047
return create (array , 0 , null );
1018
1048
}
1019
1049
1050
+ static public Object kvreduce (Object [] array , IFn f , Object init ){
1051
+ for (int i =0 ;i <array .length ;i +=2 )
1052
+ {
1053
+ if (array [i ] != null )
1054
+ init = f .invoke (init , array [i ], array [i +1 ]);
1055
+ else
1056
+ {
1057
+ INode node = (INode ) array [i +1 ];
1058
+ if (node != null )
1059
+ init = node .kvreduce (f ,init );
1060
+ }
1061
+ }
1062
+ return init ;
1063
+ }
1064
+
1020
1065
private static ISeq create (Object [] array , int i , ISeq s ) {
1021
1066
if (s != null )
1022
1067
return new NodeSeq (null , array , i , s );
0 commit comments