OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "courgette/adjustment_method.h" | 5 #include "courgette/adjustment_method.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <list> | 9 #include <list> |
10 #include <map> | 10 #include <map> |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 | 201 |
202 AssignmentCandidates* candidates(); | 202 AssignmentCandidates* candidates(); |
203 | 203 |
204 Label* label_; // The label that this info a surrogate for. | 204 Label* label_; // The label that this info a surrogate for. |
205 | 205 |
206 uint32 is_model_ : 1; // Is the label in the model? | 206 uint32 is_model_ : 1; // Is the label in the model? |
207 uint32 debug_index_ : 31; // A small number for naming the label in debug | 207 uint32 debug_index_ : 31; // A small number for naming the label in debug |
208 // output. The pair (is_model_, debug_index_) is | 208 // output. The pair (is_model_, debug_index_) is |
209 // unique. | 209 // unique. |
210 | 210 |
211 uint32 refs_; // Number of times this Label is referenced. | 211 int refs_; // Number of times this Label is referenced. |
212 | 212 |
213 LabelInfo* assignment_; // Label from other program corresponding to this. | 213 LabelInfo* assignment_; // Label from other program corresponding to this. |
214 | 214 |
215 std::vector<uint32> positions_; // Offsets into the trace of references. | 215 std::vector<uint32> positions_; // Offsets into the trace of references. |
216 | 216 |
217 private: | 217 private: |
218 AssignmentCandidates* candidates_; | 218 AssignmentCandidates* candidates_; |
219 | 219 |
220 void operator=(const LabelInfo*); // Disallow assignment only. | 220 void operator=(const LabelInfo*); // Disallow assignment only. |
221 // Public compiler generated copy constructor is needed to constuct | 221 // Public compiler generated copy constructor is needed to constuct |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 struct InterningLess { | 396 struct InterningLess { |
397 bool operator()(const Shingle& a, const Shingle& b) const; | 397 bool operator()(const Shingle& a, const Shingle& b) const; |
398 }; | 398 }; |
399 | 399 |
400 typedef std::set<Shingle, InterningLess> OwningSet; | 400 typedef std::set<Shingle, InterningLess> OwningSet; |
401 | 401 |
402 static Shingle* Find(const Trace& trace, size_t position, | 402 static Shingle* Find(const Trace& trace, size_t position, |
403 OwningSet* owning_set) { | 403 OwningSet* owning_set) { |
404 std::pair<OwningSet::iterator, bool> pair = | 404 std::pair<OwningSet::iterator, bool> pair = |
405 owning_set->insert(Shingle(trace, position)); | 405 owning_set->insert(Shingle(trace, position)); |
406 // pair.first is the newly inserted Shingle or the previouly inserted one | 406 // pair.first iterator 'points' to the newly inserted Shingle or the |
407 // that looks the same according to the comparator. | 407 // previouly inserted one that looks the same according to the comparator. |
408 pair.first->add_position(position); | 408 |
409 return &*pair.first; | 409 // const_cast required because key is const. We modify the Shingle |
| 410 // extensively but not in a way that affects InterningLess. |
| 411 Shingle* shingle = const_cast<Shingle*>(&*pair.first); |
| 412 shingle->add_position(position); |
| 413 return shingle; |
410 } | 414 } |
411 | 415 |
412 LabelInfo* at(size_t i) const { return trace_[exemplar_position_ + i]; } | 416 LabelInfo* at(size_t i) const { return trace_[exemplar_position_ + i]; } |
413 void add_position(size_t position) { positions_.push_back(position); } | 417 void add_position(size_t position) { positions_.push_back(position); } |
414 size_t position_count() const { return positions_.size(); } | 418 size_t position_count() const { return positions_.size(); } |
415 | 419 |
416 bool InModel() const { return at(0)->is_model_; } | 420 bool InModel() const { return at(0)->is_model_; } |
417 | 421 |
418 ShinglePattern* pattern() const { return pattern_; } | 422 ShinglePattern* pattern() const { return pattern_; } |
419 void set_pattern(ShinglePattern* pattern) { pattern_ = pattern; } | 423 void set_pattern(ShinglePattern* pattern) { pattern_ = pattern; } |
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
950 } else { | 954 } else { |
951 pattern->program_histogram_.insert(ShinglePattern::FreqView(shingle)); | 955 pattern->program_histogram_.insert(ShinglePattern::FreqView(shingle)); |
952 pattern->program_coverage_ += shingle->position_count(); | 956 pattern->program_coverage_ += shingle->position_count(); |
953 } | 957 } |
954 } | 958 } |
955 | 959 |
956 void InitialClassify() { | 960 void InitialClassify() { |
957 for (Shingle::OwningSet::iterator p = shingle_instances_.begin(); | 961 for (Shingle::OwningSet::iterator p = shingle_instances_.begin(); |
958 p != shingle_instances_.end(); | 962 p != shingle_instances_.end(); |
959 ++p) { | 963 ++p) { |
960 Reclassify(&*p); | 964 // GCC's set<T>::iterator::operator *() returns a const object. |
| 965 Reclassify(const_cast<Shingle*>(&*p)); |
961 } | 966 } |
962 } | 967 } |
963 | 968 |
964 // For the positions in |info|, find the shingles that overlap that position. | 969 // For the positions in |info|, find the shingles that overlap that position. |
965 void AddAffectedPositions(LabelInfo* info, ShingleSet* affected_shingles) { | 970 void AddAffectedPositions(LabelInfo* info, ShingleSet* affected_shingles) { |
966 const size_t kWidth = Shingle::kWidth; | 971 const size_t kWidth = Shingle::kWidth; |
967 for (size_t i = 0; i < info->positions_.size(); ++i) { | 972 for (size_t i = 0; i < info->positions_.size(); ++i) { |
968 size_t position = info->positions_[i]; | 973 size_t position = info->positions_[i]; |
969 // Find bounds to the subrange of |trace_| we are in. | 974 // Find bounds to the subrange of |trace_| we are in. |
970 size_t start = position < model_end_ ? 0 : model_end_; | 975 size_t start = position < model_end_ ? 0 : model_end_; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1028 // Useless pattern. | 1033 // Useless pattern. |
1029 } else { | 1034 } else { |
1030 active_non_single_use_patterns_.insert(pattern); | 1035 active_non_single_use_patterns_.insert(pattern); |
1031 AddPatternToLabelQueue(pattern, +1); | 1036 AddPatternToLabelQueue(pattern, +1); |
1032 } | 1037 } |
1033 } | 1038 } |
1034 | 1039 |
1035 void AddPatternToLabelQueue(const ShinglePattern* pattern, int sign) { | 1040 void AddPatternToLabelQueue(const ShinglePattern* pattern, int sign) { |
1036 // For each possible assignment in this pattern, update the potential | 1041 // For each possible assignment in this pattern, update the potential |
1037 // contributions to the LabelInfo queues. | 1042 // contributions to the LabelInfo queues. |
1038 size_t model_histogram_size = pattern->model_histogram_.size(); | |
1039 size_t program_histogram_size = pattern->program_histogram_.size(); | |
1040 | 1043 |
1041 // We want to find for each symbol (LabelInfo) the maximum contribution that | 1044 // We want to find for each symbol (LabelInfo) the maximum contribution that |
1042 // could be achieved by making shingle-wise assignments between shingles in | 1045 // could be achieved by making shingle-wise assignments between shingles in |
1043 // the model and shingles in the program. | 1046 // the model and shingles in the program. |
1044 // | 1047 // |
1045 // If the shingles in the histograms are independent (no two shingles have a | 1048 // If the shingles in the histograms are independent (no two shingles have a |
1046 // symbol in common) then any permutation of the assignments is possible, | 1049 // symbol in common) then any permutation of the assignments is possible, |
1047 // and the maximum contribution can be found by taking the maximum over all | 1050 // and the maximum contribution can be found by taking the maximum over all |
1048 // the pairs. | 1051 // the pairs. |
1049 // | 1052 // |
1050 // If the shingles are dependent two things happen. The maximum | 1053 // If the shingles are dependent two things happen. The maximum |
1051 // contribution to any given symbol is a sum because the symbol has | 1054 // contribution to any given symbol is a sum because the symbol has |
1052 // contributions from all the shingles containing it. Second, some | 1055 // contributions from all the shingles containing it. Second, some |
1053 // assignments are blocked by previous incompatible assignments. We want to | 1056 // assignments are blocked by previous incompatible assignments. We want to |
1054 // avoid a combinatorial search, so we ignore the blocking. | 1057 // avoid a combinatorial search, so we ignore the blocking. |
1055 | 1058 |
1056 const int kUnwieldy = 5; | 1059 const size_t kUnwieldy = 5; |
1057 | 1060 |
1058 typedef std::map<LabelInfo*, int> LabelToScore; | 1061 typedef std::map<LabelInfo*, int> LabelToScore; |
1059 typedef std::map<LabelInfo*, LabelToScore > ScoreSet; | 1062 typedef std::map<LabelInfo*, LabelToScore > ScoreSet; |
1060 ScoreSet maxima; | 1063 ScoreSet maxima; |
1061 | 1064 |
1062 size_t n_model_samples = 0; | 1065 size_t n_model_samples = 0; |
1063 for (ShinglePattern::Histogram::const_iterator model_iter = | 1066 for (ShinglePattern::Histogram::const_iterator model_iter = |
1064 pattern->model_histogram_.begin(); | 1067 pattern->model_histogram_.begin(); |
1065 model_iter != pattern->model_histogram_.end(); | 1068 model_iter != pattern->model_histogram_.end(); |
1066 ++model_iter) { | 1069 ++model_iter) { |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1309 | 1312 |
1310 //////////////////////////////////////////////////////////////////////////////// | 1313 //////////////////////////////////////////////////////////////////////////////// |
1311 | 1314 |
1312 } // namespace adjustment_method_2 | 1315 } // namespace adjustment_method_2 |
1313 | 1316 |
1314 AdjustmentMethod* AdjustmentMethod::MakeShingleAdjustmentMethod() { | 1317 AdjustmentMethod* AdjustmentMethod::MakeShingleAdjustmentMethod() { |
1315 return new adjustment_method_2::Adjuster(); | 1318 return new adjustment_method_2::Adjuster(); |
1316 } | 1319 } |
1317 | 1320 |
1318 } // namespace courgette | 1321 } // namespace courgette |
OLD | NEW |