/** * Inserts a key value pair, overwriting the old pair if the keys match. * * @return true if successful, false otherwise */ public bool rec_insert(K k, V v, int hc, int lev, INode <K, V> parent, Gen startgen, ConcurrentTrieDictionary <K, V> ct) { while (true) { MainNode <K, V> m = GCAS_READ(ct); // use -Yinline! if (m is CNode <K, V> ) { // 1) a multiway node CNode <K, V> cn = (CNode <K, V>)m; int idx = (int)((uint)hc >> lev) & 0x1f; int flag = 1 << idx; int bmp = cn.bitmap; int mask = flag - 1; int pos = JavaCompat.SparseBitcount(bmp & mask); if ((bmp & flag) != 0) { // 1a) insert below BasicNode cnAtPos = cn.array[pos]; if (cnAtPos is INode <K, V> ) { INode <K, V> _in = (INode <K, V>)cnAtPos; if (startgen == _in.gen) { return(_in.rec_insert(k, v, hc, lev + 5, this, startgen, ct)); } else { if (GCAS(cn, cn.renewed(startgen, ct), ct)) { // return rec_insert (k, v, hc, lev, parent, // startgen, ct); // tailrec continue; } else { return(false); } } } else if (cnAtPos is SNode <K, V> ) { SNode <K, V> sn = (SNode <K, V>)cnAtPos; if (sn.hc == hc && equal((K)sn.k, k, ct)) { return(GCAS(cn, cn.updatedAt(pos, new SNode <K, V>(k, v, hc), gen), ct)); } else { CNode <K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct); MainNode <K, V> nn = rn.updatedAt(pos, inode(CNode <K, V> .dual(sn, sn.hc, new SNode <K, V>(k, v, hc), hc, lev + 5, gen)), gen); return(GCAS(cn, nn, ct)); } } } else { CNode <K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct); MainNode <K, V> ncnode = rn.insertedAt(pos, flag, new SNode <K, V>(k, v, hc), gen); return(GCAS(cn, ncnode, ct)); } } else if (m is TNode <K, V> ) { clean(parent, ct, lev - 5); return(false); } else if (m is LNode <K, V> ) { LNode <K, V> ln = (LNode <K, V>)m; MainNode <K, V> nn = ln.inserted(k, v); return(GCAS(ln, nn, ct)); } throw new RuntimeException("Should not happen"); } }
private bool equal(K k1, K k2, ConcurrentTrieDictionary <K, V> ct) { return(ct.equality().equiv(k1, k2)); }
public ConcurrentTrieDictionaryIterator(int level, ConcurrentTrieDictionary <K, V> ct) : this(level, ct, true) { }
public EntrySet(ConcurrentTrieDictionary <K, V> t) { this.t = t; }
protected ConcurrentTrieDictionaryIterator <K, V> newIterator(int _lev, ConcurrentTrieDictionary <K, V> _ct, bool _mustInit) { return(new ConcurrentTrieDictionaryIterator <K, V>(_lev, _ct, _mustInit)); }