레이블이 swift인 게시물을 표시합니다. 모든 게시물 표시
레이블이 swift인 게시물을 표시합니다. 모든 게시물 표시

12/12/2015

[swift] 2D-array is continuos




[Abstract]

 In c/c++ programming, 2D-array is massively used to deal with the large and practical data as like images, video frames, or any kind of scientific ones. When accessing this array, it is too well-introduced to use double-for-statement, but my goal here is to refresh that the array itself in the physical memory space is just continuous.

 Thus in certain cases, it reduces the processing time to make use of this feature. In brief, eliminate one of the for-statement from the double-shell, and make it to calculate the address inside the for-statement to reduce the overhead of iterative assembly-branch-instructions.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <iostream>
#include <atlstr.h>
#include <math.h>
using namespace std;
 
int main(void)
{
#define ROWS 10
#define COLS 10
    
    double data[ROWS][COLS];    // data[row][col]
    double *pData = data[0];    // data[row] is pointer!!
 
    register int row, col;        // generic index for 2D-array
    register int rows = sizeof(data) / sizeof(data[0]);
    register int cols = sizeof(data[0]) / sizeof(data[0][0]);
    
    // file opened
    FILE* fin = fopen("[0] data.txt""r");
    if(!fin) return -1;
    cout << endl << endl;
 
    //------------------------------------------------------------------------
    // case[1] : generic method : double for statement
    //------------------------------------------------------------------------
    for (row = 0; row < rows; ++row)
    {
        for (col = 0; col < cols; ++col)
        {
            fscanf(fin, "%lf"&data[row][col]); // data[row][col] is value !!
            printf("%10.2lf ", data[row][col]);
        }
        cout << endl;
    }
    //------------------------------------------------------------------------
 
    cout << endl << endl;
    fclose(fin);
 
    fin = fopen("[0] data.txt""r");
    if (!fin) return -1;
 
    //------------------------------------------------------------------------
    // case[2] : continuos in memory space
    //------------------------------------------------------------------------    
    register int i;
    for (i = 0; i < rows * cols; ++i)
    {
        fscanf(fin, "%lf", pData + i);
        printf("%10.2lf "*(pData + i));
        if (i % cols == cols - 1cout << endl;
    }
    //------------------------------------------------------------------------
 
    cout << endl << endl;
    fclose(fin);
    
    return 0;
}
cs

[swift] c++, SearchNearestArray




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <Windows.h>    // rand
#include <iostream>        // cout
#include <iomanip>        // setw, setfill
using namespace std;
 
void RandomSeed(void);
void RandomizeArray(int *ary, const int& size, const int& range);
 
struct valLoc
{
    int val;
    int loc;
};
valLoc SearchNearestArray(int *arry, const int& stroke, const int& size);
 
int main(void)
{
#define ArraySize 20
 
    RandomSeed();
    register int i = 0;
    int ary[ArraySize] = { };
    register int arySize = sizeof(ary) / sizeof(ary[0]);
    {
        cout << setw(20<< setfill(' '<< "Initial Array : ";
        for (i = 0; i < arySize; ++i)
            cout << ary[i] << " ";
        cout << endl;
    }
 
    RandomizeArray(ary, arySize, 10);
    {
        cout << setw(20<< setfill(' '<< "Randomized Array : ";
        for (i = 0; i < arySize; ++i)
            cout << ary[i] << " ";
        cout << endl;
    }
 
    valLoc found = SearchNearestArray(ary, 10, arySize);
    {
        cout << endl << endl;
    }
 
    return 0;
}
 
 
valLoc SearchNearestArray(int *ary, const int& stroke, const int& size)
{
    register int dist, minDist = abs(stroke - ary[0]), loc;
    valLoc found;
 
    register int isMatched = false;
    for (register int i = 0; i < size; ++i)
    {
        if (stroke == ary[i])
        {
            isMatched = true;
            loc = i;
            break;
        }
 
        dist = abs(stroke - ary[i]);
        if (dist < minDist)
        {
            minDist = dist;
            loc = i;
        }
    }
    found.loc = loc;
 
    if(isMatched)
    {
        found.val = stroke;
        cout << ">> Matched number "
            << found.val << " at ["
            << found.loc << "]" << endl << endl;
    }
    else
    {
        found.val = ary[loc];
        cout << endl << ">> Failed to locate " << stroke
            << ". Nearest number is " << found.val << ", at ["
            << found.loc << "]"<< endl << endl;
    }
 
    return found;
}
 
void RandomizeArray(int *ary, const int& size, const int& range)
{
    for (register int i = 0; i < size; ++i)
        ary[i] = rand() % range;
}
 
void RandomSeed(void)
{
    LARGE_INTEGER SEED;
    ::QueryPerformanceCounter(&SEED);
    srand(static_cast<UINT>(SEED.QuadPart));
}
cs

[swift] c++, srand, Incremental Sort, Randomize Array



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <Windows.h>    // rand
#include <iostream>        // cout
#include <iomanip>        // setw, setfill
using namespace std;
 
void SRAND(void);
void RandomizeArray(int *ary, const int& size, const int& range);
int IncrementalSort(int *arry, const int& size);
 
int main(void)
{
    SRAND();
    register int i = 0;
    int ary[10= { };
    register int arySize = sizeof(ary) / sizeof(ary[0]);
    {
        cout << setw(20<< setfill(' '<< "Initial Array : ";
        for (i = 0; i < arySize; ++i)
            cout << ary[i] << " ";
        cout << endl;
    }
 
 
    RandomizeArray(ary, arySize, 10);
    {
        cout << setw(20<< setfill(' '<< "Randomized Array : ";
        for (i = 0; i < arySize; ++i)
            cout << ary[i] << " ";
        cout << endl;
    }
 
    IncrementalSort(ary, arySize);
    {
        cout << setw(20<< setfill(' '<< "Incremental Sort : ";
        for (i = 0; i < arySize; ++i)
            cout << ary[i] << " ";
        cout << endl << endl;
    }
 
    return 0;
}
 
int IncrementalSort(int *arry, const int& size)
{
    register int iteration = 0// To analyze the time complexity
 
    for (register int i = 0; i < size; ++i)
    {
        register int min = arry[i], minIdx, toChange = false;
        for (register int j = i + 1; j < size; ++j)
        {
            ++iteration;
 
            if (arry[j] < min)
            {
                toChange = true;
                min = arry[j];
                minIdx = j;
            }
        }
 
        if (toChange)
        {
            register int temp = arry[i];
            arry[i] = arry[minIdx];
            arry[minIdx] = temp;
        }
    }
 
    return iteration;
}
 
void RandomizeArray(int *ary, const int& size, const int& range)
{
    for (register int i = 0; i < size; ++i)
        ary[i] = rand() % range;
}
 
void SRAND(void)
{
    LARGE_INTEGER SEED;
    ::QueryPerformanceCounter(&SEED);
    srand(static_cast<UINT>(SEED.QuadPart));
}
cs