quick_log.hxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * SHA - Simple Hybesis Algorithms
4  *
5  * Copyright (c) Michael Jeulin-Lagarrigue
6  *
7  * Licensed under the MIT License, you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * https://github.com/michael-jeulinl/Simple-Hybesis-Algorithms/blob/master/LICENSE
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License is
13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and limitations under the License.
15  *
16  * The above copyright notice and this permission notice shall be included in all copies or
17  * substantial portions of the Software.
18  *
19  *=========================================================================================================*/
20 #ifndef MODULE_SORT_QUICK_LOG_HXX
21 #define MODULE_SORT_QUICK_LOG_HXX
22 
23 #include <Logger/algorithm.hxx>
24 #include <Logger/array.hxx>
25 #include <Logger/comment.hxx>
26 #include <Logger/operation.hxx>
27 #include <Logger/typedef.hxx>
28 #include <Logger/value.hxx>
29 #include <partition_log.hxx>
30 
31 // STD includes
32 #include <iterator>
33 
34 namespace SHA_Logger
35 {
38  template <typename IT, typename Compare = std::less_equal<typename std::iterator_traits<IT>::value_type>>
39  class QuickLog
40  {
41  public:
43  static const String GetName() { return "Quick_Sort"; }
44 
47  static bool WriteInfo(Writer& writer) { return true; }
48 
51  static bool WriteDoc(Writer& writer) { return true; }
52 
55  static bool WriteSrc(Writer& writer) { return true; }
56 
57  // Assert correct JSON construction.
58  ~QuickLog() { assert(this->writer->IsComplete()); }
59 
65  static Ostream& Build(Ostream& os, Options opts, const IT& begin, const IT& end, const int offset = 0)
66  {
67  std::unique_ptr<QuickLog> builder = std::unique_ptr<QuickLog>(new QuickLog(os));
68  builder->Write(opts, begin, end, offset);
69 
70  return os;
71  }
72 
77  static Writer& Build(Writer& writer, Options opts, const IT& begin, const IT& end, const int offset = 0)
78  {
79  Write(writer, opts, begin, end, offset);
80 
81  return writer;
82  }
83 
84  private:
85  QuickLog(Ostream& os) : stream(std::unique_ptr<Stream>(new Stream(os))),
86  writer(std::unique_ptr<Writer>(new Writer(*this->stream))) {}
87  QuickLog operator=(QuickLog&) {} // Not Implemented
88 
89  bool Write(Options opts, const IT& begin, const IT& end, const int offset)
90  { return Write(*this->writer, opts, begin, end, offset); }
91 
92  static bool Write(Writer& writer, Options opts, const IT& begin, const IT& end, const int offset)
93  {
94  // Do not write sequence if no data to be processed
95  const int _seqSize = static_cast<int>(std::distance(begin, end));
96  if (_seqSize < 2)
97  {
98  Comment::Build(writer, "Sequence size too small to be processed.", 0);
99  Operation::Return<bool>(writer, true);
100  return true;
101  }
102 
103  writer.StartObject();
104 
105  // Write description
106  Algo_Traits<QuickLog>::Build(writer, opts);
107 
108  // Write parameters
109  WriteParameters(writer, opts, begin, end, offset);
110 
111  // Write computation
112  WriteComputation(writer, begin, end, offset);
113 
114  writer.EndObject();
115 
116  return true;
117  }
118 
120  static bool WriteParameters(Writer& writer, Options opts,
121  const IT& begin, const IT& end, const int offset)
122  {
123  writer.Key("parameters");
124  writer.StartArray();
125  if (opts & OpIsSub)
126  {
127  const int _seqSize = static_cast<int>(std::distance(begin, end));
128  Iterator::Build(writer, kSeqName, "begin", offset);
129  Iterator::Build(writer, kSeqName, "end", offset + _seqSize);
130  }
131  else
132  {
133  Array<IT>::Build(writer, kSeqName, "begin", begin, "end", end);
134  }
135  writer.EndArray();
136 
137  return true;
138  }
139 
141  static bool WriteComputation(Writer& writer, const IT& begin, const IT& end, const int offset)
142  {
143  const auto _pivotIdx = rand() % static_cast<const int>(std::distance(begin, end));
144 
145  // Local logged variables
146  writer.Key("locals");
147  writer.StartArray();
148  auto pivot = Iterator::BuildIt<IT>(writer, kSeqName, "pivot", offset + _pivotIdx, begin + _pivotIdx,
149  "Pick Random Pivot within [begin, end]");
150  writer.EndArray();
151 
152  writer.Key("logs");
153  writer.StartArray();
154  // Proceed partition
155  auto newPivot = PartitionLog<IT, Compare>::Build(writer, OpIsSub, begin, pivot, end, offset);
156  auto newOffset = offset + std::distance(begin, newPivot);
157 
158  // Recurse on first partition
159  QuickLog<IT, Compare>::Build(writer, OpIsSub, begin, newPivot, offset);
160  // Recurse on second partition
161  QuickLog<IT, Compare>::Build(writer, OpIsSub, newPivot + 1, end, newOffset + 1);
162  writer.EndArray();
163 
164  return true;
165  }
166 
167  std::unique_ptr<Stream> stream; // Stream wrapper
168  std::unique_ptr<Writer> writer; // Writer used to fill the stream
169  };
170 }
171 
172 #endif // MODULE_SORT_QUICK_HXX
static bool WriteDoc(Writer &writer)
Definition: quick_log.hxx:51
static Ostream & Build(Ostream &os, const String &message, int level=0, const String extent="normal")
Definition: comment.hxx:43
static Ostream & Build(Ostream &os, Options opts, const IT &begin, const IT &end, const int offset=0)
Definition: quick_log.hxx:65
QuickLog operator=(QuickLog &)
Definition: quick_log.hxx:87
static bool WriteComputation(Writer &writer, const IT &begin, const IT &end, const int offset)
Definition: quick_log.hxx:141
const std::string String
Definition: typedef.hxx:36
static Writer & Build(Writer &writer, Options opts, const IT &begin, const IT &end, const int offset=0)
Definition: quick_log.hxx:77
QuickLog(Ostream &os)
Definition: quick_log.hxx:85
static const String GetName()
eg https://cs.chromium.org/chromium/src/gpu/config/software_rendering_list_json.cc ...
Definition: quick_log.hxx:43
static Ostream & Build(Ostream &os, Options opts)
Definition: algorithm.hxx:39
std::unique_ptr< Writer > writer
Definition: quick_log.hxx:168
static Ostream & Build(Ostream &os, const String &name, const String &beginName, const IT &begin, const String &endName, const IT &end)
Definition: array.hxx:42
static bool Write(Writer &writer, Options opts, const IT &begin, const IT &end, const int offset)
Definition: quick_log.hxx:92
static Ostream & Build(Ostream &os, const String &parentId, const String &name, int index, const String &comment="")
Definition: iterator.hxx:43
rapidjson::OStreamWrapper Stream
Definition: typedef.hxx:32
static bool WriteSrc(Writer &writer)
Definition: quick_log.hxx:55
static const std::string kSeqName
Definition: typedef.hxx:41
bool Write(Options opts, const IT &begin, const IT &end, const int offset)
Definition: quick_log.hxx:89
static bool WriteInfo(Writer &writer)
Definition: quick_log.hxx:47
std::ostream Ostream
Definition: typedef.hxx:37
static IT Build(Ostream &os, Options opts, const IT &begin, const IT &pivot, const IT &end, const int offSet=0)
std::unique_ptr< Stream > stream
Definition: quick_log.hxx:167
rapidjson::PrettyWriter< Stream > Writer
Definition: typedef.hxx:33
static bool WriteParameters(Writer &writer, Options opts, const IT &begin, const IT &end, const int offset)
Definition: quick_log.hxx:120