OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 void operator=(const LabelInfo*); // Disallow assignment only. | 195 void operator=(const LabelInfo*); // Disallow assignment only. |
196 // Public compiler generated copy constructor is needed to constuct | 196 // Public compiler generated copy constructor is needed to constuct |
197 // std::pair<Label*, LabelInfo> so that fresh LabelInfos can be allocated | 197 // std::pair<Label*, LabelInfo> so that fresh LabelInfos can be allocated |
198 // inside a std::map. | 198 // inside a std::map. |
199 }; | 199 }; |
200 | 200 |
201 typedef std::vector<LabelInfo*> Trace; | 201 typedef std::vector<LabelInfo*> Trace; |
202 | 202 |
203 std::string ToString(const LabelInfo* info) { | 203 std::string ToString(const LabelInfo* info) { |
204 std::string s; | 204 std::string s; |
205 StringAppendF(&s, "%c%d", "pm"[info->is_model_], info->debug_index_); | 205 base::StringAppendF(&s, "%c%d", "pm"[info->is_model_], info->debug_index_); |
206 if (info->label_->index_ != Label::kNoIndex) | 206 if (info->label_->index_ != Label::kNoIndex) |
207 StringAppendF(&s, " (%d)", info->label_->index_); | 207 base::StringAppendF(&s, " (%d)", info->label_->index_); |
208 | 208 |
209 StringAppendF(&s, " #%u", info->refs_); | 209 base::StringAppendF(&s, " #%u", info->refs_); |
210 return s; | 210 return s; |
211 } | 211 } |
212 | 212 |
213 // LabelInfoMaker maps labels to their surrogate LabelInfo objects. | 213 // LabelInfoMaker maps labels to their surrogate LabelInfo objects. |
214 class LabelInfoMaker { | 214 class LabelInfoMaker { |
215 public: | 215 public: |
216 LabelInfoMaker() : debug_label_index_gen_(0) {} | 216 LabelInfoMaker() : debug_label_index_gen_(0) {} |
217 | 217 |
218 LabelInfo* MakeLabelInfo(Label* label, bool is_model, uint32 position) { | 218 LabelInfo* MakeLabelInfo(Label* label, bool is_model, uint32 position) { |
219 LabelInfo& slot = label_infos_[label]; | 219 LabelInfo& slot = label_infos_[label]; |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 // VS2005's implementation of std::set<T>::set() requires T to have a copy | 424 // VS2005's implementation of std::set<T>::set() requires T to have a copy |
425 // constructor. | 425 // constructor. |
426 // DISALLOW_COPY_AND_ASSIGN(Shingle); | 426 // DISALLOW_COPY_AND_ASSIGN(Shingle); |
427 void operator=(const Shingle&); // Disallow assignment only. | 427 void operator=(const Shingle&); // Disallow assignment only. |
428 }; | 428 }; |
429 | 429 |
430 std::string ToString(const Shingle* instance) { | 430 std::string ToString(const Shingle* instance) { |
431 std::string s; | 431 std::string s; |
432 const char* sep = "<"; | 432 const char* sep = "<"; |
433 for (size_t i = 0; i < Shingle::kWidth; ++i) { | 433 for (size_t i = 0; i < Shingle::kWidth; ++i) { |
434 // StringAppendF(&s, "%s%x ", sep, instance.at(i)->label_->rva_); | 434 // base::StringAppendF(&s, "%s%x ", sep, instance.at(i)->label_->rva_); |
435 s += sep; | 435 s += sep; |
436 s += ToString(instance->at(i)); | 436 s += ToString(instance->at(i)); |
437 sep = ", "; | 437 sep = ", "; |
438 } | 438 } |
439 StringAppendF(&s, ">(%" PRIuS ")@{%" PRIuS "}", instance->exemplar_position_, | 439 base::StringAppendF(&s, ">(%" PRIuS ")@{%" PRIuS "}", |
440 instance->position_count()); | 440 instance->exemplar_position_, |
| 441 instance->position_count()); |
441 return s; | 442 return s; |
442 } | 443 } |
443 | 444 |
444 | 445 |
445 bool Shingle::InterningLess::operator()( | 446 bool Shingle::InterningLess::operator()( |
446 const Shingle& a, | 447 const Shingle& a, |
447 const Shingle& b) const { | 448 const Shingle& b) const { |
448 for (size_t i = 0; i < kWidth; ++i) { | 449 for (size_t i = 0; i < kWidth; ++i) { |
449 LabelInfo* info_a = a.at(i); | 450 LabelInfo* info_a = a.at(i); |
450 LabelInfo* info_b = b.at(i); | 451 LabelInfo* info_b = b.at(i); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 Histogram program_histogram_; | 517 Histogram program_histogram_; |
517 int model_coverage_; | 518 int model_coverage_; |
518 int program_coverage_; | 519 int program_coverage_; |
519 }; | 520 }; |
520 | 521 |
521 std::string ToString(const ShinglePattern::Index* index) { | 522 std::string ToString(const ShinglePattern::Index* index) { |
522 std::string s; | 523 std::string s; |
523 if (index == NULL) { | 524 if (index == NULL) { |
524 s = "<null>"; | 525 s = "<null>"; |
525 } else { | 526 } else { |
526 StringAppendF(&s, "<%d: ", index->variables_); | 527 base::StringAppendF(&s, "<%d: ", index->variables_); |
527 const char* sep = ""; | 528 const char* sep = ""; |
528 for (size_t i = 0; i < Shingle::kWidth; ++i) { | 529 for (size_t i = 0; i < Shingle::kWidth; ++i) { |
529 s += sep; | 530 s += sep; |
530 sep = ", "; | 531 sep = ", "; |
531 uint32 kind = index->kinds_[i]; | 532 uint32 kind = index->kinds_[i]; |
532 int offset = kind & ShinglePattern::kOffsetMask; | 533 int offset = kind & ShinglePattern::kOffsetMask; |
533 if (kind & ShinglePattern::kVariable) | 534 if (kind & ShinglePattern::kVariable) |
534 StringAppendF(&s, "V%d", offset); | 535 base::StringAppendF(&s, "V%d", offset); |
535 else | 536 else |
536 StringAppendF(&s, "%d", index->assigned_indexes_[offset]); | 537 base::StringAppendF(&s, "%d", index->assigned_indexes_[offset]); |
537 } | 538 } |
538 StringAppendF(&s, " %x", index->hash_); | 539 base::StringAppendF(&s, " %x", index->hash_); |
539 s += ">"; | 540 s += ">"; |
540 } | 541 } |
541 return s; | 542 return s; |
542 } | 543 } |
543 | 544 |
544 std::string HistogramToString(const ShinglePattern::Histogram& histogram, | 545 std::string HistogramToString(const ShinglePattern::Histogram& histogram, |
545 size_t snippet_max) { | 546 size_t snippet_max) { |
546 std::string s; | 547 std::string s; |
547 size_t histogram_size = histogram.size(); | 548 size_t histogram_size = histogram.size(); |
548 size_t snippet_size = 0; | 549 size_t snippet_size = 0; |
549 for (ShinglePattern::Histogram::const_iterator p = histogram.begin(); | 550 for (ShinglePattern::Histogram::const_iterator p = histogram.begin(); |
550 p != histogram.end(); | 551 p != histogram.end(); |
551 ++p) { | 552 ++p) { |
552 if (++snippet_size > snippet_max && snippet_size != histogram_size) { | 553 if (++snippet_size > snippet_max && snippet_size != histogram_size) { |
553 s += " ..."; | 554 s += " ..."; |
554 break; | 555 break; |
555 } | 556 } |
556 StringAppendF(&s, " %" PRIuS, p->count()); | 557 base::StringAppendF(&s, " %" PRIuS, p->count()); |
557 } | 558 } |
558 return s; | 559 return s; |
559 } | 560 } |
560 | 561 |
561 std::string HistogramToStringFull(const ShinglePattern::Histogram& histogram, | 562 std::string HistogramToStringFull(const ShinglePattern::Histogram& histogram, |
562 const char* indent, | 563 const char* indent, |
563 size_t snippet_max) { | 564 size_t snippet_max) { |
564 std::string s; | 565 std::string s; |
565 | 566 |
566 size_t histogram_size = histogram.size(); | 567 size_t histogram_size = histogram.size(); |
567 size_t snippet_size = 0; | 568 size_t snippet_size = 0; |
568 for (ShinglePattern::Histogram::const_iterator p = histogram.begin(); | 569 for (ShinglePattern::Histogram::const_iterator p = histogram.begin(); |
569 p != histogram.end(); | 570 p != histogram.end(); |
570 ++p) { | 571 ++p) { |
571 s += indent; | 572 s += indent; |
572 if (++snippet_size > snippet_max && snippet_size != histogram_size) { | 573 if (++snippet_size > snippet_max && snippet_size != histogram_size) { |
573 s += "...\n"; | 574 s += "...\n"; |
574 break; | 575 break; |
575 } | 576 } |
576 StringAppendF(&s, "(%" PRIuS ") ", p->count()); | 577 base::StringAppendF(&s, "(%" PRIuS ") ", p->count()); |
577 s += ToString(&(*p->instance())); | 578 s += ToString(&(*p->instance())); |
578 s += "\n"; | 579 s += "\n"; |
579 } | 580 } |
580 return s; | 581 return s; |
581 } | 582 } |
582 | 583 |
583 std::string ToString(const ShinglePattern* pattern, size_t snippet_max = 3) { | 584 std::string ToString(const ShinglePattern* pattern, size_t snippet_max = 3) { |
584 std::string s; | 585 std::string s; |
585 if (pattern == NULL) { | 586 if (pattern == NULL) { |
586 s = "<null>"; | 587 s = "<null>"; |
587 } else { | 588 } else { |
588 s = "{"; | 589 s = "{"; |
589 s += ToString(pattern->index_); | 590 s += ToString(pattern->index_); |
590 StringAppendF(&s, "; %d(%d):", | 591 base::StringAppendF(&s, "; %d(%d):", |
591 static_cast<int>(pattern->model_histogram_.size()), | 592 static_cast<int>(pattern->model_histogram_.size()), |
592 pattern->model_coverage_); | 593 pattern->model_coverage_); |
593 | 594 |
594 s += HistogramToString(pattern->model_histogram_, snippet_max); | 595 s += HistogramToString(pattern->model_histogram_, snippet_max); |
595 StringAppendF(&s, "; %d(%d):", | 596 base::StringAppendF(&s, "; %d(%d):", |
596 static_cast<int>(pattern->program_histogram_.size()), | 597 static_cast<int>(pattern->program_histogram_.size()), |
597 pattern->program_coverage_); | 598 pattern->program_coverage_); |
598 s += HistogramToString(pattern->program_histogram_, snippet_max); | 599 s += HistogramToString(pattern->program_histogram_, snippet_max); |
599 s += "}"; | 600 s += "}"; |
600 } | 601 } |
601 return s; | 602 return s; |
602 } | 603 } |
603 | 604 |
604 std::string ShinglePatternToStringFull(const ShinglePattern* pattern, | 605 std::string ShinglePatternToStringFull(const ShinglePattern* pattern, |
605 size_t max) { | 606 size_t max) { |
606 std::string s; | 607 std::string s; |
607 s += ToString(pattern->index_); | 608 s += ToString(pattern->index_); |
608 s += "\n"; | 609 s += "\n"; |
609 size_t model_size = pattern->model_histogram_.size(); | 610 size_t model_size = pattern->model_histogram_.size(); |
610 size_t program_size = pattern->program_histogram_.size(); | 611 size_t program_size = pattern->program_histogram_.size(); |
611 StringAppendF(&s, " model shingles %" PRIuS "\n", model_size); | 612 base::StringAppendF(&s, " model shingles %" PRIuS "\n", model_size); |
612 s += HistogramToStringFull(pattern->model_histogram_, " ", max); | 613 s += HistogramToStringFull(pattern->model_histogram_, " ", max); |
613 StringAppendF(&s, " program shingles %" PRIuS "\n", program_size); | 614 base::StringAppendF(&s, " program shingles %" PRIuS "\n", program_size); |
614 s += HistogramToStringFull(pattern->program_histogram_, " ", max); | 615 s += HistogramToStringFull(pattern->program_histogram_, " ", max); |
615 return s; | 616 return s; |
616 } | 617 } |
617 | 618 |
618 struct ShinglePatternIndexLess { | 619 struct ShinglePatternIndexLess { |
619 bool operator()(const ShinglePattern::Index& a, | 620 bool operator()(const ShinglePattern::Index& a, |
620 const ShinglePattern::Index& b) const { | 621 const ShinglePattern::Index& b) const { |
621 if (a.hash_ < b.hash_) return true; | 622 if (a.hash_ < b.hash_) return true; |
622 if (a.hash_ > b.hash_) return false; | 623 if (a.hash_ > b.hash_) return false; |
623 | 624 |
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1288 | 1289 |
1289 //////////////////////////////////////////////////////////////////////////////// | 1290 //////////////////////////////////////////////////////////////////////////////// |
1290 | 1291 |
1291 } // namespace adjustment_method_2 | 1292 } // namespace adjustment_method_2 |
1292 | 1293 |
1293 AdjustmentMethod* AdjustmentMethod::MakeShingleAdjustmentMethod() { | 1294 AdjustmentMethod* AdjustmentMethod::MakeShingleAdjustmentMethod() { |
1294 return new adjustment_method_2::Adjuster(); | 1295 return new adjustment_method_2::Adjuster(); |
1295 } | 1296 } |
1296 | 1297 |
1297 } // namespace courgette | 1298 } // namespace courgette |
OLD | NEW |