[go: up one dir, main page]

Menu

[8e227d]: / algo / sort_file.cpp  Maximize  Restore  History

Download this file

104 lines (85 with data), 2.4 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
/***************************************************************************
* algo/sort_file.cpp
*
* Part of the STXXL. See http://stxxl.sourceforge.net
*
* Copyright (C) 2002-2003 Roman Dementiev <dementiev@mpi-sb.mpg.de>
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
**************************************************************************/
//! \example algo/sort_file.cpp
//! This example imports a file into an \c stxxl::vector without copying its
//! content and then sorts it using stxxl::sort.
#include <stxxl/mng>
#include <stxxl/ksort>
#include <stxxl/sort>
#include <stxxl/vector>
struct my_type
{
typedef unsigned 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();
}
struct Cmp
{
bool operator () (const my_type & a, const my_type & b) const
{
return a < b;
}
static my_type min_value()
{
return my_type(0);
}
static my_type max_value()
{
return my_type(0xffffffff);
}
};
std::ostream & operator << (std::ostream & o, const my_type & obj)
{
o << obj._key;
return o;
}
int main()
{
stxxl::syscall_file f("./in", stxxl::file::DIRECT | stxxl::file::RDWR);
unsigned memory_to_use = 50 * 1024 * 1024;
typedef stxxl::vector<my_type> vector_type;
{
vector_type v(&f);
/*
STXXL_MSG("Printing...");
for(stxxl::int64 i=0; i < v.size(); i++)
STXXL_MSG(v[i].key());
*/
STXXL_MSG("Checking order...");
STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end())) ? "OK" : "WRONG"));
STXXL_MSG("Sorting...");
stxxl::ksort(v.begin(), v.end(), memory_to_use);
//stxxl::sort(v.begin(),v.end()-101,Cmp(),memory_to_use);
STXXL_MSG("Checking order...");
STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end())) ? "OK" : "WRONG"));
}
STXXL_MSG("OK");
return 0;
}