[go: up one dir, main page]

Menu

[8f591a]: / algo / stable_ksort.cpp  Maximize  Restore  History

Download this file

56 lines (40 with data), 1.3 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
#include "../mng/mng.h"
#include "stable_ksort.h"
#include "ksort.h"
#include "../containers/vector"
//! \example algo/stable_ksort.cpp
//! This is an example of how to use \c stxxl::ksort() algorithm
using namespace stxxl;
struct my_type
{
typedef unsigned long key_type;
key_type _key;
char _data[128 - sizeof(key_type)];
key_type key() const {return _key; };
my_type() {};
my_type(key_type __key):_key(__key) {};
static my_type min_value(){ return my_type(0); };
static my_type max_value(){ return my_type(0xffffffff); };
};
bool operator < (const my_type & a, const my_type & b)
{
return a.key() < b.key();
}
int main()
{
unsigned memory_to_use = 32*1024*1024;
typedef stxxl::vector<my_type> vector_type;
const stxxl::int64 n_records = 2*32*stxxl::int64(1024*1024)/sizeof(my_type);
vector_type v(n_records);
random_number32 rnd;
STXXL_MSG("Filling vector... "<< rnd() <<" "<< rnd()<<" "<<rnd())
for(stxxl::int64 i=0; i < v.size(); i++)
v[i]._key = rnd();
STXXL_MSG("Checking order...")
STXXL_MSG( ((stxxl::is_sorted(v.begin(),v.end()))?"OK":"WRONG" ));
STXXL_MSG("Sorting...")
stxxl::stable_ksort(v.begin(),v.end(),memory_to_use);
STXXL_MSG("Checking order...")
STXXL_MSG( ((stxxl::is_sorted(v.begin(),v.end()))?"OK":"WRONG" ));
return 0;
}