Project

General

Profile

Statistics
| Branch: | Revision:

root / src / scave / xyarray.cc @ a3be1d55

History | View | Annotate | Download (1.6 KB)

1
//==========================================================================
2
//  DATASORTER.CC - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17

    
18
#include <algorithm>
19
#include <vector>
20
#include <utility>
21
#include "xyarray.h"
22

    
23
USING_NAMESPACE
24

    
25
using namespace std;
26

    
27
template <typename T> static void permute(T *&array, int size, const vector<int> &permutation)
28
{
29
    // XXX could it be performed in place?
30
    T* oldArray = array;
31
    array = new T[size];
32
    for (int i = 0; i < size; ++i)
33
        array[i] = oldArray[permutation[i]];
34
    delete[] oldArray;
35
}
36

    
37
void XYArray::sortByX()
38
{
39
    if (x)
40
    {
41
        vector<pair<double,int> > xCoords;
42
        for (int i = 0; i < len; ++i)
43
            xCoords.push_back(make_pair(x[i],i));
44

    
45
        sort(xCoords.begin(), xCoords.end());
46
        vector<int> permutation;
47
        for (int i = 0; i < len; ++i)
48
        {
49
            x[i] = xCoords[i].first;
50
            permutation.push_back(xCoords[i].second);
51
        }
52

    
53
        if (y)
54
            permute(y, len, permutation);
55

    
56
        if (xp)
57
            permute(xp, len, permutation);
58

    
59
        if (evec)
60
            permute(evec, len, permutation);
61
    }
62
}