Exemplo n.º 1
0
    public virtual int func(BinaryTree.Node current, System.IComparable value)
    {
        if (value == null)
        {
            throw new System.ArgumentException();
        }
        if (current == null)
        {
            return(0);
        }
        int x;

        if (value.CompareTo(current.value) < 0)
        {
            x = 1;
        }
        else
        {
            x = 0;
        }
        return(x + func(current.left, value) + func(current.right, value));
    }
Exemplo n.º 2
0
    private int countLessThan(BinaryTree.Node current, System.IComparable value)
    {
        if (current == null)
        {
            return(0);
        }
        if (current.value == null)
        {
            return(0);
        }
        if (value == null)
        {
            return(0);
        }
        int count = 0;

        if (current.value.CompareTo(value) > 0)
        {
            return(1 + countLessThan(current.left, value) + countLessThan(current.right, value
                                                                          ));
        }
        return(countLessThan(current.left, value) + countLessThan(current.right, value));
    }
 private void CheckIfCanMergeWith(NeoDatis.Btree.IBTreeNode node)
 {
     if (nbKeys + node.GetNbKeys() > maxNbKeys)
     {
         throw new NeoDatis.Btree.Exception.BTreeException("Trying to merge two nodes with too many keys "
                                                           + nbKeys + " + " + node.GetNbKeys() + " > " + maxNbKeys);
     }
     if (nbKeys > 0)
     {
         System.IComparable greatestOfThis  = keys[nbKeys - 1];
         System.IComparable smallestOfOther = node.GetKeyAt(0);
         if (greatestOfThis.CompareTo(smallestOfOther) >= 0)
         {
             throw new NeoDatis.Btree.Exception.BTreeNodeValidationException("Trying to merge two nodes that have intersections :  "
                                                                             + ToString() + " / " + node);
         }
     }
     if (nbKeys < nbChildren)
     {
         throw new NeoDatis.Btree.Exception.BTreeNodeValidationException("Trying to merge two nodes where the first one has more children than keys"
                                                                         );
     }
 }
Exemplo n.º 4
0
    protected internal virtual int countLessThan(System.IComparable value, BinaryTree.Node
                                                 current)
    {
        if (current == null)
        {
            return(0);
        }
        int count = 0;

        if (value.CompareTo(current.value) < 0)
        {
            count++;
        }
        if (current.left != null)
        {
            count += countLessThan(value, current.left);
        }
        if (current.right != null)
        {
            count += countLessThan(value, current.right);
        }
        return(count);
    }
Exemplo n.º 5
0
    protected internal virtual int countLessThan(BinaryTree.Node current, System.IComparable
                                                 value)
    {
        if (current == null)
        {
            return(0);
        }
        int num = 0;

        if (value.CompareTo(current.value) < 0)
        {
            num++;
        }
        if (current.right != null)
        {
            num = num + countLessThan(current.right, value);
        }
        if (current.left != null)
        {
            num = num + countLessThan(current.left, value);
        }
        return(num);
    }
Exemplo n.º 6
0
 private void add(BinaryTree.Node current, System.IComparable value)
 {
     if (current == null)
     {
         root = new BinaryTree.Node(this, value);
     }
     else if (current.right == null)
     {
         current.right = new BinaryTree.Node(this, value);
     }
     else if (current.left == null)
     {
         current.left = new BinaryTree.Node(this, value);
     }
     else if (random.Next(2) == 0)
     {
         add(current.right, value);
     }
     else
     {
         add(current.left, value);
     }
 }
Exemplo n.º 7
0
    private int countLessThan(BinaryTree.Node current, System.IComparable value)
    {
        if (current == null)
        {
            return(0);
        }
        int mySum    = 0;
        int leftSum  = 0;
        int rightSum = 0;

        if (current.value.CompareTo(value) > 0)
        {
            mySum = 1;
        }
        if (current.right != null)
        {
            rightSum = countLessThan(current.right, value);
        }
        if (current.left != null)
        {
            leftSum = countLessThan(current.left, value);
        }
        return(mySum + leftSum + rightSum);
    }
Exemplo n.º 8
0
        public static T UnsignedToSigned <T>(object value)
            where T : System.IComparable
        {
            if (value == null)
            {
                throw new System.ArgumentNullException(nameof(value), "Parameter cannot be NULL.");
            }

            System.Type t    = value.GetType();
            System.Type tRet = typeof(T);

            int sizeRet   = System.Runtime.InteropServices.Marshal.SizeOf(tRet);
            int sizeValue = System.Runtime.InteropServices.Marshal.SizeOf(t);

            if (sizeRet != sizeValue)
            {
                throw new System.NotSupportedException($"Type mismatch: {tRet.Name} is not the matching signed type for {t.Name}");
            }

            System.IComparable minValue = (System.IComparable)GetMinValue(t);

            System.IComparable minValueRet = (System.IComparable)GetMinValue(tRet);
            if (minValueRet.CompareTo(System.Convert.ChangeType(0, tRet)) == 0)
            {
                throw new System.NotSupportedException($"Type mismatch: {tRet.Name} is not a signed type.");
            }

            // If we already have an signed type
            // Type mismatch already prevented
            if (minValue.CompareTo(System.Convert.ChangeType(0, t)) != 0)
            {
                return((T)value);
            }

            return((T)UnsignedToSigned(value, t));
        }
Exemplo n.º 9
0
    private int countLessThan(BinaryTree.Node current, System.IComparable value)
    {
        int count = 0;

        if (value == null)
        {
            throw new System.ArgumentException();
        }
        if (current == null)
        {
            return(0);
        }
        if (current.left == null && current.right == null && current.value.CompareTo(value
                                                                                     ) > 0)
        {
            return(1);
        }
        if (current.value.CompareTo(value) > 0)
        {
            count = 1;
        }
        return(count + countLessThan(current.left, value) + countLessThan(current.right,
                                                                          value));
    }
Exemplo n.º 10
0
 public virtual void Insert(System.IComparable key, object value)
 {
     // check if root is full
     if (root.IsFull())
     {
         NeoDatis.Btree.IBTreeNode newRoot = BuildNode();
         NeoDatis.Btree.IBTreeNode oldRoot = root;
         newRoot.SetChildAt(root, 0);
         newRoot.SetNbChildren(1);
         root = newRoot;
         Split(newRoot, oldRoot, 0);
         height++;
         persister.SaveNode(oldRoot);
         // TODO Remove the save of the new root : the save on the btree
         // should do the save on the new root(after introspector
         // refactoring)
         persister.SaveNode(newRoot);
         persister.SaveBTree(this);
         NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(newRoot, true);
     }
     InsertNonFull(root, key, value);
     size++;
     persister.SaveBTree(this);
 }
Exemplo n.º 11
0
    /**
     * Internal quicksort method that makes recursive calls.
     * Uses median-of-three partitioning and a cutoff of 10.
     * @param a an array of System.IComparable items.
     * @param left the left-most index of the subarray.
     * @param right the right-most index of the subarray.
     */
    private static void quicksort(System.IComparable [] a, int left, int right)
    {
/* 1*/ if (left + CUTOFF <= right)
        {
/* 2*/ System.IComparable pivot = median3(a, left, right);

            // Begin partitioning
/* 3*/ int i = left, j = right - 1;
/* 4*/ for ( ; ;)
            {
/* 5*/ while (a[++i].CompareTo(pivot) < 0)
                {
                }
/* 6*/ while (a[--j].CompareTo(pivot) > 0)
                {
                }
/* 7*/ if (i < j)
                {
/* 8*/ swapReferences(a, i, j);
                }
                else
                {
/* 9*/ break;
                }
            }

/*10*/ swapReferences(a, i, right - 1); // Restore pivot

/*11*/ quicksort(a, left, i - 1);       // Sort small elements
/*12*/ quicksort(a, i + 1, right);      // Sort large elements
        }
        else                            // Do an insertion sort on the subarray
/*13*/ {
            insertionSort(a, left, right);
        }
    }
Exemplo n.º 12
0
 public KeyAndValue(System.IComparable key, object value)
 {
     this.key   = key;
     this.value = value;
 }
Exemplo n.º 13
0
 public virtual void SetKey(System.IComparable key)
 {
     this.key = key;
 }
Exemplo n.º 14
0
 internal Node(BinaryTree _enclosing, System.IComparable setValue)
 {
     this._enclosing = _enclosing;
     this.value      = setValue;
 }
Exemplo n.º 15
0
		public KeyAndValue(System.IComparable key, object value)
		{
			this.key = key;
			this.value = value;
		}
Exemplo n.º 16
0
		public virtual void SetKey(System.IComparable key)
		{
			this.key = key;
		}
 public virtual object Search(System.IComparable key)
 {
     NeoDatis.Btree.IBTreeNodeOneValuePerKey theRoot = (NeoDatis.Btree.IBTreeNodeOneValuePerKey
                                                        )GetRoot();
     return(theRoot.Search(key));
 }
Exemplo n.º 18
0
 protected internal override int countLessThan(System.IComparable value)
 {
     return(countLessThan(value, root));
 }
Exemplo n.º 19
0
        private static void CreateNumberControl(DisplayNameAttribute control, PropertyInfo prop, Vector2 anchorPosition)
        {
            UIRangeAttribute rangeAttr     = prop.GetCustomAttribute <UIRangeAttribute>();
            bool             sliderControl = rangeAttr != null && rangeAttr.Slider;

            RectTransform element = Object.Instantiate(sliderControl ? sliderTemplate : inputTemplate, multiplayerContent, false);

            SetupUIElement(element, control, prop, anchorPosition);

            bool isFloatingPoint = prop.PropertyType == typeof(float) || prop.PropertyType == typeof(double);

            if (sliderControl)
            {
                Slider slider = element.GetComponentInChildren <Slider>();
                slider.minValue     = rangeAttr.Min;
                slider.maxValue     = rangeAttr.Max;
                slider.wholeNumbers = !isFloatingPoint;
                Text sliderThumbText = slider.GetComponentInChildren <Text>();
                slider.onValueChanged.RemoveAllListeners();
                slider.onValueChanged.AddListener((value) =>
                {
                    prop.SetValue(tempMultiplayerOptions, value, null);
                    sliderThumbText.text = value.ToString("0");
                });

                tempToUICallbacks[prop.Name] = () =>
                {
                    slider.value         = (float)prop.GetValue(tempMultiplayerOptions, null);
                    sliderThumbText.text = slider.value.ToString("0");
                };
            }
            else
            {
                InputField input = element.GetComponentInChildren <InputField>();

                input.onValueChanged.RemoveAllListeners();
                input.onValueChanged.AddListener((str) => {
                    try
                    {
                        var converter            = TypeDescriptor.GetConverter(prop.PropertyType);
                        System.IComparable value = (System.IComparable)converter.ConvertFromString(str);

                        if (rangeAttr != null)
                        {
                            System.IComparable min = (System.IComparable)System.Convert.ChangeType(rangeAttr.Min, prop.PropertyType);
                            System.IComparable max = (System.IComparable)System.Convert.ChangeType(rangeAttr.Max, prop.PropertyType);
                            if (value.CompareTo(min) < 0)
                            {
                                value = min;
                            }
                            if (value.CompareTo(max) > 0)
                            {
                                value = max;
                            }
                            input.text = value.ToString();
                        }

                        prop.SetValue(tempMultiplayerOptions, value, null);
                    }
                    catch
                    {
                        // If the char is not a number, rollback to previous value
                        input.text = prop.GetValue(tempMultiplayerOptions, null).ToString();
                    }
                });

                tempToUICallbacks[prop.Name] = () =>
                {
                    input.text = prop.GetValue(tempMultiplayerOptions, null).ToString();
                };
            }
        }
Exemplo n.º 20
0
 public void add(System.IComparable value)
 {
     add(root, value);
 }
 public virtual void SetKeyAndValueAt(System.IComparable key, object value, int index
                                      )
 {
     keys[index]   = key;
     values[index] = value;
 }
Exemplo n.º 22
0
 public SimpleCompareKey(System.IComparable key)
 {
     this.key = key;
 }
Exemplo n.º 23
0
        /// <summary>Returns the value of the deleted key</summary>
        /// <param name="node"></param>
        /// <param name="keyAndValue"></param>
        /// <returns></returns>
        /// <exception cref="System.Exception">System.Exception</exception>
        protected virtual object InternalDelete(NeoDatis.Btree.IBTreeNode node, NeoDatis.Btree.IKeyAndValue
                                                keyAndValue)
        {
            int  position     = node.GetPositionOfKey(keyAndValue.GetKey());
            bool keyIsHere    = position > 0;
            int  realPosition = -1;

            NeoDatis.Btree.IBTreeNode leftNode  = null;
            NeoDatis.Btree.IBTreeNode rightNode = null;
            try
            {
                if (node.IsLeaf())
                {
                    if (keyIsHere)
                    {
                        object deletedValue = node.DeleteKeyForLeafNode(keyAndValue);
                        GetPersister().SaveNode(node);
                        return(deletedValue);
                    }
                    // key does not exist
                    return(null);
                }
                if (!keyIsHere)
                {
                    // descend
                    realPosition = -position - 1;
                    NeoDatis.Btree.IBTreeNode child = node.GetChildAt(realPosition, true);
                    if (child.GetNbKeys() == degree - 1)
                    {
                        node = PrepareForDelete(node, child, realPosition);
                        return(InternalDelete(node, keyAndValue));
                    }
                    return(InternalDelete(child, keyAndValue));
                }
                // Here,the node is not a leaf and contains the key
                realPosition = position - 1;
                System.IComparable currentKey   = node.GetKeyAt(realPosition);
                object             currentValue = node.GetValueAsObjectAt(realPosition);
                // case 2a
                leftNode = node.GetChildAt(realPosition, true);
                if (leftNode.GetNbKeys() >= degree)
                {
                    NeoDatis.Btree.IKeyAndValue prev = GetBiggest(leftNode, true);
                    node.SetKeyAndValueAt(prev, realPosition);
                    NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(node, node == root);
                    GetPersister().SaveNode(node);
                    return(currentValue);
                }
                // case 2b
                rightNode = node.GetChildAt(realPosition + 1, true);
                if (rightNode.GetNbKeys() >= degree)
                {
                    NeoDatis.Btree.IKeyAndValue next = GetSmallest(rightNode, true);
                    node.SetKeyAndValueAt(next, realPosition);
                    NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(node, node == root);
                    GetPersister().SaveNode(node);
                    return(currentValue);
                }
                // case 2c
                // Here, both left and right part have degree-1 keys
                // remove the element to be deleted from node (shifting left all
                // right
                // elements, link to right link does not exist anymore)
                // insert the key to be deleted in left child and merge the 2 nodes.
                // rightNode should be deleted
                // if node is root, then leftNode becomes the new root and node
                // should be deleted
                //
                node.DeleteKeyAndValueAt(realPosition, true);
                leftNode.InsertKeyAndValue(currentKey, currentValue);
                leftNode.MergeWith(rightNode);
                // If node is the root and is empty
                if (!node.HasParent() && node.GetNbKeys() == 0)
                {
                    persister.DeleteNode(node);
                    root = leftNode;
                    leftNode.SetParent(null);
                    // The height has been decreased. No need to save btree here.
                    // The calling delete method will save it.
                    height--;
                }
                else
                {
                    node.SetChildAt(leftNode, realPosition);
                    // Node must only be validated if it is not the root
                    NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(node, node == root);
                }
                persister.DeleteNode(rightNode);
                NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(leftNode, leftNode == root);
                GetPersister().SaveNode(node);
                GetPersister().SaveNode(leftNode);
                return(InternalDelete(leftNode, keyAndValue));
            }
            finally
            {
            }
        }
		public SimpleCompareKey(System.IComparable key)
		{
			this.key = key;
		}
Exemplo n.º 25
0
 /// <summary>LESS OR EQUAL</summary>
 /// <param name="attributeName">The attribute name</param>
 /// <param name="value">The value</param>
 /// <returns>The criterion</returns>
 public static NeoDatis.Odb.Core.Query.Criteria.ICriterion Le(string attributeName
                                                              , System.IComparable value)
 {
     return(new NeoDatis.Odb.Core.Query.Criteria.ComparisonCriterion(attributeName, value
                                                                     , NeoDatis.Odb.Core.Query.Criteria.ComparisonCriterion.ComparisonTypeLe));
 }
 public virtual System.Collections.IList Search(System.IComparable key)
 {
     NeoDatis.Btree.IBTreeNodeMultipleValuesPerKey theRoot = (NeoDatis.Btree.IBTreeNodeMultipleValuesPerKey
                                                              )GetRoot();
     return(theRoot.Search(key));
 }
 public abstract void InsertKeyAndValue(System.IComparable key, object value);
Exemplo n.º 28
0
 protected internal virtual int countLessThan(System.IComparable value)
 {
     return(0);
 }
 /// <summary>A geenric compare method</summary>
 /// <param name="c1"></param>
 /// <param name="c2"></param>
 /// <returns></returns>
 public static int Compare(System.IComparable c1, System.IComparable c2)
 {
     return(c1.CompareTo(c2));
 }
Exemplo n.º 30
0
        public static int compare(object v1, object v2)
        {
            if (v1 == v2)
            {
                return(0);
            }
            if (v1 == null)
            {
                return(-1);
            }
            if (v2 == null)
            {
                return(1);
            }
            System.IConvertible cv1 = v1 as System.IConvertible;
            if (cv1 != null)
            {
                System.IConvertible cv2 = v2 as System.IConvertible;

                if (cv2 == null)
                {
                    throw new System.ArgumentException("Cannot compare " + v1.GetType().ToString() + " and " + v2.GetType().ToString());
                }

                switch (cv1.GetTypeCode())
                {
                case System.TypeCode.String:
                    if (cv2.GetTypeCode() != System.TypeCode.String)
                    {
                        throw new System.ArgumentException("Cannot compare " + v1.GetType().ToString() + " and " + v2.GetType().ToString());
                    }
                    string s1     = v1 as string;
                    string s2     = v2 as string;
                    int    i      = 0;
                    int    l1     = s1.Length;
                    int    l2     = s2.Length;
                    bool   active = true;
                    while (active)
                    {
                        char h1; char h2;
                        if (i >= l1)
                        {
                            h1     = (char)0;
                            active = false;
                        }
                        else
                        {
                            h1 = s1[i];
                        }

                        if (i >= l2)
                        {
                            h2     = (char)0;
                            active = false;
                        }
                        else
                        {
                            h2 = s2[i];
                        }

                        int v = h1 - h2;
                        if (v > 0)
                        {
                            return(1);
                        }
                        else if (v < 0)
                        {
                            return(-1);
                        }

                        i++;
                    }
                    return(0);

                case System.TypeCode.Double:
                    double d1 = (double)v1;
                    double d2 = cv2.ToDouble(null);

                    return((d1 < d2) ? -1 : (d1 > d2) ? 1 : 0);

                default:
                    double d1d = cv1.ToDouble(null);
                    double d2d = cv2.ToDouble(null);
                    return((d1d < d2d) ? -1 : (d1d > d2d) ? 1 : 0);
                }
            }

            System.IComparable c1 = v1 as System.IComparable;
            System.IComparable c2 = v2 as System.IComparable;

            if (c1 == null || c2 == null)
            {
                if (c1 == c2)
                {
                    return(0);
                }

                throw new System.ArgumentException("Cannot compare " + v1.GetType().ToString() + " and " + v2.GetType().ToString());
            }

            return(c1.CompareTo(c2));
        }
Exemplo n.º 31
0
 public binTree(System.IComparable val) : base(val, null, true)
 {
     //which=true は、このノードが要素を持っている事を示す。
     //ToDo:コンストラクションロジックは未だ書きおわっていない。
 }
Exemplo n.º 32
0
 public Pair(System.IComparable key, object val)
 {
     Key = key;
     Val = val;
 }
Exemplo n.º 33
0
 public static int BinarySearch <T>(this System.Span <T> span, System.IComparable <T> comparable)
 {
     throw null;
 }
Exemplo n.º 34
0
    /**
     * Mergesort algorithm.
     * @param a an array of System.IComparable items.
     */
    public static void mergeSort(System.IComparable [] a)
    {
        System.IComparable [] tmpArray = new System.IComparable[a.Length];

        mergeSort(a, tmpArray, 0, a.Length - 1);
    }