Last change
on this file since 259 was
259,
checked in by Jari Häkkinen, 18 years ago
|
Moved out shuffle from vecotr class.
|
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
Id
|
File size:
944 bytes
|
Line | |
---|
1 | // $Id: gslapi_utility.cc 259 2005-03-04 01:09:55Z jari $ |
---|
2 | |
---|
3 | #include "gslapi_utility.h" |
---|
4 | #include <utility> |
---|
5 | |
---|
6 | #include "random_singleton.h" |
---|
7 | #include "vector.h" |
---|
8 | |
---|
9 | |
---|
10 | namespace theplu { |
---|
11 | namespace gslapi { |
---|
12 | |
---|
13 | |
---|
14 | std::pair<size_t, |
---|
15 | size_t> minmax_index(const vector& vec, |
---|
16 | const std::vector<size_t>& subset) |
---|
17 | { |
---|
18 | size_t min_index = subset[0]; |
---|
19 | size_t max_index = subset[0]; |
---|
20 | for (unsigned int i=1; i<subset.size(); i++) { |
---|
21 | if (vec[subset[i]] < vec[min_index]) |
---|
22 | min_index = subset[i]; |
---|
23 | else if (vec[subset[i]] > vec[max_index]) |
---|
24 | max_index = subset[i]; |
---|
25 | } |
---|
26 | return std::pair<size_t,size_t>(min_index, max_index); |
---|
27 | } |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | void shuffle(vector& invec) |
---|
32 | { |
---|
33 | vector vec(invec); |
---|
34 | theplu::cpptools::random_singleton* |
---|
35 | rnd=theplu::cpptools::random_singleton::get_instance(); |
---|
36 | for (size_t i=0; i<vec.size(); i++){ |
---|
37 | size_t index = rnd->get_uniform_int(vec.size()-i); |
---|
38 | invec[i]=vec(index); |
---|
39 | vec(index)=vec(vec.size()-i-1); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | }} // of namespace gslapi and namespace thep |
---|
Note: See
TracBrowser
for help on using the repository browser.