コード例 #1
0
ファイル: CNode.cs プロジェクト: jsbattig/nctrie
        // lends itself towards being parallelizable by choosing
        // a random starting offset in the array
        // => if there are concurrent size computations, they start
        // at different positions, so they are more likely to
        // to be independent
        private int computeSize(ConcurrentTrieDictionary <K, V> ct)
        {
            int i  = 0;
            int sz = 0;
            //  int offset = (array.length > 0) ?
            // // util.Random.nextInt(array.length) /* <-- benchmarks show that
            // // this causes observable contention */
            // scala.concurrent.forkjoin.ThreadLocalRandom.current.nextInt (0,
            // array.length)
            // : 0;

            int offset = 0;

            while (i < array.Length)
            {
                int       pos  = (i + offset) % array.Length;
                BasicNode elem = array[pos];
                if (elem is SNode <K, V> )
                {
                    sz += 1;
                }
                else if (elem is INode <K, V> )
                {
                    sz += ((INode <K, V>)elem).cachedSize(ct);
                }
                i += 1;
            }
            return(sz);
        }
コード例 #2
0
 void advance()
 {
     if (depth >= 0)
     {
         int npos = stackpos[depth] + 1;
         if (npos < stack[depth].Length)
         {
             stackpos[depth] = npos;
             BasicNode elem = stack[depth][npos];
             if (elem is SNode <K, V> )
             {
                 current = (SNode <K, V>)elem;
             }
             else if (elem is INode <K, V> )
             {
                 readin((INode <K, V>)elem);
             }
         }
         else
         {
             depth -= 1;
             advance();
         }
     }
     else
     {
         current = null;
     }
 }
コード例 #3
0
ファイル: CNode.cs プロジェクト: jsbattig/nctrie
        // - if the branching factor is 1 for this CNode, and the child
        // is a tombed SNode, returns its tombed version
        // - otherwise, if there is at least one non-null node below,
        // returns the version of this node with at least some null-inodes
        // removed (those existing when the op began)
        // - if there are only null-i-nodes below, returns null
        public MainNode <K, V> toCompressed(ConcurrentTrieDictionary <K, V> ct, int lev, Gen gen)
        {
            int bmp = bitmap;
            int i   = 0;

            BasicNode[] arr      = array;
            BasicNode[] tmparray = new BasicNode[arr.Length];
            while (i < arr.Length)
            { // construct new bitmap
                BasicNode sub = arr[i];
                if (sub is INode <K, V> )
                {
                    INode <K, V>    _in       = (INode <K, V>)sub;
                    MainNode <K, V> inodemain = _in.gcasRead(ct);
                    // assert(inodemain != null);
                    tmparray[i] = resurrect(_in, inodemain);
                }
                else if (sub is SNode <K, V> )
                {
                    tmparray[i] = sub;
                }
                i += 1;
            }

            return(new CNode <K, V>(bmp, tmparray, gen).toContracted(lev));
        }
コード例 #4
0
ファイル: CNode.cs プロジェクト: jsbattig/nctrie
        public CNode <K, V> removedAt(int pos, int flag, Gen gen)
        {
            BasicNode[] arr = array;
            int         len = arr.Length;

            BasicNode[] narr = new BasicNode[len - 1];
            JavaCompat.ArrayCopy(arr, 0, narr, 0, pos);
            JavaCompat.ArrayCopy(arr, pos + 1, narr, pos, len - pos - 1);
            return(new CNode <K, V>(bitmap ^ flag, narr, gen));
        }
コード例 #5
0
ファイル: CNode.cs プロジェクト: jsbattig/nctrie
        public CNode <K, V> updatedAt(int pos, BasicNode nn, Gen gen)
        {
            int len = array.Length;

            BasicNode[] narr = new BasicNode[len];
            JavaCompat.ArrayCopy(array, 0, narr, 0, len);
            array.CopyTo(narr, 0);
            narr[pos] = nn;
            return(new CNode <K, V>(bitmap, narr, gen));
        }
コード例 #6
0
ファイル: CNode.cs プロジェクト: jsbattig/nctrie
        public CNode <K, V> insertedAt(int pos, int flag, BasicNode nn, Gen gen)
        {
            int len = array.Length;
            int bmp = bitmap;

            BasicNode[] narr = new BasicNode[len + 1];
            JavaCompat.ArrayCopy(array, 0, narr, 0, pos);
            narr[pos] = nn;
            JavaCompat.ArrayCopy(array, pos, narr, pos + 1, len - pos);
            return(new CNode <K, V>(bmp | flag, narr, gen));
        }
コード例 #7
0
ファイル: INode.cs プロジェクト: jsbattig/nctrie
 void cleanParent(Object nonlive, INode <K, V> parent, ConcurrentTrieDictionary <K, V> ct, int hc, int lev, Gen startgen)
 {
     while (true)
     {
         MainNode <K, V> pm = parent.GCAS_READ(ct);
         if (pm is CNode <K, V> )
         {
             CNode <K, V> cn   = (CNode <K, V>)pm;
             int          idx  = (int)((uint)hc >> (lev - 5)) & 0x1f;
             int          bmp  = cn.bitmap;
             int          flag = 1 << idx;
             if ((bmp & flag) == 0)
             {
             } // somebody already removed this i-node, we're done
             else
             {
                 int       pos = JavaCompat.SparseBitcount(bmp & (flag - 1));
                 BasicNode sub = cn.array[pos];
                 if (sub == this)
                 {
                     if (nonlive is TNode <K, V> )
                     {
                         TNode <K, V>    tn  = (TNode <K, V>)nonlive;
                         MainNode <K, V> ncn = cn.updatedAt(pos, tn.copyUntombed(), gen).toContracted(lev - 5);
                         if (!parent.GCAS(cn, ncn, ct))
                         {
                             if (ct.readRoot().gen == startgen)
                             {
                                 // cleanParent (nonlive, parent, ct, hc,
                                 // lev, startgen);
                                 // tailrec
                                 continue;
                             }
                         }
                     }
                 }
             }
         }
         else
         {
             // parent is no longer a cnode, we're done
         }
         break;
     }
 }
コード例 #8
0
ファイル: CNode.cs プロジェクト: jsbattig/nctrie
        /**
         * Returns a copy of this cnode such that all the i-nodes below it are
         * copied to the specified generation `ngen`.
         */
        public CNode <K, V> renewed(Gen ngen, ConcurrentTrieDictionary <K, V> ct)
        {
            int i = 0;

            BasicNode[] arr = array;
            int         len = arr.Length;

            BasicNode[] narr = new BasicNode[len];
            while (i < len)
            {
                BasicNode elem = arr[i];
                if (elem is INode <K, V> )
                {
                    INode <K, V> _in = (INode <K, V>)elem;
                    narr[i] = _in.copyToGen(ngen, ct);
                }
                else if (elem is BasicNode)
                {
                    narr[i] = elem;
                }
                i += 1;
            }
            return(new CNode <K, V>(bitmap, narr, ngen));
        }
コード例 #9
0
ファイル: INode.cs プロジェクト: jsbattig/nctrie
        /**
         * Removes the key associated with the given value.
         *
         * @param v
         *            if null, will remove the key irregardless of the value;
         *            otherwise removes only if binding contains that exact key
         *            and value
         * @return null if not successful, an Option[V] indicating the previous
         *         value otherwise
         */
        public Option <V> rec_remove(K k, V v, int hc, int lev, INode <K, V> parent, Gen startgen, ConcurrentTrieDictionary <K, V> ct)
        {
            MainNode <K, V> m = GCAS_READ(ct); // use -Yinline!

            if (m is CNode <K, V> )
            {
                CNode <K, V> cn   = (CNode <K, V>)m;
                int          idx  = (int)((uint)hc >> lev) & 0x1f;
                int          bmp  = cn.bitmap;
                int          flag = 1 << idx;
                if ((bmp & flag) == 0)
                {
                    return(Option <V> .makeOption());
                }
                else
                {
                    int        pos = JavaCompat.SparseBitcount(bmp & (flag - 1));
                    BasicNode  sub = cn.array[pos];
                    Option <V> res = null;
                    if (sub is INode <K, V> )
                    {
                        INode <K, V> _in = (INode <K, V>)sub;
                        if (startgen == _in.gen)
                        {
                            res = _in.rec_remove(k, v, hc, lev + 5, this, startgen, ct);
                        }
                        else
                        {
                            if (GCAS(cn, cn.renewed(startgen, ct), ct))
                            {
                                res = rec_remove(k, v, hc, lev, parent, startgen, ct);
                            }
                            else
                            {
                                res = null;
                            }
                        }
                    }
                    else if (sub is SNode <K, V> )
                    {
                        SNode <K, V> sn = (SNode <K, V>)sub;
                        if (sn.hc == hc && equal(sn.k, k, ct) && (v == null || v.Equals(sn.v)))
                        {
                            MainNode <K, V> ncn = cn.removedAt(pos, flag, gen).toContracted(lev);
                            if (GCAS(cn, ncn, ct))
                            {
                                res = Option <V> .makeOption(sn.v);
                            }
                            else
                            {
                                res = null;
                            }
                        }
                        else
                        {
                            res = Option <V> .makeOption();
                        }
                    }

                    if (res is None <V> || (res == null))
                    {
                        return(res);
                    }
                    else
                    {
                        if (parent != null)
                        { // never tomb at root
                            MainNode <K, V> n = GCAS_READ(ct);
                            if (n is TNode <K, V> )
                            {
                                cleanParent(n, parent, ct, hc, lev, startgen);
                            }
                        }

                        return(res);
                    }
                }
            }
            else if (m is TNode <K, V> )
            {
                clean(parent, ct, lev - 5);
                return(null);
            }
            else if (m is LNode <K, V> )
            {
                LNode <K, V> ln = (LNode <K, V>)m;
                if (v == null)
                {
                    Option <V>      optv = ln.get(k);
                    MainNode <K, V> nn   = ln.removed(k, ct);
                    if (GCAS(ln, nn, ct))
                    {
                        return(optv);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    Option <V> tmp = ln.get(k);
                    if (tmp is Some <V> )
                    {
                        Some <V> tmp1 = (Some <V>)tmp;
                        if (tmp1.Equals(v))
                        {
                            MainNode <K, V> nn = ln.removed(k, ct);
                            if (GCAS(ln, nn, ct))
                            {
                                return(tmp);
                            }
                            else
                            {
                                return(null);
                            }
                        }
                    }
                }
            }
            throw new RuntimeException("Should not happen");
        }
コード例 #10
0
ファイル: INode.cs プロジェクト: jsbattig/nctrie
        /**
         * Looks up the value associated with the key.
         *
         * @return null if no value has been found, RESTART if the operation
         *         wasn't successful, or any other value otherwise
         */
        public Object rec_lookup(K k, 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 multinode
                    CNode <K, V> cn   = (CNode <K, V>)m;
                    int          idx  = (int)((uint)hc >> lev) & 0x1f;
                    int          flag = 1 << idx;
                    int          bmp  = cn.bitmap;
                    if ((bmp & flag) == 0)
                    {
                        return(null); // 1a) bitmap shows no binding
                    }
                    else
                    { // 1b) bitmap contains a value - descend
                        int       pos = ((uint)bmp == 0xffffffff) ? idx : JavaCompat.SparseBitcount(bmp & (flag - 1));
                        BasicNode sub = cn.array[pos];
                        if (sub is INode <K, V> )
                        {
                            INode <K, V> _in = (INode <K, V>)sub;
                            if (ct.isReadOnly() || (startgen == ((INodeBase <K, V>)sub).gen))
                            {
                                return(_in.rec_lookup(k, hc, lev + 5, this, startgen, ct));
                            }
                            else
                            {
                                if (GCAS(cn, cn.renewed(startgen, ct), ct))
                                {
                                    // return rec_lookup (k, hc, lev, parent,
                                    // startgen, ct);
                                    // Tailrec
                                    continue;
                                }
                                else
                                {
                                    return(RESTART); // used to be throw
                                }
                                // RestartException
                            }
                        }
                        else if (sub is SNode <K, V> )
                        {
                            // 2) singleton node
                            SNode <K, V> sn = (SNode <K, V>)sub;
                            if (((SNode <K, V>)sub).hc == hc && equal(sn.k, k, ct))
                            {
                                return(sn.v);
                            }
                            else
                            {
                                return(null);
                            }
                        }
                    }
                }
                else if (m is TNode <K, V> )
                {
                    // 3) non-live node
                    return(cleanReadOnly((TNode <K, V>)m, lev, parent, ct, k, hc));
                }
                else if (m is LNode <K, V> )
                {
                    // 5) an l-node
                    Option <V> tmp = ((LNode <K, V>)m).get(k);
                    return((tmp is Option <V>) ? tmp : null);
                }
                throw new RuntimeException("Should not happen");
            }
        }
コード例 #11
0
ファイル: INode.cs プロジェクト: jsbattig/nctrie
        /**
         * Inserts a new key value pair, given that a specific condition is met.
         *
         * @param cond
         *            null - don't care if the key was there
         *            KEY_ABSENT - key wasn't there
         *            KEY_PRESENT - key was there
         *            other value `v` - key must be bound to `v`
         * @return null if unsuccessful, Option[V] otherwise (indicating
         *         previous value bound to the key)
         */
        public Option <V> rec_insertif(K k, V v, int hc, Object cond, 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_insertif(k, v, hc, cond, lev + 5, this, startgen, ct));
                            }
                            else
                            {
                                if (GCAS(cn, cn.renewed(startgen, ct), ct))
                                {
                                    // return rec_insertif (k, v, hc, cond, lev,
                                    // parent, startgen, ct);
                                    // tailrec
                                    continue;
                                }
                                else
                                {
                                    return(null);
                                }
                            }
                        }
                        else if (cnAtPos is SNode <K, V> )
                        {
                            SNode <K, V> sn = (SNode <K, V>)cnAtPos;
                            if (cond == null)
                            {
                                if (sn.hc == hc && equal(sn.k, k, ct))
                                {
                                    if (GCAS(cn, cn.updatedAt(pos, new SNode <K, V>(k, v, hc), gen), ct))
                                    {
                                        return(Option <V> .makeOption(sn.v));
                                    }
                                    else
                                    {
                                        return(null);
                                    }
                                }
                                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);
                                    if (GCAS(cn, nn, ct))
                                    {
                                        return(Option <V> .makeOption()); // None;
                                    }
                                    else
                                    {
                                        return(null);
                                    }
                                }
                            }
                            else if (cond == INode <K, V> .KEY_ABSENT)
                            {
                                if (sn.hc == hc && equal(sn.k, k, ct))
                                {
                                    return(Option <V> .makeOption(sn.v));
                                }
                                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);
                                    if (GCAS(cn, nn, ct))
                                    {
                                        return(Option <V> .makeOption()); // None
                                    }
                                    else
                                    {
                                        return(null);
                                    }
                                }
                            }
                            else if (cond == INode <K, V> .KEY_PRESENT)
                            {
                                if (sn.hc == hc && equal(sn.k, k, ct))
                                {
                                    if (GCAS(cn, cn.updatedAt(pos, new SNode <K, V>(k, v, hc), gen), ct))
                                    {
                                        return(Option <V> .makeOption(sn.v));
                                    }
                                    else
                                    {
                                        return(null);
                                    }
                                }
                                else
                                {
                                    return(Option <V> .makeOption());// None;
                                }
                            }
                            else
                            {
                                if (sn.hc == hc && equal(sn.k, k, ct) && sn.v.Equals(cond))
                                {
                                    if (GCAS(cn, cn.updatedAt(pos, new SNode <K, V>(k, v, hc), gen), ct))
                                    {
                                        return(Option <V> .makeOption(sn.v));
                                    }
                                    else
                                    {
                                        return(null);
                                    }
                                }
                                else
                                {
                                    return(Option <V> .makeOption()); // None
                                }
                            }
                        }
                    }
                    else if (cond == null || cond == INode <K, V> .KEY_ABSENT)
                    {
                        CNode <K, V> rn     = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
                        CNode <K, V> ncnode = rn.insertedAt(pos, flag, new SNode <K, V>(k, v, hc), gen);
                        if (GCAS(cn, ncnode, ct))
                        {
                            return(Option <V> .makeOption());// None
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else if (cond == INode <K, V> .KEY_PRESENT)
                    {
                        return(Option <V> .makeOption());// None;
                    }
                    else
                    {
                        return(Option <V> .makeOption()); // None
                    }
                }
                else if (m is TNode <K, V> )
                {
                    clean(parent, ct, lev - 5);
                    return(null);
                }
                else if (m is LNode <K, V> )
                {
                    // 3) an l-node
                    LNode <K, V> ln = (LNode <K, V>)m;
                    if (cond == null)
                    {
                        Option <V> optv = ln.get(k);
                        if (insertln(ln, k, v, ct))
                        {
                            return(optv);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else if (cond == INode <K, V> .KEY_ABSENT)
                    {
                        Option <V> t = ln.get(k);
                        if (t == null)
                        {
                            if (insertln(ln, k, v, ct))
                            {
                                return(Option <V> .makeOption());// None
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        else
                        {
                            return(t);
                        }
                    }
                    else if (cond == INode <K, V> .KEY_PRESENT)
                    {
                        Option <V> t = ln.get(k);
                        if (t != null)
                        {
                            if (insertln(ln, k, v, ct))
                            {
                                return(t);
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        else
                        {
                            return(null); // None
                        }
                    }
                    else
                    {
                        Option <V> t = ln.get(k);
                        if (t != null)
                        {
                            if (((Some <V>)t).get().Equals(cond))
                            {
                                if (insertln(ln, k, v, ct))
                                {
                                    return(new Some <V>((V)cond));
                                }
                                else
                                {
                                    return(null);
                                }
                            }
                            else
                            {
                                return(Option <V> .makeOption());
                            }
                        }
                    }
                }
                //                throw new RuntimeException ("Should not happen");
            }
        }
コード例 #12
0
ファイル: INode.cs プロジェクト: jsbattig/nctrie
        /**
         * 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");
            }
        }