Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_sort.hpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd7b8a17e8611543bd0d33ec6ecf005baf507c79 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once
#include "coding/file_reader.hpp"
#include "coding/file_writer.hpp"
#include "base/base.hpp"
#include "base/logging.hpp"
#include "base/exception.hpp"
#include "std/algorithm.hpp"
#include "std/cstdlib.hpp"
#include "std/functional.hpp"
#include "std/queue.hpp"
#include "std/unique_ptr.hpp"
#include "std/string.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"

template <typename LessT>
struct Sorter
{
  LessT m_Less;
  Sorter(LessT lessF) : m_Less(lessF) {}
  template <typename IterT> void operator() (IterT beg, IterT end) const
  {
    sort(beg, end, m_Less);
  }
};

template <
    typename T,                                       // Item type.
    class OutputSinkT = FileWriter,                   // Sink to output into result file.
    typename LessT = less<T>,                         // Item comparator.
    template <typename LessT1> class SorterT = Sorter // Item sorter.
>
class FileSorter
{
public:
  FileSorter(size_t bufferBytes,
             string const & tmpFileName,
             OutputSinkT & outputSink,
             LessT fLess = LessT()) :
  m_TmpFileName(tmpFileName),
  m_BufferCapacity(max(size_t(16), bufferBytes / sizeof(T))),
  m_OutputSink(outputSink),
  m_ItemCount(0),
  m_Less(fLess)
  {
    m_Buffer.reserve(m_BufferCapacity);
    m_pTmpWriter.reset(new FileWriter(tmpFileName));
  }

  void Add(T const & item)
  {
    if (m_Buffer.size() == m_BufferCapacity)
      FlushToTmpFile();
    m_Buffer.push_back(item);
    ++m_ItemCount;
  }

  void SortAndFinish()
  {
    ASSERT(m_pTmpWriter.get(), ());
    FlushToTmpFile();

    // Write output.
    {
      m_pTmpWriter.reset();
      FileReader reader(m_TmpFileName);
      ItemIndexPairGreater fGreater(m_Less);
      PriorityQueueType q(fGreater);
      for (uint32_t i = 0; i < m_ItemCount; i += m_BufferCapacity)
        Push(q, i, reader);

      while (!q.empty())
      {
        m_OutputSink(q.top().first);
        uint32_t const i = q.top().second + 1;
        q.pop();
        if (i % m_BufferCapacity != 0 && i < m_ItemCount)
          Push(q, i, reader);
      }
    }
    FileWriter::DeleteFileX(m_TmpFileName);
  }

  ~FileSorter()
  {
    if (m_pTmpWriter.get())
    {
      try
      {
        SortAndFinish();
      }
      catch(RootException const & e)
      {
        LOG(LERROR, (e.Msg()));
      }
      catch(std::exception const & e)
      {
        LOG(LERROR, (e.what()));
      }
    }
  }

private:
  struct ItemIndexPairGreater
  {
    explicit ItemIndexPairGreater(LessT fLess) : m_Less(fLess) {}
    inline bool operator() (pair<T, uint32_t> const & a, pair<T, uint32_t> const & b) const
    {
      return m_Less(b.first, a.first);
    }
    LessT m_Less;
  };

  typedef priority_queue<pair<T, uint32_t>, vector<pair<T, uint32_t> >, ItemIndexPairGreater>
      PriorityQueueType;

  void FlushToTmpFile()
  {
    if (m_Buffer.empty())
      return;
    SorterT<LessT> sorter(m_Less);
    sorter(m_Buffer.begin(), m_Buffer.end());
    m_pTmpWriter->Write(&m_Buffer[0], m_Buffer.size() * sizeof(T));
    m_Buffer.clear();
  }

  void Push(PriorityQueueType & q, uint32_t i, FileReader const & reader)
  {
    T item;
    reader.Read(static_cast<uint64_t>(i) * sizeof(T), &item, sizeof(T));
    q.push(pair<T, uint32_t>(item, i));
  }

  string const m_TmpFileName;
  size_t const m_BufferCapacity;
  OutputSinkT & m_OutputSink;
  unique_ptr<FileWriter> m_pTmpWriter;
  vector<T> m_Buffer;
  uint32_t m_ItemCount;
  LessT m_Less;
};