@@ -144,19 +144,19 @@ boolean insert(final MutableTrieMap<K, V> ct, final INode<K, V> in, final int po
144144 final K key , final V val , final int hc , final int lev ) {
145145 final CNode <K , V > next ;
146146 if (!sn .matches (hc , key )) {
147- final var rn = gen == in .gen ? this : renewed (gen , ct );
147+ final var rn = gen == in .gen ? this : renewed (ct , gen );
148148 next = rn .updatedAt (pos , new INode <>(in , sn , key , val , hc , lev ), gen );
149149 } else {
150150 next = updatedAt (pos , key , val , hc , gen );
151151 }
152- return in .gcasWrite (next , ct );
152+ return in .gcasWrite (ct , next );
153153 }
154154
155155 boolean insert (final MutableTrieMap <K , V > ct , final INode <K , V > in , final int pos , final int flag , final K key ,
156156 final V val , final int hc ) {
157157 final var ngen = in .gen ;
158- final var rn = gen == ngen ? this : renewed (ngen , ct );
159- return in .gcasWrite (rn .toInsertedAt (this , ngen , pos , flag , key , val , hc ), ct );
158+ final var rn = gen == ngen ? this : renewed (ct , ngen );
159+ return in .gcasWrite (ct , rn .toInsertedAt (this , ngen , pos , flag , key , val , hc ));
160160 }
161161
162162 @ Nullable Result <V > insertIf (final MutableTrieMap <K , V > ct , final Gen startGen , final int hc , final K key ,
@@ -194,16 +194,16 @@ boolean insert(final MutableTrieMap<K, V> ct, final INode<K, V> in, final int po
194194 if (!sn .matches (hc , key )) {
195195 if (cond == null || cond == ABSENT ) {
196196 final var ngen = in .gen ;
197- final var rn = gen == ngen ? this : renewed (ngen , ct );
198- return in .gcasWrite (rn .toUpdatedAt (this , pos , new INode <>(in , sn , key , val , hc , lev ), ngen ), ct )
197+ final var rn = gen == ngen ? this : renewed (ct , ngen );
198+ return in .gcasWrite (ct , rn .toUpdatedAt (this , pos , new INode <>(in , sn , key , val , hc , lev ), ngen ))
199199 ? Result .empty () : null ;
200200 }
201201 return Result .empty ();
202202 }
203203 if (cond == ABSENT ) {
204204 return sn .toResult ();
205205 } else if (cond == null || cond == PRESENT || cond .equals (sn .value ())) {
206- return in .gcasWrite (updatedAt (pos , key , val , hc , gen ), ct ) ? sn .toResult () : null ;
206+ return in .gcasWrite (ct , updatedAt (pos , key , val , hc , gen )) ? sn .toResult () : null ;
207207 }
208208 return Result .empty ();
209209 }
@@ -226,7 +226,7 @@ boolean insert(final MutableTrieMap<K, V> ct, final INode<K, V> in, final int po
226226 if (!sn .matches (hc , key ) || cond != null && !cond .equals (sn .value ())) {
227227 return Result .empty ();
228228 }
229- return parent .gcasWrite (toRemoved (ct , flag , pos , lev ), ct ) ? sn .toResult () : null ;
229+ return parent .gcasWrite (ct , toRemoved (ct , flag , pos , lev )) ? sn .toResult () : null ;
230230 } else {
231231 throw invalidElement (sub );
232232 }
@@ -252,7 +252,7 @@ MainNode<K, V> toCompressed(final TrieMap<K, V> ct, final Gen ngen, final int le
252252 final var narr = newArray (len );
253253 for (int i = 0 ; i < len ; i ++) {
254254 final var tmp = arr [i ];
255- narr [i ] = tmp instanceof INode <K , V > in ? resurrect (in , ct ) : tmp ;
255+ narr [i ] = tmp instanceof INode <K , V > in ? in . resurrect (ct ) : tmp ;
256256 }
257257
258258 return toUpdated (ngen , lev , narr , bitmap );
@@ -275,7 +275,7 @@ private MainNode<K, V> toUpdated(final Gen ngen, final int lev, final Branch<K,
275275
276276 // tries to gcasWrite() a copy of this CNode renewed to ngen
277277 private boolean renew (final TrieMap <K , V > ct , final INode <K , V > in , final Gen ngen ) {
278- return in .gcasWrite (renewed ( ngen , ct ), ct );
278+ return in .gcasWrite (ct , renewed ( ct , ngen ) );
279279 }
280280
281281 @ Override
@@ -298,22 +298,22 @@ private int computeSize(final ImmutableTrieMap<?, ?> ct) {
298298 final int len = array .length ;
299299 return switch (len ) {
300300 case 0 -> 0 ;
301- case 1 -> elementSize (array [0 ], ct );
301+ case 1 -> elementSize (ct , array [0 ]);
302302 default -> {
303303 final int offset = ThreadLocalRandom .current ().nextInt (len );
304304 int sz = 0 ;
305305 for (int i = offset ; i < len ; ++i ) {
306- sz += elementSize (array [i ], ct );
306+ sz += elementSize (ct , array [i ]);
307307 }
308308 for (int i = 0 ; i < offset ; ++i ) {
309- sz += elementSize (array [i ], ct );
309+ sz += elementSize (ct , array [i ]);
310310 }
311311 yield sz ;
312312 }
313313 };
314314 }
315315
316- private static int elementSize (final Branch <?, ?> elem , final ImmutableTrieMap <?, ?> ct ) {
316+ private static int elementSize (final ImmutableTrieMap <?, ?> ct , final Branch <?, ?> elem ) {
317317 if (elem instanceof SNode ) {
318318 return 1 ;
319319 } else if (elem instanceof INode <?, ?> inode ) {
@@ -353,13 +353,13 @@ private CNode<K, V> toUpdatedAt(final CNode<K, V> prev, final int pos, final Bra
353353 * Returns a copy of this cnode such that all the i-nodes below it are copied to the specified generation
354354 * {@code ngen}.
355355 */
356- private CNode <K , V > renewed (final Gen ngen , final TrieMap <K , V > ct ) {
356+ private CNode <K , V > renewed (final TrieMap <K , V > ct , final Gen ngen ) {
357357 final var arr = array ;
358358 final int len = arr .length ;
359359 final var narr = newArray (len );
360360 for (int i = 0 ; i < len ; i ++) {
361361 final var tmp = arr [i ];
362- narr [i ] = tmp instanceof INode <K , V > in ? in .copyToGen (ngen , ct ) : tmp ;
362+ narr [i ] = tmp instanceof INode <K , V > in ? in .copyToGen (ct , ngen ) : tmp ;
363363 }
364364 return new CNode <>(this , ngen , bitmap , narr );
365365 }
@@ -374,10 +374,6 @@ private Branch<K, V>[] newArray(final int size) {
374374 return new Branch [size ];
375375 }
376376
377- private static <K , V > Branch <K , V > resurrect (final INode <K , V > in , final TrieMap <K , V > ct ) {
378- return in .gcasReadNonNull (ct ) instanceof TNode <K , V > tn ? new SNode <>(tn ) : in ;
379- }
380-
381377 // Visible for testing
382378 static VerifyException invalidElement (final Branch <?, ?> elem ) {
383379 throw new VerifyException ("A CNode can contain only INodes and SNodes, not " + elem );
0 commit comments