Sort of proxy class (thirty-three)

        Before we learned about sorting algorithms, the relationship between our class sorting (Sort) and an array of classes (Array) as follows.

Pictures .png

        So we need to sort associated with an array of classes and class among them, how to associate it? We need to add a few member functions in sorting class, the specific function prototype is as follows

Pictures .png

        Specific code as follows, we add the following code in Array.h

T* array() const
{
    return m_array;
}

        The above function is used to obtain the head address of the array. Sort.h code is as follows

template <typename T>
static void Select(Array<T>& array, bool min2max = true)
{
    Select(array.array(), array.length(), min2max);
}

template <typename T>
static void Insert(Array<T>& array, bool min2max = true)
{
    Insert(array.array(), array.length(), min2max);
}

template <typename T>
static void Bubble(Array<T>& array, bool min2max = true)
{
    Bubble(array.array(), array.length(), min2max);
}

template <typename T>
static void Shell(Array<T>& array, bool min2max = true)
{
    Shell(array.array(), array.length(), min2max);
}

template <typename T>
static void Merge(Array<T>& array, bool min2max = true)
{
    Merge(array.array(), array.length(), min2max);
}

template <typename T>
static void Quick(Array<T>& array, bool min2max = true)
{
    Quick(array.array(), array.length(), min2max);
}

        We add the following test code in the main.cpp

#include <iostream>
#include "Sort.h"
#include "StaticArray.h"

using namespace std;
using namespace DTLib;

int main ()
{
    StaticArray<int, 5> sa;

    for(int i=0; i<5; i++)
    {
        sa [i] = i;
    }

    Bubble :: sort (in, false);

    for(int i=0; i<5; i++)
    {
        cout << sa[i] << endl;
    }
    
    return 0;
}

        Sort results were as follows

Pictures .png

        We have seen already sorted correctly. So in engineering applications sorted, there is a classic problem: when it is to be ranked when the data element is bulky objects, we should improve the efficiency of how to sort it? The following sample code to us as the analysis

#include <iostream>
#include <ctime>
#include "Sort.h"

using namespace std;
using namespace DTLib;

struct Test : public Object
{
    int id;
    int data1[1000];
    double data2[500];

    bool operator < (const Test& obj)
    {
        return id < obj.id;
    }

    bool operator >= (const Test& obj)
    {
        return id >= obj.id;
    }

    bool operator > (const Test& obj)
    {
        return id > obj.id;
    }

    bool operator <= (const Test& obj)
    {
        return id <= obj.id;
    }
};

Test t[1000];

int main ()
{
    clock_t begin = 0;
    clock_t end = 0;

    for(int i=0; i<1000; i++)
    {
        t[i].id = i;
    }

    begin = clock();

    Sort::Bubble(t, 1000, false);

    end = clock();

    cout << "Time : " << (end - begin) << endl;

    return 0;
}

        Let's look at time spent, the results are as follows

Pictures .png

        We see that it takes a long time. So we have to analyze. In the sorting process, to the inevitable exchange operation; however, the nature of the exchange operation is another copy of the data element, such as when a large volume of data elements, the switching operation takes enormous. Let's look at the classic approach: proxy mode.

        1, the proxy object to be arranged rows of data elements;

        2, of the sequence consisting of the proxy object sorting;

        3, when the need to access data elements in order to complete the sequence by accessing the proxy.

        In other words, we turn to manipulate the original data objects by manipulating proxy object. as follows:

Pictures .png

        Agent results as shown below

Pictures .png

        We see small objects instead of large data object data, the specific code as follows

#include <iostream>
#include <ctime>
#include "Sort.h"

using namespace std;
using namespace DTLib;

struct Test : public Object
{
    int id;
    int data1[1000];
    double data2[500];

    bool operator < (const Test& obj)
    {
        return id < obj.id;
    }

    bool operator >= (const Test& obj)
    {
        return id >= obj.id;
    }

    bool operator > (const Test& obj)
    {
        return id > obj.id;
    }

    bool operator <= (const Test& obj)
    {
        return id <= obj.id;
    }
};

class TestProxy : public Object
{
protected:
    Test* m_pTest;
public:
    int id()
    {
        return m_pTest->id;
    }

    int* data1()
    {
        return m_pTest->data1;
    }

    double* data2()
    {
        return m_pTest->data2;
    }

    Test& test() const
    {
        return *m_pTest;
    }

    bool operator < (const TestProxy& obj)
    {
        return test() < obj.test();
    }

    bool operator >= (const TestProxy& obj)
    {
        return test() >= obj.test();
    }

    bool operator > (const TestProxy& obj)
    {
        return test() > obj.test();
    }

    bool operator <= (const TestProxy& obj)
    {
        return test() <= obj.test();
    }

    Test& operator = (Test& test)
    {
        m_pTest = &test;

        return test;
    }
};

Test t[1000];
TestProxy pt[1000];

int main ()
{
    clock_t begin = 0;
    clock_t end = 0;

    for(int i=0; i<1000; i++)
    {
        t[i].id = i;
        pt[i] = t[i];
    }

    begin = clock();

    Sort::Bubble(pt, 1000, false);

    end = clock();

    cout << "Time : " << (end - begin) << endl;

    return 0;
}

        The results are as follows

Pictures .png

        We see efficiency is improved by about 50 times, which is the classic proxy class mode. We proxy class by learning, summarized as follows: 1, by volume when sorting objects is very large, be done indirectly using a proxy model; 2, using the proxy mode operation effectively avoid the time-consuming exchange of large objects; 3, Agent mode solution is the embodiment of space for time thought.

Guess you like

Origin blog.51cto.com/12810168/2409426