-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion_sort.cpp
More file actions
53 lines (40 loc) · 1.52 KB
/
version_sort.cpp
File metadata and controls
53 lines (40 loc) · 1.52 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
#include <fstream> // std::ifstream, std::ofstream
#include <vector> // std::vector
#include <string> // std::string
#include <iterator> // std::ifstream_iterator, std::oftream_iterator
#include <algorithm> // std::sort
#include <numeric> // std::numeric
#include <cassert> // assert
struct op
{
int* p ;
unsigned long long operator()(unsigned long long u, char c)const
{
if (c == '.' )
{
assert(p != NULL);
assert(*p >= 8);
*p -= 8 ;
return u << 8 ;
}
int b = ( u & 0xff ) * 10 + ( c - '0' );
assert( ( c >= '0' && c <= '9' ) ) ;
assert( b < 256 ) ;
return ( u & ~0xffull ) + b ;
}
};
static unsigned long long to_int(const std::string& s){
int n = 56;
struct op o = {&n};
unsigned long long result = std::accumulate(s.begin(), s.end(), 0ULL , o ) ;
return result << n ;
}
static bool version_comparator(const std::string& lhs, const std::string& rhs ) { return to_int(lhs) < to_int(rhs); }
int main( int argc, char* argv[] )
{
std::ifstream input("version.txt");
std::ofstream output("sorted_version.txt");
std::vector< std::string > versions( (std::istream_iterator< std::string >(input)), std::istream_iterator< std::string >() ) ;
std::sort(versions.begin(), versions.end() , &version_comparator);
std::copy(versions.begin(), versions.end(), std::ostream_iterator<std::string>(output, "\n") ) ;
}