クイックソート

Namespace: fvalgcli
Assembly: fvalgcli (in fvalgcli.dll) Version: 3.1.0.0 (3.1.0.11)

Syntax

C#
public static void fnFIE_qsort_ex(
	IntPtr pvbase,
	SIZE_T num,
	SIZE_T size,
	CompareExDelegate comp,
	IntPtr param
)
Visual Basic
Public Shared Sub fnFIE_qsort_ex ( 
	pvbase As IntPtr,
	num As SIZE_T,
	size As SIZE_T,
	comp As CompareExDelegate,
	param As IntPtr
)

Parameters

pvbase
Type: System..::..IntPtr
並べ替える配列の先頭
num
Type: fvalgcli..::..SIZE_T
配列の要素数
size
Type: fvalgcli..::..SIZE_T
配列の要素のサイズ
comp
Type: fvalgcli..::..CompareExDelegate
比較関数
param
Type: System..::..IntPtr
比較関数に渡すパラメータ

Examples

C# Copy imageCopy
//    $Revision: 1.1 $

using System;
using System.Collections.Generic;
using System.Text;
using fvalgcli;

namespace TC
{
    public partial class FIE
    {
        [FvPluginExecute]
        public void fnFIE_qsort_ex()
        {
            DPNT_T_PTR points = DPNT_T_PTR.Zero;
            F_DEDGE_PTR edges = F_DEDGE_PTR.Zero;
            int num = 5;
            INT_PTR param = INT_PTR.Zero;

            try
            {
                // 比較対象の配列.
                points = DPNT_T_PTR.alloc(num);
                points[0] = DPNT_T.init(1, 9);
                points[1] = DPNT_T.init(3, 5);
                points[2] = DPNT_T.init(2, 3);
                points[3] = DPNT_T.init(5, 8);
                points[4] = DPNT_T.init(3, 3);

                // 比較対象の配列.
                edges = F_DEDGE_PTR.alloc(num);
                edges[0] = F_DEDGE.init(1, 9, 0, 0);
                edges[1] = F_DEDGE.init(3, 5, 0, 0);
                edges[2] = F_DEDGE.init(2, 3, 0, 0);
                edges[3] = F_DEDGE.init(5, 8, 0, 0);
                edges[4] = F_DEDGE.init(3, 3, 0, 0);

                // 比較関数.
                CompareExDelegate comp = bubble_sort_ex_comp;

                // 任意のパラメータ.
                param = INT_PTR.alloc(1);

                // 処理の実行.(1)
                param[0] = 1;
                api.fnFIE_qsort_ex(points, num, DPNT_T_PTR.SizeOfItem, comp, param);

                // 処理の実行.(2)
                param[0] = 2;
                api.fnFIE_qsort_ex(edges, num, F_DEDGE_PTR.SizeOfItem, comp, param);

                for (int i = 0; i < num; i++)
                    Console.WriteLine("fnFIE_qsort_ex: points[{0}]={1},{2}", i, points[i].x, points[i].y);
                for (int i = 0; i < num; i++)
                    Console.WriteLine("fnFIE_qsort_ex: edges[{0}]={1},{2}", i, edges[i].x, edges[i].y);
            }
            finally
            {
                // オブジェクトの開放.
                points.Dispose();
                edges.Dispose();
                param.Dispose();
            }
        }

        /// <summary>
        /// 配列要素の比較関数 (X順次いでY順)
        /// </summary>
        /// <param name="param">任意のパラメータ</param>
        /// <param name="elem1">配列要素1</param>
        /// <param name="elem2">配列要素2</param>
        /// <returns>
        /// 返値は2つの要素の大小関係を表す値を返します。
        /// <0: elem1 は elem2 より小さい。
        /// =0: elem1 は elem2 と等しい。
        /// >0: elem1 は elem2 より大きい。
        /// </returns>
        private static int qsort_ex_comp(System.IntPtr param, System.IntPtr elem1, System.IntPtr elem2)
        {
            int option = ((INT_PTR)param)[0];
            switch (option)
            {
                default:
                case 1:
                    {
                        DPNT_T _elem1 = ((DPNT_T_PTR)elem1)[0];
                        DPNT_T _elem2 = ((DPNT_T_PTR)elem2)[0];
                        if (_elem1.x < _elem2.x) return -1;
                        if (_elem1.x > _elem2.x) return 1;
                        if (_elem1.y < _elem2.y) return -1;
                        if (_elem1.y > _elem2.y) return 1;
                        return 0;
                    }
                case 2:
                    {
                        F_DEDGE _elem1 = ((F_DEDGE_PTR)elem1)[0];
                        F_DEDGE _elem2 = ((F_DEDGE_PTR)elem2)[0];
                        if (_elem1.x < _elem2.x) return -1;
                        if (_elem1.x > _elem2.x) return 1;
                        if (_elem1.y < _elem2.y) return -1;
                        if (_elem1.y > _elem2.y) return 1;
                        return 0;
                    }
            }
        }
    }
}


Visual Basic Copy imageCopy
'    $Revision: 1.1 $

Imports System.Collections.Generic
Imports System.Text
Imports fvalgcli

Public Partial Class FIE
    <FvPluginExecute> _
    Public Sub fnFIE_qsort_ex()
        Dim points As DPNT_T_PTR = DPNT_T_PTR.Zero
        Dim edges As F_DEDGE_PTR = F_DEDGE_PTR.Zero
        Dim num As Integer = 5
        Dim param As INT_PTR = INT_PTR.Zero

        Try
            ' 比較対象の配列.
            points = DPNT_T_PTR.alloc(num)
            points(0) = DPNT_T.init(1, 9)
            points(1) = DPNT_T.init(3, 5)
            points(2) = DPNT_T.init(2, 3)
            points(3) = DPNT_T.init(5, 8)
            points(4) = DPNT_T.init(3, 3)

            ' 比較対象の配列.
            edges = F_DEDGE_PTR.alloc(num)
            edges(0) = F_DEDGE.init(1, 9, 0, 0)
            edges(1) = F_DEDGE.init(3, 5, 0, 0)
            edges(2) = F_DEDGE.init(2, 3, 0, 0)
            edges(3) = F_DEDGE.init(5, 8, 0, 0)
            edges(4) = F_DEDGE.init(3, 3, 0, 0)

            ' 比較関数.
            Dim comp As CompareExDelegate = AddressOf bubble_sort_ex_comp

            ' 任意のパラメータ.
            param = INT_PTR.alloc(1)

            ' 処理の実行.(1)
            param(0) = 1
            api.fnFIE_qsort_ex(points, num, DPNT_T_PTR.SizeOfItem, comp, param)

            ' 処理の実行.(2)
            param(0) = 2
            api.fnFIE_qsort_ex(edges, num, F_DEDGE_PTR.SizeOfItem, comp, param)

            For i As Integer = 0 To num - 1
                Console.WriteLine("fnFIE_qsort_ex: points[{0}]={1},{2}", i, points(i).x, points(i).y)
            Next
            For i As Integer = 0 To num - 1
                Console.WriteLine("fnFIE_qsort_ex: edges[{0}]={1},{2}", i, edges(i).x, edges(i).y)
            Next
        Finally
            ' オブジェクトの開放.
            points.Dispose()
            edges.Dispose()
            param.Dispose()
        End Try
    End Sub

    ''' <summary>
    ''' 配列要素の比較関数 (X順次いでY順)
    ''' </summary>
    ''' <param name="param">任意のパラメータ</param>
    ''' <param name="elem1">配列要素1</param>
    ''' <param name="elem2">配列要素2</param>
    ''' <returns>
    ''' 返値は2つの要素の大小関係を表す値を返します。
    ''' <0: elem1 は elem2 より小さい。
    ''' =0: elem1 は elem2 と等しい。
    ''' >0: elem1 は elem2 より大きい。
    ''' </returns>
    Private Shared Function qsort_ex_comp(param As System.IntPtr, elem1 As System.IntPtr, elem2 As System.IntPtr) As Integer
        Dim [option] As Integer = CType(param, INT_PTR)(0)
        If [option] = 2 Then
            Dim _elem1 As F_DEDGE = CType(elem1, F_DEDGE_PTR)(0)
            Dim _elem2 As F_DEDGE = CType(elem2, F_DEDGE_PTR)(0)
            If _elem1.x < _elem2.x Then
                Return -1
            End If
            If _elem1.x > _elem2.x Then
                Return 1
            End If
            If _elem1.y < _elem2.y Then
                Return -1
            End If
            If _elem1.y > _elem2.y Then
                Return 1
            End If
            Return 0
        Else
            Dim _elem1 As DPNT_T = CType(elem1, DPNT_T_PTR)(0)
            Dim _elem2 As DPNT_T = CType(elem2, DPNT_T_PTR)(0)
            If _elem1.x < _elem2.x Then
                Return -1
            End If
            If _elem1.x > _elem2.x Then
                Return 1
            End If
            If _elem1.y < _elem2.y Then
                Return -1
            End If
            If _elem1.y > _elem2.y Then
                Return 1
            End If
            Return 0
        End If
    End Function
End Class

See Also