Advertisement
zhangsongcui

file mapping

Feb 12th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <boost/filesystem/v3/operations.hpp>
  3. #include <boost/interprocess/file_mapping.hpp>
  4. #include <boost/interprocess/mapped_region.hpp>
  5. #include <boost/chrono.hpp>
  6. #include <boost/array.hpp>
  7. #include <fstream>
  8.  
  9. int main()
  10. {
  11.     using namespace std;
  12.     using namespace boost;
  13.     chrono::system_clock::time_point now = chrono::system_clock::now();
  14.     size_t size;
  15.     {
  16.         interprocess::file_mapping mfile("mapped_file.txt", interprocess::read_write);
  17.         interprocess::mapped_region region(mfile, interprocess::read_write);
  18.         array<char, 23> *const addr = (array<char, 23> *)region.get_address();
  19.         size = region.get_size() / 23;
  20.         sort(addr, addr + size, [] (array<char, 23> const &buf1, array<char, 23> const &buf2) {
  21.             return memcmp(&buf1, &buf2, 21) < 0;
  22.         });
  23.         size = unique(addr, addr + size, [] (array<char, 23> const &buf1, array<char, 23> const &buf2) {
  24.             return memcmp(&buf1, &buf2, 21) == 0;
  25.         }) - addr;
  26.     }
  27.     filesystem::resize_file("mapped_file.txt", size * 23);
  28.     cout << chrono::system_clock::now() - now;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement