Example #1
0
            internal bool IsSupersetOf(Node other)
            {
                if (other.Count > Count)
                {
                    return(false);
                }
                var iter = new TreeIterator(this);

                return(other.ForEachWhileNode(node => {
                    if (!iter.MoveNext())
                    {
                        return false;
                    }
                    var myCur = iter.Current;
                    var compare = Comparer.Compare(node.Key, myCur.Key);
                    if (compare > 0)
                    {
                        while ((compare = Comparer.Compare(node.Key, iter.Current.Key)) > 0)
                        {
                            if (!iter.MoveNext())
                            {
                                return false;
                            }
                        }
                    }
                    return compare == 0;
                }));
            }
Example #2
0
        private long?TryFindSmallValue(Transaction tx, TreeIterator it, int num)
        {
            do
            {
                var stream = it.CreateReaderForCurrent();
                {
                    var current          = new StreamBitArray(stream);
                    var currentSectionId = it.CurrentKey.CreateReader().ReadBigEndianInt64();

                    long?page;
                    if (current.SetCount < num)
                    {
                        if (TryFindSmallValueMergingTwoSections(tx, it, num, current, currentSectionId, out page))
                        {
                            return(page);
                        }
                        continue;
                    }

                    if (TryFindContinuousRange(tx, it, num, current, currentSectionId, out page))
                    {
                        return(page);
                    }

                    //could not find a continuous so trying to merge
                    if (TryFindSmallValueMergingTwoSections(tx, it, num, current, currentSectionId, out page))
                    {
                        return(page);
                    }
                }
            } while (it.MoveNext());

            return(null);
        }
        public bool FindTarget(TreeNode root, int k)
        {
            var left  = new TreeIterator(root, true);
            var right = new TreeIterator(root, false);

            while (left.Current != right.Current)
            {
                int sum = left.Current.val + right.Current.val;
                if (sum == k)
                {
                    return(true);
                }

                if (sum < k)
                {
                    left.MoveNext();
                }
                else
                {
                    right.MoveNext();
                }
            }

            return(false);
        }
Example #4
0
        public void TestFlatAB()
        {
            ITreeAdaptor  adaptor = new CommonTreeAdaptor();
            TreeWizard    wiz     = new TreeWizard(adaptor, tokens);
            CommonTree    t       = (CommonTree)wiz.Create("(nil A B)");
            TreeIterator  it      = new TreeIterator(t);
            StringBuilder buf     = new StringBuilder();
            bool          first   = true;

            while (it.MoveNext())
            {
                CommonTree n = (CommonTree)it.Current;

                if (!first)
                {
                    buf.Append(" ");
                }

                first = false;
                buf.Append(n);
            }
            string expecting = "nil DOWN A B UP EOF";
            string found     = buf.ToString();

            Assert.AreEqual(expecting, found);
        }
Example #5
0
            public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator()
            {
                var iterator = new TreeIterator(this);

                while (iterator.MoveNext())
                {
                    yield return(Kvp.Of(iterator.Current.Key, iterator.Current.Value));
                }
            }
Example #6
0
            public bool IsSupersetOf(Node other)
            {
                if (other.NodeCount > NodeCount)
                {
                    return(false);
                }
                if (other.Count > Count)
                {
                    return(false);
                }
                var iter = new TreeIterator(this);

                //The idea is tht we go over the nodes in order of hash, from smallest to largest.
                //If we find a node in `other` that is smaller than the current node in `this
                //This means that node doesn't exist in `this` at all, so it isn't a superset.
                return(other.ForEachWhileNode(node => {
                    if (!iter.MoveNext())
                    {
                        return false;
                    }
                    var cur = iter.Current;
                    if (node.Hash > cur.Hash)
                    {
                        while (node.Hash > iter.Current.Hash)
                        {
                            if (!iter.MoveNext())
                            {
                                return false;
                            }
                        }
                        cur = iter.Current;
                    }
                    if (node.Hash < cur.Hash)
                    {
                        return false;
                    }
                    var result = node.Bucket.ForEachWhile((k, v) => cur.Bucket.Find(k).IsSome);
                    return result;
                }));
            }
Example #7
0
            public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator()
            {
                var iterator = new TreeIterator(this);

                while (iterator.MoveNext())
                {
                    var bucket = iterator.Current.Bucket;
                    while (!bucket.IsEmpty)
                    {
                        yield return(Kvp.Of(bucket.Key, bucket.Value));

                        bucket = bucket.Next;
                    }
                }
            }
Example #8
0
            /// <summary>
            ///     Returns an iterator for retrieving all the elements shared between this tree and another tree.
            /// </summary>
            /// <param name="other"></param>
            /// <returns></returns>
            public IEnumerable <StructTuple <Node, Node> > IntersectElements(Node other)
            {
                if (IsEmpty || other.IsEmpty)
                {
                    yield break;
                }
                var aIterator = new TreeIterator(this);
                var bIterator = new TreeIterator(other);
                var pivotInA  = true;
                var success   = aIterator.MoveNext();
                var pivot     = aIterator.Current;

                while (!aIterator.IsEnded || !bIterator.IsEnded)
                {
                    var trySeekIn = pivotInA ? bIterator : aIterator;
                    int cmpResult;
                    success = trySeekIn.SeekGreaterThan(pivot.Key, out cmpResult);
                    if (!success)
                    {
                        break;
                    }
                    var maybePivot = trySeekIn.Current;
                    if (cmpResult == 0)
                    {
                        yield return(pivotInA ? StructTuple.Create(pivot, maybePivot) : StructTuple.Create(maybePivot, pivot));

                        pivotInA  = !pivotInA;
                        trySeekIn = pivotInA ? aIterator : bIterator;
                        success   = trySeekIn.MoveNext();
                        if (!success)
                        {
                            break;
                        }
                        pivot = trySeekIn.Current;
                    }
                    else
                    {
                        pivot    = maybePivot;
                        pivotInA = !pivotInA;                         //If the pivot was in X, it's now in Y...
                    }
                }
            }
Example #9
0
        public void TestAB()
        {
            ITreeAdaptor adaptor = new CommonTreeAdaptor();
            TreeWizard wiz = new TreeWizard( adaptor, tokens );
            CommonTree t = (CommonTree)wiz.Create( "(A B)" );
            TreeIterator it = new TreeIterator( t );
            StringBuilder buf = new StringBuilder();
            bool first = true;
            while ( it.MoveNext() )
            {
                CommonTree n = (CommonTree)it.Current;

                if ( !first )
                    buf.Append( " " );

                first = false;
                buf.Append( n );
            }
            string expecting = "A DOWN B UP EOF";
            string found = buf.ToString();
            Assert.AreEqual( expecting, found );
        }
Example #10
0
        private long?TryFindLargeValue(Transaction tx, TreeIterator it, int num)
        {
            int   numberOfNeededFullSections = num / NumberOfPagesInSection;
            int   numberOfExtraBitsNeeded    = num % NumberOfPagesInSection;
            int   foundSections  = 0;
            Slice startSection   = null;
            long? startSectionId = null;
            var   sections       = new List <Slice>();

            do
            {
                var stream = it.CreateReaderForCurrent();
                {
                    var current          = new StreamBitArray(stream);
                    var currentSectionId = it.CurrentKey.CreateReader().ReadBigEndianInt64();

                    //need to find full free pages
                    if (current.SetCount < NumberOfPagesInSection)
                    {
                        ResetSections(ref foundSections, sections, ref startSection, ref startSectionId);
                        continue;
                    }

                    //those sections are not following each other in the memory
                    if (startSectionId != null && currentSectionId != startSectionId + foundSections)
                    {
                        ResetSections(ref foundSections, sections, ref startSection, ref startSectionId);
                    }

                    //set the first section of the sequence
                    if (startSection == null)
                    {
                        startSection   = it.CurrentKey;
                        startSectionId = currentSectionId;
                    }

                    sections.Add(it.CurrentKey);
                    foundSections++;

                    if (foundSections != numberOfNeededFullSections)
                    {
                        continue;
                    }

                    //we found enough full sections now we need just a bit more
                    if (numberOfExtraBitsNeeded == 0)
                    {
                        foreach (var section in sections)
                        {
                            tx.State.FreeSpaceRoot.Delete(section);
                        }

                        return(startSectionId * NumberOfPagesInSection);
                    }

                    var nextSectionId = currentSectionId + 1;
                    var nextId        = new Slice(EndianBitConverter.Big.GetBytes(nextSectionId));
                    var read          = tx.State.FreeSpaceRoot.Read(nextId);
                    if (read == null)
                    {
                        //not a following next section
                        ResetSections(ref foundSections, sections, ref startSection, ref startSectionId);
                        continue;
                    }

                    var next = new StreamBitArray(read.Reader);

                    if (next.HasStartRangeCount(numberOfExtraBitsNeeded) == false)
                    {
                        //not enough start range count
                        ResetSections(ref foundSections, sections, ref startSection, ref startSectionId);
                        continue;
                    }

                    //mark selected bits to false
                    if (next.SetCount == numberOfExtraBitsNeeded)
                    {
                        tx.State.FreeSpaceRoot.Delete(nextId);
                    }
                    else
                    {
                        for (int i = 0; i < numberOfExtraBitsNeeded; i++)
                        {
                            next.Set(i, false);
                        }
                        tx.State.FreeSpaceRoot.Add(nextId, next.ToStream());
                    }

                    foreach (var section in sections)
                    {
                        tx.State.FreeSpaceRoot.Delete(section);
                    }

                    return(startSectionId * NumberOfPagesInSection);
                }
            } while (it.MoveNext());

            return(null);
        }