bubble_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_Bubble_LOG_HXX
21 #define MODULE_SORT_Bubble_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 BubbleLog
40  {
41  public:
43  static const String GetName() { return "Bubble_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  ~BubbleLog() { assert(this->writer->IsComplete()); }
59 
65  static Ostream& Build(Ostream& os, Options opts, const IT& begin, const IT& end)
66  {
67  std::unique_ptr<BubbleLog> builder = std::unique_ptr<BubbleLog>(new BubbleLog(os));
68  builder->Write(opts, begin, end);
69 
70  return os;
71  }
72 
77  static Writer& Build(Writer& writer, Options opts, const IT& begin, const IT& end)
78  {
79  Write(writer, opts, begin, end);
80 
81  return writer;
82  }
83 
84  private:
85  BubbleLog(Ostream& os) : stream(std::unique_ptr<Stream>(new Stream(os))),
86  writer(std::unique_ptr<Writer>(new Writer(*this->stream))) {}
87  BubbleLog operator=(BubbleLog&) {} // Not Implemented
88 
89  bool Write(Options opts, const IT& begin, const IT& end)
90  { return Write(*this->writer, opts, begin, end); }
91 
92  static bool Write(Writer& writer, Options opts, const IT& begin, const IT& end)
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<BubbleLog>::Build(writer, opts);
107 
108  // Write parameters
109  WriteParameters(writer, opts, begin, end);
110 
111  // Write computation
112  WriteComputation(writer, begin, end);
113 
114  writer.EndObject();
115 
116  return true;
117  }
118 
120  static bool WriteParameters(Writer& writer, Options opts, const IT& begin, const IT& end)
121  {
122  writer.Key("parameters");
123  writer.StartArray();
124  if (opts & OpIsSub)
125  {
126  const int _seqSize = static_cast<int>(std::distance(begin, end));
127  Iterator::Build(writer, kSeqName, "begin", 0);
128  Iterator::Build(writer, kSeqName, "end", _seqSize);
129  }
130  else
131  {
132  Array<IT>::Build(writer, kSeqName, "begin", begin, "end", end);
133  }
134  writer.EndArray();
135 
136  return true;
137  }
138 
140  static bool WriteComputation(Writer& writer, const IT& begin, const IT& end)
141  {
142  int _itIdx = 0;
143  int endIdx = -1;
144  bool hasSwapped;
145 
146  // Local logged variables
147  writer.Key("locals");
148  writer.StartArray();
149  auto curIt = Iterator::BuildIt<IT>(writer, kSeqName, "it", 0, begin);
150  auto pivot = Iterator::BuildIt<IT>(writer, kSeqName, "pivot", 1, begin + 1);
151  writer.EndArray();
152 
153  // Proceed sort
154  writer.Key("logs");
155  writer.StartArray();
156  // for each element - bubble it up until the end.
157  for (auto it = begin; it < end - 1; ++it, --endIdx)
158  {
159  hasSwapped = false;
160  _itIdx = 0;
161  for (; curIt < end + endIdx; ++curIt)
162  {
163  if (Compare()(*(curIt + 1), *curIt))
164  {
165  Operation::Swap(writer, "it", "pivot");
166  std::swap(*curIt, *(curIt + 1));
167  hasSwapped = true;
168  }
169 
170  Operation::Set<int>(writer, "it", ++_itIdx);
171  Operation::Set<int>(writer, "pivot", _itIdx + 1);
172  }
173 
174  if (!hasSwapped)
175  break;
176 
177  Operation::Set<int>(writer, "it", 0);
178  Operation::Set<int>(writer, "pivot", 1);
179  curIt = begin;
180  }
181 
182  Operation::Return<bool>(writer, true);
183  writer.EndArray();
184 
185  return true;
186  }
187 
188  std::unique_ptr<Stream> stream; // Stream wrapper
189  std::unique_ptr<Writer> writer; // Writer used to fill the stream
190  };
191 }
192 
193 #endif // MODULE_SORT_Bubble_HXX
static bool Write(Writer &writer, Options opts, const IT &begin, const IT &end)
Definition: bubble_log.hxx:92
BubbleLog(Ostream &os)
Definition: bubble_log.hxx:85
static bool WriteComputation(Writer &writer, const IT &begin, const IT &end)
Definition: bubble_log.hxx:140
BubbleLog operator=(BubbleLog &)
Definition: bubble_log.hxx:87
static Writer & Build(Writer &writer, Options opts, const IT &begin, const IT &end)
Definition: bubble_log.hxx:77
bool Write(Options opts, const IT &begin, const IT &end)
Definition: bubble_log.hxx:89
static const String GetName()
eg https://cs.chromium.org/chromium/src/gpu/config/software_rendering_list_json.cc ...
Definition: bubble_log.hxx:43
static Ostream & Build(Ostream &os, const String &message, int level=0, const String extent="normal")
Definition: comment.hxx:43
const std::string String
Definition: typedef.hxx:36
static bool WriteInfo(Writer &writer)
Definition: bubble_log.hxx:47
static Ostream & Build(Ostream &os, Options opts)
Definition: algorithm.hxx:39
static Ostream & Build(Ostream &os, Options opts, const IT &begin, const IT &end)
Definition: bubble_log.hxx:65
std::unique_ptr< Stream > stream
Definition: bubble_log.hxx:188
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 WriteDoc(Writer &writer)
Definition: bubble_log.hxx:51
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: bubble_log.hxx:55
static const std::string kSeqName
Definition: typedef.hxx:41
std::unique_ptr< Writer > writer
Definition: bubble_log.hxx:189
std::ostream Ostream
Definition: typedef.hxx:37
rapidjson::PrettyWriter< Stream > Writer
Definition: typedef.hxx:33
static bool WriteParameters(Writer &writer, Options opts, const IT &begin, const IT &end)
Definition: bubble_log.hxx:120
static Ostream & Swap(Ostream &os, const String &aName, const String &bName)
Definition: operation.hxx:79