-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathShufleObjects.cpp
More file actions
149 lines (125 loc) · 3.84 KB
/
ShufleObjects.cpp
File metadata and controls
149 lines (125 loc) · 3.84 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
///////////////////////////////////////////////////////////
// ShufleObjects.cpp
// Implementation of the Class ShufleObjects
// Created on: 07-Lie-2013 20:07:32
// Original author: Povilas
///////////////////////////////////////////////////////////
/*! \class ShufleObjects
\brief A class of static methods for shuffling the DataObjects in ObjectMatrix.
*/
#include "ShufleObjects.h"
#include "DataObject.h"
#include "Statistics.h"
//#include <time.h>
//#include <stdlib.h>
//#include <list>
#include <iostream>
ShufleObjects::ShufleObjects()
{
}
ShufleObjects::~ShufleObjects()
{
}
std::vector<unsigned int> ShufleObjects::byBubleSort(ObjectMatrix objectMatrix) // didejimo tvarka
{
int n = objectMatrix.getObjectCount();
int tmp_index = 0;
double tmp_disp = 0.0;
bool shufled = false;
std::vector<unsigned int> shufledIndexes;
shufledIndexes.reserve(n);
for (int i = 0; i < n; i++)
shufledIndexes.push_back(i);
int l = 1;
do
{
shufled = true;
for (int i = 0; i < n - l; i++)
{
if (objectMatrix.getObjectAt(shufledIndexes.at(i)).getFeatureAt(0) > objectMatrix.getObjectAt(shufledIndexes.at(i + 1)).getFeatureAt(0))
{
tmp_index = shufledIndexes.at(i);
shufledIndexes.at(i) = shufledIndexes.at(i + 1);
shufledIndexes.at(i + 1) = tmp_index;
shufled = false;
}
}
l++;
}
while (!shufled);
return shufledIndexes;
}
std::vector<unsigned int> ShufleObjects::byBubleSortDsc(ObjectMatrix objectMatrix) // mazejimo tvarka
{
int n = objectMatrix.getObjectCount();
int tmp_index = 0;
double tmp_disp = 0.0;
bool shufled = false;
std::vector<unsigned int> shufledIndexes;
shufledIndexes.reserve(n);
for (int i = 0; i < n; i++)
shufledIndexes.push_back(i);
int l = 1;
do
{
shufled = true;
for (int i = 0; i < n - l; i++)
{
if (objectMatrix.getObjectAt(shufledIndexes.at(i)).getFeatureAt(0) < objectMatrix.getObjectAt(shufledIndexes.at(i + 1)).getFeatureAt(0))
{
tmp_index = shufledIndexes.at(i);
shufledIndexes.at(i) = shufledIndexes.at(i + 1);
shufledIndexes.at(i + 1) = tmp_index;
shufled = false;
}
}
l++;
}
while (!shufled);
/* for (int i = 0; i < n; i++)
std::cout << objectMatrix.getObjectAt(shufledIndexes.at(i)).getFeatureAt(0) << std::endl;*/
return shufledIndexes;
}
std::vector<unsigned int> ShufleObjects::byRand(ObjectMatrix objectMatrix)
{
unsigned int n = objectMatrix.getObjectCount();
std::vector<unsigned int> currentIndexes;
std::vector<unsigned int> shufledIndexes;
shufledIndexes.reserve(n);
currentIndexes.reserve(n);
for (unsigned int i = 0; i < n; i++)
currentIndexes.push_back(i);
double r = 0.0;
int index = 0;
do
{
r = Statistics::getRandom(0, n - 1); //-1 since 0 is included
index = static_cast<int>(r);
while (currentIndexes.at(index) == -1)
index = (index + 1) % n;
shufledIndexes.push_back(index);
currentIndexes.at(index) = -1;
}
while (shufledIndexes.size() < n);
return shufledIndexes;
}
std::vector<unsigned int> ShufleObjects::shufleObjectMatrix(ShufleEnum shufleEnum, ObjectMatrix objectMatrix)
{
std::vector<unsigned int> shufled;
switch (shufleEnum)
{
case 1:
shufled = ShufleObjects::byRand(objectMatrix);
break;
case 2:
shufled = ShufleObjects::byBubleSort(objectMatrix);
break;
case 3:
shufled = ShufleObjects::byBubleSortDsc(objectMatrix);
break;
default:
shufled = ShufleObjects::byRand(objectMatrix);
break;
}
return shufled;
}