Exemplo n.º 1
0
    // static void swap(List<T> arr, int index1, int index2)
    // {
    //  T tmp = arr[index1];
    //  arr[index1] = arr[index2];
    //  arr[index2] = tmp;
    // }

    // static T SelectMedian(List<T> a, int left, int right, CompareFunction<T> _compareFunction, params object[] args)
    // {
    //     int midIndex = (left + right)>>1;

    //     int diff = _compareFunction(a[left], a[midIndex], args);
    //     if (diff > 0)
    //     {
    //         swap(a, left, midIndex);
    //     }

    //     diff = _compareFunction(a[left], a[right], args);
    //     if (diff > 0)
    //     {
    //         swap(a, left, right);
    //     }

    //     diff = _compareFunction(a[midIndex], a[right], args);
    //     if (diff > 0)
    //     {
    //         swap(a, midIndex, right);
    //     }

    //     swap(a, midIndex, right);

    //     return a[right-1]; //返回中轴
    // }

    ////// test //////////////////////////

    public static void test_find()
    {
        List <int> arr = new List <int>(10);

        arr.Add(2);
        arr.Add(9);
        arr.Add(4);
        arr.Add(5);
        arr.Add(3);
        arr.Add(2);
        arr.Add(8);
        arr.Add(1);
        arr.Add(7);

        CQuickSort.Sort <int>(arr, 0, arr.Count - 1, test_compare_function);

        string out1 = "";

        for (int i = 0; i < arr.Count; i++)
        {
            out1 += " " + arr[i].ToString();
        }
        //Debug.LogError(out1);

        int _index = -1;

        _index = CQuickSort.Search <int>(arr, 9, test_compare_function);

        //Debug.LogError("_index " + _index);
    }
Exemplo n.º 2
0
    public T this[K _key]
    {
        get
        {
            T obj;
            if (TryGet(_key, out obj))
            {
                return(obj);
            }

            return(default(T));
        }
        set
        {
            int index = CQuickSort.Search <K>(mListKey, _key, CompareFunction);
            if (index < 0)
            {
                Add(_key, value);
                throw new Exception("Key not exist, key " + _key);
            }
            else
            {
                mListValue[index] = value;
            }
        }
    }
Exemplo n.º 3
0
    public void DisConnectNode(PathNode _a, PathNode _b)
    {
        int index = -1;

        index = CQuickSort.Search <PathNode>(_a.mLink, _b, SortPathNodeCompare);
        if (index >= 0)
        {
            _a.mLink.RemoveAt(index);
        }
        index = CQuickSort.Search <PathNode>(_b.mLink, _a, SortPathNodeCompare);
        if (index >= 0)
        {
            _b.mLink.RemoveAt(index);
        }
        index = CQuickSort.Search <int>(_a.mLinkID, _b.mId, SortIntCompare);
        if (index >= 0)
        {
            _a.mLinkID.RemoveAt(index);
        }
        index = CQuickSort.Search <int>(_b.mLinkID, _a.mId, SortIntCompare);
        if (index >= 0)
        {
            _b.mLinkID.RemoveAt(index);
        }
    }
Exemplo n.º 4
0
 public PathNode GeneratePathNode(int _groupId, int _refeshIndex, List <PathNode> result)
 {
     if (_refeshIndex == mRefeshIndex)
     {
         return(mPathNode);
     }
     mRefeshIndex        = _refeshIndex;
     mPathNode           = new PathNode();
     mPathNode.mId       = sMaxID++;
     mPathNode.mName     = mName;
     mPathNode.mGroupID  = _groupId;
     mPathNode.mValue    = mValue;
     mPathNode.mPosition = transform.position;
     for (int i = 0; i < mListNode.Count; i++)
     {
         PathNode linknode = mListNode[i].GeneratePathNode(_groupId, _refeshIndex, result);
         if (linknode != null)
         {
             mPathNode.mLinkID.Add(linknode.mId);
         }
     }
     CQuickSort.Sort <int>(mPathNode.mLinkID, 0, mPathNode.mLinkID.Count - 1, PathNodeManager.SortIntCompare);
     result.Add(mPathNode);
     return(mPathNode);
 }
Exemplo n.º 5
0
    public void RemoveNode(PathNode _a)
    {
        int index = CQuickSort.Search <PathNode>(mListNodes, _a, SortPathNodeCompare);

        if (index >= 0)
        {
            mListNodes.RemoveAt(index);
        }
    }
Exemplo n.º 6
0
    //whether contain the key
    public bool ContainsKey(K _key)
    {
        int index = CQuickSort.Search <K>(mListKey, _key, CompareFunction);

        if (index >= 0)
        {
            return(true);
        }
        return(false);
    }
Exemplo n.º 7
0
    //try get value by key
    public bool TryGet(K _key, out T _value)
    {
        int index = CQuickSort.Search <K>(mListKey, _key, CompareFunction);

        if (index >= 0)
        {
            _value = mListValue[index];
            return(true);
        }
        _value = default(T);
        return(false);
    }
Exemplo n.º 8
0
    public void ConnectNode(PathNode _a, PathNode _b)
    {
        int index = -1;

        if (CQuickSort.Search <PathNode>(_a.mLink, _b, SortPathNodeCompare) >= 0)
        {
            Debug.LogError("Error: Alread have node in link, Can't connect again.");
            return;
        }
        _a.mLink.Add(_b);
        _b.mLink.Add(_a);
        _a.mLinkID.Add(_b.mId);
        _b.mLinkID.Add(_a.mId);
    }
Exemplo n.º 9
0
    public void RemoveLog(MissionLog _log)
    {
        if (_log == null)
        {
            return;
        }

        int index = CQuickSort.Search <MissionLog>(mListMissionLog, _log, CompareLog);

        if (index >= 0)
        {
            mListMissionLog.RemoveAt(index);
        }
    }
Exemplo n.º 10
0
    //remove by key
    public bool Remove(K _key)
    {
        int index = CQuickSort.Search <K>(mListKey, _key, CompareFunction);

        if (index < 0)
        {
            throw new Exception("no key to remove, key:" + _key);
            return(false);
        }

        mListKey.RemoveAt(index);
        mListValue.RemoveAt(index);

        return(true);
    }
Exemplo n.º 11
0
    public static void test_search_insert()
    {
        List <int> arr = new List <int>(10);

        arr.Add(1);
        arr.Add(2);
        arr.Add(5);
        arr.Add(6);
        arr.Add(7);
        arr.Add(8);
        arr.Add(9);

        string out1 = "";

        for (int i = 0; i < arr.Count; i++)
        {
            out1 += " " + arr[i].ToString();
        }
        //Debug.LogError(out1);

        int _index = -1;

        int search_insert_num = 0;

        //Debug.LogError("SearchInsert " + search_insert_num);
        _index = CQuickSort.SearchInsert <int>(arr, search_insert_num, test_compare_function);
        //Debug.LogError("_index " + _index);

        search_insert_num = 3;
        //Debug.LogError("SearchInsert " + search_insert_num);
        _index = CQuickSort.SearchInsert <int>(arr, search_insert_num, test_compare_function);
        //Debug.LogError("_index " + _index);

        search_insert_num = 10;
        //Debug.LogError("SearchInsert " + search_insert_num);
        _index = CQuickSort.SearchInsert <int>(arr, search_insert_num, test_compare_function);
        //Debug.LogError("_index " + _index);

        search_insert_num = 9;
        //Debug.LogError("SearchInsert " + search_insert_num);
        _index = CQuickSort.SearchInsert <int>(arr, search_insert_num, test_compare_function);
        //Debug.LogError("_index " + _index);

        search_insert_num = 7;
        //Debug.LogError("SearchInsert " + search_insert_num);
        _index = CQuickSort.SearchInsert <int>(arr, search_insert_num, test_compare_function);
        //Debug.LogError("_index " + _index);
    }
    public void SaveBinary(string pathfile)
    {
        FileStream   fs = new FileStream(pathfile, FileMode.Create);
        BinaryWriter bw = new BinaryWriter(fs);

        Refesh();
        List <PathNode> lstnode = GeneratePathNode();

        Debug.LogError("lstnode.Count " + lstnode.Count);

        CQuickSort.Sort <PathNode>(lstnode, 0, lstnode.Count - 1, PathNodeManager.SortPathNodeCompare); //sort
        bw.Write(lstnode.Count);
        for (int i = 0; i < lstnode.Count; i++)
        {
            // Debug.LogError("node id " + lstnode[i].mId);
            lstnode[i].Write(bw);
        }
        bw.Close();
        fs.Close();
        AssetDatabase.Refresh();
    }
Exemplo n.º 13
0
    //add by key and value
    public bool Add(K _key, T _value)
    {
        int index = CQuickSort.Search <K>(mListKey, _key, CompareFunction);

        if (index >= 0)
        {
            throw new Exception("already have key " + _key);
            return(false);
        }

        index = CQuickSort.SearchInsert <K>(mListKey, _key, CompareFunction);
        if (index >= mListKey.Count)
        {
            mListKey.Add(_key);
            mListValue.Add(_value);
        }
        else
        {
            mListKey.Insert(index, _key);
            mListValue.Insert(index, _value);
        }

        return(true);
    }
Exemplo n.º 14
0
    ////////////////////////
    // 1.把所有点放在二维坐标系中,则纵坐标最小的点一定是凸包上的点
    // 2.把所有点的坐标平移一下,使 P0 作为原点
    // 3.计算各个点相对于 P0 的幅角 α ,按从小到大的顺序对各个点排序。当 α 相同时,距离 P0 比较近的排在前面。我们由几何知识可以知道,结果中第一个点 P1 和最后一个点一定是凸包上的点。
    // (以上是准备步骤,以下开始求凸包)
    // 以上,我们已经知道了凸包上的第一个点 P0 和第二个点 P1,我们把它们放在栈里面。现在从步骤3求得的那个结果里,把 P1 后面的那个点拿出来做当前点,即 P2 。接下来开始找第三个点:
    // 4.连接P0和栈顶的那个点,得到直线 L 。看当前点是在直线 L 的右边还是左边。如果在直线的右边就执行步骤5;如果在直线上,或者在直线的左边就执行步骤6。
    // 5.如果在右边,则栈顶的那个元素不是凸包上的点,把栈顶元素出栈。执行步骤4。
    // 6.当前点是凸包上的点,把它压入栈,执行步骤7。
    // 7.检查当前的点 P2 是不是步骤3那个结果的最后一个元素。是最后一个元素的话就结束。如果不是的话就把 P2 后面那个点做当前点,返回步骤4。
    // 最后,栈中的元素就是凸包上的点了。

    // convex
    public static List <Vector2> ConvexHull(List <Vector2> vex)
    {
        List <Vector2> result = new List <Vector2> (16);

        CQuickSort.Sort <Vector2> (vex, 0, vex.Count - 1, ConvexCompare1);
        for (int i = 1; i < vex.Count - 1;)
        {
            Vector2 v1 = vex[i - 1];
            Vector2 v2 = vex[i];
            Vector2 v3 = v1 - v2;
            if (IsEqualZero(v3))
            {
                vex.RemoveAt(i);
            }
            else
            {
                i++;
            }
        }
        _TempConvexFirst = vex[0];
        result.Add(vex[0]);

        CQuickSort.Sort <Vector2> (vex, 1, vex.Count - 1, ConvexCompare2);
        result.Add(vex[1]);

        for (int i = 2; i < vex.Count; i++)
        {
            // Debug.LogError("index " + i);
            while (ConvexPointCross(result[result.Count - 2], result[result.Count - 1], vex[i]) < 0)
            {
                result.RemoveAt(result.Count - 1);
            }
            result.Add(vex[i]);
        }
        return(result);
    }
Exemplo n.º 15
0
 // Counter clock wise operation
 public static void CCW(List <Vector2> vex)
 {
     CQuickSort.Sort <Vector2> (vex, 0, vex.Count - 1, ConvexCompare1);
     _TempConvexFirst = vex[0];
     CQuickSort.Sort <Vector2> (vex, 1, vex.Count - 1, ConvexCompare2);
 }
Exemplo n.º 16
0
    public static void test_sort()
    {
        List <int> arr = new List <int>(10);

        arr.Add(2);
        arr.Add(9);
        arr.Add(4);
        arr.Add(5);
        arr.Add(3);
        arr.Add(2);
        arr.Add(8);
        arr.Add(1);
        arr.Add(7);

        // List<int> arr = new List<int>(10);
        // arr.Add(2);
        // arr.Add(1);

        // List<int> arr = new List<int>(10);
        // arr.Add(2);
        // arr.Add(1);
        // arr.Add(4);

        // List<int> arr = new List<int>(10);
        // arr.Add(1);
        // arr.Add(2);
        // arr.Add(3);
        // arr.Add(4);
        // arr.Add(5);
        // arr.Add(6);
        // arr.Add(7);
        // arr.Add(8);

        // List<int> arr = new List<int>(10);
        // arr.Add(2);
        // arr.Add(2);
        // arr.Add(2);
        // arr.Add(2);
        // arr.Add(2);
        // arr.Add(2);
        // arr.Add(2);
        // arr.Add(2);

        string out1 = "";

        out1 = "";
        for (int i = 0; i < arr.Count; i++)
        {
            out1 += " " + arr[i].ToString();
        }
        //Debug.LogError(out1);

        CQuickSort.Sort <int>(arr, 0, arr.Count - 1, test_compare_function);

        out1 = "";
        for (int i = 0; i < arr.Count; i++)
        {
            out1 += " " + arr[i].ToString();
        }
        //Debug.LogError(out1);
    }