Browse code

Implemented suggestions of moving sample deletion from sampleIndices in Gapsrunner.cpp to sample in Random.cpp

Tiger Gao authored on 06/06/2018 15:00:44
Showing 2 changed files

... ...
@@ -22,38 +22,12 @@ static std::vector< std::vector<unsigned> > sampleIndices(unsigned n, unsigned n
22 22
 
23 23
     for (unsigned i = 0; i < (n - 1); ++i)
24 24
     {
25
-        std::vector<unsigned> set = gaps::random::sample(toBeSampled, (int) (n - 1) / (nSets - 1));
26
-        sampleIndices.push_back(set);
27
-        for(std::vector<unsigned>::iterator it = set.begin(); it != set.end(); ++it)
28
-        {
29
-            unsigned index = *it;
30
-            std::vector<unsigned>::iterator it2 = toBeSampled.begin();
31
-            while(it2 != toBeSampled.end())
32
-            {
33
-                if(*it2 == index)
34
-                {
35
-                    it2 = toBeSampled.erase(it);
36
-                }
37
-                else
38
-                    it2++;
39
-            }
40
-        }
25
+        sampleIndices.push_back(gaps::random::sample(toBeSampled, (int) (n - 1) / (nSets - 1)));
41 26
     }
42
-    std::vector<unsigned> set = gaps::random::sample(toBeSampled, (n - 1) % (nSets - 1));
43
-    sampleIndices.push_back(set);
44
-    for(std::vector<unsigned>::iterator it = set.begin(); it != set.end(); ++it)
27
+
28
+    if (!toBeSampled.empty())
45 29
     {
46
-        unsigned index = *it;
47
-        std::vector<unsigned>::iterator it2 = toBeSampled.begin();
48
-        while(it2 != toBeSampled.end())
49
-        {
50
-            if(*it2 == index)
51
-            {
52
-                it2 = toBeSampled.erase(it);
53
-            }
54
-            else
55
-                it2++;
56
-        }
30
+        sampleIndices.push_back(toBeSampled);
57 31
     }
58 32
 
59 33
     return sampleIndices;
... ...
@@ -21,6 +21,7 @@
21 21
 #include <boost/random/uniform_real_distribution.hpp>
22 22
 
23 23
 #include <stdint.h>
24
+#include <algorithm>
24 25
 
25 26
 #ifdef __GAPS_OPENMP__
26 27
     #include <omp.h>
... ...
@@ -180,7 +181,20 @@ float gaps::random::inverseGammaSample(float a, float b, float mean, float sd)
180 181
     return gaps::random::q_gamma(u, mean, sd);
181 182
 }
182 183
 
183
-std::vector<unsigned> sample(const std::vector<unsigned> &elements, unsigned n) {
184
+std::vector<unsigned> sample(std::vector<unsigned> &elements, unsigned n) {
185
+    std::vector<unsigned> sampleVect;
186
+    for (unsigned i = 0; i < n; ++i)
187
+    {
188
+        if (elements.empty()) {
189
+            break;
190
+        }
191
+        unsigned sampleIndex = gaps::random::uniform64(0, elements.size());
192
+        sampleVect.push_back(elements.at(sampleIndex));
193
+        elements.erase(std::remove(elements.begin(), elements.end(), elements.at(sampleIndex)), elements.end());
194
+    }
195
+    return sampleVect;
196
+
197
+    /*
184 198
     std::vector<unsigned> sampleVect;
185 199
     std::vector<unsigned> sampledIndices;
186 200
     for (unsigned i = 0; i < n; ++i)
... ...
@@ -197,4 +211,5 @@ std::vector<unsigned> sample(const std::vector<unsigned> &elements, unsigned n)
197 211
         }
198 212
     }
199 213
     return sampleVect;
214
+    */
200 215
 }