public ITreeSortNode <T> Insert(T aValue)
 {
     if (root == null)
     {
         if (DuplicateHandling == DuplicateHandlingType.CollectDuplicates)
         {
             root = new MultiInstanceAVLTreeSortNode <T> (aValue, Sentinel);
         }
         else
         {
             root = new SingleInstanceAVLTreeSortNode <T> (aValue, Sentinel);
         }
         NodeCount++;
         return(root);
     }
     else
     {
         ITreeSortNode <T> newNode = null;
         if (Root.InsertNode(args, ref root, aValue, out newNode) == true)
         {
             return(newNode);
         }
         return(null);
     }
 }
        private int IndexOf(IAVLTreeSortNode <T> aNode, T aValue, int aIdx)
        {
            if (aNode.IsSentinel == true)
            {
                return(-1);
            }
            int compare = OnSortMethod(aNode.Value, aValue);

            if (compare > 0)
            {
                return(IndexOf(aNode.LowValueChild, aValue, aIdx));
            }
            else if (compare < 0)
            {
                return(IndexOf(aNode.HighValueChild, aValue, aIdx + aNode.InstanceCount + aNode.LowWeight));
            }
            else
            {
                for (int i = 0; i < aNode.InstanceCount; i++)
                {
                    if (aNode[i].Equals(aValue) == true)
                    {
                        return(aIdx + i);
                    }
                }
                return(-1);
            }
        }
 public void Remove(T aValue, InstanceInformation aInstances)
 {
     lock (this) {
         Deleted = null;
         if (((DuplicateHandling == DuplicateHandlingType.CollectDuplicates) &&
              (aInstances != InstanceInformation.AllInstances)) ||
             (aInstances == InstanceInformation.ObjectInstance))
         {
             IAVLTreeSortNode <T> t = FindAndMark(aValue);
             if (t == null)
             {
                 return;
             }
             if (t.InstanceCount > 1)
             {
                 t.RemoveValue(aValue);
             }
             else
             {
                 Remove(aValue, InstanceInformation.AllInstances);
             }
             return;
         }
         Root.Delete(args, ref root, aValue);
     }
 }
Exemple #4
0
 // constuctor for the sentinel node
 internal AVLTreeSortNode()
 {
     level          = SENTINEL_ID;
     lowValueChild  = this;
     highValueChild = this;
     InitStore();
 }
        private IAVLTreeSortNode <T> FindAndMark(IAVLTreeSortNode <T> aNode, T aValue)
        {
            if (aNode.IsSentinel == true)
            {
                return(null);
            }
            int compare = OnSortMethod(aNode.Value, aValue);
            IAVLTreeSortNode <T> res = null;

            if (compare > 0)
            {
                res = FindAndMark(aNode.LowValueChild, aValue);
            }
            else if (compare < 0)
            {
                res = FindAndMark(aNode.HighValueChild, aValue);
            }
            else
            {
                aNode.Weight = -1;
                return(aNode);
            }
            if (res != null)
            {
                aNode.Weight = -1;
            }
            return(res);
        }
Exemple #6
0
 // constuctor for regular nodes (that all start life as leaf nodes)
 internal AVLTreeSortNode(T aValue, IAVLTreeSortNode <T> aSentinel)
 {
     level          = 1;
     weight         = -1;
     lowValueChild  = aSentinel;
     highValueChild = aSentinel;
     InitStore();
     AddValue(aValue);
 }
Exemple #7
0
        public bool Delete(SortEventArgs <T> aArgs, ref IAVLTreeSortNode <T> aNode, T value)
        {
            if (aNode == aArgs.Tree.Sentinel)
            {
                return(aArgs.Tree.Deleted != null);
            }

            int compare = -aArgs.OnCompare(value, aNode.Value);

            if (compare < 0)
            {
                if (Delete(aArgs, ref (aNode as AVLTreeSortNode <T, Store>).highValueChild, value) == false)
                {
                    return(false);
                }
                (aNode as AVLTreeSortNode <T, Store>).HighValueChild.Weight = -1;
            }
            else
            {
                if (compare == 0)
                {
                    aArgs.Tree.Deleted = aNode;
                }
                if (Delete(aArgs, ref (aNode as AVLTreeSortNode <T, Store>).lowValueChild, value) == false)
                {
                    return(false);
                }
                (aNode as AVLTreeSortNode <T, Store>).LowValueChild.Weight = -1;
            }

            if (aArgs.Tree.Deleted != null)
            {
                aArgs.Tree.DeletedNode = aArgs.Tree.Deleted;
                aArgs.Tree.Deleted     = null;
                aNode        = aNode.LowValueChild;
                aNode.Weight = -1;
                aNode.LowValueChild.Weight = -1;
            }
            else if ((aNode.HighValueChild.Level < aNode.Level - 1) || (aNode.LowValueChild.Level < aNode.Level - 1))
            {
                --aNode.Level;
                if (aNode.LowValueChild.Level > aNode.Level)
                {
                    aNode.LowValueChild.Level = aNode.Level;
                }
                Skew(ref aNode);
                Skew(ref (aNode as AVLTreeSortNode <T, Store>).lowValueChild);
                Skew(ref (aNode.LowValueChild as AVLTreeSortNode <T, Store>).lowValueChild);
                Split(ref aNode);
                Split(ref (aNode as AVLTreeSortNode <T, Store>).highValueChild);
            }

            return(true);
        }
Exemple #8
0
        public bool InsertNode(SortEventArgs <T> aArgs, ref IAVLTreeSortNode <T> aNode, T value, out ITreeSortNode <T> aNewNode)
        {
            aNewNode = null;
            if (aNode == aArgs.Tree.Sentinel)
            {
                aNode    = CreateNode(value, aArgs.Tree.Sentinel);
                aNewNode = aNode;
                return(true);
            }

            int compare = -aArgs.OnCompare(value, aNode.Value);

            if (compare < 0)
            {
                if (InsertNode(aArgs, ref (aNode as AVLTreeSortNode <T, Store>).highValueChild, value, out aNewNode) == false)
                {
                    return(false);
                }
                (aNode as AVLTreeSortNode <T, Store>).HighValueChild.Weight = -1;
            }
            else if (compare > 0)
            {
                if (InsertNode(aArgs, ref (aNode as AVLTreeSortNode <T, Store>).lowValueChild, value, out aNewNode) == false)
                {
                    return(false);
                }
                (aNode as AVLTreeSortNode <T, Store>).LowValueChild.Weight = -1;
            }
            else
            {
                switch (aArgs.DuplicateHandling)
                {
                case DuplicateHandlingType.CollectDuplicates:
                    aNode.AddValue(value);
                    aNewNode = aNode;
                    return(true);

                case DuplicateHandlingType.ThrowException:
                    throw new DuplicateItemException();

                default:
                    return(false);
                }
                return(false);
            }

            Weight = -1;

            Skew(ref aNode);
            Split(ref aNode);

            return(true);
        }
 private IAVLTreeSortNode <T> GetNodeFor(IAVLTreeSortNode <T> aNode, int aIdx)
 {
     if (aIdx <= aNode.LowWeight)
     {
         return(GetNodeFor(aNode.LowValueChild, aIdx));
     }
     else if (aIdx > (aNode.LowWeight + aNode.InstanceCount))
     {
         return(GetNodeFor(aNode.HighValueChild, aIdx - (aNode.LowWeight + aNode.InstanceCount)));
     }
     else
     {
         return(aNode);
     }
 }
 internal BaseAVLTreeSort(CompareMethod <T> aSortMethod, DuplicateHandlingType aDuplicateHandling)
 {
     sortMethod        = aSortMethod;
     duplicateHandling = aDuplicateHandling;
     args = CreateSortArgs();
     if (DuplicateHandling == DuplicateHandlingType.CollectDuplicates)
     {
         sentinel = new MultiInstanceAVLTreeSortNode <T>();
     }
     else
     {
         sentinel = new SingleInstanceAVLTreeSortNode <T>();
     }
     //sentinel = new Node<T>();
     root    = sentinel;
     deleted = null;
 }
        public virtual IEnumerable Distinct()
        {
            IAVLTreeSortNode <T> curr;
            IAVLTreeSortNode <T> last = null;

            lock (this) {
                for (int i = 0; i < Count; i++)
                {
                    curr = tree.GetNodeFor(i);
                    if ((curr != last) && (curr != null))
                    {
                        yield return(curr.Value);
                    }
                    last = curr;
                }
            }
        }
Exemple #12
0
        private void Skew(ref IAVLTreeSortNode <T> aNode)
        {
            if (aNode.Level == aNode.HighValueChild.Level)
            {
                // rotate low

                IAVLTreeSortNode <T> high = aNode.HighValueChild;
                aNode.HighValueChild.Weight = -1;

                aNode.HighValueChild        = high.LowValueChild;
                aNode.HighValueChild.Weight = -1;

                high.LowValueChild        = aNode;
                high.LowValueChild.Weight = -1;

                aNode        = high;
                aNode.Weight = -1;
            }
        }
Exemple #13
0
        private void Split(ref IAVLTreeSortNode <T> aNode)
        {
            if (aNode.LowValueChild.LowValueChild.Level == aNode.Level)
            {
                // rotate high
                IAVLTreeSortNode <T> low = aNode.LowValueChild;
                aNode.LowValueChild.Weight = -1;

                aNode.LowValueChild        = low.HighValueChild;
                aNode.LowValueChild.Weight = -1;

                low.HighValueChild        = aNode;
                low.HighValueChild.Weight = -1;

                aNode        = low;
                aNode.Weight = -1;

                aNode.Level++;
            }
        }
        private ITreeSortNode <T> Find(IAVLTreeSortNode <T> aNode, T aValue)
        {
            if (aNode.IsSentinel == true)
            {
                return(null);
            }
            int compare = OnSortMethod(aNode.Value, aValue);

            if (compare > 0)
            {
                return(Find(aNode.LowValueChild, aValue));
            }
            else if (compare < 0)
            {
                return(Find(aNode.HighValueChild, aValue));
            }
            else
            {
                return(aNode);
            }
        }
Exemple #15
0
 internal MultiInstanceAVLTreeSortNode(T aValue, IAVLTreeSortNode <T> aSentinel)
     : base(aValue, aSentinel)
 {
 }
Exemple #16
0
 public override IAVLTreeSortNode <T> CreateNode(T aValue, IAVLTreeSortNode <T> aSentinel)
 {
     return(new MultiInstanceAVLTreeSortNode <T> (aValue, aSentinel));
 }
Exemple #17
0
 public virtual IAVLTreeSortNode <T> CreateNode(T aValue, IAVLTreeSortNode <T> aSentinel)
 {
     throw new NotImplementedException();
 }
Exemple #18
0
 public virtual void Disconnect()
 {
     lowValueChild  = null;
     highValueChild = null;
 }