コード例 #1
0
        private static bool AddNonDuplicateExtracted(ref AVLNode <TKey, TValue>?node, TKey key, TValue value, IComparer <TKey> comparer, AVLNode <TKey, TValue>?created)
        {
            // Ok, it has for node only
            if (node == null)
            {
                if (created == null)
                {
                    created = new AVLNode <TKey, TValue>(key, value);
                }

                var found = Interlocked.CompareExchange(ref node, created, null);
                if (found == null)
                {
                    return(true);
                }

                node = found;
            }

            var compare = comparer.Compare(key, node.Key);

            if (compare == 0)
            {
                return(false);
            }

            try
            {
                return(compare < 0
                    ? AddNonDuplicateExtracted(ref node._left, key, value, comparer, created)
                    : AddNonDuplicateExtracted(ref node._right, key, value, comparer, created));
            }
            finally
            {
                MakeBalanced(ref node);
            }
        }
コード例 #2
0
        internal static void Add(ref AVLNode <TKey, TValue>?node, TKey key, TValue value, IComparer <TKey> comparer)
        {
            var created = new AVLNode <TKey, TValue>(key, value);

            AddExtracted(ref node, key, comparer, created);
        }
コード例 #3
0
 internal static void Bound(AVLNode <TKey, TValue> node, TKey key, IComparer <TKey> comparer, out AVLNode <TKey, TValue> lower, out AVLNode <TKey, TValue> upper)
 {
     lower = null;
     upper = null;
     while (node != null)
     {
         var compare = comparer.Compare(key, node.Key);
         if (compare <= 0)
         {
             upper = node;
         }
         if (compare >= 0)
         {
             lower = node;
         }
         if (compare == 0)
         {
             break;
         }
         node = compare < 0 ? node._left : node._right;
     }
 }
コード例 #4
0
        internal static AVLNode <TKey, TValue>?RemoveNearestRight(ref AVLNode <TKey, TValue>?node, TKey key, IComparer <TKey> comparer)
        {
            AVLNode <TKey, TValue>?result = null;

            return(RemoveNearestRightExtracted(ref node, ref result, key, comparer));
        }
コード例 #5
0
        private static bool AddNonDuplicateExtracted(ref AVLNode <TKey, TValue>?node, TKey key, TValue value, IComparer <TKey> comparer, AVLNode <TKey, TValue>?created)
        {
#if DEBUG
            // NOTICE this method has no null check in the public build as an optimization, this is just to appease the dragons
            if (comparer == null)
            {
                throw new ArgumentNullException(nameof(comparer));
            }
#endif
            // Ok, it has for node only
            if (node == null)
            {
                if (created == null)
                {
                    created = new AVLNode <TKey, TValue>(key, value);
                }

                var found = Interlocked.CompareExchange(ref node, created, null);
                if (found == null)
                {
                    return(true);
                }

                node = found;
            }

            var compare = comparer.Compare(key, node.Key);
            if (compare == 0)
            {
                return(false);
            }

            try
            {
                return(compare < 0 ? AddNonDuplicateExtracted(ref node._left, key, value, comparer, created) : AddNonDuplicateExtracted(ref node._right, key, value, comparer, created));
            }
            finally
            {
                MakeBalanced(ref node);
            }
        }
コード例 #6
0
 public void Clear()
 {
     _root  = null;
     _count = 0;
 }
コード例 #7
0
 public AVLNode GetNearestRight(TKey key)
 {
     return(AVLNode.GetNearestRight(_root, key, _comparison));
 }
コード例 #8
0
        public void Add(TKey key, TValue value)
        {
            AVLNode <TKey, TValue> .Add(ref _root, key, value, _comparer);

            Count++;
        }
コード例 #9
0
 public void Bound(TKey key, out AVLNode <TKey, TValue>?lower, out AVLNode <TKey, TValue>?upper)
 {
     AVLNode <TKey, TValue> .Bound(_root, key, _comparer, out lower, out upper);
 }
コード例 #10
0
 public AVLTree()
 {
     _root     = null;
     _comparer = Comparer <TKey> .Default;
 }
コード例 #11
0
 public AVLTree(IComparer <TKey> comparer)
 {
     _root     = null;
     _comparer = comparer ?? Comparer <TKey> .Default;
 }
コード例 #12
0
ファイル: AVLTree.cs プロジェクト: NN---/Theraot
 public AVLNode GetNearestLeft(TKey key)
 {
     return(AVLNode.GetNearestLeft(_root, key, _comparer));
 }
コード例 #13
0
ファイル: AVLTree.cs プロジェクト: NN---/Theraot
            internal static AVLNode RemoveNearestLeft(ref AVLNode node, TKey key, IComparer <TKey> comparer)
            {
                AVLNode result = null;

                return(RemoveNearestLeftExtracted(ref node, ref result, key, comparer));
            }
コード例 #14
0
ファイル: AVLTree.cs プロジェクト: NN---/Theraot
 public AVLNode RemoveNearestLeft(TKey key)
 {
     return(AVLNode.RemoveNearestLeft(ref _root, key, _comparer));
 }
コード例 #15
0
 public void Add(TKey key, TValue value)
 {
     AVLNode.Add(ref _root, key, value, _comparison);
     _count++;
 }
コード例 #16
0
 public IEnumerator <AVLNode <TKey, TValue> > GetEnumerator()
 {
     return(AVLNode <TKey, TValue> .EnumerateRoot(_root).GetEnumerator());
 }
コード例 #17
0
 public void Bound(TKey key, out AVLNode lower, out AVLNode upper)
 {
     AVLNode.Bound(_root, key, _comparison, out lower, out upper);
 }
コード例 #18
0
 public AVLNode <TKey, TValue>?GetNearestRight(TKey key)
 {
     return(AVLNode <TKey, TValue> .GetNearestRight(_root, key, _comparer));
 }
コード例 #19
0
 public IEnumerator <AVLNode> GetEnumerator()
 {
     return(AVLNode.EnumerateRoot(_root).GetEnumerator());
 }
コード例 #20
0
        private static AVLNode <TKey, TValue> GetOrAddExtracted(ref AVLNode <TKey, TValue>?node, TKey key, Func <TKey, TValue> factory, IComparer <TKey> comparer, AVLNode <TKey, TValue>?created, out bool isNew)
        {
            if (node == null)
            {
                if (created == null)
                {
                    created = new AVLNode <TKey, TValue>(key, factory(key));
                }
                var found = Interlocked.CompareExchange(ref node, created, null);
                if (found == null)
                {
                    isNew = true;
                    return(created);
                }
                node = found;
            }
            var compare = comparer.Compare(key, node.Key);

            if (compare == 0)
            {
                isNew = false;
                return(node);
            }
            try
            {
                return(compare < 0
                           ? GetOrAddExtracted(ref node._left, key, factory, comparer, created, out isNew)
                           : GetOrAddExtracted(ref node._right, key, factory, comparer, created, out isNew));
            }
            finally
            {
                MakeBalanced(ref node);
            }
        }
コード例 #21
0
 internal static AVLNode <TKey, TValue> GetOrAdd(ref AVLNode <TKey, TValue> node, TKey key, Func <TKey, TValue> factory, IComparer <TKey> comparer, out bool isNew)
 {
     return(GetOrAddExtracted(ref node, key, factory, comparer, null, out isNew));
 }
コード例 #22
0
 public AVLNode RemoveNearestRight(TKey key)
 {
     return(AVLNode.RemoveNearestRight(ref _root, key, _comparison));
 }
コード例 #23
0
        private static void AddExtracted(ref AVLNode <TKey, TValue>?node, TKey key, IComparer <TKey> comparer, AVLNode <TKey, TValue> created)
        {
#if DEBUG
            // NOTICE this method has no null check in the public build as an optimization, this is just to appease the dragons
            if (comparer == null)
            {
                throw new ArgumentNullException(nameof(comparer));
            }
#endif
            // Ok, it has for node only
            int compare;
            if (node == null || (compare = comparer.Compare(key, node.Key)) == 0)
            {
                if (Interlocked.CompareExchange(ref node, created, null) == null)
                {
                    return;
                }

                compare = -node._balance;
            }

            if (compare < 0)
            {
                AddExtracted(ref node._left, key, comparer, created);
            }
            else
            {
                AddExtracted(ref node._right, key, comparer, created);
            }

            MakeBalanced(ref node);
        }
コード例 #24
0
 public AVLTree()
 {
     _root       = null;
     _comparison = Comparer <TKey> .Default.Compare;
 }
コード例 #25
0
        private static AVLNode <TKey, TValue> GetOrAddExtracted(ref AVLNode <TKey, TValue> node, TKey key, Func <TKey, TValue> factory, IComparer <TKey> comparer, AVLNode <TKey, TValue> created, out bool isNew)
        {
#if DEBUG
            // NOTICE this method has no null check in the public build as an optimization, this is just to appease the dragons
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (comparer == null)
            {
                throw new ArgumentNullException(nameof(comparer));
            }
#endif
            // Ok, it has for node only
            if (node == null)
            {
                if (created == null)
                {
                    created = new AVLNode <TKey, TValue>(key, factory(key));
                    factory = null;
                }
                var found = Interlocked.CompareExchange(ref node, created, null);
                if (found == null)
                {
                    isNew = true;
                    return(created);
                }
                node = found;
            }
            var compare = comparer.Compare(key, node.Key);
            if (compare == 0)
            {
                isNew = false;
                return(node);
            }
            try
            {
                return(compare < 0
                           ? GetOrAddExtracted(ref node._left, key, factory, comparer, created, out isNew)
                           : GetOrAddExtracted(ref node._right, key, factory, comparer, created, out isNew));
            }
            finally
            {
                MakeBalanced(ref node);
            }
        }
コード例 #26
0
 public AVLTree(IComparer <TKey> comparer)
 {
     _root       = null;
     _comparison = (comparer ?? Comparer <TKey> .Default).Compare;
 }
コード例 #27
0
 internal static bool AddNonDuplicate(ref AVLNode <TKey, TValue>?node, TKey key, TValue value, IComparer <TKey> comparer)
 {
     return(AddNonDuplicateExtracted(ref node, key, value, comparer, null));
 }
コード例 #28
0
 public AVLTree(Comparison <TKey> comparison)
 {
     _root       = null;
     _comparison = comparison ?? Comparer <TKey> .Default.Compare;
 }
コード例 #29
0
ファイル: AVLTree.cs プロジェクト: csharpHub/Theraot
 public AVLNode <TKey, TValue>?RemoveNearestRight(TKey key)
 {
     return(AVLNode <TKey, TValue> .RemoveNearestRight(ref _root, key, _comparer));
 }
コード例 #30
0
        private static void AddExtracted(ref AVLNode <TKey, TValue>?node, TKey key, IComparer <TKey> comparer, AVLNode <TKey, TValue> created)
        {
            // Ok, it has for node only
            int compare;

            if (node == null || (compare = comparer.Compare(key, node.Key)) == 0)
            {
                if (Interlocked.CompareExchange(ref node, created, null) == null)
                {
                    return;
                }

                compare = -node._balance;
            }

            if (compare < 0)
            {
                AddExtracted(ref node._left, key, comparer, created);
            }
            else
            {
                AddExtracted(ref node._right, key, comparer, created);
            }

            MakeBalanced(ref node);
        }